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.pojo;
018
019 import org.apache.camel.Consumer;
020 import org.apache.camel.Exchange;
021 import org.apache.camel.NoSuchEndpointException;
022 import org.apache.camel.Processor;
023 import org.apache.camel.Producer;
024 import org.apache.camel.Component;
025 import org.apache.camel.impl.DefaultEndpoint;
026 import org.apache.camel.impl.DefaultProducer;
027 import org.apache.camel.spi.Provider;
028
029 import java.lang.reflect.InvocationTargetException;
030
031 /**
032 * Represents a pojo endpoint that uses reflection
033 * to send messages around.
034 *
035 * @version $Revision: 519973 $
036 */
037 public class PojoEndpoint extends DefaultEndpoint<PojoExchange> {
038 private Object pojo;
039
040 public PojoEndpoint(String uri, Component component, Object pojo) {
041 super(uri, component);
042 this.pojo = pojo;
043 }
044
045 public Producer<PojoExchange> createProducer() throws Exception {
046 final Object pojo = getPojo();
047 if (pojo == null) {
048 throw new NoPojoAvailableException(this);
049 }
050
051 return new DefaultProducer(this) {
052 public void process(Exchange exchange) {
053 PojoExchange pojoExchange = toExchangeType(exchange);
054 invoke(pojo, pojoExchange);
055 exchange.copyFrom(pojoExchange);
056 }
057 };
058 }
059
060 public Consumer<PojoExchange> createConsumer(Processor processor) throws Exception {
061 throw new Exception("You cannot consume from pojo endpoints.");
062 }
063
064 /**
065 * This causes us to invoke the endpoint Pojo using reflection.
066 *
067 * @param pojo
068 */
069 public static void invoke(Object pojo, PojoExchange exchange) {
070 PojoInvocation invocation = exchange.getInvocation();
071 try {
072 Object response = invocation.getMethod().invoke(pojo, invocation.getArgs());
073 exchange.getOut().setBody(response);
074 }
075 catch (InvocationTargetException e) {
076 exchange.setException(e.getCause());
077 }
078 catch (RuntimeException e) {
079 throw e;
080 }
081 catch (Throwable e) {
082 throw new RuntimeException(e);
083 }
084 }
085
086 public PojoExchange createExchange() {
087 return new PojoExchange(getContext());
088 }
089
090 public boolean isSingleton() {
091 return true;
092 }
093
094 public Object getPojo() {
095 return pojo;
096 }
097
098 public void setPojo(Object pojo) {
099 this.pojo = pojo;
100 }
101 }