Clover coverage report -
Coverage timestamp: Sat Jul 7 2007 16:41:13 CEST
file stats: LOC: 338   Methods: 12
NCLOC: 224   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
JMSHelper.java 26.8% 41.7% 75% 39.6%
coverage coverage
 1    /**
 2    * JTRunner is free software; you can redistribute it and/or modify it under the
 3    * terms of the GNU General Public License as published by the Free Software
 4    * Foundation; either version 2, or (at your option) any later version.
 5    */
 6   
 7    package jtr.jms;
 8   
 9    import java.lang.reflect.*;
 10    import java.util.*;
 11    import javax.jms.*;
 12    import javax.jms.Queue;
 13    import jtr.assigner.util.BeanUtilsExtension;
 14   
 15    import org.apache.commons.beanutils.*;
 16   
 17    import jtr.config.jms.JMSConfig;
 18    import jtr.config.jms.JMSConnection;
 19    import jtr.config.jms.JMSConnectionFactory;
 20    import jtr.config.jms.JMSDestination;
 21    import jtr.config.jms.JMSSession;
 22    import jtr.enterprise.LocatorException;
 23    import jtr.runners.*;
 24    import org.apache.log4j.Logger;
 25   
 26    /**
 27    * This helper class performs the injection of the following JMS related objects
 28    * into a concrete runner implementing the <code>IRunnerJMS</code> interface:
 29    * <br>
 30    * 1. queues<br>
 31    * 2. topics<br>
 32    * 3. connection factories<br>
 33    * 4. connections<br>
 34    * 5. sessions<br>
 35    * The information required for performing these steps is retrieved from the jms
 36    * configuration associated with the current runner.<br>
 37    * The attributes named <code>property</code> that you can
 38    * find into the <code>jms</code> section of the <code>jtr.xml</code> file
 39    * refers to the name of the properties your runner bean should expose.
 40    *
 41    * @author Francesco Russo (frusso@dev.java.net)
 42    * @version 4.0
 43    * @since 1.1
 44    */
 45    public class JMSHelper {
 46   
 47    /**
 48    * Performs the injection of all the JMS related objects into the given
 49    * runner.<br>
 50    * <b>Note:</b> you are not required to invoke this method from your
 51    * concrete IRunner implementation: the JTR runtime will take care for you.
 52    *
 53    * @param target
 54    * The concrete <code>IRunner</code> to be injected
 55    * @param jmsConfig
 56    * The JMS configuration associated with the above IRunner
 57    * @throws JMSInjectionException
 58    * Something went wrong during the injection
 59    */
 60  14 public static void inject(IRunner target, JMSConfig jmsConfig) throws JMSInjectionException {
 61  14 try {
 62  14 injectQueues(target, jmsConfig);
 63  14 injectTopics(target, jmsConfig);
 64  14 injectConnectionFactories(target, jmsConfig);
 65    } catch (Exception e) {
 66  0 String msg = "Caught an exception while injecting JMS administered object into IRunner " + target.getDefaultName();
 67  0 logger.fatal(msg, e);
 68  0 throw new JMSInjectionException(msg, e);
 69    }
 70    }
 71   
 72  14 protected static void injectQueues(IRunner target, JMSConfig jmsConfig) throws JMSInjectionException, LocatorException, IllegalAccessException, InvocationTargetException {
 73  14 logger.debug("Injecting Queues...");
 74  14 Hashtable ht = target.getEnterprise().toHashtable();
 75  14 Iterator iter = jmsConfig.getQueues().iterator();
 76  14 while (iter.hasNext()) {
 77  14 JMSDestination dest = (JMSDestination) iter.next();
 78  14 Queue queue = (Queue) AdministeredObjectsLocator.getDestination(dest.getJndi(), ht);
 79  14 BeanUtilsExtension.setProperty(target, dest.getProperty(), queue);
 80  14 logger.debug("injected queue " + dest.getJndi());
 81    }
 82  14 logger.debug("Injecting Queues... done!");
 83    }
 84   
 85  14 protected static void injectTopics(IRunner target, JMSConfig jmsConfig) throws JMSInjectionException, LocatorException, IllegalAccessException, InvocationTargetException {
 86  14 logger.debug("Injecting Topics...");
 87  14 Hashtable ht = target.getEnterprise().toHashtable();
 88  14 Iterator iter = jmsConfig.getTopics().iterator();
 89  14 while (iter.hasNext()) {
 90  0 JMSDestination dest = (JMSDestination) iter.next();
 91  0 Topic topic = (Topic) AdministeredObjectsLocator.getDestination(dest.getJndi(), ht);
 92  0 BeanUtilsExtension.setProperty(target, dest.getProperty(), topic);
 93  0 logger.debug("injected topic " + dest.getJndi());
 94    }
 95  14 logger.debug("Injecting Topics... done!");
 96    }
 97   
 98  14 protected static void injectConnectionFactories(IRunner target, JMSConfig jmsConfig) throws LocatorException, InvocationTargetException, IllegalAccessException, IllegalAccessException,
 99    InvocationTargetException, JMSInjectionException, JMSException {
 100  14 logger.debug("Injecting ConnectionFactories...");
 101  14 Hashtable ht = target.getEnterprise().toHashtable();
 102  14 Iterator iter = jmsConfig.getConnectionFactories().iterator();
 103  14 while (iter.hasNext()) {
 104  14 JMSConnectionFactory jmscf = (JMSConnectionFactory) iter.next();
 105  14 ConnectionFactory cf = (ConnectionFactory) AdministeredObjectsLocator.getConnectionFactory(jmscf.getJndi(), ht);
 106  14 BeanUtilsExtension.setProperty(target, jmscf.getProperty(), cf);
 107  14 logger.debug("injected connectionFactory " + jmscf.getJndi());
 108   
 109  14 injectConnections(target, jmscf, cf, jmsConfig.getUniqueName());
 110    }
 111  14 logger.debug("Injecting ConnectionFactories... done!");
 112    }
 113   
 114  14 protected static void injectConnections(IRunner target, JMSConnectionFactory jmscf, ConnectionFactory cf, String configKey) throws JMSException, JMSInjectionException, InvocationTargetException, IllegalAccessException {
 115  14 logger.debug("Injecting Connections...");
 116  14 Iterator connIter = jmscf.getConnections().iterator();
 117  14 while (connIter.hasNext()) {
 118  14 Connection c = null;
 119    // test if the connection is shareable
 120    // if it is, test if it is already cached
 121    // otherwise create and cache it before returning
 122  14 JMSConnection jmsc = (JMSConnection) connIter.next();
 123  14 if(jmsc.isShared()) {
 124    // lock-cache
 125    // check-cache
 126    // 1. cache-hit:
 127    // a. injectXxx
 128    // 2. cache-miss:
 129    // a. createXxxConnection
 130    // b. setClientID
 131    // c. injectXxx
 132    // d. add to cache
 133    // unlock-cache
 134  0 synchronized(connectionCache) {
 135  0 if(connectionCache.containsKey(configKey)) {
 136    // cache-hit
 137  0 c = connectionCache.get(configKey);
 138  0 injectSessions(target,jmsc,c);
 139    } else {
 140    // cache-miss
 141  0 c = createConnection(jmsc,cf);
 142  0 setClientId(target,jmsc,c);
 143  0 injectSessions(target,jmsc,c);
 144  0 connectionCache.put(configKey,c);
 145    }
 146    }
 147    } else {
 148    // createXxxConnection
 149    // setClientID
 150    // injectXxx
 151  14 c = createConnection(jmsc,cf);
 152  14 setClientId(target,jmsc,c);
 153  14 injectSessions(target,jmsc,c);
 154    }
 155    // let's inject the connection into the runner
 156  14 BeanUtilsExtension.setProperty(target, jmsc.getProperty(), c);
 157  14 logger.debug("injected connection " + c);
 158    }
 159    }
 160   
 161    /**
 162    * injectXATopicSessions
 163    *
 164    * @param target
 165    * IRunner
 166    * @param jmsc
 167    * JMSConnection
 168    * @param xatc
 169    * XATopicConnection
 170    * @throws javax.jms.JMSException
 171    * @throws java.lang.reflect.InvocationTargetException
 172    * @throws java.lang.IllegalAccessException
 173    */
 174  0 protected static void injectXATopicSessions(IRunner target, JMSConnection jmsc, XATopicConnection xatc) throws JMSException, InvocationTargetException, IllegalAccessException {
 175  0 logger.debug("Injecting XATopicSessions...");
 176  0 Iterator sessionIter = jmsc.getSessions().iterator();
 177  0 while (sessionIter.hasNext()) {
 178  0 JMSSession jmss = (JMSSession) sessionIter.next();
 179  0 TopicSession ts = xatc.createTopicSession(jmss.getTransacted(), jmss.getAckMode());
 180  0 BeanUtilsExtension.setProperty(target, jmss.getProperty(), ts);
 181  0 logger.debug("injected XATopicSession");
 182    }
 183  0 logger.debug("Injecting XATopicSessions... done!");
 184    }
 185   
 186    /**
 187    * injectXAQueueSessions
 188    *
 189    * @param target
 190    * IRunner
 191    * @param jmsc
 192    * JMSConnection
 193    * @param xaqc
 194    * XAQueueConnection
 195    * @throws javax.jms.JMSException
 196    * @throws java.lang.reflect.InvocationTargetException
 197    * @throws java.lang.IllegalAccessException
 198    */
 199  0 protected static void injectXAQueueSessions(IRunner target, JMSConnection jmsc, XAQueueConnection xaqc) throws JMSException, InvocationTargetException, IllegalAccessException {
 200  0 logger.debug("Injecting XAQueueSessions...");
 201  0 Iterator sessionIter = jmsc.getSessions().iterator();
 202  0 while (sessionIter.hasNext()) {
 203  0 JMSSession jmss = (JMSSession) sessionIter.next();
 204  0 QueueSession qs = xaqc.createQueueSession(jmss.getTransacted(), jmss.getAckMode());
 205  0 BeanUtilsExtension.setProperty(target, jmss.getProperty(), qs);
 206  0 logger.debug("injected XAQueueSession");
 207    }
 208  0 logger.debug("Injecting XAQueueSessions... done!");
 209    }
 210   
 211  14 protected static void injectQueueSessions(IRunner target, JMSConnection jmsc, QueueConnection qc) throws JMSException, InvocationTargetException, IllegalAccessException {
 212  14 logger.debug("Injecting QueueSessions...");
 213  14 Iterator sessionIter = jmsc.getSessions().iterator();
 214  14 while (sessionIter.hasNext()) {
 215  14 JMSSession jmss = (JMSSession) sessionIter.next();
 216  14 QueueSession qs = qc.createQueueSession(jmss.getTransacted(), jmss.getAckMode());
 217  14 BeanUtilsExtension.setProperty(target, jmss.getProperty(), qs);
 218  14 logger.debug("injected queueSession");
 219    }
 220  14 logger.debug("Injecting QueueSessions... done!");
 221    }
 222   
 223  0 protected static void injectTopicSessions(IRunner target, JMSConnection jmsc, TopicConnection tc) throws JMSException, InvocationTargetException, IllegalAccessException {
 224  0 logger.debug("Injecting TopicSessions...");
 225  0 Iterator sessionIter = jmsc.getSessions().iterator();
 226  0 while (sessionIter.hasNext()) {
 227  0 JMSSession jmss = (JMSSession) sessionIter.next();
 228  0 TopicSession ts = tc.createTopicSession(jmss.getTransacted(), jmss.getAckMode());
 229  0 BeanUtilsExtension.setProperty(target, jmss.getProperty(), ts);
 230  0 logger.debug("injected topicSession");
 231    }
 232  0 logger.debug("Injecting TopicSessions... done!");
 233    }
 234   
 235  14 private static void injectSessions(IRunner target, JMSConnection jmsc, Connection c) throws JMSException, InvocationTargetException, IllegalAccessException, JMSInjectionException {
 236  14 if (JMSConnection.isQueueConnection(jmsc)) {
 237    // this is a Queue connection...
 238  14 injectQueueSessions(target,jmsc,(QueueConnection)c);
 239    } else {
 240  0 if (JMSConnection.isTopicConnection(jmsc)) {
 241    // this is an Topic connection
 242  0 injectTopicSessions(target,jmsc,(TopicConnection)c);
 243    } else {
 244  0 if (JMSConnection.isXaQueueConnection(jmsc)) {
 245    // this is an XAQueue connection
 246  0 injectXAQueueSessions(target,jmsc,(XAQueueConnection)c);
 247    } else {
 248  0 if (JMSConnection.isXaTopicConnection(jmsc)) {
 249    // this is an XATopic connection
 250  0 injectXATopicSessions(target,jmsc,(XATopicConnection)c);
 251    } else {
 252    // this is an unknown connection type
 253  0 String msg = "Unable to instantiate sessions for a connection of type " + jmsc.getType();
 254  0 logger.error(msg);
 255  0 throw new JMSInjectionException(msg);
 256    }
 257    }
 258    }
 259    }
 260    }
 261   
 262  14 private static void setClientId(IRunner target, JMSConnection jmsc, Connection c) throws JMSException {
 263  14 if (jmsc.getClientId() != null) {
 264    // we have to set the clientID for the
 265    // current connection
 266  14 logger.debug("Setting clientID " + jmsc.getClientId() + " for the current connection...");
 267  14 if(jmsc.getClientId().equals(DEFAULT_CLIENT_ID))
 268  14 c.setClientID(target.getName());
 269    else
 270  0 c.setClientID(jmsc.getClientId());
 271    }
 272    }
 273   
 274  14 private static Connection createConnection(JMSConnection jmsc, ConnectionFactory cf) throws JMSInjectionException, JMSException {
 275  14 Connection c = null;
 276  14 if (JMSConnection.isQueueConnection(jmsc)) {
 277    // this is a queue connection...
 278  14 if (jmsc.getUserName() == null && jmsc.getPassword() == null) {
 279  14 c = ((QueueConnectionFactory) cf).createQueueConnection();
 280  0 } else if (jmsc.getUserName() != null && jmsc.getPassword() != null) {
 281  0 c = ((QueueConnectionFactory) cf).createQueueConnection(jmsc.getUserName(), jmsc.getPassword());
 282    } else {
 283  0 String msg = "Invalid userName/password null values combination";
 284  0 logger.error(msg);
 285  0 throw new JMSInjectionException(msg);
 286    }
 287    } else {
 288  0 if (JMSConnection.isTopicConnection(jmsc)) {
 289  0 if (jmsc.getUserName() == null && jmsc.getPassword() == null) {
 290  0 c = ((TopicConnectionFactory) cf).createTopicConnection();
 291  0 } else if (jmsc.getUserName() != null && jmsc.getPassword() != null) {
 292  0 c = ((TopicConnectionFactory) cf).createTopicConnection(jmsc.getUserName(), jmsc.getPassword());
 293    } else {
 294  0 String msg = "Invalid userName/password null values combination";
 295  0 logger.error(msg);
 296  0 throw new JMSInjectionException(msg);
 297    }
 298    } else {
 299  0 if (JMSConnection.isXaQueueConnection(jmsc)) {
 300    // this is an XAQueue connection
 301  0 if (jmsc.getUserName() == null && jmsc.getPassword() == null) {
 302  0 c = ((XAQueueConnectionFactory) cf).createXAQueueConnection();
 303  0 } else if (jmsc.getUserName() != null && jmsc.getPassword() != null) {
 304  0 c = ((XAQueueConnectionFactory) cf).createXAQueueConnection(jmsc.getUserName(), jmsc.getPassword());
 305    } else {
 306  0 String msg = "Invalid userName/password null values combination";
 307  0 logger.error(msg);
 308  0 throw new JMSInjectionException(msg);
 309    }
 310    } else {
 311  0 if (JMSConnection.isXaTopicConnection(jmsc)) {
 312    // this is an XATopic connection
 313  0 if (jmsc.getUserName() == null && jmsc.getPassword() == null) {
 314  0 c = ((XATopicConnectionFactory) cf).createXATopicConnection();
 315  0 } else if (jmsc.getUserName() != null && jmsc.getPassword() != null) {
 316  0 c = ((XATopicConnectionFactory) cf).createXATopicConnection(jmsc.getUserName(), jmsc.getPassword());
 317    } else {
 318  0 String msg = "Invalid userName/password null values combination";
 319  0 logger.error(msg);
 320  0 throw new JMSInjectionException(msg);
 321    }
 322    } else {
 323    // this is an unknown connection type
 324  0 String msg = "Unable to instantiate a JMS connection of type " + jmsc.getType();
 325  0 logger.error(msg);
 326  0 throw new JMSInjectionException(msg);
 327    }
 328    }
 329    }
 330    }
 331   
 332  14 return c;
 333    }
 334   
 335    private static HashMap<String,Connection> connectionCache = new HashMap<String,Connection>();
 336    private static Logger logger = Logger.getLogger(JMSHelper.class);
 337    private static final String DEFAULT_CLIENT_ID = "${runner.id}";
 338    }