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