KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > connectors > system > MQJMXConnectorHelper


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.system;
25
26 import java.lang.reflect.Method JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31 import javax.resource.spi.ResourceAdapter JavaDoc;
32 import com.sun.enterprise.Switch;
33 import com.sun.enterprise.admin.target.Target;
34 import com.sun.enterprise.admin.target.TargetBuilder;
35 import com.sun.enterprise.admin.target.TargetType;
36 import com.sun.enterprise.config.ConfigException;
37 import com.sun.enterprise.config.serverbeans.AppserverClusterViewFromCacheRepository;
38 import com.sun.enterprise.config.serverbeans.Config;
39 import com.sun.enterprise.config.serverbeans.JmsHost;
40 import com.sun.enterprise.config.serverbeans.JmsService;
41 import com.sun.enterprise.config.serverbeans.Server;
42 import com.sun.enterprise.connectors.ConnectorRegistry;
43 import com.sun.enterprise.connectors.ConnectorRuntime;
44 import com.sun.enterprise.connectors.ConnectorRuntimeException;
45 import com.sun.enterprise.server.ApplicationServer;
46 import com.sun.enterprise.util.i18n.StringManager;
47 import com.sun.enterprise.server.ManagerFactory;
48 import com.sun.logging.LogDomains;
49 import com.sun.enterprise.config.serverbeans.ServerHelper;
50 import com.sun.enterprise.config.ConfigContext;
51
52 //import javax.management.*;
53
//import javax.management.remote.*;
54
//import com.sun.messaging.AdminConnectionFactory;
55
//import com.sun.messaging.jms.management.*;
56

57 /**
58  * A Utility class to obtain JMXConnectorInfo for MQ broker instances
59  * @author Sivakumar Thyagarajan sivakumar.thyagarajan@sun.com
60  */

61 public class MQJMXConnectorHelper {
62     static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.RSR_LOGGER);
63     private static final String JavaDoc JMXSERVICEURLLIST = "JMXServiceURLList";
64     private static final String JavaDoc JMXCONNECTORENV = "JMXConnectorEnv";
65     
66     private static StringManager sm = StringManager.getManager(
67                                                    MQJMXConnectorHelper.class);
68     
69
70     public static MQJMXConnectorInfo[] getMQJMXConnectorInfo(final String JavaDoc target)
71                                 throws ConnectorRuntimeException {
72         try {
73             final Target tgt = getTarget(target);
74             final Config config = tgt.getConfigs()[0];
75             final JmsService jmsService = config.getJmsService();
76             
77             ActiveJmsResourceAdapter air = getMQAdapter();
78             final Class JavaDoc mqRAClassName = air.getResourceAdapter().getClass();
79             
80             MQJMXConnectorInfo mqjmxForServer = (MQJMXConnectorInfo)
81             java.security.AccessController.doPrivileged
82                 (new java.security.PrivilegedExceptionAction JavaDoc() {
83                  public java.lang.Object JavaDoc run() throws Exception JavaDoc {
84                      if(!isClustered(tgt)) {
85                          _logger.log(Level.FINE, "Getting JMX connector for" +
86                                                " standalone target " + target);
87                          return _getMQJMXConnectorInfo(tgt.getName(),
88                                                       jmsService, mqRAClassName);
89                      } else {
90                          _logger.log(Level.FINE, "Getting JMX connector for" +
91                                                     " cluster target " + target);
92                          return _getMQJMXConnectorInfoForCluster(tgt.getName(),
93                                                       jmsService, mqRAClassName);
94                      }
95                  }
96             });
97           
98             return new MQJMXConnectorInfo[]{mqjmxForServer};
99         } catch (Exception JavaDoc e) {
100             //e.printStackTrace();
101
ConnectorRuntimeException cre = new ConnectorRuntimeException(e.getMessage());
102             cre.initCause(e);
103             throw cre;
104         }
105     }
106
107     /*
108      * Starts the MQ RA in the DAS, as all MQ related operations are
109      * performed in DAS.
110      */

111     private static ActiveJmsResourceAdapter getMQAdapter() throws Exception JavaDoc {
112         //Start ActiveJMSResourceAdapter.
113
final String JavaDoc mqraModuleName = ConnectorRuntime.DEFAULT_JMS_ADAPTER;
114
115         ActiveJmsResourceAdapter air = (ActiveJmsResourceAdapter)
116         java.security.AccessController.doPrivileged
117             (new java.security.PrivilegedExceptionAction JavaDoc() {
118                  public java.lang.Object JavaDoc run() throws Exception JavaDoc {
119                      ManagerFactory.getSAConnectorModulesManager().
120                      loadOneSystemApp(mqraModuleName, true);
121                      return (ActiveJmsResourceAdapter) ConnectorRegistry.getInstance().
122                            getActiveResourceAdapter(mqraModuleName);
123              }
124         });
125         return air;
126     }
127
128     /**
129      * Gets the <code>MQJMXConnector</code> object for a cluster. Since this code is
130      * executed in DAS, an admin API is used to resolve hostnames and ports of
131      * cluster instances for LOCAL type brokers while creating the connectionURL.
132      */

133     private static MQJMXConnectorInfo _getMQJMXConnectorInfoForCluster(
134                     String JavaDoc target, JmsService jmsService, Class JavaDoc mqRAClassName)
135                     throws ConnectorRuntimeException {
136         // Create a new RA instance.
137
ResourceAdapter JavaDoc raInstance = null;
138         // Set the ConnectionURL.
139
try {
140             MQAddressList list = null;
141
142             if (jmsService.getType().equalsIgnoreCase(ActiveJmsResourceAdapter.REMOTE)) {
143                 list = getDefaultAddressList(jmsService);
144             } else {
145                 String JavaDoc domainurl =
146                     ApplicationServer.getServerContext().getServerConfigURL();
147                 AppserverClusterViewFromCacheRepository rep
148                     = new AppserverClusterViewFromCacheRepository(domainurl);
149
150                 java.util.Map JavaDoc<String JavaDoc,JmsHost> hostMap =
151                     rep.getResolvedLocalJmsHostsInCluster(target);
152
153                 if ( hostMap.size() == 0 ) {
154                     String JavaDoc msg = sm.getString("mqjmx.no_jms_hosts");
155                     throw new ConnectorRuntimeException(msg);
156                 }
157                 
158                 list = new MQAddressList();
159                 for (JmsHost host : hostMap.values()) {
160                     list.addMQUrl(host);
161                 }
162             }
163
164             String JavaDoc connectionUrl = list.toString();
165             raInstance = getConfiguredRA(mqRAClassName, connectionUrl);
166         } catch (Exception JavaDoc e) {
167             e.printStackTrace();
168             ConnectorRuntimeException cre = new ConnectorRuntimeException(e.getMessage());
169             cre.initCause(e);
170             throw cre;
171         }
172         
173         try {
174             String JavaDoc jmxServiceURL = null, jmxServiceURLList = null;
175             Map JavaDoc<String JavaDoc, ?> jmxConnectorEnv = null;
176             Method JavaDoc[] methds = raInstance.getClass().getMethods();
177             for (int i = 0; i < methds.length; i++) {
178                 Method JavaDoc m = methds[i];
179                 if (m.getName().equalsIgnoreCase("get" + JMXSERVICEURLLIST)){
180                     jmxServiceURLList = (String JavaDoc)m.invoke(raInstance, new Object JavaDoc[]{});
181                     if (jmxServiceURLList != null && !jmxServiceURLList.trim().equals("")){
182                         jmxServiceURL = getFirstJMXServiceURL(jmxServiceURLList);
183                     }
184                 } else if (m.getName().equalsIgnoreCase("get" + JMXCONNECTORENV)){
185                     jmxConnectorEnv = (Map JavaDoc<String JavaDoc,?>)m.invoke(raInstance, new Object JavaDoc[]{});
186                 }
187             }
188             MQJMXConnectorInfo mqInfo = new MQJMXConnectorInfo(target,
189                             ActiveJmsResourceAdapter.getBrokerInstanceName(jmsService) ,
190                             jmsService.getType(), jmxServiceURL, jmxConnectorEnv);
191             return mqInfo;
192         } catch (Exception JavaDoc e) {
193             e.printStackTrace();
194             ConnectorRuntimeException cre = new ConnectorRuntimeException(e.getMessage());
195             cre.initCause(e);
196             throw cre;
197         }
198     }
199
200     /**
201      * Gets the <code>MQJMXConnector</code> object for a standalone server instances
202      * and destinations created in DAS/PE instance. The default address list is used
203      * while setting the ConnectionURL in MQ RA.
204      */

205     private static MQJMXConnectorInfo _getMQJMXConnectorInfo(
206                     String JavaDoc targetName, JmsService jmsService, Class JavaDoc mqRAClassName)
207                                         throws ConnectorRuntimeException {
208         try {
209             //If DAS, use the default address list, else obtain
210

211             String JavaDoc connectionURL = null;
212             
213             boolean isDAS = isDAS(targetName);
214             
215             if (isDAS) {
216                 logFine(" _getMQJMXConnectorInfo - in DAS");
217                 _logger.log(Level.FINE,"In DAS");
218                 connectionURL = getDefaultAddressList(jmsService).toString();
219             } else {
220                 //Standalone server instance
221
_logger.log(Level.FINE,"not in DAS");
222                 logFine(" _getMQJMXConnectorInfo - NOT in DAS");
223                 String JavaDoc domainurl =
224                 ApplicationServer.getServerContext().getServerConfigURL();
225                 JmsService serverJmsService= getJmsServiceOfStandaloneServerInstance(targetName);
226                 MQAddressList mqadList = new MQAddressList(serverJmsService, targetName);
227                 mqadList.setup();
228                 connectionURL = mqadList.toString();
229             }
230             logFine(" _getMQJMXConnectorInfo - connection URL " + connectionURL);
231             
232             ResourceAdapter JavaDoc raInstance = getConfiguredRA(mqRAClassName, connectionURL);
233             String JavaDoc jmxServiceURL = null, jmxServiceURLList = null;
234             Map JavaDoc<String JavaDoc, ?> jmxConnectorEnv = null;
235             Method JavaDoc[] methds = raInstance.getClass().getMethods();
236             for (int i = 0; i < methds.length; i++) {
237                 Method JavaDoc m = methds[i];
238         if (m.getName().equalsIgnoreCase("get" + JMXSERVICEURLLIST)){
239                     jmxServiceURLList = (String JavaDoc)m.invoke(raInstance, new Object JavaDoc[]{});
240                 } else if (m.getName().equalsIgnoreCase("get" + JMXCONNECTORENV)){
241                     jmxConnectorEnv = (Map JavaDoc<String JavaDoc,?>)m.invoke(raInstance, new Object JavaDoc[]{});
242                 }
243             }
244             logFine(" _getMQJMXConnectorInfo - jmxServiceURLList " + jmxServiceURLList);
245             logFine(" _getMQJMXConnectorInfo - jmxConnectorEnv " + jmxConnectorEnv);
246             jmxServiceURL = getFirstJMXServiceURL(jmxServiceURLList);
247             
248             MQJMXConnectorInfo mqInfo = new MQJMXConnectorInfo(targetName,
249                             ActiveJmsResourceAdapter.getBrokerInstanceName(jmsService) ,
250                             jmsService.getType(), jmxServiceURL, jmxConnectorEnv);
251             return mqInfo;
252         } catch (Exception JavaDoc e) {
253             e.printStackTrace();
254             ConnectorRuntimeException cre = new ConnectorRuntimeException(e.getMessage());
255             cre.initCause(e);
256             throw cre;
257         }
258     }
259     
260     private static boolean isDAS(String JavaDoc targetName) throws ConfigException {
261         ConfigContext con = com.sun.enterprise.admin.server.core.AdminService.getAdminService().getAdminContext().getAdminConfigContext();
262         if (isAConfig(targetName)) {
263             return false;
264         }
265         return ServerHelper.isDAS(con, targetName);
266     }
267     
268     private static boolean isAConfig(String JavaDoc targetName) throws ConfigException {
269         ConfigContext con = com.sun.enterprise.admin.server.core.AdminService.getAdminService().getAdminContext().getAdminConfigContext();
270         return ServerHelper.isAConfig(con, targetName);
271     }
272     
273
274     private static JmsService getJmsServiceOfStandaloneServerInstance(String JavaDoc targetName) throws ConfigException {
275         logFine("getJMSServiceOfSI LL " + targetName);
276         ConfigContext con = com.sun.enterprise.admin.server.core.AdminService.getAdminService().getAdminContext().getAdminConfigContext();
277         
278         Config cfg = null;
279         if (isAConfig(targetName)) {
280             cfg = ServerHelper.getConfigByName(con, targetName);
281         } else {
282             cfg = ServerHelper.getConfigForServer(con, targetName);
283         }
284         
285         logFine("cfg " + cfg);
286         JmsService jmsService = cfg.getJmsService();
287         logFine("jmsservice " + jmsService);
288         return jmsService;
289     }
290
291     /*
292      * Configures an instance of MQ-RA with the connection URL passed in.
293      * This configured RA is then used to obtain the JMXServiceURL/JMXServiceURLList
294      */

295     private static ResourceAdapter JavaDoc getConfiguredRA(Class JavaDoc mqRAclassname,
296                                        String JavaDoc connectionURL) throws Exception JavaDoc {
297         ResourceAdapter JavaDoc raInstance = (ResourceAdapter JavaDoc) mqRAclassname.newInstance();
298         Method JavaDoc setConnectionURL = mqRAclassname.getMethod(
299                        "set" + ActiveJmsResourceAdapter.CONNECTION_URL,
300                         new Class JavaDoc[] { String JavaDoc.class});
301         setConnectionURL.invoke(raInstance, new Object JavaDoc[] {connectionURL});
302         logFine(" getConfiguredRA - set connectionURL as " + connectionURL);
303         return raInstance;
304     }
305     
306     private static MQAddressList getDefaultAddressList(JmsService jmsService)
307                                                        throws ConfigException {
308         MQAddressList list = new MQAddressList(jmsService);
309         list.setup();
310         return list;
311     }
312     
313     private static String JavaDoc getFirstJMXServiceURL(String JavaDoc jmxServiceURLList) {
314         //If type is REMOTE, MQ RA returns a null jmxServiceURL and a non-null
315
//jmxServiceURLList for PE also.
316
if ((jmxServiceURLList == null) || ("".equals(jmxServiceURLList))) {
317             return jmxServiceURLList;
318         } else {
319             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(jmxServiceURLList, " ");
320             return tokenizer.nextToken();
321         }
322     }
323
324     private static Target getTarget(String JavaDoc target) throws ConfigException {
325         final TargetType[] vaildTargetTypes = new TargetType[] {
326                         TargetType.CONFIG,
327                         TargetType.SERVER,
328                         TargetType.DOMAIN,
329                         TargetType.CLUSTER,
330                         TargetType.STANDALONE_SERVER,
331                         TargetType.UNCLUSTERED_SERVER,
332                         TargetType.STANDALONE_CLUSTER,
333                         TargetType.DAS };
334         final Target tgt = TargetBuilder.INSTANCE.createTarget(
335                         vaildTargetTypes, target,
336                         com.sun.enterprise.admin.server.core.AdminService.getAdminService().getAdminContext().getAdminConfigContext());
337         assert tgt != null;
338         return tgt;
339     }
340
341     private static boolean isClustered(Target tgt) throws ConfigException {
342         return tgt.getType() == TargetType.CLUSTER;
343     }
344
345     private static void logFine(String JavaDoc s) {
346         if (_logger.isLoggable(Level.FINE)) {
347             _logger.log(Level.FINE, "MQJMXConnectorHelper :: " + s);
348         }
349     }
350     
351     /*
352     //Simple method for testing
353     private static void test(MQJMXConnectorInfo mqjmxForServer) {
354         try {
355             MBeanServerConnection mbsc = mqjmxForServer.getMQMBeanServerConnection();
356             ObjectName objName = new ObjectName(
357                             MQObjectName.DESTINATION_MONITOR_MANAGER_MBEAN_NAME);
358             String destinationObjNames[] = (String[]) mbsc.invoke(objName,
359                             DestinationOperations.GET_DESTINATIONS, null, null);
360             _logger.log(Level.FINE,"Listing destinations:");
361             for (int i = 0; i < destinationObjNames.length; ++i) {
362                 ObjectName oneDestObjName = new ObjectName(
363                                 destinationObjNames[i]);
364                 _logger.log(Level.FINE,"\tName: "
365                                 + mbsc.getAttribute(oneDestObjName,
366                                                 DestinationAttributes.NAME));
367                 _logger.log(Level.FINE,"\tType: "+ mbsc.getAttribute(oneDestObjName,
368                                 DestinationAttributes.TYPE_LABEL));
369                 _logger.log(Level.FINE,"\tState: " + mbsc.getAttribute(oneDestObjName,
370                                 DestinationAttributes.STATE_LABEL));
371                 _logger.log(Level.FINE,"\tMessages: " + mbsc.getAttribute(oneDestObjName,
372                                 DestinationAttributes.AVG_NUM_MSGS));
373                 _logger.log(Level.FINE,"");
374             }
375         } catch (Exception e) {
376             e.printStackTrace();
377         }
378     }
379 */

380 }
381
Popular Tags