001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.camel.component.dataformat; 018 019 import org.apache.camel.AsyncCallback; 020 import org.apache.camel.Component; 021 import org.apache.camel.Consumer; 022 import org.apache.camel.Exchange; 023 import org.apache.camel.Processor; 024 import org.apache.camel.Producer; 025 import org.apache.camel.impl.DefaultAsyncProducer; 026 import org.apache.camel.impl.DefaultEndpoint; 027 import org.apache.camel.processor.MarshalProcessor; 028 import org.apache.camel.processor.UnmarshalProcessor; 029 import org.apache.camel.spi.DataFormat; 030 import org.apache.camel.spi.UriEndpoint; 031 import org.apache.camel.spi.UriParam; 032 import org.apache.camel.util.ServiceHelper; 033 034 @UriEndpoint(scheme = "dataformat") 035 public class DataFormatEndpoint extends DefaultEndpoint { 036 037 private MarshalProcessor marshal; 038 private UnmarshalProcessor unmarshal; 039 @UriParam 040 private DataFormat dataFormat; 041 @UriParam 042 private String operation; 043 044 public DataFormatEndpoint() { 045 } 046 047 public DataFormatEndpoint(String endpointUri, Component component, DataFormat dataFormat) { 048 super(endpointUri, component); 049 this.dataFormat = dataFormat; 050 } 051 052 public DataFormat getDataFormat() { 053 return dataFormat; 054 } 055 056 public void setDataFormat(DataFormat dataFormat) { 057 this.dataFormat = dataFormat; 058 } 059 060 public String getOperation() { 061 return operation; 062 } 063 064 public void setOperation(String operation) { 065 this.operation = operation; 066 } 067 068 @Override 069 public Producer createProducer() throws Exception { 070 return new DefaultAsyncProducer(this) { 071 @Override 072 public boolean process(Exchange exchange, AsyncCallback callback) { 073 if (marshal != null) { 074 return marshal.process(exchange, callback); 075 } else { 076 return unmarshal.process(exchange, callback); 077 } 078 } 079 080 @Override 081 public String toString() { 082 return "DataFormatProducer[" + dataFormat + "]"; 083 } 084 }; 085 } 086 087 @Override 088 public Consumer createConsumer(Processor processor) throws Exception { 089 throw new UnsupportedOperationException("Cannot consume from data format"); 090 } 091 092 @Override 093 public boolean isSingleton() { 094 return true; 095 } 096 097 @Override 098 protected void doStart() throws Exception { 099 if (operation.equals("marshal")) { 100 marshal = new MarshalProcessor(dataFormat); 101 marshal.setCamelContext(getCamelContext()); 102 } else { 103 unmarshal = new UnmarshalProcessor(dataFormat); 104 unmarshal.setCamelContext(getCamelContext()); 105 } 106 107 ServiceHelper.startServices(dataFormat, marshal, unmarshal); 108 super.doStart(); 109 } 110 111 @Override 112 protected void doStop() throws Exception { 113 ServiceHelper.stopServices(marshal, unmarshal, dataFormat); 114 super.doStop(); 115 } 116 }