KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jms > JmsEndpoint


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.jms;
18
19 import java.io.InputStream JavaDoc;
20 import java.lang.reflect.Constructor JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import javax.jbi.component.ComponentLifeCycle;
25 import javax.jbi.servicedesc.ServiceEndpoint;
26 import javax.jms.ConnectionFactory JavaDoc;
27 import javax.jms.Destination JavaDoc;
28 import javax.resource.spi.ActivationSpec JavaDoc;
29 import javax.resource.spi.BootstrapContext JavaDoc;
30 import javax.resource.spi.ResourceAdapter JavaDoc;
31 import javax.wsdl.Definition;
32 import javax.wsdl.Port;
33 import javax.wsdl.Service;
34
35 import org.apache.servicemix.common.ExchangeProcessor;
36 import org.apache.servicemix.jbi.security.auth.AuthenticationService;
37 import org.apache.servicemix.jbi.security.keystore.KeystoreManager;
38 import org.apache.servicemix.soap.SoapEndpoint;
39
40 /**
41  *
42  * @author gnodet
43  * @version $Revision: 433626 $
44  * @org.apache.xbean.XBean element="endpoint"
45  * description="A jms endpoint"
46  *
47  */

48 public class JmsEndpoint extends SoapEndpoint {
49     
50     //
51
// Jms informations
52
//
53
protected String JavaDoc initialContextFactory;
54     protected String JavaDoc jndiProviderURL;
55     protected String JavaDoc destinationStyle;
56     protected String JavaDoc jndiConnectionFactoryName;
57     protected String JavaDoc jndiDestinationName;
58     protected String JavaDoc jmsProviderDestinationName;
59     //
60
// Spring configuration
61
//
62
protected ConnectionFactory JavaDoc connectionFactory;
63     protected Destination JavaDoc destination;
64     protected String JavaDoc processorName;
65     //
66
// JCA config
67
//
68
protected ResourceAdapter JavaDoc resourceAdapter;
69     protected ActivationSpec JavaDoc activationSpec;
70     protected BootstrapContext JavaDoc bootstrapContext;
71     protected boolean synchronous;
72     // Other configuration flags
73
protected boolean needJavaIdentifiers;
74     
75     /**
76      * The BootstrapContext to use for a JCA consumer endpoint.
77      *
78      * @return the bootstrapContext
79      */

80     public BootstrapContext JavaDoc getBootstrapContext() {
81         return bootstrapContext;
82     }
83
84     /**
85      * @param bootstrapContext the bootstrapContext to set
86      */

87     public void setBootstrapContext(BootstrapContext JavaDoc bootstrapContext) {
88         this.bootstrapContext = bootstrapContext;
89     }
90
91     /**
92      * For a JCA consumer endpoint, indicates if the JBI exchange
93      * should be sent synchronously or asynchronously.
94      * This changes the transaction boundary.
95      *
96      * @return the synchronous
97      */

98     public boolean isSynchronous() {
99         return synchronous;
100     }
101
102     /**
103      * @param synchronous the synchronous to set
104      */

105     public void setSynchronous(boolean synchronous) {
106         this.synchronous = synchronous;
107     }
108
109     /**
110      * The ActivatioSpec to use on this JCA consumer endpoint.
111      *
112      * @return the activationSpec
113      */

114     public ActivationSpec JavaDoc getActivationSpec() {
115         return activationSpec;
116     }
117
118     /**
119      * @param activationSpec the activationSpec to set
120      */

121     public void setActivationSpec(ActivationSpec JavaDoc activationSpec) {
122         this.activationSpec = activationSpec;
123     }
124
125     /**
126      * The ResourceAdapter to use on this JCA consumer endpoint.
127      *
128      * @return the resourceAdapter
129      */

130     public ResourceAdapter JavaDoc getResourceAdapter() {
131         return resourceAdapter;
132     }
133
134     /**
135      * @param resourceAdapter the resourceAdapter to set
136      */

137     public void setResourceAdapter(ResourceAdapter JavaDoc resourceAdapter) {
138         this.resourceAdapter = resourceAdapter;
139     }
140
141     /**
142      * The class name of the JNDI InitialContextFactory to use.
143      *
144      * @return Returns the initialContextFactory.
145      */

146     public String JavaDoc getInitialContextFactory() {
147         return initialContextFactory;
148     }
149
150     /**
151      * @param initialContextFactory The initialContextFactory to set.
152      */

153     public void setInitialContextFactory(String JavaDoc initialContextFactory) {
154         this.initialContextFactory = initialContextFactory;
155     }
156
157     /**
158      * The name of the destination create by a call to
159      * <code>Session.createQueue</code> or <code>Session.createTopic</code>.
160      * This property is used when <code>destination</code> and
161      * <code>jndiDestinationName</code> are
162      * both <code>null</code>.
163      *
164      * @return Returns the jmsProviderDestinationName.
165      */

166     public String JavaDoc getJmsProviderDestinationName() {
167         return jmsProviderDestinationName;
168     }
169
170     /**
171      * @param jmsProviderDestinationName The jmsProviderDestinationName to set.
172      */

173     public void setJmsProviderDestinationName(String JavaDoc jmsProviderDestinationName) {
174         this.jmsProviderDestinationName = jmsProviderDestinationName;
175     }
176
177     /**
178      * The name of the JMS ConnectionFactory to lookup in JNDI.
179      * Used if <code>connectionFactory</code> is <code>null</code>
180      *
181      * @return Returns the jndiConnectionFactoryName.
182      */

183     public String JavaDoc getJndiConnectionFactoryName() {
184         return jndiConnectionFactoryName;
185     }
186
187     /**
188      * @param jndiConnectionFactoryName The jndiConnectionFactoryName to set.
189      */

190     public void setJndiConnectionFactoryName(String JavaDoc jndiConnectionFactoryName) {
191         this.jndiConnectionFactoryName = jndiConnectionFactoryName;
192     }
193
194     /**
195      * The name of the JMS Destination to lookup in JNDI.
196      * Used if <code>destination</code> is <code>null</code>.
197      *
198      * @return Returns the jndiDestinationName.
199      */

200     public String JavaDoc getJndiDestinationName() {
201         return jndiDestinationName;
202     }
203
204     /**
205      * @param jndiDestinationName The jndiDestinationName to set.
206      */

207     public void setJndiDestinationName(String JavaDoc jndiDestinationName) {
208         this.jndiDestinationName = jndiDestinationName;
209     }
210
211     /**
212      * The provider URL used to create the JNDI context.
213      *
214      * @return Returns the jndiProviderURL.
215      */

216     public String JavaDoc getJndiProviderURL() {
217         return jndiProviderURL;
218     }
219
220     /**
221      * @param jndiProviderURL The jndiProviderURL to set.
222      */

223     public void setJndiProviderURL(String JavaDoc jndiProviderURL) {
224         this.jndiProviderURL = jndiProviderURL;
225     }
226
227     /**
228      * Used to select the destination type used with the jmsProviderDestinationName.
229      * Can be <code>queue</code> or <code>topic</code>.
230      *
231      * @return Returns the destinationStyle.
232      */

233     public String JavaDoc getDestinationStyle() {
234         return destinationStyle;
235     }
236
237     /**
238      * @param destinationStyle The destinationStyle to set.
239      */

240     public void setDestinationStyle(String JavaDoc destinationStyle) {
241         this.destinationStyle = destinationStyle;
242     }
243
244     /**
245      * A configured ConnectionFactory to use on this endpoint.
246      *
247      * @return Returns the connectionFactory.
248      */

249     public ConnectionFactory JavaDoc getConnectionFactory() {
250         return connectionFactory;
251     }
252
253     /**
254      * @param connectionFactory The connectionFactory to set.
255      */

256     public void setConnectionFactory(ConnectionFactory JavaDoc connectionFactory) {
257         this.connectionFactory = connectionFactory;
258     }
259     
260     /**
261      * A configured Destination to use on this endpoint.
262      *
263      * @return Returns the destination.
264      */

265     public Destination JavaDoc getDestination() {
266         return destination;
267     }
268
269     /**
270      * @param destination The destination to set.
271      */

272     public void setDestination(Destination JavaDoc destination) {
273         this.destination = destination;
274     }
275     
276     /**
277      * @return if jms properties should be spec compliant
278      */

279     public boolean isNeedJavaIdentifiers() {
280         return needJavaIdentifiers;
281     }
282     
283     /**
284      * @param needJavaIdentifiers if jms properties should be spec compliant
285      */

286     public void setNeedJavaIdentifiers(boolean needJavaIdentifiers) {
287         this.needJavaIdentifiers = needJavaIdentifiers;
288     }
289
290     /**
291      * The role of this endpoint.
292      * Must be <code>consumer</code> or <code>provider</code>.
293      *
294      * @org.apache.xbean.Property alias="role"
295      * @param role
296      */

297     public void setRoleAsString(String JavaDoc role) {
298         super.setRoleAsString(role);
299     }
300
301     protected ExchangeProcessor createProviderProcessor() {
302         return createProcessor("provider");
303     }
304
305     protected ExchangeProcessor createConsumerProcessor() {
306         return createProcessor("consumer");
307     }
308     
309     protected ExchangeProcessor createProcessor(String JavaDoc type) {
310         try {
311             String JavaDoc procName = processorName;
312             if (processorName == null) {
313                 JmsLifeCycle lf = (JmsLifeCycle) getServiceUnit().getComponent().getLifeCycle();
314                 procName = lf.getConfiguration().getProcessorName();
315             }
316             String JavaDoc uri = "META-INF/services/org/apache/servicemix/jms/" + procName;
317             InputStream JavaDoc in = loadResource(uri);
318             Properties JavaDoc props = new Properties JavaDoc();
319             props.load(in);
320             String JavaDoc className = props.getProperty(type);
321             Class JavaDoc cl = loadClass(className);
322             Constructor JavaDoc cns = cl.getConstructor(new Class JavaDoc[] { getClass() });
323             return (ExchangeProcessor) cns.newInstance(new Object JavaDoc[] { this });
324         } catch (Exception JavaDoc e) {
325             throw new RuntimeException JavaDoc("Could not create processor of type " + type + " and name " + processorName, e);
326         }
327     }
328
329     /**
330      * Attempts to load the class on the current thread context class loader or
331      * the class loader which loaded us
332      */

333     protected Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
334         ClassLoader JavaDoc contextClassLoader = Thread.currentThread().getContextClassLoader();
335         if (contextClassLoader != null) {
336             try {
337                 return contextClassLoader.loadClass(name);
338             }
339             catch (ClassNotFoundException JavaDoc e) {
340             }
341         }
342         return getClass().getClassLoader().loadClass(name);
343     }
344
345     /**
346      * Loads the resource from the given URI
347      */

348     protected InputStream JavaDoc loadResource(String JavaDoc uri) {
349         // lets try the thread context class loader first
350
InputStream JavaDoc in = Thread.currentThread().getContextClassLoader().getResourceAsStream(uri);
351         if (in == null) {
352             in = getClass().getClassLoader().getResourceAsStream(uri);
353             if (in == null) {
354                 logger.debug("Could not find resource: " + uri);
355             }
356         }
357         return in;
358     }
359
360     protected ServiceEndpoint createExternalEndpoint() {
361         return new JmsExternalEndpoint(this);
362     }
363
364     protected void overrideDefinition(Definition def) {
365         Service svc = null;
366         Port port = null;
367         if (targetService != null && targetEndpoint != null) {
368             svc = def.getService(targetService);
369             port = (svc != null) ? svc.getPort(targetEndpoint) : null;
370         } else if (targetService != null) {
371             svc = def.getService(targetService);
372             if (svc != null) {
373                 Iterator JavaDoc it = svc.getPorts().values().iterator();
374                 port = (it.hasNext()) ? (Port) it.next() : null;
375             }
376         } else if (targetInterfaceName != null) {
377             Iterator JavaDoc it = def.getServices().values().iterator();
378             svc = it .hasNext() ? (Service) it.next() : null;
379             if (svc != null) {
380                 it = svc.getPorts().values().iterator();
381                 port = (it.hasNext()) ? (Port) it.next() : null;
382             }
383         } else {
384             svc = def.getService(service);
385             port = (svc != null) ? svc.getPort(endpoint) : null;
386         }
387         if (port != null) {
388             port.getExtensibilityElements().clear();
389             /*
390             if (isSoap()) {
391                 SOAPAddress address = new SOAPAddressImpl();
392                 address.setLocationURI(getLocationURI());
393                 port.addExtensibilityElement(address);
394                 def.addNamespace("soap", "http://schemas.xmlsoap.org/wsdl/soap/");
395             } else {
396                 HTTPAddress address = new HTTPAddressImpl();
397                 address.setLocationURI(getLocationURI());
398                 port.addExtensibilityElement(address);
399                 def.addNamespace("http", "http://schemas.xmlsoap.org/wsdl/http/");
400             }
401             */

402             // TODO: add binding information
403
svc.getPorts().clear();
404             svc.addPort(port);
405             definition = def;
406         }
407     }
408
409     public String JavaDoc toString() {
410         return "JMSEndpoint[service: " + service + ", " +
411                 "endpoint: " + endpoint + ", " +
412                 "address: " + jndiDestinationName + "(" + destinationStyle + "), " +
413                 "soap: " + soap + "]";
414     }
415
416     /**
417      * Specifies the processor family to use for this endpoint.
418      * Can be:
419      * <ul>
420      * <li><code>multiplexing</code> (default)</li>
421      * <li><code>standard</code></li>
422      * <li><code>jca</code></li>
423      * </ul>
424      *
425      * @return Returns the processorName.
426      */

427     public String JavaDoc getProcessorName() {
428         return processorName;
429     }
430
431     /**
432      * @param processorName The processorName to set.
433      */

434     public void setProcessorName(String JavaDoc processorName) {
435         this.processorName = processorName;
436     }
437     
438     public JmsConfiguration getConfiguration() {
439         JmsLifeCycle lifeCycle = (JmsLifeCycle) getServiceUnit().getComponent().getLifeCycle();
440         return lifeCycle.getConfiguration();
441     }
442
443     public AuthenticationService getAuthenticationService() {
444         ComponentLifeCycle lf = getServiceUnit().getComponent().getLifeCycle();
445         return ((JmsLifeCycle) lf).getAuthenticationService();
446     }
447
448     public KeystoreManager getKeystoreManager() {
449         ComponentLifeCycle lf = getServiceUnit().getComponent().getLifeCycle();
450         return ((JmsLifeCycle) lf).getKeystoreManager();
451     }
452
453 }
Popular Tags