1 17 18 21 package org.quartz.jobs.ee.jms; 22 23 import org.quartz.JobDataMap; 24 25 import javax.naming.Context ; 26 import javax.naming.InitialContext ; 27 import javax.naming.NamingException ; 28 import java.lang.reflect.InvocationTargetException ; 29 import java.lang.reflect.Method ; 30 import java.util.Hashtable ; 31 32 38 public class JmsHelper { 39 40 public static final String INITIAL_CONTEXT_FACTORY = "java.naming.factory.initial"; 41 42 public static final String PROVIDER_URL = "java.naming.provider.url"; 43 44 public static final String PRINCIPAL = "java.naming.security.principal"; 45 46 public static final String CREDENTIALS = "java.naming.security.credentials"; 47 48 public static final String JMS_CONNECTION_FACTORY_JNDI = "jms.connection.factory"; 49 50 public static final String JMS_DESTINATION_JNDI = "jms.destination"; 51 52 public static final String JMS_USER = "jms.user"; 53 54 public static final String JMS_PASSWORD = "jms.password"; 55 56 public static final String JMS_ACK_MODE = "jms.acknowledge"; 57 58 public static final String JMS_USE_TXN = "jms.use.transaction"; 59 60 public static final String JMS_MSG_FACTORY_CLASS_NAME = "jms.message.factory.class.name"; 61 62 private JmsHelper() { 63 } 64 65 public static InitialContext getInitialContext(final JobDataMap jobDataMap) throws NamingException 66 { 67 68 Hashtable params = new Hashtable (2); 69 70 String initialContextFactory = 71 jobDataMap.getString(INITIAL_CONTEXT_FACTORY); 72 73 if (initialContextFactory != null) { 74 params.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory); 75 } 76 77 String providerUrl = jobDataMap.getString(PROVIDER_URL); 78 if (providerUrl != null) { 79 params.put(Context.PROVIDER_URL, providerUrl); 80 } 81 82 String principal = jobDataMap.getString(PRINCIPAL); 83 if ( principal != null ) { 84 params.put( Context.SECURITY_PRINCIPAL, principal ); 85 } 86 87 String credentials = jobDataMap.getString(CREDENTIALS); 88 if ( credentials != null ) { 89 params.put( Context.SECURITY_CREDENTIALS, credentials ); 90 } 91 92 if (params.size() == 0) { 93 return new InitialContext (); 94 } else { 95 return new InitialContext (params); 96 } 97 98 } 99 100 public static boolean isDestinationSecure(JobDataMap jobDataMap) { 101 String user = jobDataMap.getString(JmsHelper.JMS_USER); 102 String pw = jobDataMap.getString(JmsHelper.JMS_PASSWORD); 103 104 return (user != null && pw != null); 105 106 } 107 108 113 public static void closeResource(Object resource) { 114 if(resource == null) { 116 return; 117 } 118 119 try { 120 121 final Method m = resource.getClass().getMethod("close", new Class [0]); 122 m.invoke(resource, new Object [0]); 123 124 } catch (SecurityException e) { 125 } catch (IllegalArgumentException e) { 126 } catch (NoSuchMethodException e) { 127 } catch (IllegalAccessException e) { 128 } catch (InvocationTargetException e) { 129 } 130 131 } 132 133 public static boolean useTransaction(JobDataMap jobDataMap) { 134 return false; 135 } 136 137 143 public static JmsMessageFactory getMessageFactory(String factoryName) throws JmsJobException 144 { 145 146 try { 147 148 Class clazz = Class.forName(factoryName); 149 JmsMessageFactory messageFactory = (JmsMessageFactory)clazz.newInstance(); 150 return messageFactory; 151 152 } catch (ClassNotFoundException e) { 153 154 throw new JmsJobException(e.getMessage(), e); 155 156 } catch (InstantiationException e) { 157 158 throw new JmsJobException(e.getMessage(), e); 159 160 } catch (IllegalAccessException e) { 161 162 throw new JmsJobException(e.getMessage(), e); 163 } 164 165 166 } 167 168 } 169 | Popular Tags |