KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jsr181 > xfire > JbiProxyFactoryBean


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.jsr181.xfire;
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.component.ComponentContext;
25 import javax.naming.InitialContext JavaDoc;
26 import javax.xml.namespace.QName JavaDoc;
27
28 import org.apache.servicemix.client.ClientFactory;
29 import org.apache.servicemix.client.DefaultServiceMixClient;
30 import org.apache.servicemix.client.ServiceMixClient;
31 import org.apache.servicemix.client.ServiceMixClientFacade;
32 import org.apache.servicemix.jbi.container.JBIContainer;
33 import org.apache.servicemix.jsr181.Jsr181LifeCycle;
34 import org.codehaus.xfire.XFire;
35 import org.springframework.beans.factory.FactoryBean;
36
37
38 /**
39  *
40  * @author chirino
41  * @version $Revision: 407481 $
42  * @org.apache.xbean.XBean element="proxy"
43  * description="A jsr181 proxy"
44  *
45  */

46 public class JbiProxyFactoryBean implements FactoryBean {
47
48     private String JavaDoc name = ClientFactory.DEFAULT_JNDI_NAME;
49     private JBIContainer container;
50     private ClientFactory factory;
51     private ComponentContext context;
52     private Class JavaDoc type;
53     private Object JavaDoc proxy;
54     private InvocationHandler JavaDoc jbiInvocationHandler;
55     private QName JavaDoc service;
56     private QName JavaDoc interfaceName;
57     private String JavaDoc endpoint;
58     
59     public Object JavaDoc getObject() throws Exception JavaDoc {
60         if( proxy == null ) {
61             proxy = createProxy();
62         }
63         return proxy;
64     }
65
66     private Object JavaDoc createProxy() throws Exception JavaDoc {
67         
68         return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class JavaDoc[]{type}, new InvocationHandler JavaDoc(){
69             public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
70                 InvocationHandler JavaDoc next = getJBIInvocationHandler();
71                 return next.invoke(proxy, method, args);
72             }
73         });
74         
75     }
76     
77     synchronized private InvocationHandler JavaDoc getJBIInvocationHandler() throws Exception JavaDoc {
78         if( jbiInvocationHandler == null ) {
79             XFire xfire = Jsr181LifeCycle.createXFire(getInternalContext());
80             Object JavaDoc o = JbiProxy.create(xfire, context, interfaceName, service, endpoint, type);
81             jbiInvocationHandler = Proxy.getInvocationHandler(o);
82         }
83         return jbiInvocationHandler;
84     }
85
86     public Class JavaDoc getObjectType() {
87         return type;
88     }
89
90     public boolean isSingleton() {
91         return true;
92     }
93     
94     protected ComponentContext getInternalContext() throws Exception JavaDoc {
95         if (context == null) {
96             if (factory == null) {
97                 if (context != null) {
98                     factory = new ClientFactory() {
99                         public ServiceMixClient createClient() throws JBIException {
100                             return new ServiceMixClientFacade(context);
101                         }
102                     };
103                 } else if (container != null) {
104                     factory = container.getClientFactory();
105                 } else {
106                     factory = (ClientFactory) new InitialContext JavaDoc().lookup(name);
107                 }
108             }
109             DefaultServiceMixClient client = new DefaultServiceMixClient(container);
110             context = client.getContext();
111         }
112         return context;
113     }
114
115     public Class JavaDoc getType() {
116         return type;
117     }
118
119     public void setType(Class JavaDoc type) {
120         this.type = type;
121     }
122
123     public String JavaDoc getEndpoint() {
124         return endpoint;
125     }
126
127     public void setEndpoint(String JavaDoc endpointName) {
128         this.endpoint = endpointName;
129     }
130
131     public QName JavaDoc getInterfaceName() {
132         return interfaceName;
133     }
134
135     public void setInterfaceName(QName JavaDoc interfaceName) {
136         this.interfaceName = interfaceName;
137     }
138
139     public QName JavaDoc getService() {
140         return service;
141     }
142
143     public void setService(QName JavaDoc service) {
144         this.service = service;
145     }
146
147     /**
148      * @return the context
149      */

150     public ComponentContext getContext() {
151         return context;
152     }
153
154     /**
155      * @param context the context to set
156      */

157     public void setContext(ComponentContext context) {
158         this.context = context;
159     }
160
161     /**
162      * @return the container
163      */

164     public JBIContainer getContainer() {
165         return container;
166     }
167
168     /**
169      * @param container the container to set
170      */

171     public void setContainer(JBIContainer container) {
172         this.container = container;
173     }
174
175     /**
176      * @return the factory
177      */

178     public ClientFactory getFactory() {
179         return factory;
180     }
181
182     /**
183      * @param factory the factory to set
184      */

185     public void setFactory(ClientFactory factory) {
186         this.factory = factory;
187     }
188
189     /**
190      * @return the name
191      */

192     public String JavaDoc getName() {
193         return name;
194     }
195
196     /**
197      * @param name the name to set
198      */

199     public void setName(String JavaDoc name) {
200         this.name = name;
201     }
202
203 }
204
Popular Tags