KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > jobs > ee > jms > JmsHelper


1 /*
2  * Copyright 2004-2005 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  *
16  */

17
18 /*
19  * Previously Copyright (c) 2001-2004 James House
20  */

21 package org.quartz.jobs.ee.jms;
22
23 import org.quartz.JobDataMap;
24
25 import javax.naming.Context JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30 import java.util.Hashtable JavaDoc;
31
32 /**
33  * Utility class that aids in the processing of JMS based jobs and sending
34  * of <code>javax.jms.Message</code>
35  *
36  * @author Weston M. Price
37  */

38 public class JmsHelper {
39     
40     public static final String JavaDoc INITIAL_CONTEXT_FACTORY = "java.naming.factory.initial";
41     
42     public static final String JavaDoc PROVIDER_URL = "java.naming.provider.url";
43
44     public static final String JavaDoc PRINCIPAL = "java.naming.security.principal";
45
46     public static final String JavaDoc CREDENTIALS = "java.naming.security.credentials";
47
48     public static final String JavaDoc JMS_CONNECTION_FACTORY_JNDI = "jms.connection.factory";
49
50     public static final String JavaDoc JMS_DESTINATION_JNDI = "jms.destination";
51
52     public static final String JavaDoc JMS_USER = "jms.user";
53     
54     public static final String JavaDoc JMS_PASSWORD = "jms.password";
55     
56     public static final String JavaDoc JMS_ACK_MODE = "jms.acknowledge";
57         
58     public static final String JavaDoc JMS_USE_TXN = "jms.use.transaction";
59         
60     public static final String JavaDoc JMS_MSG_FACTORY_CLASS_NAME = "jms.message.factory.class.name";
61     
62     private JmsHelper() {
63     }
64     
65     public static InitialContext JavaDoc getInitialContext(final JobDataMap jobDataMap) throws NamingException JavaDoc
66     {
67         
68         Hashtable JavaDoc params = new Hashtable JavaDoc(2);
69         
70         String JavaDoc initialContextFactory =
71             jobDataMap.getString(INITIAL_CONTEXT_FACTORY);
72     
73         if (initialContextFactory != null) {
74             params.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
75         }
76         
77         String JavaDoc providerUrl = jobDataMap.getString(PROVIDER_URL);
78         if (providerUrl != null) {
79             params.put(Context.PROVIDER_URL, providerUrl);
80         }
81
82         String JavaDoc principal = jobDataMap.getString(PRINCIPAL);
83         if ( principal != null ) {
84             params.put( Context.SECURITY_PRINCIPAL, principal );
85         }
86
87         String JavaDoc 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 JavaDoc();
94         } else {
95             return new InitialContext JavaDoc(params);
96         }
97         
98     }
99         
100     public static boolean isDestinationSecure(JobDataMap jobDataMap) {
101         String JavaDoc user = jobDataMap.getString(JmsHelper.JMS_USER);
102         String JavaDoc pw = jobDataMap.getString(JmsHelper.JMS_PASSWORD);
103         
104         return (user != null && pw != null);
105         
106     }
107     
108     /**
109      * Closes a resource that has a <code>close()</code> method.
110      *
111      * @param resource the resource to close.
112      */

113     public static void closeResource(Object JavaDoc resource) {
114         //TODO determine if vargs can be used in Quartz (1.5 features)
115
if(resource == null) {
116             return;
117         }
118         
119         try {
120             
121             final Method JavaDoc m = resource.getClass().getMethod("close", new Class JavaDoc[0]);
122             m.invoke(resource, new Object JavaDoc[0]);
123         
124         } catch (SecurityException JavaDoc e) {
125         } catch (IllegalArgumentException JavaDoc e) {
126         } catch (NoSuchMethodException JavaDoc e) {
127         } catch (IllegalAccessException JavaDoc e) {
128         } catch (InvocationTargetException JavaDoc e) {
129         }
130         
131     }
132
133     public static boolean useTransaction(JobDataMap jobDataMap) {
134         return false;
135     }
136     
137     /**
138      * Creates the <code>JmsMessageFactory</code>
139      * @param factoryName
140      * @return
141      * @throws JmsJobException
142      */

143     public static JmsMessageFactory getMessageFactory(String JavaDoc factoryName) throws JmsJobException
144     {
145         
146         try {
147         
148             Class JavaDoc clazz = Class.forName(factoryName);
149             JmsMessageFactory messageFactory = (JmsMessageFactory)clazz.newInstance();
150             return messageFactory;
151         
152         } catch (ClassNotFoundException JavaDoc e) {
153             
154             throw new JmsJobException(e.getMessage(), e);
155             
156         } catch (InstantiationException JavaDoc e) {
157
158             throw new JmsJobException(e.getMessage(), e);
159         
160         } catch (IllegalAccessException JavaDoc e) {
161
162             throw new JmsJobException(e.getMessage(), e);
163         }
164         
165         
166     }
167     
168 }
169
Popular Tags