KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > connectors > util > JmsRaUtil


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
24 package com.sun.enterprise.connectors.util;
25
26 import com.sun.enterprise.config.serverbeans.*;
27 import com.sun.enterprise.config.*;
28 import com.sun.enterprise.server.*;
29 import com.sun.enterprise.deployment.*;
30 import com.sun.enterprise.server.*;
31 import com.sun.enterprise.connectors.*;
32 import com.sun.enterprise.connectors.system.*;
33 import com.sun.enterprise.connectors.inflow.*;
34 import com.sun.enterprise.util.zip.*;
35 import com.sun.logging.LogDomains;
36 import java.io.*;
37 import java.util.logging.*;
38 import java.util.jar.*;
39
40 /**
41  *
42  * @author
43  */

44 public class JmsRaUtil {
45
46     private final String JavaDoc MQ_RAR = "imqjmsra.rar";
47
48     private final String JavaDoc SYSTEM_APP_DIR =
49         "lib" + File.separator + "install" + File.separator + "applications";
50
51     private final String JavaDoc MQ_RAR_MANIFEST =
52         ConnectorConstants.DEFAULT_JMS_ADAPTER + File.separator + "META-INF"
53         + File.separator + "MANIFEST.MF";
54
55     // Manifest version tag.
56
private final String JavaDoc MANIFEST_TAG = "Implementation-Version";
57
58     private static final String JavaDoc propName_reconnect_delay_in_seconds =
59         "reconnect-delay-in-seconds";
60     private static final String JavaDoc propName_reconnect_max_retries =
61         "reconnect-max-retries";
62     private static final String JavaDoc propName_reconnect_enabled =
63         "reconnect-enabled";
64     private static final int DEFAULT_RECONNECT_DELAY = 60;
65     private static final int DEFAULT_RECONNECT_RETRIES = 60;
66
67     private static final String JavaDoc propName_cmt_max_runtime_exceptions
68         = "cmt-max-runtime-exceptions";
69     private static final int DEFAULT_CMT_MAX_RUNTIME_EXCEPTIONS = 1;
70
71     private int cmtMaxRuntimeExceptions = DEFAULT_CMT_MAX_RUNTIME_EXCEPTIONS;
72
73     private int reconnectDelayInSeconds = DEFAULT_RECONNECT_DELAY;
74     private int reconnectMaxRetries = DEFAULT_RECONNECT_RETRIES;
75     private boolean reconnectEnabled = false;
76
77     ServerContext sc = null;
78     ConfigContext ctx = null;
79     JmsService js = null;
80     MQAddressList list = null;
81
82     static Logger _mdblogger = LogDomains.getLogger(LogDomains.MDB_LOGGER);
83     static Logger _rarlogger = LogDomains.getLogger(LogDomains.RSR_LOGGER);
84
85     public JmsRaUtil() throws ConnectorRuntimeException {
86         this(null);
87     }
88
89     public JmsRaUtil(JmsService js) throws ConnectorRuntimeException {
90         try {
91             if (js != null) {
92             this.js = js;
93             } else {
94             sc = ApplicationServer.getServerContext();
95             ctx = sc.getConfigContext();
96             this.js = ServerBeansFactory.getJmsServiceBean(ctx);
97             }
98             list = new MQAddressList(this.js);
99 // if (isClustered() && ! this.js.getType().equals(
100
// ActiveJmsResourceAdapter.REMOTE)) {
101
// list.setupForLocalCluster();
102
// } else {
103
list.setup();
104 // }
105
} catch(ConfigException ce) {
106             throw handleException(ce);
107         }
108     }
109
110     public String JavaDoc getUrl() {
111     try {
112             return list.toString();
113     } catch (Exception JavaDoc e) {
114         return null;
115     }
116     }
117     
118     public static boolean isClustered() throws ConnectorRuntimeException {
119         try {
120               ConfigContext ctxt = ApplicationServer.getServerContext().getConfigContext();
121               return ServerHelper.isServerClustered(ctxt,
122                               ApplicationServer.getServerContext().getInstanceName());
123           } catch (ConfigException e) {
124               throw handleException(e);
125           }
126      }
127     
128     public String JavaDoc getJMSServiceType(){
129         return this.js.getType();
130     }
131
132     public MQAddressList getUrlList() {
133         return list;
134     }
135
136     public boolean getReconnectEnabled() {
137         return js.isReconnectEnabled();
138     }
139
140     public String JavaDoc getReconnectInterval() {
141         return js.getReconnectIntervalInSeconds();
142     }
143
144     public String JavaDoc getReconnectAttempts() {
145         return js.getReconnectAttempts();
146     }
147
148     public String JavaDoc getAddressListIterations() {
149         return js.getAddresslistIterations();
150     }
151
152     public String JavaDoc getAddressListBehaviour() {
153         return js.getAddresslistBehavior();
154     }
155
156     public void setMdbContainerProperties(){
157         com.sun.enterprise.config.serverbeans.MdbContainer mdbc = null;
158         try {
159             com.sun.enterprise.server.ServerContext sc =
160                 com.sun.enterprise.server.ApplicationServer.getServerContext();
161
162             mdbc = com.sun.enterprise.config.serverbeans.ServerBeansFactory.
163                   getMdbContainerBean(sc.getConfigContext());
164
165         }
166         catch (Exception JavaDoc e) {
167             _mdblogger.log(Level.WARNING, "containers.mdb.config_exception",
168                         new Object JavaDoc[]{e.getMessage()});
169             if (_mdblogger.isLoggable(Level.FINE)) {
170                 _mdblogger.log(Level.FINE, e.getClass().getName(), e);
171             }
172         }
173
174         if (mdbc != null) {
175             ElementProperty[] props = mdbc.getElementProperty();
176             if (props != null) {
177                 for (int i = 0; i < props.length; i++) {
178                     ElementProperty p = props[i];
179                     if (p == null) continue;
180                     String JavaDoc name = p.getName();
181                     if (name == null) continue;
182                     try {
183                         if (name.equals(propName_reconnect_enabled)) {
184                             if (p.getValue() == null) continue;
185                             reconnectEnabled =
186                                 Boolean.valueOf(p.getValue()).booleanValue();
187                         }
188                         else if (name.equals
189                                  (propName_reconnect_delay_in_seconds)) {
190                             try {
191                                 reconnectDelayInSeconds =
192                                     Integer.parseInt(p.getValue());
193                             } catch (Exception JavaDoc e) {
194                                 _mdblogger.log(Level.WARNING,
195                                     "containers.mdb.config_exception",
196                                     new Object JavaDoc[]{e.getMessage()});
197                             }
198                         }
199                         else if (name.equals(propName_reconnect_max_retries)) {
200                             try {
201                                 reconnectMaxRetries =
202                                     Integer.parseInt(p.getValue());
203                             } catch (Exception JavaDoc e) {
204                                 _mdblogger.log(Level.WARNING,
205                                     "containers.mdb.config_exception",
206                                     new Object JavaDoc[]{e.getMessage()});
207                             }
208                         }
209                         else if (name.equals
210                                  (propName_cmt_max_runtime_exceptions)) {
211                             try {
212                                 cmtMaxRuntimeExceptions =
213                                     Integer.parseInt(p.getValue());
214                             } catch (Exception JavaDoc e) {
215                                 _mdblogger.log(Level.WARNING,
216                                     "containers.mdb.config_exception",
217                                     new Object JavaDoc[]{e.getMessage()});
218                             }
219                         }
220                     } catch (Exception JavaDoc e) {
221                         _mdblogger.log(Level.WARNING,
222                                     "containers.mdb.config_exception",
223                                     new Object JavaDoc[]{e.getMessage()});
224                         if (_mdblogger.isLoggable(Level.FINE)) {
225                             _mdblogger.log(Level.FINE, e.getClass().getName(), e);
226                         }
227                     }
228                 }
229             }
230         }
231         if (reconnectDelayInSeconds < 0) {
232             reconnectDelayInSeconds = DEFAULT_RECONNECT_DELAY;
233         }
234         if (reconnectMaxRetries < 0) {
235             reconnectMaxRetries = DEFAULT_RECONNECT_RETRIES;
236         }
237         if (_mdblogger.isLoggable(Level.FINE)) {
238             _mdblogger.log(Level.FINE,
239                 propName_reconnect_delay_in_seconds+"="+
240                 reconnectDelayInSeconds +", "+
241                 propName_reconnect_max_retries+ "="+reconnectMaxRetries + ", "+
242                 propName_reconnect_enabled+"="+reconnectEnabled);
243         }
244
245         //Now set all these properties in the active resource adapter
246
MdbContainerProps.setReconnectDelay(reconnectDelayInSeconds);
247         MdbContainerProps.setReconnectMaxRetries(reconnectMaxRetries);
248         MdbContainerProps.setReconnectEnabled(reconnectEnabled);
249         MdbContainerProps.setMaxRuntimeExceptions(cmtMaxRuntimeExceptions);
250
251     }
252
253     public void configureDescriptor(ConnectorDescriptor cd) {
254
255         Object JavaDoc[] envProps = cd.getConfigProperties().toArray();
256     
257         for (int i = 0; i < envProps.length; i++) {
258             EnvironmentProperty envProp = (EnvironmentProperty) envProps[i];
259             String JavaDoc name = envProp.getName();
260         if (!name.equals("ConnectionURL")) {
261             continue;
262         }
263             String JavaDoc userValue = getUrl();
264             if (userValue != null) {
265                 cd.removeConfigProperty(envProp);
266                 cd.addConfigProperty(new EnvironmentProperty(
267                               name, userValue, userValue, envProp.getType()));
268             }
269
270         }
271
272     }
273
274     /**
275      * Obtains the Implementation-Version from the MQ Client libraries
276      * that are deployed in the application server and in MQ installation
277      * directory.
278      */

279     public void upgradeIfNecessary() {
280
281         String JavaDoc installedMqVersion = null;
282         String JavaDoc deployedMqVersion = null;
283
284         try {
285            installedMqVersion = getInstalledMqVersion();
286            _rarlogger.log(Level.FINE,"installedMQVersion :: " + installedMqVersion);
287            deployedMqVersion = getDeployedMqVersion();
288            _rarlogger.log(Level.FINE,"deployedMQVersion :: " + deployedMqVersion);
289         }catch (Exception JavaDoc e) {
290         return;
291         }
292
293         String JavaDoc deployed_dir =
294            java.lang. System.getProperty(Constants.INSTALL_ROOT)
295            + File.separator + SYSTEM_APP_DIR + File.separator
296            + ConnectorConstants.DEFAULT_JMS_ADAPTER;
297
298         // If the Manifest entry has different versions, then attempt to
299
// explode the MQ resource adapter.
300
if (!installedMqVersion.equals(deployedMqVersion)) {
301            try {
302                _rarlogger.log(Level.INFO, "jmsra.upgrade_started" );
303            ZipFile rarFile = new ZipFile(java.lang.System.getProperty
304                                  (Constants.INSTALL_IMQ_LIB) +
305                                  File.separator + MQ_RAR, deployed_dir);
306                rarFile.explode();
307                _rarlogger.log(Level.INFO, "jmsra.upgrade_completed");
308        } catch(ZipFileException ze) {
309                _rarlogger.log(Level.SEVERE,"jmsra.upgrade_failed",ze.getMessage());
310            }
311         }
312
313     }
314
315     private String JavaDoc getInstalledMqVersion() throws Exception JavaDoc {
316        String JavaDoc ver = null;
317        // Full path of installed Mq Client library
318
String JavaDoc installed_dir =
319            java.lang.System.getProperty(Constants.INSTALL_IMQ_LIB);
320        String JavaDoc jarFile = installed_dir + File.separator + MQ_RAR;
321        _rarlogger.log(Level.FINE,"Installed MQ JAR " + jarFile);
322
323        try {
324            JarFile jFile = new JarFile(jarFile);
325            Manifest mf = jFile.getManifest();
326            ver = mf.getMainAttributes().getValue(MANIFEST_TAG);
327            return ver;
328        } catch (Exception JavaDoc e) {
329            _rarlogger.log(Level.WARNING, "jmsra.upgrade_check_failed",
330                        e.getMessage() + ":" + jarFile );
331            throw e;
332        }
333     }
334
335     private String JavaDoc getDeployedMqVersion() throws Exception JavaDoc {
336        String JavaDoc ver = null;
337         // Full path of Mq client library that is deployed in appserver.
338
String JavaDoc deployed_dir =
339            java.lang.System.getProperty(Constants.INSTALL_ROOT)
340            + File.separator + SYSTEM_APP_DIR;
341        String JavaDoc manifestFile = deployed_dir + File.separator +
342                              MQ_RAR_MANIFEST;
343        _rarlogger.log(Level.FINE,"Deployed MQ version Manifest file" + manifestFile);
344        try {
345            Manifest mf = new Manifest(new FileInputStream(manifestFile));
346            ver = mf.getMainAttributes().getValue(MANIFEST_TAG);
347            return ver;
348        } catch (Exception JavaDoc e) {
349            _rarlogger.log(Level.WARNING, "jmsra.upgrade_check_failed",
350                        e.getMessage() + ":" + manifestFile );
351            throw e;
352        }
353    }
354
355    private static ConnectorRuntimeException handleException(Exception JavaDoc e) {
356         ConnectorRuntimeException cre =
357              new ConnectorRuntimeException(e.getMessage());
358         cre.initCause(e);
359         return cre;
360     }
361 }
362
Popular Tags