001: /*
002: * Copyright 2004,2005 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.synapse.transport.jms;
017:
018: import org.apache.axis2.transport.OutTransportInfo;
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021:
022: import javax.jms.ConnectionFactory;
023: import javax.jms.Destination;
024: import javax.naming.Context;
025: import javax.naming.InitialContext;
026: import javax.naming.NameNotFoundException;
027: import javax.naming.NamingException;
028: import java.util.Hashtable;
029:
030: /**
031: * The JMS OutTransportInfo is a holder of information to send an outgoing message
032: * (e.g. a Response) to a JMS destination. Thus at a minimum a reference to a
033: * ConnectionFactory and a Destination are held
034: */
035: public class JMSOutTransportInfo implements OutTransportInfo {
036:
037: private static final Log log = LogFactory
038: .getLog(JMSOutTransportInfo.class);
039:
040: /**
041: * this is a reference to the underlying JMS connection factory when sending messages
042: * through connection factories not defined to the transport sender
043: */
044: private ConnectionFactory connectionFactory = null;
045: /**
046: * this is a reference to a JMS Connection Factory instance, which has a reference
047: * to the underlying actual connection factory, an open connection to the JMS provider
048: * and optionally a session already available for use
049: */
050: private JMSConnectionFactory jmsConnectionFactory = null;
051: /** the Destination queue or topic for the outgoing message */
052: private Destination destination = null;
053: /** the Destination queue or topic for the outgoing message i.e. JMSConstants.DESTINATION_TYPE_QUEUE, DESTINATION_TYPE_TOPIC */
054: private String destinationType = JMSConstants.DESTINATION_TYPE_QUEUE;
055: /** the EPR properties when the out-transport info is generated from a target EPR */
056: private Hashtable properties = null;
057: /** the target EPR string where applicable */
058: private String targetEPR = null;
059: private String contentType = null;
060:
061: /**
062: * Creates an instance using the given connection factory and destination
063: *
064: * @param connectionFactory the connection factory
065: * @param dest the destination
066: */
067: JMSOutTransportInfo(ConnectionFactory connectionFactory,
068: Destination dest) {
069: this .connectionFactory = connectionFactory;
070: this .destination = dest;
071: }
072:
073: /**
074: * Creates an instance using the given JMS connection factory and destination
075: *
076: * @param jmsConnectionFactory the JMS connection factory
077: * @param dest the destination
078: */
079: JMSOutTransportInfo(JMSConnectionFactory jmsConnectionFactory,
080: Destination dest) {
081: this .jmsConnectionFactory = jmsConnectionFactory;
082: this .destination = dest;
083: }
084:
085: /**
086: * Creates and instance using the given URL
087: *
088: * @param targetEPR the target EPR
089: */
090: JMSOutTransportInfo(String targetEPR) {
091: this .targetEPR = targetEPR;
092: if (!targetEPR.startsWith(JMSConstants.JMS_PREFIX)) {
093: handleException("Invalid prefix for a JMS EPR : "
094: + targetEPR);
095: } else {
096: properties = JMSUtils.getProperties(targetEPR);
097: String destinationType = (String) properties
098: .get(JMSConstants.DEST_PARAM_TYPE);
099: setDestinationType(destinationType);
100: }
101: }
102:
103: /**
104: * Provides a lazy load when created with a target EPR. This method performs actual
105: * lookup for the connection factory and desination
106: */
107: public void loadConnectionFactoryFromProperies() {
108: if (properties != null) {
109: Context context = null;
110: try {
111: context = new InitialContext(properties);
112: } catch (NamingException e) {
113: handleException(
114: "Could not get an initial context using "
115: + properties, e);
116: }
117: connectionFactory = getConnectionFactory(context,
118: properties);
119: destination = getDestination(context, targetEPR);
120: }
121: }
122:
123: /**
124: * Get the referenced ConnectionFactory using the properties from the context
125: *
126: * @param context the context to use for lookup
127: * @param props the properties which contains the JNDI name of the factory
128: * @return the connection factory
129: */
130: private ConnectionFactory getConnectionFactory(Context context,
131: Hashtable props) {
132: try {
133:
134: String conFacJndiName = (String) props
135: .get(JMSConstants.CONFAC_JNDI_NAME_PARAM);
136: if (conFacJndiName != null) {
137: return (ConnectionFactory) context
138: .lookup(conFacJndiName);
139: } else {
140: handleException("Connection Factory JNDI name cannot be determined");
141: }
142: } catch (NamingException e) {
143: handleException("Connection Factory JNDI name cannot be determined");
144: }
145: return null;
146: }
147:
148: /**
149: * Get the JMS destination specified by the given URL from the context
150: *
151: * @param context the Context to lookup
152: * @param url URL
153: * @return the JMS destination, or null if it does not exist
154: */
155: private Destination getDestination(Context context, String url) {
156: String destinationName = JMSUtils.getDestination(url);
157: try {
158: return (Destination) context.lookup(destinationName);
159: } catch (NameNotFoundException e) {
160: if (log.isDebugEnabled()) {
161: log.debug("Cannot locate destination : "
162: + destinationName + " using " + url);
163: }
164: } catch (NamingException e) {
165: handleException("Cannot locate destination : "
166: + destinationName + " using " + url, e);
167: }
168: return null;
169: }
170:
171: /**
172: * Look up for the given destination
173: * @param replyDest
174: * @return
175: */
176: public Destination getReplyDestination(String replyDest) {
177: try {
178: return (Destination) jmsConnectionFactory.getContext()
179: .lookup(replyDest);
180: } catch (NameNotFoundException e) {
181: if (log.isDebugEnabled()) {
182: log.debug("Cannot locate reply destination : "
183: + replyDest, e);
184: }
185: } catch (NamingException e) {
186: handleException("Cannot locate reply destination : "
187: + replyDest, e);
188: }
189: return null;
190: }
191:
192: private void handleException(String s) {
193: log.error(s);
194: throw new AxisJMSException(s);
195: }
196:
197: private void handleException(String s, Exception e) {
198: log.error(s, e);
199: throw new AxisJMSException(s, e);
200: }
201:
202: public Destination getDestination() {
203: return destination;
204: }
205:
206: public ConnectionFactory getConnectionFactory() {
207: return connectionFactory;
208: }
209:
210: public JMSConnectionFactory getJmsConnectionFactory() {
211: return jmsConnectionFactory;
212: }
213:
214: public void setContentType(String contentType) {
215: this .contentType = contentType;
216: }
217:
218: public Hashtable getProperties() {
219: return properties;
220: }
221:
222: public String getTargetEPR() {
223: return targetEPR;
224: }
225:
226: public String getDestinationType() {
227: return destinationType;
228: }
229:
230: public void setDestinationType(String destinationType) {
231: if (destinationType != null) {
232: this.destinationType = destinationType;
233: }
234: }
235:
236: }
|