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