001: /*
002: * Copyright 2004-2005 OpenSymphony
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy
006: * of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations
014: * under the License.
015: *
016: */
017:
018: /*
019: * Previously Copyright (c) 2001-2004 James House
020: */
021: package org.quartz.jobs.ee.jms;
022:
023: import org.quartz.JobDataMap;
024:
025: import javax.naming.Context;
026: import javax.naming.InitialContext;
027: import javax.naming.NamingException;
028: import java.lang.reflect.InvocationTargetException;
029: import java.lang.reflect.Method;
030: import java.util.Hashtable;
031:
032: /**
033: * Utility class that aids in the processing of JMS based jobs and sending
034: * of <code>javax.jms.Message</code>
035: *
036: * @author Weston M. Price
037: */
038: public class JmsHelper {
039:
040: public static final String INITIAL_CONTEXT_FACTORY = "java.naming.factory.initial";
041:
042: public static final String PROVIDER_URL = "java.naming.provider.url";
043:
044: public static final String PRINCIPAL = "java.naming.security.principal";
045:
046: public static final String CREDENTIALS = "java.naming.security.credentials";
047:
048: public static final String JMS_CONNECTION_FACTORY_JNDI = "jms.connection.factory";
049:
050: public static final String JMS_DESTINATION_JNDI = "jms.destination";
051:
052: public static final String JMS_USER = "jms.user";
053:
054: public static final String JMS_PASSWORD = "jms.password";
055:
056: public static final String JMS_ACK_MODE = "jms.acknowledge";
057:
058: public static final String JMS_USE_TXN = "jms.use.transaction";
059:
060: public static final String JMS_MSG_FACTORY_CLASS_NAME = "jms.message.factory.class.name";
061:
062: private JmsHelper() {
063: }
064:
065: public static InitialContext getInitialContext(
066: final JobDataMap jobDataMap) throws NamingException {
067:
068: Hashtable params = new Hashtable(2);
069:
070: String initialContextFactory = jobDataMap
071: .getString(INITIAL_CONTEXT_FACTORY);
072:
073: if (initialContextFactory != null) {
074: params.put(Context.INITIAL_CONTEXT_FACTORY,
075: initialContextFactory);
076: }
077:
078: String providerUrl = jobDataMap.getString(PROVIDER_URL);
079: if (providerUrl != null) {
080: params.put(Context.PROVIDER_URL, providerUrl);
081: }
082:
083: String principal = jobDataMap.getString(PRINCIPAL);
084: if (principal != null) {
085: params.put(Context.SECURITY_PRINCIPAL, principal);
086: }
087:
088: String credentials = jobDataMap.getString(CREDENTIALS);
089: if (credentials != null) {
090: params.put(Context.SECURITY_CREDENTIALS, credentials);
091: }
092:
093: if (params.size() == 0) {
094: return new InitialContext();
095: } else {
096: return new InitialContext(params);
097: }
098:
099: }
100:
101: public static boolean isDestinationSecure(JobDataMap jobDataMap) {
102: String user = jobDataMap.getString(JmsHelper.JMS_USER);
103: String pw = jobDataMap.getString(JmsHelper.JMS_PASSWORD);
104:
105: return (user != null && pw != null);
106:
107: }
108:
109: /**
110: * Closes a resource that has a <code>close()</code> method.
111: *
112: * @param resource the resource to close.
113: */
114: public static void closeResource(Object resource) {
115: //TODO determine if vargs can be used in Quartz (1.5 features)
116: if (resource == null) {
117: return;
118: }
119:
120: try {
121:
122: final Method m = resource.getClass().getMethod("close",
123: new Class[0]);
124: m.invoke(resource, new Object[0]);
125:
126: } catch (SecurityException e) {
127: } catch (IllegalArgumentException e) {
128: } catch (NoSuchMethodException e) {
129: } catch (IllegalAccessException e) {
130: } catch (InvocationTargetException e) {
131: }
132:
133: }
134:
135: public static boolean useTransaction(JobDataMap jobDataMap) {
136: return false;
137: }
138:
139: /**
140: * Creates the <code>JmsMessageFactory</code>
141: * @param factoryName
142: * @return
143: * @throws JmsJobException
144: */
145: public static JmsMessageFactory getMessageFactory(String factoryName)
146: throws JmsJobException {
147:
148: try {
149:
150: Class clazz = Class.forName(factoryName);
151: JmsMessageFactory messageFactory = (JmsMessageFactory) clazz
152: .newInstance();
153: return messageFactory;
154:
155: } catch (ClassNotFoundException e) {
156:
157: throw new JmsJobException(e.getMessage(), e);
158:
159: } catch (InstantiationException e) {
160:
161: throw new JmsJobException(e.getMessage(), e);
162:
163: } catch (IllegalAccessException e) {
164:
165: throw new JmsJobException(e.getMessage(), e);
166: }
167:
168: }
169:
170: }
|