001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)JBossMQConnectionHelper.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.binding.proxy.jms;
030:
031: import com.sun.jbi.binding.proxy.connection.ConnectionException;
032: import com.sun.jbi.binding.proxy.connection.ConnectionHelper;
033:
034: import java.util.Hashtable;
035: import java.util.logging.Logger;
036:
037: import javax.jms.ConnectionFactory;
038: import javax.jms.Queue;
039: import javax.jms.Topic;
040:
041: import javax.management.MBeanServerConnection;
042: import javax.management.ObjectName;
043:
044: import javax.naming.Context;
045: import javax.naming.InitialContext;
046:
047: /**
048: * Helper for ConnectionManagers
049: *
050: * @author Sun Microsystems, Inc.
051: */
052: public class JBossMQConnectionHelper implements ConnectionHelper {
053: /**
054: *
055: */
056:
057: /**
058: *
059: */
060: String mHost;
061:
062: /**
063: *
064: */
065:
066: /**
067: *
068: */
069: String mPort;
070:
071: /**
072: *
073: */
074:
075: /**
076: *
077: */
078: ConnectionFactory mFactory;
079:
080: /**
081: *
082: */
083:
084: /**
085: *
086: */
087: private static final String DEFAULT_HOST = "localhost";
088:
089: /**
090: *
091: */
092:
093: /**
094: *
095: */
096: private static final String DEFAULT_PORT = "1100";
097:
098: /**
099: *
100: */
101:
102: /**
103: *
104: */
105: private static final String PROTOCOL = "jnp://";
106:
107: /**
108: *
109: */
110:
111: /**
112: *
113: */
114: private Context mContext = null;
115:
116: /**
117: *
118: */
119:
120: /**
121: *
122: */
123: private Logger mLog;
124:
125: /**
126: * Creates a new JBossMQConnectionHelper object.
127: *
128: * @param host
129: * @param port
130: */
131: public JBossMQConnectionHelper(String host, String port) {
132: mHost = host;
133: mPort = port;
134: mLog = Logger.getLogger("com.sun.jbi.binding.proxy.jms");
135: prepare();
136: }
137:
138: /**
139: *
140: */
141: public void prepare() {
142: Hashtable env = new Hashtable();
143:
144: try {
145: String ctxfac = System.getProperty(
146: "com.sun.jbi.binding.proxy.connection.factory",
147: "org.jnp.interfaces.NamingContextFactory");
148: String urlprefix = System.getProperty(
149: "com.sun.jbi.binding.proxy.connection.urlprefix",
150: "jboss.naming:org.jnp.interfaces");
151: String user = System.getProperty(
152: "com.sun.jbi.binding.proxy.connection.user",
153: "admin");
154: String password = System.getProperty(
155: "com.sun.jbi.binding.proxy.connection.password",
156: "admin");
157: env.put(Context.INITIAL_CONTEXT_FACTORY, ctxfac);
158: env.put(Context.URL_PKG_PREFIXES, urlprefix);
159: if ((user != null) || (!user.trim().equals(""))) {
160: env.put(Context.SECURITY_PRINCIPAL, user);
161: }
162: if (password != null) {
163: env.put(Context.SECURITY_CREDENTIALS, password);
164: }
165:
166: if ((mHost == null) || (mHost.equals(""))) {
167: mHost = DEFAULT_HOST;
168: }
169:
170: if ((mPort == null) || (mPort.equals(""))) {
171: mPort = DEFAULT_PORT;
172: }
173:
174: String providerurl = "localhost" + ":" + mPort;
175:
176: env.put(Context.PROVIDER_URL, providerurl);
177: mLog.info("Going to get the context");
178: mContext = new InitialContext(env);
179:
180: mLog.info("Got the context");
181: mLog.info(env.toString());
182: } catch (Exception e) {
183: e.printStackTrace();
184: mLog.severe("Cannot get Initial context using "
185: + env.toString());
186: mContext = null;
187: }
188: }
189:
190: /**
191: * Return a JMS ConnectionFactory.
192: *
193: * @return ConnectionFactory
194: *
195: * @throws ConnectionException
196: */
197: public synchronized ConnectionFactory getConnectionFactory()
198: throws ConnectionException {
199: if (mContext == null) {
200: throw new ConnectionException("No context");
201: }
202:
203: ConnectionFactory confac = null;
204:
205: try {
206: confac = (ConnectionFactory) mContext
207: .lookup("XAConnectionFactory");
208: } catch (Exception e) {
209: e.printStackTrace();
210: mLog.severe("Cannot lookup factory ConnectionFactory");
211: }
212:
213: return confac;
214: }
215:
216: /**
217: * Return a JMS Queue.
218: *
219: * @param name
220: *
221: * @return Queue.
222: *
223: * @throws ConnectionException
224: */
225: public Queue getQueue(String name) throws ConnectionException {
226: try {
227: return ((Queue) mContext.lookup("queue/" + name));
228: } catch (javax.naming.NamingException nEx) {
229: // means the queue does not exist;
230: mLog.info("Destination " + name
231: + " not found, Creating ....");
232:
233: return createQueue(name);
234: }
235: }
236:
237: /**
238: *
239: *
240: * @param qname
241: *
242: * @return
243: *
244: * @throws ConnectionException
245: */
246: private Queue createQueue(String qname) throws ConnectionException {
247: if (mContext == null) {
248: throw new ConnectionException("No context available");
249: }
250:
251: try {
252:
253: MBeanServerConnection mbs = (MBeanServerConnection) mContext
254: .lookup("jmx/invoker/SingletonRMIAdaptor");
255: ObjectName objname = new ObjectName(
256: "jboss.mq:service=DestinationManager");
257: Object[] params = new String[1];
258: params[0] = qname;
259:
260: String[] signature = new String[1];
261: signature[0] = "java.lang.String";
262: mbs.invoke(objname, "createQueue", params, signature);
263: mLog.info("Created Destination " + qname);
264:
265: return (Queue) mContext.lookup("queue/" + qname);
266: } catch (Exception ce) {
267: throw new ConnectionException(ce);
268: }
269: }
270:
271: /**
272: *
273: *
274: * @param tname
275: *
276: * @return
277: *
278: * @throws ConnectionException
279: */
280: private Topic createTopic(String tname) throws ConnectionException {
281: if (mContext == null) {
282: throw new ConnectionException("No context available");
283: }
284:
285: try {
286: MBeanServerConnection mbs = (MBeanServerConnection) mContext
287: .lookup("jmx/invoker/SingletonRMIAdaptor");
288: ObjectName objname = new ObjectName(
289: "jboss.mq:service=DestinationManager");
290: Object[] params = new String[1];
291: params[0] = tname;
292:
293: String[] signature = new String[1];
294: signature[0] = "java.lang.String";
295:
296: mbs.invoke(objname, "createTopic", params, signature);
297: mLog.info("Created Destination " + tname);
298:
299: return (Topic) mContext.lookup("topic/" + tname);
300: } catch (Exception ce) {
301: throw new ConnectionException(ce);
302: }
303: }
304:
305: /**
306: * Return a JMS Topic.
307: *
308: * @param name
309: *
310: * @return Topic.
311: *
312: * @throws ConnectionException
313: */
314: public Topic getTopic(String name) throws ConnectionException {
315: try {
316: return ((Topic) mContext.lookup("topic/" + name));
317: } catch (javax.naming.NamingException nEx) {
318: mLog.info("Destination " + name
319: + " not found, Creating ....");
320:
321: return createTopic(name);
322: }
323: }
324: }
|