KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > oracle > jms > OracleJmsSupport


1 /*
2  * $Id: OracleJmsSupport.java 3937 2006-11-20 16:04:25Z lajos $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.oracle.jms;
12
13 import java.util.Map JavaDoc;
14
15 import javax.jms.Connection JavaDoc;
16 import javax.jms.ConnectionFactory JavaDoc;
17 import javax.jms.Destination JavaDoc;
18 import javax.jms.JMSException JavaDoc;
19 import javax.jms.MessageConsumer JavaDoc;
20 import javax.jms.Queue JavaDoc;
21 import javax.jms.QueueSession JavaDoc;
22 import javax.jms.Session JavaDoc;
23 import javax.jms.Topic JavaDoc;
24 import javax.jms.TopicSession JavaDoc;
25 import javax.naming.Context JavaDoc;
26
27 import oracle.jms.AQjmsSession;
28
29 import org.mule.providers.jms.Jms102bSupport;
30 import org.mule.providers.jms.JmsConnector;
31
32 /**
33  * Extends the standard Mule JMS Provider with functionality specific to Oracle's JMS
34  * implementation based on Advanced Queueing (Oracle AQ). Oracle 9i supports the JMS
35  * 1.0.2b specification while Oracle 10g supports JMS 1.1
36  *
37  * @author <a HREF="mailto:carlson@hotpop.com">Travis Carlson</a>
38  * @author henks
39  * @see OracleJmsConnector
40  * @see org.mule.providers.jms.Jms102bSupport
41  * @see <a HREF="http://otn.oracle.com/pls/db102/">Streams Advanced Queuing</a>
42  * @see <a
43  * HREF="http://www.lc.leidenuniv.nl/awcourse/oracle/appdev.920/a96587/jcreate.htm#103729">Oracle9i
44  * J2EE Compliance</a>
45  */

46 public class OracleJmsSupport extends Jms102bSupport
47 {
48
49     /**
50      * Since we can't access the endpoint's properties directly from the scope of
51      * this class, we save a copy of the properties in this local variable for easy
52      * reference.
53      */

54     private Map JavaDoc endpointProperties;
55
56     public OracleJmsSupport(JmsConnector connector,
57                             Context JavaDoc context,
58                             boolean jndiDestinations,
59                             boolean forceJndiDestinations)
60     {
61         super(connector, context, jndiDestinations, forceJndiDestinations);
62     }
63
64     /**
65      * Returns an OracleJmsConnection to masquerade the fact that there might be
66      * several javax.jms.Connections open (one per session).
67      *
68      * @see OracleJmsConnection
69      */

70     public Connection JavaDoc createConnection(ConnectionFactory JavaDoc connectionFactory) throws JMSException JavaDoc
71     {
72         return createConnection(connectionFactory, /* username */null, /* password */null);
73     }
74
75     /**
76      * Returns an OracleJmsConnection to masquerade the fact that there might be
77      * several javax.jms.Connections open (one per session).
78      *
79      * @see OracleJmsConnection
80      */

81     public javax.jms.Connection JavaDoc createConnection(ConnectionFactory JavaDoc connectionFactory,
82                                                  String JavaDoc username,
83                                                  String JavaDoc password) throws JMSException JavaDoc
84     {
85         return new OracleJmsConnection((OracleJmsConnector) connector);
86     }
87
88     /**
89      * In order to receive messages from a queue whose payload is an ADT (Oracle
90      * Advanced Data Type), we must pass the payload factory as a parameter when
91      * creating the receiver/subscriber.
92      *
93      * @see OracleJmsConnector#PAYLOADFACTORY_PROPERTY
94      */

95     public MessageConsumer JavaDoc createConsumer(Session JavaDoc session,
96                                           Destination JavaDoc destination,
97                                           String JavaDoc messageSelector,
98                                           boolean noLocal,
99                                           String JavaDoc durableName,
100                                           boolean topic) throws JMSException JavaDoc
101     {
102
103         Object JavaDoc payloadFactory = getPayloadFactory();
104         if (payloadFactory == null)
105         {
106             return super.createConsumer(session, destination, messageSelector, noLocal, durableName, topic);
107         }
108         else
109         {
110             if (topic && session instanceof TopicSession JavaDoc)
111             {
112                 if (durableName == null)
113                 {
114                     return ((AQjmsSession) session).createSubscriber((Topic JavaDoc) destination, messageSelector,
115                         noLocal);
116                 }
117                 else
118                 {
119                     return ((AQjmsSession) session).createDurableSubscriber((Topic JavaDoc) destination,
120                         durableName, messageSelector, noLocal, payloadFactory);
121                 }
122             }
123             else if (session instanceof QueueSession JavaDoc)
124             {
125                 if (messageSelector != null)
126                 {
127                     return ((AQjmsSession) session).createReceiver((Queue JavaDoc) destination, messageSelector,
128                         payloadFactory);
129                 }
130                 else
131                 {
132                     return ((AQjmsSession) session).createReceiver((Queue JavaDoc) destination, payloadFactory);
133                 }
134             }
135             else
136             {
137                 throw new IllegalArgumentException JavaDoc("Session and domain type do not match");
138             }
139         }
140     }
141
142     /**
143      * The standard Oracle JMS classes ({@code oracle.jms}) do not support dynamic
144      * (i.e., run-time) creation of queues. This is only possible through the
145      * (non-standard) administrative classes ({@code oracle.AQ}). Therefore this
146      * method, which calls {@code AQjmsSession.createQueue(name)} or
147      * {@code AQjmsSession.createTopic(name)} will inevitably fail. The failure
148      * <i>should</i> produce a {@code JMSException} but for some reason it doesn't
149      * (maybe an Oracle bug) and just returns null. In this case, we generate the
150      * appropriate exception.
151      */

152     public Destination JavaDoc createDestination(Session JavaDoc session, String JavaDoc name, boolean topic) throws JMSException JavaDoc
153     {
154         Destination JavaDoc dest = super.createDestination(session, name, topic);
155         if (dest != null)
156         {
157             return dest;
158         }
159         else
160         {
161             throw new JMSException JavaDoc(
162                     "Oracle JMS was unable to bind to the "
163                             + (topic ? "topic" : "queue")
164                             + ": "
165                             + name
166                             + " but gives no exception nor error message to explain why (that's what you get for using proprietary software...)");
167         }
168     }
169
170     /**
171      * The standard Oracle JMS classes ({@code oracle.jms}) do not support dynamic
172      * (i.e., run-time) creation of queues. This is only possible through the
173      * (non-standard) administrative classes ({@code oracle.AQ}). Therefore this
174      * method, which calls {@code AQjmsSession.createQueue(name)} or
175      * {@code AQjmsSession.createTopic(name)} will inevitably fail. The failure
176      * <i>should</i> produce a {@code JMSException} but for some reason it doesn't
177      * (maybe an Oracle bug) and just returns null. In this case, we generate the
178      * appropriate exception.
179      */

180     public Destination JavaDoc createTemporaryDestination(Session JavaDoc session, boolean topic) throws JMSException JavaDoc
181     {
182         Destination JavaDoc dest = super.createTemporaryDestination(session, topic);
183         if (dest != null)
184         {
185             return dest;
186         }
187         else
188         {
189             throw new JMSException JavaDoc("Unable to create temporary " + (topic ? "topic" : "queue"));
190         }
191     }
192
193     /**
194      * Get the payload factory class, if defined, from the connector or endpoint's
195      * properties.
196      *
197      * @see OracleJmsConnector#PAYLOADFACTORY_PROPERTY
198      */

199     public Object JavaDoc getPayloadFactory() throws JMSException JavaDoc
200     {
201
202         // Get the global property set on the connector, if any.
203
String JavaDoc payloadFactoryClass = ((OracleJmsConnector) connector).getPayloadFactory();
204
205         // If the property has been set for this endpoint, it overrides the global
206
// setting.
207
if ((endpointProperties != null)
208             && (endpointProperties.get(OracleJmsConnector.PAYLOADFACTORY_PROPERTY) != null))
209         {
210             payloadFactoryClass = (String JavaDoc) endpointProperties.get(OracleJmsConnector.PAYLOADFACTORY_PROPERTY);
211         }
212
213         Object JavaDoc payloadFactory = null;
214         if (payloadFactoryClass != null)
215         {
216             Throwable JavaDoc ex = null;
217             try
218             {
219                 // TODO ClassUtils call is more suitable here
220
payloadFactory = Class.forName(payloadFactoryClass).newInstance();
221             }
222             catch (ClassNotFoundException JavaDoc e)
223             {
224                 ex = e;
225             }
226             catch (IllegalAccessException JavaDoc e)
227             {
228                 ex = e;
229             }
230             catch (InstantiationException JavaDoc e)
231             {
232                 ex = e;
233             }
234             if (ex != null)
235             {
236                 throw new JMSException JavaDoc("Unable to instantiate payload factory class " + payloadFactoryClass
237                         + ": " + ex.getMessage());
238             }
239         }
240         return payloadFactory;
241     }
242
243     public Map JavaDoc getEndpointProperties()
244     {
245         return endpointProperties;
246     }
247
248     public void setEndpointProperties(Map JavaDoc endpointProperties)
249     {
250         this.endpointProperties = endpointProperties;
251     }
252
253 }
254
Popular Tags