KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > jms > IASJmsUtil


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.jms;
24
25 import java.io.*;
26 import java.util.*;
27 import java.util.logging.*;
28
29 import javax.naming.*;
30 import javax.jms.*;
31 import com.sun.enterprise.util.FileUtil;
32 import com.sun.enterprise.ServerConfiguration;
33 import com.sun.enterprise.repository.*;
34 import com.sun.enterprise.util.i18n.StringManager;
35 import com.sun.enterprise.server.ApplicationServer;
36 import com.sun.enterprise.server.ServerContext;
37 import com.sun.enterprise.config.serverbeans.*;
38 import com.sun.messaging.jmq.jmsspi.JMSAdminFactory;
39 import com.sun.messaging.jmq.jmsspi.JMSAdmin;
40 import com.sun.logging.LogDomains;
41
42 import com.sun.appserv.server.util.Version;
43
44 /**
45  * JMS Utilities.
46  *
47  */

48 public class IASJmsUtil {
49
50     private static Logger _logger = LogDomains.getLogger(LogDomains.JMS_LOGGER);
51
52     //Names of default JMS XA Connection Factories used by message bean container.
53
public static final String JavaDoc MDB_CONTAINER_QUEUE_XACF = "MDB_CONTAINER_QUEUE_CF__jmsxa_default";
54     public static final String JavaDoc MDB_CONTAINER_TOPIC_XACF = "MDB_CONTAINER_TOPIC_CF__jmsxa_default";
55     public static final String JavaDoc MQ_DIR_NAME = "imq";
56
57     private static String JavaDoc DEFAULT_SERVER = "server";
58     private static String JavaDoc DEFAULT_MQ_INSTANCE = "imqbroker";
59
60     public static final String JavaDoc DEFAULT_USER = "admin";
61     public static final String JavaDoc DEFAULT_PASSWORD = "admin";
62     public static final String JavaDoc DEFAULT_MAX_ACTIVE_CONSUMERS = "-1";
63     public static final String JavaDoc MAX_ACTIVE_CONSUMERS_ATTRIBUTE = "MaxNumActiveConsumers";
64     public static final String JavaDoc MAX_ACTIVE_CONSUMERS_PROPERTY = "maxNumActiveConsumers";
65
66     private static StringManager _sm = StringManager.getManager(IASJmsUtil.class);
67
68     private static final boolean debug = false;
69
70
71     /**
72      * Form the name of the internal XA Connection Factory
73      * for each javax.jms.ConnectionFactory
74      */

75     public static String JavaDoc getXAConnectionFactoryName(String JavaDoc
76                                                     connectionFactoryName) {
77         return "JMSXA" + connectionFactoryName + "__jmsxa";
78     }
79
80     public static String JavaDoc getDestinationName(Destination destination) {
81         String JavaDoc name = null;
82         try {
83             name = (destination instanceof javax.jms.Queue JavaDoc) ?
84                 ((javax.jms.Queue JavaDoc) destination).getQueueName() :
85                 ((javax.jms.Topic JavaDoc) destination).getTopicName();
86         } catch(Exception JavaDoc e) {}
87         return name;
88     }
89
90     public static void checkVersion(JMSAdmin ja) {
91         float version = 0.0f;
92         String JavaDoc vs = "?.?";
93
94         try {
95             vs = ja.getVersion();
96             version = Float.parseFloat(vs);
97         }
98         catch (Exception JavaDoc e) {
99             throw new RuntimeException JavaDoc("Error detected while parsing JMS " +
100                 "provider SPI version string (" + vs + ").");
101         }
102
103         if (version < 2.0 || version >= 3.0)
104             throw new RuntimeException JavaDoc(
105                 "Incorrect JMS Provider SPI version detected (" +
106                     vs + ")." +
107                 " Please make sure that you are using the correct" +
108                 " version of the JMS provider.");
109     }
110
111     /**
112      * JMSAdminFactory is used to get a handle to the
113      * administration capabilities of a JMS provider.
114      * Use Class.forName() to bootstrap an instance
115      * in order to avoid compile-time dependencies on
116      * implementation class names.
117      */

118     public static JMSAdminFactory getJMSAdminFactory() throws Exception JavaDoc {
119         JMSAdminFactory jmsAdminFactory = null;
120
121         Class JavaDoc jmsAdminFactoryClass = Class.forName("com.sun.messaging.jmq.admin.jmsspi.JMSAdminFactoryImpl");
122         jmsAdminFactory = (JMSAdminFactory)jmsAdminFactoryClass.newInstance();
123
124         checkVersion(jmsAdminFactory.getJMSAdmin());
125
126         return jmsAdminFactory;
127     }
128
129     protected static JMSAdmin getJMSAdmin() throws Exception JavaDoc {
130         JMSAdmin jmsAdmin = JmsProviderLifecycle.getJMSAdmin();
131         if (jmsAdmin == null) {
132             jmsAdmin = getJMSAdminFactory().getJMSAdmin();
133         }
134         return jmsAdmin;
135     }
136
137     /**
138      * Validate a JMS Selector for syntactic correctness.
139      * @throws Exception if system error contacting JMS Service
140      * @throws JMSException if syntax error
141      */

142     public static void validateJMSSelector(String JavaDoc selector)
143         throws Exception JavaDoc, JMSException {
144
145         getJMSAdmin().validateJMSSelector(selector);
146     }
147
148     /**
149      * @return the ClientID property name or null
150      */

151     public static String JavaDoc clientIDPropertyName() throws Exception JavaDoc {
152         return getJMSAdmin().clientIDPropertyName();
153     }
154
155     /**
156      * wrap a JMS standard XAQueue/TopicConnectionFactory or Queue/TopicConnectionFactory
157      *
158      * This method is used for foreign (non-built-in) JMS provider
159      *
160      * @return a JMSXAConnectionFactory object
161      * @throws JMSException if syntax error
162      */

163     public static Object JavaDoc wrapJMSConnectionFactoryObject(Object JavaDoc obj)
164         throws Exception JavaDoc, JMSException {
165
166         return getJMSAdmin().wrapJMSConnectionFactoryObject(obj);
167     }
168
169     /**
170      * Computes the instance name for the MQ broker.
171      */

172     public static String JavaDoc getBrokerInstanceName(String JavaDoc asDomain,
173         String JavaDoc asInstance, JmsService js) {
174
175         ElementProperty[] jmsProperties = js.getElementProperty();
176
177         String JavaDoc instanceName = null;
178         String JavaDoc suffix = null;
179
180         if (jmsProperties != null) {
181             for (int ii=0; ii < jmsProperties.length; ii++) {
182                 ElementProperty p = jmsProperties[ii];
183                 String JavaDoc name = p.getName();
184
185                 if (name.equals("instance-name"))
186                     instanceName = p.getValue();
187                 if (name.equals("instance-name-suffix"))
188                     suffix = p.getValue();
189                 if (name.equals("append-version") &&
190                     Boolean.valueOf(p.getValue()).booleanValue()) {
191                     suffix = Version.getMajorVersion() + "_" +
192                         Version.getMinorVersion();
193                 }
194             }
195         }
196
197         if (instanceName != null)
198             return instanceName;
199
200         if (asInstance.equals(DEFAULT_SERVER)) {
201             instanceName = DEFAULT_MQ_INSTANCE;
202         } else {
203             instanceName = asDomain + "_" + asInstance;
204         }
205
206         if (suffix != null)
207             instanceName = instanceName + "_" + suffix;
208
209         return instanceName;
210     }
211
212     /**
213      * Getting default value for Maximum active Queue consumers.
214      */

215     public static String JavaDoc getDefaultMaxActiveConsumers () {
216         return DEFAULT_MAX_ACTIVE_CONSUMERS;
217     }
218
219     /**
220      * Getting Name Maximum active consumers property;
221      */

222     public static String JavaDoc getMaxActiveConsumersProperty () {
223         return MAX_ACTIVE_CONSUMERS_PROPERTY;
224     }
225
226     /**
227      * Getting Name Maximum active consumers attribute;
228      */

229     public static String JavaDoc getMaxActiveConsumersAttribute () {
230         return MAX_ACTIVE_CONSUMERS_ATTRIBUTE;
231     }
232 }
233
Popular Tags