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.servicemix.cxfse;
018
019 import javax.jbi.component.ComponentContext;
020 import javax.jbi.messaging.DeliveryChannel;
021 import javax.naming.InitialContext;
022 import javax.xml.namespace.QName;
023
024 import org.apache.activemq.util.IdGenerator;
025 import org.apache.cxf.Bus;
026 import org.apache.cxf.BusFactory;
027 import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
028 import org.apache.cxf.transport.ConduitInitiatorManager;
029 import org.apache.cxf.transport.jbi.JBITransportFactory;
030 import org.apache.servicemix.client.ClientFactory;
031 import org.apache.servicemix.client.ServiceMixClient;
032 import org.apache.servicemix.jbi.container.JBIContainer;
033
034 import org.springframework.beans.factory.DisposableBean;
035 import org.springframework.beans.factory.FactoryBean;
036 import org.springframework.beans.factory.InitializingBean;
037
038 /**
039 *
040 * @author ffang
041 * @org.apache.xbean.XBean element="proxy" description="A CXF proxy"
042 *
043 */
044 public class CxfSeProxyFactoryBean implements FactoryBean, InitializingBean,
045 DisposableBean {
046
047 private String name = ClientFactory.DEFAULT_JNDI_NAME;
048
049 private JBIContainer container;
050
051 private ClientFactory factory;
052
053 private ComponentContext context;
054
055 private Class type;
056
057 private Object proxy;
058
059 private QName service;
060
061 private QName interfaceName;
062
063 private String endpoint;
064
065 private boolean propagateSecuritySubject;
066
067 private ServiceMixClient client;
068
069 private boolean useJBIWrapper = true;
070
071 public Object getObject() throws Exception {
072 if (proxy == null) {
073 proxy = createProxy();
074 }
075 return proxy;
076 }
077
078 private Object createProxy() throws Exception {
079 JaxWsProxyFactoryBean cf = new JaxWsProxyFactoryBean();
080 cf.setServiceName(getService());
081 if (getEndpoint() != null) {
082 cf.setEndpointName(new QName(getService().getNamespaceURI(), getEndpoint()));
083 }
084 cf.setServiceClass(type);
085 cf.setAddress("jbi://" + new IdGenerator().generateSanitizedId());
086 if (isUseJBIWrapper()) {
087 cf.setBindingId(org.apache.cxf.binding.jbi.JBIConstants.NS_JBI_BINDING);
088 }
089 Bus bus = BusFactory.getDefaultBus();
090 JBITransportFactory jbiTransportFactory = (JBITransportFactory) bus
091 .getExtension(ConduitInitiatorManager.class)
092 .getConduitInitiator(CxfSeComponent.JBI_TRANSPORT_ID);
093 if (getInternalContext() != null) {
094 DeliveryChannel dc = getInternalContext().getDeliveryChannel();
095 if (dc != null) {
096 jbiTransportFactory.setDeliveryChannel(dc);
097 }
098 }
099 return cf.create();
100 }
101
102 public Class getObjectType() {
103 return type;
104 }
105
106 public boolean isSingleton() {
107 return true;
108 }
109
110 protected ComponentContext getInternalContext() throws Exception {
111 if (context == null) {
112 if (factory == null) {
113 if (container != null) {
114 factory = container.getClientFactory();
115 } else {
116 factory = (ClientFactory) new InitialContext().lookup(name);
117 }
118 }
119 client = factory.createClient();
120 context = client.getContext();
121 }
122 return context;
123 }
124
125 public Class getType() {
126 return type;
127 }
128
129 public void setType(Class type) {
130 this.type = type;
131 }
132
133 public String getEndpoint() {
134 return endpoint;
135 }
136
137 public void setEndpoint(String endpointName) {
138 this.endpoint = endpointName;
139 }
140
141 public QName getInterfaceName() {
142 return interfaceName;
143 }
144
145 public void setInterfaceName(QName interfaceName) {
146 this.interfaceName = interfaceName;
147 }
148
149 public QName getService() {
150 return service;
151 }
152
153 public void setService(QName service) {
154 this.service = service;
155 }
156
157 /**
158 * @return the context
159 */
160 public ComponentContext getContext() {
161 return context;
162 }
163
164 /**
165 * @param context
166 * the context to set
167 */
168 public void setContext(ComponentContext context) {
169 this.context = context;
170 }
171
172 /**
173 * @return the container
174 */
175 public JBIContainer getContainer() {
176 return container;
177 }
178
179 /**
180 * @param container
181 * the container to set
182 */
183 public void setContainer(JBIContainer container) {
184 this.container = container;
185 }
186
187 /**
188 * @return the factory
189 */
190 public ClientFactory getFactory() {
191 return factory;
192 }
193
194 /**
195 * @param factory
196 * the factory to set
197 */
198 public void setFactory(ClientFactory factory) {
199 this.factory = factory;
200 }
201
202 /**
203 * @return the name
204 */
205 public String getName() {
206 return name;
207 }
208
209 /**
210 * @param name
211 * the name to set
212 */
213 public void setName(String name) {
214 this.name = name;
215 }
216
217 /**
218 * @return the propagateSecuritySubject
219 */
220 public boolean isPropagateSecuritySubject() {
221 return propagateSecuritySubject;
222 }
223
224 /**
225 * @param propagateSecuritySubject
226 * the propagateSecuritySubject to set
227 */
228 public void setPropagateSecuritySubject(boolean propagateSecuritySubject) {
229 this.propagateSecuritySubject = propagateSecuritySubject;
230 }
231
232 public void afterPropertiesSet() throws Exception {
233 if (type == null) {
234 throw new IllegalArgumentException("type must be set");
235 }
236 }
237
238 public void destroy() throws Exception {
239 if (client != null) {
240 client.close();
241 client = null;
242 }
243 }
244
245 public void setUseJBIWrapper(boolean useJBIWrapper) {
246 this.useJBIWrapper = useJBIWrapper;
247 }
248
249 public boolean isUseJBIWrapper() {
250 return useJBIWrapper;
251 }
252
253 }