KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > reflection > ProxyInOutBinding


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.reflection;
18
19 import java.lang.reflect.InvocationHandler JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.lang.reflect.Proxy JavaDoc;
22
23 import javax.jbi.JBIException;
24 import javax.jbi.messaging.ExchangeStatus;
25 import javax.jbi.messaging.InOut;
26 import javax.jbi.messaging.NormalizedMessage;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.servicemix.components.util.ComponentSupport;
31 import org.apache.servicemix.jbi.RuntimeJBIException;
32
33 /**
34  * A Proxy factory which sends the method invocations into the JBI container
35  * for processing.
36  *
37  * @version $Revision: 426415 $
38  */

39 public class ProxyInOutBinding extends ComponentSupport implements InvocationHandler JavaDoc {
40     
41     private static final Log log = LogFactory.getLog(ProxyInOutBinding.class);
42
43     private ClassLoader JavaDoc cl;
44     private final Class JavaDoc[] interfaces;
45     
46     public ProxyInOutBinding(Object JavaDoc target) {
47         this( target.getClass().getClassLoader(), target.getClass().getInterfaces());
48     }
49     
50     public ProxyInOutBinding(ClassLoader JavaDoc cl, Class JavaDoc [] interfaces) {
51         this.cl = cl;
52         this.interfaces = interfaces;
53     }
54     
55     public Object JavaDoc createProxy() {
56         return Proxy.newProxyInstance(cl, interfaces, this);
57     }
58
59     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
60         if (log.isTraceEnabled()) {
61             log.trace("Invoked: " + proxy);
62         }
63         try {
64             InOut messageExchange = getDeliveryChannel().createExchangeFactory().createInOutExchange();
65             NormalizedMessage inMessage = messageExchange.createMessage();
66             inMessage.setProperty("proxy", proxy);
67             inMessage.setProperty("method", method);
68             inMessage.setProperty("args", args);
69
70             messageExchange.setInMessage(inMessage);
71             if (getDeliveryChannel().sendSync(messageExchange)) {
72                 NormalizedMessage outMessage = messageExchange.getOutMessage();
73                 return getBody(outMessage);
74             } else if ( messageExchange.getStatus() == ExchangeStatus.ERROR ) {
75                 throw messageExchange.getError();
76             }
77             return null;
78         }
79         catch (JBIException e) {
80             throw new RuntimeJBIException(e);
81         }
82     }
83 }
84
Popular Tags