KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > JMSDestination


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 /*
25  * $Id: JMSDestination.java,v 1.14 2006/03/19 08:44:31 sivakumart Exp $
26  * author sreenivas.munnangi@sun.com
27  */

28
29 package com.sun.enterprise.admin.mbeans;
30
31 import java.io.PrintWriter JavaDoc;
32 import java.io.StringReader JavaDoc;
33 import java.io.StringWriter JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Enumeration JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Properties JavaDoc;
38 import java.util.logging.Level JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40
41 import javax.management.Attribute JavaDoc;
42 import javax.management.AttributeList JavaDoc;
43 import javax.management.MBeanServerConnection JavaDoc;
44 import javax.management.ObjectName JavaDoc;
45
46 import com.sun.enterprise.admin.common.JMSDestinationInfo;
47 import com.sun.enterprise.admin.common.constant.AdminConstants;
48 import com.sun.enterprise.admin.common.constant.JMSAdminConstants;
49 import com.sun.enterprise.admin.common.exception.JMSAdminException;
50 import com.sun.enterprise.admin.target.Target;
51 import com.sun.enterprise.admin.target.TargetType;
52 import com.sun.enterprise.connectors.ConnectorRuntime;
53 import com.sun.enterprise.connectors.system.MQJMXConnectorInfo;
54 import com.sun.enterprise.connectors.system.ActiveJmsResourceAdapter;
55 import com.sun.enterprise.util.i18n.StringManager;
56 import com.sun.messaging.jms.management.server.DestinationType;
57 import com.sun.messaging.jms.management.server.MQObjectName;
58
59 /**
60  * A helper class used for performing MQ related administration
61  * operations using MQ's JMX API.
62  *
63  * @author Sreenivas Munnangi
64  * @author Sivakumar Thyagarajan
65  * @since SJSAS9.0
66  */

67 public class JMSDestination {
68
69     // flag to enable the use of JMX for JMS destination commands
70
// if false uses the old behavior
71
// The value for DONT_USE_MQ_JMX can be set thru sysproperty
72
private static final boolean USE_JMX =
73         !(Boolean.getBoolean("DONT_USE_MQ_JMX"));
74
75     private static final Logger JavaDoc sLogger =
76         Logger.getLogger(AdminConstants.kLoggerName);
77     private static final StringManager localStrings =
78         StringManager.getManager( JMSDestination.class);
79
80
81     // default constructor
82
public JMSDestination () {
83     }
84
85     // create-jmsdest
86
public void createJMSDestination(String JavaDoc destName, String JavaDoc destType,
87         Properties JavaDoc destProps, String JavaDoc tgtName) throws JMSAdminException {
88
89         sLogger.log(Level.FINE, "createJMSDestination ...");
90                 MQJMXConnectorInfo mqInfo = getMQJMXConnectorInfo(tgtName);
91
92         //MBeanServerConnection mbsc = getMBeanServerConnection(tgtName);
93
try {
94             MBeanServerConnection JavaDoc mbsc = mqInfo.getMQMBeanServerConnection();
95             ObjectName JavaDoc on = new ObjectName JavaDoc(
96                 MQObjectName.DESTINATION_MANAGER_CONFIG_MBEAN_NAME);
97             String JavaDoc [] signature = null;
98             AttributeList JavaDoc destAttrs = null;
99             Object JavaDoc [] params = null;
100
101             if (destProps != null) {
102                 destAttrs = convertProp2Attrs(destProps);
103             }
104                         
105             setAppserverDefaults(destAttrs, mqInfo);
106
107             if (destType.equalsIgnoreCase(JMSAdminConstants.JMS_DEST_TYPE_TOPIC)) {
108                 destType = DestinationType.TOPIC;
109             } else if (destType.equalsIgnoreCase(JMSAdminConstants.JMS_DEST_TYPE_QUEUE)) {
110                 destType = DestinationType.QUEUE;
111             }
112
113             signature = new String JavaDoc [] {
114                     "java.lang.String",
115                     "java.lang.String",
116                     "javax.management.AttributeList"};
117             params = new Object JavaDoc [] {destType, destName, destAttrs};
118
119             mbsc.invoke(on, "create", params, signature);
120         } catch (Exception JavaDoc e) {
121                     logAndHandleException(e, "admin.mbeans.rmb.error_creating_jms_dest");
122         } finally {
123                     try {
124                         if(mqInfo != null) {
125                             mqInfo.closeMQMBeanServerConnection();
126                         }
127                     } catch (Exception JavaDoc e) {
128                       handleException(e);
129                     }
130                 }
131     }
132
133     // delete-jmsdest
134
public void deleteJMSDestination(String JavaDoc destName, String JavaDoc destType,
135         String JavaDoc tgtName)
136         throws JMSAdminException {
137
138         sLogger.log(Level.FINE, "deleteJMSDestination ...");
139                 MQJMXConnectorInfo mqInfo = getMQJMXConnectorInfo(tgtName);
140
141         //MBeanServerConnection mbsc = getMBeanServerConnection(tgtName);
142

143         try {
144             MBeanServerConnection JavaDoc mbsc = mqInfo.getMQMBeanServerConnection();
145             ObjectName JavaDoc on = new ObjectName JavaDoc(
146                 MQObjectName.DESTINATION_MANAGER_CONFIG_MBEAN_NAME);
147             String JavaDoc [] signature = null;
148             Object JavaDoc [] params = null;
149
150             signature = new String JavaDoc [] {
151                 "java.lang.String",
152                 "java.lang.String"};
153
154             if (destType.equalsIgnoreCase("topic")) {
155                 destType = DestinationType.TOPIC;
156             } else if (destType.equalsIgnoreCase("queue")) {
157                 destType = DestinationType.QUEUE;
158             }
159             params = new Object JavaDoc [] {destType, destName};
160             mbsc.invoke(on, "destroy", params, signature);
161         } catch (Exception JavaDoc e) {
162                    //log JMX Exception trace as WARNING
163
logAndHandleException(e, "admin.mbeans.rmb.error_deleting_jms_dest");
164                 } finally {
165                     try {
166                         if(mqInfo != null) {
167                             mqInfo.closeMQMBeanServerConnection();
168                         }
169                     } catch (Exception JavaDoc e) {
170                       handleException(e);
171                     }
172                 }
173     }
174
175     // list-jmsdest
176
public JMSDestinationInfo [] listJMSDestinations(String JavaDoc tgtName, String JavaDoc destType)
177         throws JMSAdminException {
178
179         sLogger.log(Level.FINE, "listJMSDestination ...");
180                 MQJMXConnectorInfo mqInfo = getMQJMXConnectorInfo(tgtName);
181
182         //MBeanServerConnection mbsc = getMBeanServerConnection(tgtName);
183
try {
184                         MBeanServerConnection JavaDoc mbsc = mqInfo.getMQMBeanServerConnection();
185             ObjectName JavaDoc on = new ObjectName JavaDoc(
186                 MQObjectName.DESTINATION_MANAGER_CONFIG_MBEAN_NAME);
187             String JavaDoc [] signature = null;
188             Object JavaDoc [] params = null;
189
190             ObjectName JavaDoc [] dests = (ObjectName JavaDoc [])mbsc.invoke(on, "getDestinations", params, signature);
191             if ((dests != null) && (dests.length > 0)) {
192                 List JavaDoc<JMSDestinationInfo> jmsdi = new ArrayList JavaDoc<JMSDestinationInfo>();
193                 for (int i=0; i<dests.length; i++) {
194                     on = dests[i];
195
196                     String JavaDoc jdiType = DestinationType.toStringLabel(on.getKeyProperty("desttype"));
197                     String JavaDoc jdiName = on.getKeyProperty("name");
198
199                     // check if the destination name has double quotes at the beginning
200
// and end, if yes strip them
201
if ((jdiName != null) && (jdiName.length() > 1)) {
202                         if (jdiName.indexOf('"') == 0) {
203                             jdiName = jdiName.substring(1);
204                         }
205                         if (jdiName.lastIndexOf('"') == (jdiName.length() - 1)) {
206                             jdiName = jdiName.substring(0, jdiName.lastIndexOf('"'));
207                         }
208                     }
209
210                     JMSDestinationInfo jdi = new JMSDestinationInfo(jdiName, jdiType);
211
212                     if(destType == null) {
213                         jmsdi.add(jdi);
214                     } else if (destType.equals(JMSAdminConstants.JMS_DEST_TYPE_TOPIC)
215                             || destType.equals(JMSAdminConstants.JMS_DEST_TYPE_QUEUE)) {
216                         //Physical Destination Type specific listing
217
if (jdiType.equalsIgnoreCase(destType)) {
218                             jmsdi.add(jdi);
219                         }
220                     }
221                 }
222                 return (JMSDestinationInfo[]) jmsdi.toArray(new JMSDestinationInfo[]{});
223             }
224         } catch (Exception JavaDoc e) {
225                     //log JMX Exception trace as WARNING
226
logAndHandleException(e, "admin.mbeans.rmb.error_listing_jms_dest");
227                 } finally {
228                     try {
229                         if(mqInfo != null) {
230                             mqInfo.closeMQMBeanServerConnection();
231                         }
232                     } catch (Exception JavaDoc e) {
233                       handleException(e);
234                     }
235                 }
236
237         return null;
238     }
239
240
241     // JMSPing
242
// String status of jms ping RUNNING or exception
243
public String JavaDoc JMSPing(String JavaDoc tgtName)
244         throws JMSAdminException {
245
246         sLogger.log(Level.FINE, "JMSPing ...");
247                 MQJMXConnectorInfo mqInfo = null;
248         try {
249             MQJMXConnectorInfo [] cInfo =
250                 ConnectorRuntime.getRuntime().getMQJMXConnectorInfo(tgtName);
251             if ((cInfo == null) || (cInfo.length < 1)) {
252                 throw new JMSAdminException(
253                         localStrings.getString("admin.mbeans.rmb.error_obtaining_jms"));
254             }
255             int k = -1;
256             for (int i=0; i<cInfo.length; i++) {
257                 if (tgtName.equals(cInfo[i].getASInstanceName())) {
258                     k = i;
259                     break;
260                 }
261             }
262             if (k == -1) {
263                 throw new JMSAdminException(
264                 localStrings.getString("admin.mbeans.rmb.invalid_server_instance", tgtName));
265             }
266                         mqInfo = cInfo[k];
267
268             MBeanServerConnection JavaDoc mbsc = cInfo[k].getMQMBeanServerConnection();
269                         //perform some work on the connection to check for connection health.
270
mbsc.getMBeanCount();
271
272         } catch (Exception JavaDoc e) {
273                     //log JMX Exception trace as WARNING
274
logAndHandleException(e, "admin.mbeans.rmb.error_pinging_jms");
275                 } finally {
276                     try {
277                         if(mqInfo != null) {
278                             mqInfo.closeMQMBeanServerConnection();
279                         }
280                     } catch (Exception JavaDoc e) {
281                       handleException(e);
282                     }
283                 }
284         return JMSAdminConstants.JMS_HOST_RUNNING;
285     }
286
287
288     // purge-jmsdest
289
public void purgeJMSDestination(String JavaDoc destName, String JavaDoc destType, String JavaDoc tgtName)
290         throws JMSAdminException {
291
292         sLogger.log(Level.FINE, "purgeJMSDestination ...");
293                 MQJMXConnectorInfo mqInfo = getMQJMXConnectorInfo(tgtName);
294
295         //MBeanServerConnection mbsc = getMBeanServerConnection(tgtName);
296
try {
297
298                         MBeanServerConnection JavaDoc mbsc = mqInfo.getMQMBeanServerConnection();
299             if (destType.equalsIgnoreCase("topic")) {
300                 destType = DestinationType.TOPIC;
301             } else if (destType.equalsIgnoreCase("queue")) {
302                 destType = DestinationType.QUEUE;
303             }
304             ObjectName JavaDoc on =
305                 MQObjectName.createDestinationConfig(destType, destName);
306             String JavaDoc [] signature = null;
307             Object JavaDoc [] params = null;
308
309             mbsc.invoke(on, "purge", params, signature);
310         } catch (Exception JavaDoc e) {
311                     //log JMX Exception trace as WARNING
312
logAndHandleException(e, "admin.mbeans.rmb.error_purging_jms_dest");
313                 } finally {
314                     try {
315                         if(mqInfo != null) {
316                             mqInfo.closeMQMBeanServerConnection();
317                         }
318                     } catch (Exception JavaDoc e) {
319                       handleException(e);
320                     }
321                 }
322     }
323
324
325         private MQJMXConnectorInfo getMQJMXConnectorInfo(String JavaDoc tgtName)
326                                                     throws JMSAdminException {
327                 sLogger.log(Level.FINE, "getMQJMXConnectorInfo for " + tgtName);
328                 MQJMXConnectorInfo mcInfo = null;
329                                                                                                                                               
330                 try {
331                         MQJMXConnectorInfo [] cInfo =
332                                 ConnectorRuntime.getRuntime().getMQJMXConnectorInfo(tgtName);
333                         if ((cInfo == null) || (cInfo.length < 1)) {
334                                 throw new JMSAdminException(
335                         localStrings.getString("admin.mbeans.rmb.error_obtaining_jms"));
336                         }
337                         mcInfo = cInfo[0];
338                                                                                                                                               
339                 } catch (Exception JavaDoc e) {
340                     handleException(e);
341                 }
342                 return mcInfo;
343         }
344
345 /*
346     private MBeanServerConnection getMBeanServerConnection(String tgtName)
347         throws JMSAdminException {
348
349         sLogger.log(Level.FINE, "getMBeanServerConnection for " + tgtName);
350         MBeanServerConnection mbsc = null;
351
352         try {
353             MQJMXConnectorInfo [] cInfo =
354                 ConnectorRuntime.getRuntime().getMQJMXConnectorInfo(tgtName);
355             if ((cInfo == null) || (cInfo.length < 1)) {
356                 throw new JMSAdminException(
357                         localStrings.getString("admin.mbeans.rmb.error_obtaining_jms"));
358             }
359             mbsc = cInfo[0].getMQMBeanServerConnection();
360             sLogger.log(Level.FINE, "MBeanServerConnection = " + mbsc);
361
362         } catch (Exception e) {
363                     handleException(e);
364                 }
365
366         return mbsc;
367     }
368 */

369
370         private void setAppserverDefaults(AttributeList JavaDoc destAttrs,
371                                              MQJMXConnectorInfo info) {
372
373          if (destAttrs == null) {
374         destAttrs = new AttributeList JavaDoc();
375          }
376
377              if (info.getBrokerType().equalsIgnoreCase(ActiveJmsResourceAdapter.LOCAL)) {
378                  String JavaDoc localDelivery = "LocalDeliveryPreferred";
379                  boolean notPresent = true;
380                  for (Object JavaDoc obj : destAttrs) {
381                      Attribute JavaDoc attrib = (Attribute JavaDoc) obj;
382                      if (attrib.getName().equals(localDelivery)) {
383                          notPresent = false;
384                      }
385                  }
386                  if (notPresent) {
387                     Attribute JavaDoc attrib = new Attribute JavaDoc (localDelivery,
388                                        new Boolean JavaDoc("true"));
389                     destAttrs.add(attrib);
390                  }
391              }
392         }
393
394     //XXX: To refactor into a Generic attribute type mapper, so that it is extensible later.
395
private AttributeList JavaDoc convertProp2Attrs(Properties JavaDoc destProps) {
396
397         AttributeList JavaDoc destAttrs = new AttributeList JavaDoc();
398
399         String JavaDoc propName = null;
400         String JavaDoc propValue = null;
401
402         for (Enumeration JavaDoc e = destProps.propertyNames() ; e.hasMoreElements() ;) {
403             propName = (String JavaDoc) e.nextElement();
404
405             if (propName.equals("AutoCreateQueueMaxNumActiveConsumers")) {
406                 destAttrs.add(new Attribute JavaDoc("AutoCreateQueueMaxNumActiveConsumers",
407                 new Integer JavaDoc(destProps.getProperty("AutoCreateQueueMaxNumActiveConsumers"))));
408             } else if (propName.equals("maxNumActiveConsumers")) {
409                 destAttrs.add(new Attribute JavaDoc("MaxNumActiveConsumers",
410                 new Integer JavaDoc(destProps.getProperty("maxNumActiveConsumers"))));
411             } else if (propName.equals("MaxNumActiveConsumers")) {
412                 destAttrs.add(new Attribute JavaDoc("MaxNumActiveConsumers",
413                 new Integer JavaDoc(destProps.getProperty("MaxNumActiveConsumers"))));
414             } else if (propName.equals("AutoCreateQueueMaxNumBackupConsumers")) {
415                 destAttrs.add(new Attribute JavaDoc("AutoCreateQueueMaxNumBackupConsumers",
416                 new Integer JavaDoc(destProps.getProperty("AutoCreateQueueMaxNumBackupConsumers"))));
417             } else if (propName.equals("AutoCreateQueues")) {
418                 boolean b = false;
419                 propValue = destProps.getProperty("AutoCreateQueues");
420                 if (propValue.equalsIgnoreCase("true")) {
421                     b = true;
422                 }
423                 destAttrs.add(new Attribute JavaDoc("AutoCreateQueues", new Boolean JavaDoc(b)));
424             } else if (propName.equals("AutoCreateTopics")) {
425                 boolean b = false;
426                 propValue = destProps.getProperty("AutoCreateTopics");
427                 if (propValue.equalsIgnoreCase("true")) {
428                     b = true;
429                 }
430                 destAttrs.add(new Attribute JavaDoc("AutoCreateTopics", new Boolean JavaDoc(b)));
431             } else if (propName.equals("DMQTruncateBody")) {
432                 boolean b = false;
433                 propValue = destProps.getProperty("DMQTruncateBody");
434                 if (propValue.equalsIgnoreCase("true")) {
435                     b = true;
436                 }
437                 destAttrs.add(new Attribute JavaDoc("DMQTruncateBody", new Boolean JavaDoc(b)));
438             } else if (propName.equals("LogDeadMsgs")) {
439                 boolean b = false;
440                 propValue = destProps.getProperty("LogDeadMsgs");
441                 if (propValue.equalsIgnoreCase("true")) {
442                     b = true;
443                 }
444                 destAttrs.add(new Attribute JavaDoc("LogDeadMsgs", new Boolean JavaDoc(b)));
445             } else if (propName.equals("MaxBytesPerMsg")) {
446                 destAttrs.add(new Attribute JavaDoc("MaxBytesPerMsg",
447                 new Long JavaDoc(destProps.getProperty("MaxBytesPerMsg"))));
448             } else if (propName.equals("MaxNumMsgs")) {
449                 destAttrs.add(new Attribute JavaDoc("MaxNumMsgs",
450                 new Long JavaDoc(destProps.getProperty("MaxNumMsgs"))));
451             } else if (propName.equals("MaxTotalMsgBytes")) {
452                 destAttrs.add(new Attribute JavaDoc("MaxTotalMsgBytes",
453                 new Long JavaDoc(destProps.getProperty("MaxTotalMsgBytes"))));
454             } else if (propName.equals("NumDestinations")) {
455                 destAttrs.add(new Attribute JavaDoc("NumDestinations",
456                 new Integer JavaDoc(destProps.getProperty("NumDestinations"))));
457             }
458         }
459         return destAttrs;
460     }
461     
462     /**
463      * Logs an exception via the logger and throws a JMSAdminException.
464      *
465      * This method exists as exceptions that occur while invoke
466      * MQ JMX operations currently have "nested" exception messages
467      * and it is very difficult for a user to understand the real
468      * cause from the exception message. We now throw a generic message
469      * and log the actual exception in the server log so that users could
470      * refer to the server log for more information.
471      */

472     private void logAndHandleException(Exception JavaDoc e, String JavaDoc errorMsg)
473                                         throws JMSAdminException {
474         //log JMX Exception trace as WARNING
475
StringWriter JavaDoc s = new StringWriter JavaDoc();
476         e.getCause().printStackTrace(new PrintWriter JavaDoc(s));
477         sLogger.log(Level.WARNING, s.toString());
478         
479         JMSAdminException je = new JMSAdminException(localStrings.getString(errorMsg));
480         handleException(je);
481     }
482         
483     
484     private void handleException(Exception JavaDoc e)
485                                 throws JMSAdminException {
486
487         if (e instanceof JMSAdminException) {
488             throw ((JMSAdminException)e);
489         }
490
491         String JavaDoc msg = e.getMessage();
492
493         JMSAdminException jae;
494         if (msg == null) {
495             jae = new JMSAdminException();
496         } else {
497             jae = new JMSAdminException(msg);
498         }
499
500         /*
501          * Don't do this for now because the CLI does not include jms.jar
502          * (at least not yet) in the classpath. Sending over a JMSException
503          * will cause a class not found exception to be thrown.
504          */

505         //jae.setLinkedException(e);
506

507         throw jae;
508     }
509     
510     /**
511      * Determines whether MQ JMX connector needs to be used for MQ related
512      * administrative operations.
513      *
514      * @param target Target on which a commands needs to be executed
515      */

516     public static boolean useJMX(Target target) {
517         /*
518         if(JMSDestination.USE_JMX) {
519             if ((target.getType() == TargetType.DAS) || (target.getType() == TargetType.SERVER)) {
520                 return true;
521             }
522         }
523        return false;
524        */

525        return USE_JMX;
526     }
527
528 }
529
Popular Tags