KickJava   Java API By Example, From Geeks To Geeks.

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


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.InOnly;
25 import javax.jbi.messaging.NormalizedMessage;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.servicemix.components.util.ComponentSupport;
30 import org.apache.servicemix.jbi.RuntimeJBIException;
31
32 /**
33  * A Proxy factory which sends the method invocations into the JBI container
34  * for processing.
35  *
36  * @version $Revision: 426415 $
37  */

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