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: * @(#)MQManager.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.jms.mq;
030:
031: import com.sun.jbi.StringTranslator;
032:
033: import com.sun.jbi.binding.jms.EndpointBean;
034: import com.sun.jbi.binding.jms.JMSBindingContext;
035: import com.sun.jbi.binding.jms.JMSBindingResources;
036:
037: import com.sun.jbi.binding.jms.config.Config;
038:
039: import com.sun.jbi.binding.jms.config.ConfigConstants;
040: import com.sun.jbi.binding.jms.util.UtilBase;
041:
042: import java.util.Hashtable;
043:
044: import java.util.logging.Logger;
045:
046: import javax.jms.Connection;
047: import javax.jms.Destination;
048: import javax.jms.JMSSecurityException;
049: import javax.jms.Queue;
050: import javax.jms.QueueConnection;
051: import javax.jms.QueueConnectionFactory;
052: import javax.jms.Topic;
053: import javax.jms.TopicConnection;
054: import javax.jms.TopicConnectionFactory;
055:
056: import javax.naming.InitialContext;
057: import javax.naming.Context;
058:
059: /**
060: *
061: *
062: * @author Sun Microsystems Inc.
063: */
064: public final class MQManager extends UtilBase implements
065: JMSBindingResources {
066: /**
067: * Connection table.
068: */
069: private Hashtable mConnectionTable;
070: /**
071: * Destination table.
072: */
073: private Hashtable mDestinationTable;
074: /**
075: * Initial context.
076: */
077: private InitialContext mInitialContext;
078:
079: /**
080: * Logger.
081: */
082: private Logger mLogger;
083:
084: /**
085: * Temporary connection.
086: */
087: private MQConnection mTempCon;
088: /**
089: * Temporary session.
090: */
091: private MQSession mTmpSession;
092: /**
093: * Session pool.
094: */
095: private MQSessionPool mSessionPool;
096: /**
097: * Host name.
098: */
099: private String mHostName;
100: /**
101: * Initial factory.
102: */
103: private String mInitialFactory;
104: /**
105: * Protocol string.
106: */
107: private String mProtocol;
108: /**
109: * Provider url.
110: */
111: private String mProviderUrl;
112: /**
113: * Temporary password.
114: */
115: private String mTempPassword;
116: /**
117: * Temporary user.
118: */
119: private String mTempUser;
120: /**
121: * Temporary destination.
122: */
123: private String mTemporaryDestination;
124: /**
125: * Temporary factory.
126: */
127: private String mTemporaryFactory;
128:
129: /**
130: * i18n.
131: */
132: private StringTranslator mStringTranslator;
133:
134: /**
135: * Security principal.
136: */
137: private String mSecurityPrincipal;
138:
139: /**
140: * Credentials.
141: */
142: private String mCredentials;
143:
144: /**
145: * Security level.
146: */
147: private String mSecurityLevel;
148:
149: /**
150: * Creates a new MQManager object.
151: *
152: * @param intitialfactory initial factory.
153: * @param providerurl provider url string.
154: */
155: public MQManager(String intitialfactory, String providerurl,
156: String principal, String credentials, String level) {
157: mProviderUrl = providerurl;
158: mInitialFactory = intitialfactory;
159: mSecurityPrincipal = principal;
160: mCredentials = credentials;
161: mSecurityLevel = level;
162: mConnectionTable = new Hashtable();
163: mDestinationTable = new Hashtable();
164: mSessionPool = new MQSessionPool();
165: mSessionPool.setMaxSessionsPerConnection(5);
166: mSessionPool.setMinSessionsPerConnection(2);
167: mTemporaryDestination = JMSBindingContext.getInstance()
168: .getConfig().getReceiverDestination();
169: mTemporaryFactory = JMSBindingContext.getInstance().getConfig()
170: .getReceiverFactory();
171: mTempUser = JMSBindingContext.getInstance().getConfig()
172: .getReceiverConnectionUser();
173: mTempPassword = JMSBindingContext.getInstance().getConfig()
174: .getReceiverConnectionPassword();
175:
176: mLogger = JMSBindingContext.getInstance().getLogger();
177: mStringTranslator = JMSBindingContext.getInstance()
178: .getStringTranslator();
179: }
180:
181: /**
182: * Gets destination.
183: *
184: * @param lookupname lookup name.
185: *
186: * @return destination wrapper.
187: */
188: public MQDestination getDestination(Object lookupname) {
189: MQDestination dest = null;
190: clear();
191:
192: if (destinationExists(lookupname)) {
193: return (MQDestination) mDestinationTable.get(lookupname);
194: }
195:
196: try {
197: if (lookupname instanceof java.lang.String) {
198: javax.jms.Destination jmsdest = (javax.jms.Destination) mInitialContext
199: .lookup((java.lang.String) lookupname);
200: dest = new MQDestination(jmsdest);
201: } else if (lookupname instanceof javax.jms.Destination) {
202: dest = new MQDestination(
203: (javax.jms.Destination) lookupname);
204: } else {
205: setError("INTERNAL ERROR : Wrong Destination Type ");
206: }
207: } catch (Exception e) {
208: setError("Destination lookup failed ");
209: setError(e.getMessage());
210: e.printStackTrace();
211: }
212:
213: return dest;
214: }
215:
216: /**
217: * Gets the destination name.
218: *
219: * @param style topic or q.
220: * @param destlookupname destination lookup name.
221: *
222: * @return mq destination name.
223: */
224: /* public MQDestination getDestination(
225: int style,
226: String destlookupname)
227: {
228: if (style == ConfigConstants.QUEUE)
229: {
230: return getQueueDestination(destlookupname);
231: }
232: else if (style == ConfigConstants.TOPIC)
233: {
234: return getTopicDestination(destlookupname);
235: }
236: else
237: {
238: return null;
239: }
240: }
241: */
242:
243: /**
244: * Gets the provider url.
245: *
246: * @return provider url string.
247: */
248: public String getProviderURL() {
249: return mProviderUrl;
250: }
251:
252: /**
253: * Gets the session.
254: *
255: * @param tran transacted or non-transacted.
256: * @param eb endpoint bean.
257: *
258: * @return wrapper to a JMS session.
259: */
260: public MQSession getSession(boolean tran, EndpointBean eb) {
261: MQSession session = mSessionPool.getSession(eb);
262:
263: if (session != null) {
264: session.setBean(eb);
265:
266: if (tran) {
267: session.setTransacted();
268: }
269: }
270:
271: return session;
272: }
273:
274: /**
275: * Get temporary queue.
276: *
277: * @return wrapper to MQ destination.
278: */
279: public MQDestination getTemporaryQueue() {
280: MQDestination dest = getQueueDestination(mTemporaryDestination);
281:
282: return dest;
283: }
284:
285: /**
286: * Get temporary session.
287: *
288: * @return Wrapper to MQsession.
289: */
290: public MQSession getTemporarySession() {
291: if (mTmpSession != null) {
292: return mTmpSession;
293: }
294: mLogger
295: .fine("Temporay connection factory "
296: + mTemporaryFactory);
297: mTempCon = createQueueConnection(mTemporaryFactory, mTempUser,
298: mTempPassword);
299:
300: if (!isValid()) {
301: return null;
302: }
303:
304: MQDestination dest = getQueueDestination(mTemporaryDestination);
305:
306: if (!isValid()) {
307: return null;
308: }
309: mLogger.fine("Temporary Session not available , Creating");
310: createTmpSession(dest);
311:
312: return mTmpSession;
313: }
314:
315: /**
316: * Close the connection.
317: *
318: * @param con JMS connection wrapper.
319: */
320: public void closeConnection(MQConnection con) {
321: if (con == null) {
322: return;
323: }
324:
325: con.decrementUsage();
326: con.stop();
327: con.close();
328: deRegisterConnection(con);
329: }
330:
331: /**
332: * Closes the destination.
333: *
334: * @param dest wrapper to destination.
335: */
336: public void closeDestination(MQDestination dest) {
337: if (dest != null) {
338: deRegisterDestination(dest);
339: }
340: }
341:
342: /**
343: * Creates a connection.
344: *
345: * @param factory factory name.
346: * @param user user id.
347: * @param password password.
348: * @param style topic or queue.
349: *
350: * @return wrapper connection.
351: */
352: public MQConnection createConnection(String factory, String user,
353: String password, int style) {
354: clear();
355:
356: if (style == ConfigConstants.QUEUE) {
357: return createQueueConnection(factory, user, password);
358: } else if (style == ConfigConstants.TOPIC) {
359: return createTopicConnection(factory, user, password);
360: } else {
361: mLogger.severe("**UNSUPPORTED DESTINATION STYLE**");
362:
363: return null;
364: }
365: }
366:
367: /**
368: * Creates queue connection.
369: *
370: * @param confactory connection.
371: * @param user user id.
372: * @param password password.
373: *
374: * @return wrapper to mq connection.
375: */
376: public MQConnection createQueueConnection(String confactory,
377: String user, String password) {
378: clear();
379:
380: MQConnection connection = null;
381: connection = isAvailable(confactory);
382: if (connection != null) {
383: connection.incrementUsage();
384: return connection;
385: }
386:
387: try {
388:
389: javax.jms.QueueConnectionFactory mConnFactory = (javax.jms.QueueConnectionFactory) mInitialContext
390: .lookup(confactory);
391:
392: QueueConnection conn = null;
393:
394: if (user.trim().equals("")) {
395: conn = mConnFactory.createQueueConnection();
396: } else {
397: conn = mConnFactory.createQueueConnection(user,
398: password);
399: }
400: mLogger.finest("Connection factory is " + conn.toString());
401: connection = new MQConnection(confactory, conn,
402: ConfigConstants.QUEUE);
403: } catch (JMSSecurityException se) {
404: mLogger.severe("**SECURITY EXCEPTION**");
405: mLogger.severe(se.getMessage());
406: setError("**SECURITY EXCEPTION**");
407: setError(se.getMessage());
408:
409: return null;
410: } catch (Exception e) {
411: mLogger.info(e.getMessage());
412: setError("**CONNECTION FAILED**");
413: setError(e.getMessage());
414: setException(e);
415: e.printStackTrace();
416:
417: return null;
418: }
419:
420: registerConnection(confactory, connection);
421:
422: return connection;
423: }
424:
425: /**
426: * Create topic connection.
427: *
428: * @param confactory connection factory.
429: * @param user user id.
430: * @param password password.
431: *
432: * @return Mq connection wrapper.
433: */
434: public MQConnection createTopicConnection(String confactory,
435: String user, String password) {
436: MQConnection connection = null;
437: clear();
438: connection = isAvailable(confactory);
439:
440: if (connection != null) {
441: return connection;
442: }
443:
444: try {
445: javax.jms.TopicConnectionFactory mConnFactory = (javax.jms.TopicConnectionFactory) mInitialContext
446: .lookup(confactory);
447:
448: if (user.trim().equals("")) {
449: ;
450: }
451:
452: TopicConnection conn = mConnFactory.createTopicConnection(
453: user, password);
454:
455: connection = new MQConnection(confactory, conn,
456: ConfigConstants.TOPIC);
457: } catch (JMSSecurityException se) {
458: se.printStackTrace();
459: setError("Cannot create connection due to security error");
460: setError(se.getMessage());
461:
462: return null;
463: } catch (Exception e) {
464: e.printStackTrace();
465: setError("Cannot create connection ");
466: setError(e.getMessage());
467:
468: return null;
469: }
470:
471: registerConnection(confactory, connection);
472:
473: return connection;
474: }
475:
476: /**
477: * Checks if destination exists.
478: *
479: * @param dest detsination name.
480: *
481: * @return true if destination exists.
482: */
483: public boolean destinationExists(Object dest) {
484: try {
485: if (dest instanceof java.lang.String) {
486: return mDestinationTable
487: .containsKey((java.lang.String) dest);
488: } else if (dest instanceof javax.jms.Destination) {
489: MQDestination tmpdest = new MQDestination(
490: (javax.jms.Destination) dest);
491: String tmpdestname = tmpdest.getDestinationName();
492: tmpdest = null;
493: return mDestinationTable
494: .containsKey((java.lang.String) tmpdestname);
495: }
496: } catch (Exception e) {
497: e.printStackTrace();
498: }
499:
500: return false;
501: }
502:
503: /**
504: * Initialises the manager.
505: */
506: public boolean init() {
507: Hashtable env = new Hashtable();
508:
509: /* JNDI lookup code
510: InitialContext ctx = JMSBindingContext.getInstance().getContext().getNamingContext();
511: */
512: try {
513: clear();
514: env.put(Context.INITIAL_CONTEXT_FACTORY, mInitialFactory);
515: env.put(Context.PROVIDER_URL, mProviderUrl);
516: if (mSecurityPrincipal != null) {
517: env.put(Context.SECURITY_PRINCIPAL, mSecurityPrincipal);
518: }
519: if (mCredentials != null) {
520: env.put(Context.SECURITY_PRINCIPAL, mCredentials);
521: }
522: if (mSecurityLevel != null) {
523: env.put(Context.SECURITY_PRINCIPAL, mSecurityLevel);
524: }
525:
526: mInitialContext = new InitialContext(env);
527: } catch (Exception e) {
528: e.printStackTrace();
529: setException(e);
530: setError("Cannot get Initial context with environment "
531: + env.toString());
532: setError(e.getMessage());
533:
534: return false;
535: }
536:
537: if (mInitialContext != null) {
538: return true;
539: } else {
540: return false;
541: }
542: }
543:
544: /**
545: * Releases the session.
546: *
547: * @param session wrapper to MQ session.
548: */
549: public void releaseSession(MQSession session) {
550: mSessionPool.releaseSession(session);
551: }
552:
553: /**
554: * Reelases the temporary session.
555: */
556: public void releaseTemporarySession() {
557: if (mTmpSession == null) {
558: return;
559: }
560:
561: mTmpSession.stopReceiving();
562:
563: closeConnection(mTempCon);
564: }
565:
566: /**
567: * Checks if a connection is already available for the factory.
568: *
569: * @param fac connection factory.
570: *
571: * @return wrapper to JMS connection of oneis found.
572: */
573: private MQConnection isAvailable(String fac) {
574: MQConnection con = null;
575:
576: try {
577: con = (MQConnection) mConnectionTable.get(fac);
578: } catch (Exception e) {
579: e.printStackTrace();
580: }
581:
582: return con;
583: }
584:
585: /**
586: * Returns a queue destination.
587: *
588: * @param dest destination name.
589: *
590: * @return wrapper to destination.
591: */
592: private MQDestination getQueueDestination(String dest) {
593: MQDestination destination = null;
594: clear();
595:
596: if (destinationExists(dest)) {
597: return (MQDestination) mDestinationTable.get(dest);
598: }
599:
600: try {
601: javax.jms.Queue queue = (javax.jms.Queue) mInitialContext
602: .lookup(dest);
603: destination = new MQDestination(queue,
604: ConfigConstants.QUEUE, dest);
605: } catch (Throwable e) {
606: e.printStackTrace();
607: setError("Cannot look up queue ");
608: setError(e.getMessage());
609:
610: return null;
611: }
612:
613: registerDestination(dest, destination);
614:
615: return destination;
616: }
617:
618: /**
619: * Gets the topic destination.
620: *
621: * @param dest topic destination.
622: *
623: * @return wrapper to JMS destination.
624: */
625: private MQDestination getTopicDestination(String dest) {
626: MQDestination destination = null;
627: clear();
628:
629: if (destinationExists(dest)) {
630:
631: return (MQDestination) mDestinationTable.get(dest);
632: }
633:
634: try {
635: javax.jms.Topic topic = (javax.jms.Topic) mInitialContext
636: .lookup(dest);
637: destination = new MQDestination(topic,
638: ConfigConstants.TOPIC, dest);
639: } catch (Exception e) {
640: e.printStackTrace();
641:
642: setError("Cannot look up queue ");
643: setError(e.getMessage());
644:
645: return null;
646: }
647:
648: registerDestination(dest, destination);
649:
650: return destination;
651: }
652:
653: /**
654: * Creates temporary session.
655: *
656: * @param dest wrapper to JMS destination.
657: */
658: private void createTmpSession(MQDestination dest) {
659: mLogger.fine("Creating Temporary Session");
660: if ((mTempCon == null) || (dest == null)) {
661: return;
662: }
663:
664: if (mTmpSession == null) {
665: mTmpSession = new MQSession();
666: mTmpSession.setConnection(mTempCon);
667: mTmpSession.setDestination(dest);
668:
669: // mTmpSession.setName("Temporary Session");
670: mTmpSession.setStyle(mTempCon.getStyle());
671: }
672:
673: mLogger.fine("Created Temporary Session");
674: }
675:
676: /**
677: * Deregisters the connection.
678: *
679: * @param connection wrapper to JMS connection.
680: */
681: private void deRegisterConnection(MQConnection connection) {
682: try {
683: mConnectionTable.remove(connection.getFactory());
684: } catch (Exception e) {
685: mLogger.severe(mStringTranslator.getString(
686: JMS_DEREGISTER_CON_FAILED, e.getMessage()));
687: setWarning(mStringTranslator.getString(
688: JMS_DEREGISTER_CON_FAILED, e.getMessage()));
689: }
690: }
691:
692: /**
693: * Deregisters the destination.
694: *
695: * @param dest wrapper to JMS destination.
696: */
697: private void deRegisterDestination(MQDestination dest) {
698: try {
699: mDestinationTable.remove(dest.getLookupName());
700: } catch (Exception e) {
701: mLogger.severe(mStringTranslator.getString(
702: JMS_DEREGISTER_DEST_FAILED, e.getMessage()));
703: }
704: }
705:
706: /**
707: * Registres the connection.
708: *
709: * @param factory connection factory.
710: * @param connection Wrapper to JMS connection.
711: */
712: private void registerConnection(String factory,
713: MQConnection connection) {
714: try {
715: mConnectionTable.put(factory, connection);
716: connection.incrementUsage();
717: } catch (Exception e) {
718: e.printStackTrace();
719: }
720: }
721:
722: /**
723: * Registers destination.
724: *
725: * @param name destination name.
726: * @param dest wrapper to destination.
727: */
728: private void registerDestination(String name, MQDestination dest) {
729: try {
730: mDestinationTable.put(name, dest);
731: } catch (Exception e) {
732: e.printStackTrace();
733: }
734: }
735: }
|