KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > mejb > ManagementBean


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ManagementBean.java,v 1.14 2005/06/13 17:12:59 vivekl Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.mejb;
26
27 import java.io.IOException JavaDoc;
28 import java.rmi.RemoteException JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Set JavaDoc;
32
33 import javax.ejb.CreateException JavaDoc;
34 import javax.ejb.SessionBean JavaDoc;
35 import javax.ejb.SessionContext JavaDoc;
36 import javax.management.Attribute JavaDoc;
37 import javax.management.AttributeList JavaDoc;
38 import javax.management.AttributeNotFoundException JavaDoc;
39 import javax.management.InstanceAlreadyExistsException JavaDoc;
40 import javax.management.InstanceNotFoundException JavaDoc;
41 import javax.management.IntrospectionException JavaDoc;
42 import javax.management.InvalidAttributeValueException JavaDoc;
43 import javax.management.MBeanAttributeInfo JavaDoc;
44 import javax.management.MBeanException JavaDoc;
45 import javax.management.MBeanInfo JavaDoc;
46 import javax.management.MBeanOperationInfo JavaDoc;
47 import javax.management.MBeanServer JavaDoc;
48 import javax.management.MBeanServerConnection JavaDoc;
49 import javax.management.MalformedObjectNameException JavaDoc;
50 import javax.management.ObjectName JavaDoc;
51 import javax.management.QueryExp JavaDoc;
52 import javax.management.ReflectionException JavaDoc;
53 import javax.management.j2ee.ListenerRegistration JavaDoc;
54
55 import org.objectweb.jonas.common.Log;
56 import org.objectweb.jonas.jmx.J2eeObjectName;
57 import org.objectweb.jonas.jmx.JmxService;
58 import org.objectweb.jonas.management.j2eemanagement.ManagementListener;
59 import org.objectweb.jonas.management.j2eemanagement.ManagementListenerMBean;
60 import org.objectweb.jonas.service.ServiceManager;
61 import org.objectweb.util.monolog.api.BasicLevel;
62 import org.objectweb.util.monolog.api.Logger;
63
64 /**
65  * This is the Management EJB implementation for JOnAS.
66  * A MEJB instance is created and deployed at JOnAS start time.
67  * It is registered in the ejb/mgmt naming subcontext.
68  *
69  * The current implementation allows access to managed resources registered in
70  * the current (local) MBean server via the standard management methods defined in the
71  * javax.management.j2ee.Management interface.
72  *
73  * It also allows access to managed resources registered in remote MBean servers
74  * which belong to the current management domain, via management methods exposed as a
75  * WebService endpoint (defined in the ManagementEndpoint interface).
76  *
77  * @author Adriana Danes
78  * @author Vivek Lakshmanan
79  * @author Matt Wringe
80  */

81 public class ManagementBean implements SessionBean JavaDoc, ManagementEndpoint {
82
83     /**
84      * Logger
85      */

86     private static Logger logger = Log.getLogger(Log.JONAS_MEJB);
87     /**
88      * reference to the context object
89      */

90     private SessionContext JavaDoc sessionContext = null;
91     /**
92      * The current JOnAS server's jmx service.
93      * The jmx service is used to get connections to remote MBean servers
94      */

95     private JmxService jmxService = null;
96     /**
97      * Connection to the current JOnAS server's MBean server
98      */

99     private MBeanServerConnection JavaDoc jmxServerConnection = null;
100     /**
101      * The current server name
102      */

103     private String JavaDoc serverName = null;
104     /**
105      * The current domain name
106      */

107     private String JavaDoc domainName = null;
108     /**
109      * The remote NotificationListener proxy name
110      */

111     private String JavaDoc proxyName = "MEJB_listener";
112
113     /**
114      * ejbCreate method
115      *
116      * Get the MBeanServer reference to allow local access
117      */

118     public void ejbCreate() throws CreateException JavaDoc {
119         try {
120             jmxService = (JmxService) ServiceManager.getInstance().getJmxService();
121             serverName = jmxService.getJonasServerName();
122             domainName = jmxService.getDomainName();
123             jmxServerConnection = (MBeanServerConnection JavaDoc) jmxService.getJmxServer();
124         } catch (Exception JavaDoc e) {
125             throw new CreateException JavaDoc("Could not create Management bean: " + e.getMessage());
126         }
127     }
128
129     /*====================== javax.ejb.SessionBean implementation =================*/
130
131     public void ejbActivate() {
132         // Nothing to do when the MEJB is activated
133
}
134     public void ejbPassivate() {
135         // Nothing to do when the MEJB is passivated
136
}
137     public void ejbRemove() {
138         // Nothing to do when the MEJB is removed
139
}
140     /**
141      * Sets the associated session context.
142      * @param sessionContext - A SessionContext interface for the instance.
143      */

144     public void setSessionContext(SessionContext JavaDoc sessionContext) {
145         this.sessionContext = sessionContext;
146     }
147
148     /*========================= Management interface implementation ============================*/
149     /*=============== The management methods are invoked on the local MBeanServer ========*/
150
151
152     public Object JavaDoc getAttribute(ObjectName JavaDoc name, String JavaDoc attribute) throws
153     MBeanException JavaDoc,
154     AttributeNotFoundException JavaDoc,
155     InstanceNotFoundException JavaDoc,
156     ReflectionException JavaDoc,
157     RemoteException JavaDoc {
158
159         try {
160             return jmxServerConnection.getAttribute(name, attribute);
161         } catch (java.io.IOException JavaDoc ioe) {
162             throw new RemoteException JavaDoc("Object getAttribute(ObjectName, String) failed", ioe);
163         }
164     }
165
166     public AttributeList JavaDoc getAttributes(ObjectName JavaDoc name, String JavaDoc[] attributes) throws
167     InstanceNotFoundException JavaDoc,
168     ReflectionException JavaDoc,
169     RemoteException JavaDoc {
170
171         try {
172             return jmxServerConnection.getAttributes(name, attributes);
173         } catch (java.io.IOException JavaDoc ioe) {
174             throw new RemoteException JavaDoc("AttributeList getAttributes(ObjectName, String[]) failed", ioe);
175         }
176     }
177
178     public String JavaDoc getDefaultDomain() throws RemoteException JavaDoc {
179
180         try {
181             return jmxServerConnection.getDefaultDomain();
182         } catch (java.io.IOException JavaDoc ioe) {
183             throw new RemoteException JavaDoc("String getDefaultDomain() failed", ioe);
184         }
185     }
186
187     public Integer JavaDoc getMBeanCount() throws RemoteException JavaDoc {
188
189         try {
190             return jmxServerConnection.getMBeanCount();
191         } catch (java.io.IOException JavaDoc ioe) {
192             throw new RemoteException JavaDoc("Integer getMBeanCount() failed", ioe);
193         }
194     }
195
196     public MBeanInfo JavaDoc getMBeanInfo(ObjectName JavaDoc name) throws
197     IntrospectionException JavaDoc,
198     InstanceNotFoundException JavaDoc,
199     ReflectionException JavaDoc,
200     RemoteException JavaDoc {
201
202         try {
203             return jmxServerConnection.getMBeanInfo(name);
204         } catch (java.io.IOException JavaDoc ioe) {
205             throw new RemoteException JavaDoc("MBeanInfo getMBeanInfo(ObjectName) failed", ioe);
206         }
207     }
208
209     public Object JavaDoc invoke(ObjectName JavaDoc name, String JavaDoc operationName, Object JavaDoc[] params, String JavaDoc[] signature) throws
210     MBeanException JavaDoc,
211     InstanceNotFoundException JavaDoc,
212     ReflectionException JavaDoc,
213     RemoteException JavaDoc {
214
215         try {
216             return jmxServerConnection.invoke(name, operationName, params, signature);
217         } catch (java.io.IOException JavaDoc ioe) {
218             throw new RemoteException JavaDoc("Object invoke(ObjectName, String, Object[], String[]) failed", ioe);
219         }
220     }
221
222     public boolean isRegistered(ObjectName JavaDoc name) throws RemoteException JavaDoc {
223
224         try {
225             return jmxServerConnection.isRegistered(name);
226         } catch (java.io.IOException JavaDoc ioe) {
227             throw new RemoteException JavaDoc("boolean isRegistered(ObjectName) failed", ioe);
228         }
229     }
230
231     public Set JavaDoc queryNames(ObjectName JavaDoc name, QueryExp JavaDoc query) throws RemoteException JavaDoc {
232         try {
233             return jmxServerConnection.queryNames(name, query);
234         } catch (java.io.IOException JavaDoc ioe) {
235             throw new RemoteException JavaDoc("Set queryNames(ObjectName, QueryExp) failed", ioe);
236         }
237     }
238
239     public void setAttribute(ObjectName JavaDoc name, Attribute JavaDoc attribute) throws
240     MBeanException JavaDoc,
241     AttributeNotFoundException JavaDoc,
242     InstanceNotFoundException JavaDoc,
243     InvalidAttributeValueException JavaDoc,
244     ReflectionException JavaDoc,
245     RemoteException JavaDoc {
246
247         try {
248             jmxServerConnection.setAttribute(name, attribute);
249         } catch (java.io.IOException JavaDoc ioe) {
250             throw new RemoteException JavaDoc("void setAttribute(ObjectName, Attribute) failed", ioe);
251         }
252     }
253
254     public AttributeList JavaDoc setAttributes(ObjectName JavaDoc name, AttributeList JavaDoc attributes) throws
255     InstanceNotFoundException JavaDoc,
256     ReflectionException JavaDoc,
257     RemoteException JavaDoc {
258
259         try {
260             return jmxServerConnection.setAttributes(name, attributes);
261         } catch (java.io.IOException JavaDoc ioe) {
262             throw new RemoteException JavaDoc("AttributeList setAttributes(ObjectName, AttributeList) failed", ioe);
263         }
264     }
265
266     /**
267      * Returns the ListenerRegistration implementation object which allows the client to register
268      * a event notification listener.
269      * This method also creates a MBean
270      * @return An instance of the class implementing the ListenerRegistration interface.
271      * <code>null</code> is returned if
272      */

273     public ListenerRegistration JavaDoc getListenerRegistry() throws RemoteException JavaDoc {
274         // Create and register the ManagemenentListener MBean if this is not already done
275
ObjectName JavaDoc listenerOn = J2eeObjectName.ManagementListener(domainName, serverName);
276         boolean isRegisteredListener;
277         isRegisteredListener = jmxService.getJmxServer().isRegistered(listenerOn);
278         if (!isRegisteredListener) {
279             ManagementListenerMBean listenerMBean = new ManagementListener(proxyName);
280             try {
281                 jmxService.getJmxServer().registerMBean(listenerMBean, listenerOn);
282             } catch (InstanceAlreadyExistsException JavaDoc ae) {
283                 // Not possible as tested before
284
} catch (Exception JavaDoc e) {
285                 // Could not register MBean -> the management bean can't handle notifications
286
throw new RemoteException JavaDoc("Can not return ListenerRegistration implementation", e);
287             }
288         }
289         // Create ListenerRegistration object
290
return new ListenerRegistrationImpl(listenerOn, proxyName);
291     }
292
293     /*=============== The management methods are invoked on Remote MBeanServers ==========*/
294
295     /**
296      * Returns a connection for a server in the domain. Any error messages in getting
297      * the server connection is thrown as a remote exception.
298      * @param domainServerName The server name.
299      * @return A connection to the server.
300      */

301     private MBeanServerConnection JavaDoc getServerConnection (String JavaDoc domainServerName) {
302         //if the domainServerName is null, return the connection to the current server
303
if (domainServerName == null || domainServerName.equals(serverName)) {
304             return jmxServerConnection;
305         }
306         return jmxService.getServerConnection(domainServerName);
307     }
308
309     public Object JavaDoc getAttribute (String JavaDoc domainServerName, ObjectName JavaDoc name, String JavaDoc attribute) throws
310     AttributeNotFoundException JavaDoc,
311     InstanceNotFoundException JavaDoc,
312     MBeanException JavaDoc,
313     ReflectionException JavaDoc,
314     RemoteException JavaDoc {
315
316         MBeanServerConnection JavaDoc connection = getServerConnection(domainServerName);
317         if (connection == null) {
318             throw new RemoteException JavaDoc ("Could not connect to server " + domainServerName);
319         } try {
320             return connection.getAttribute(name, attribute);
321         } catch (IOException JavaDoc ioe) {
322             throw new RemoteException JavaDoc ("Object getAttribute(String, ObjectName, String) failed", ioe);
323         }
324     }
325
326
327     public AttributeList JavaDoc getAttributes(String JavaDoc domainServerName, ObjectName JavaDoc name, String JavaDoc[] attributes) throws
328     InstanceNotFoundException JavaDoc,
329     ReflectionException JavaDoc,
330     RemoteException JavaDoc {
331
332         MBeanServerConnection JavaDoc connection = getServerConnection(domainServerName);
333         if (connection == null) {
334             throw new RemoteException JavaDoc ("Could not connect to server " + domainServerName);
335         } try {
336             return connection.getAttributes(name, attributes);
337         } catch (IOException JavaDoc ioe) {
338             throw new RemoteException JavaDoc("AttributeList getAttributes(String, ObjectName, String[]) failed", ioe);
339         }
340     }
341
342
343
344     public Integer JavaDoc getMBeanCount(String JavaDoc domainServerName) throws RemoteException JavaDoc {
345
346         MBeanServerConnection JavaDoc connection = getServerConnection(domainServerName);
347         if (connection == null) {
348                 throw new RemoteException JavaDoc ("Could not connect to server " + domainServerName);
349         } try {
350             return connection.getMBeanCount();
351         } catch (IOException JavaDoc ioe) {
352             throw new RemoteException JavaDoc("Integer getMBeanCount(String) failed", ioe);
353         }
354     }
355
356
357     public MBeanInfo JavaDoc getMBeanInfo(String JavaDoc domainServerName, ObjectName JavaDoc name) throws
358     IntrospectionException JavaDoc,
359     InstanceNotFoundException JavaDoc,
360     ReflectionException JavaDoc,
361     RemoteException JavaDoc {
362
363         MBeanServerConnection JavaDoc connection = getServerConnection (domainServerName);
364         if (connection == null) {
365             throw new RemoteException JavaDoc ("Could not connect to server " + domainServerName);
366         } try {
367             return connection.getMBeanInfo(name);
368         } catch (IOException JavaDoc ioe) {
369             throw new RemoteException JavaDoc("MBeanInfo getMbeanInfo(String, ObjectName) failed", ioe);
370         }
371     }
372
373     public Object JavaDoc invoke(String JavaDoc domainServerName, ObjectName JavaDoc name, String JavaDoc operationName, Object JavaDoc[] params, String JavaDoc[] signature) throws
374     MBeanException JavaDoc,
375     InstanceNotFoundException JavaDoc,
376     ReflectionException JavaDoc,
377     RemoteException JavaDoc {
378
379         MBeanServerConnection JavaDoc connection = getServerConnection (domainServerName);
380         if (connection == null) {
381             throw new RemoteException JavaDoc ("Could not connect to server " + domainServerName);
382         } try {
383             return connection.invoke (name, operationName, params, signature);
384         } catch (IOException JavaDoc ioe) {
385             throw new RemoteException JavaDoc("Object invoke(String, ObjectName, String, Object[], String[]) failed", ioe);
386         }
387     }
388
389     public boolean isRegistered(String JavaDoc domainServerName, ObjectName JavaDoc name) throws RemoteException JavaDoc {
390
391         MBeanServerConnection JavaDoc connection = getServerConnection(domainServerName);
392         if (connection == null) {
393             throw new RemoteException JavaDoc ("Could not connect to server " + domainServerName);
394         } try {
395             return connection.isRegistered(name);
396         } catch (java.io.IOException JavaDoc ioe) {
397             throw new RemoteException JavaDoc ("boolean isRegistered(String, ObjectName) failed", ioe);
398         }
399     }
400
401     public Set JavaDoc queryNames(String JavaDoc domainServerName, ObjectName JavaDoc name, QueryExp JavaDoc query) throws RemoteException JavaDoc {
402
403         MBeanServerConnection JavaDoc connection = getServerConnection(domainServerName);
404         if (connection == null) {
405             throw new RemoteException JavaDoc ("Could not connect to server " + domainServerName);
406         } try {
407             return connection.queryNames (name, query);
408         } catch (java.io.IOException JavaDoc ioe) {
409             throw new RemoteException JavaDoc ("Set queryNames(String, ObjectName, QueryExp) failed", ioe);
410         }
411     }
412
413     public void setAttribute(String JavaDoc domainServerName, ObjectName JavaDoc name, Attribute JavaDoc attribute) throws
414     MBeanException JavaDoc,
415     AttributeNotFoundException JavaDoc,
416     InstanceNotFoundException JavaDoc,
417     InvalidAttributeValueException JavaDoc,
418     ReflectionException JavaDoc,
419     RemoteException JavaDoc {
420
421         MBeanServerConnection JavaDoc connection = getServerConnection(domainServerName);
422         if (connection == null) {
423             throw new RemoteException JavaDoc ("Could not connect to server " + domainServerName);
424         } try {
425             connection.setAttribute (name, attribute);
426         } catch (java.io.IOException JavaDoc ioe) {
427             throw new RemoteException JavaDoc ("void setAttribute(String, ObjectName, Attribute) failed", ioe);
428         }
429     }
430
431     public AttributeList JavaDoc setAttributes(String JavaDoc domainServerName, ObjectName JavaDoc name, AttributeList JavaDoc attributes) throws
432     InstanceNotFoundException JavaDoc,
433     ReflectionException JavaDoc,
434     RemoteException JavaDoc {
435
436         MBeanServerConnection JavaDoc connection = getServerConnection(domainServerName);
437         if (connection == null) {
438             throw new RemoteException JavaDoc("Could not connect to server " + domainServerName);
439         } try {
440             return connection.setAttributes(name, attributes);
441         } catch (java.io.IOException JavaDoc ioe) {
442             throw new RemoteException JavaDoc("AttributeList setAttributes(String, ObjectName, AttributeList) failed", ioe);
443         }
444     }
445
446
447     /*================== ManagementBean Monitoring Endpoint implementation ===============*/
448     /*=============== The management methods are invoked on Remote MBeanServers ==========*/
449
450     /*
451      * Implementation for the ManagementEndpoint interface begins here
452      *
453      * -the ManagementEndpoint allows monitoring of the server remotely using its web
454      * service.
455      * -many of these methods look the same as the ones above but their
456      * data types have been changed to make them easier for a web service to use
457      * them.
458      * -The only method used in the ManagementEndpoint interface that is
459      * not listed below is getMBeanCount which was already web service friendly
460      * -Special expections are thrown when errors occur. These persist to the
461      * web service client. Exception Class:
462      * org.objectweb.jonas.mejb.ManagementEndpointException.
463      */

464
465     /**
466      * Returns the names of the servers in the domain.
467      * @return The names of the servers in the domain.
468      * @throws ManagementEndpointException If any errors occur.
469      * @throws RemoteException If a connection error occurs.
470      */

471     public String JavaDoc[] getServers() throws ManagementEndpointException, RemoteException JavaDoc {
472         String JavaDoc j2eeDomainName = domainName + ":j2eeType=J2EEDomain,name=" + this.domainName;
473         return getAttribute(serverName, j2eeDomainName, "serverNames");
474     }
475
476
477     /**
478      * @see ManagementEndpoint#getAttribute(String, String, String)
479      */

480     public String JavaDoc[] getAttribute(String JavaDoc domainServerName, String JavaDoc objectName, String JavaDoc attribute)
481             throws ManagementEndpointException {
482         try {
483             return getObjectValue(getAttribute(domainServerName, new ObjectName JavaDoc(objectName), attribute));
484         } catch (Exception JavaDoc e) {
485             ManagementEndpointException mex = new ManagementEndpointException();
486             mex.setExceptionType(e.getClass().toString());
487             mex.setMessage("Problem in getAttribute service call for objectname: " + objectName
488                     + " - the request was not completed: " + e.getMessage());
489             logger.log(BasicLevel.ERROR, mex.getMessage(), e);
490             throw (ManagementEndpointException) mex.initCause(e);
491         }
492     }
493
494     /**
495      * @see ManagementEndpoint#isRegistered(String, String)
496      */

497     public boolean isRegistered(String JavaDoc domainServerName, String JavaDoc objectName) throws ManagementEndpointException {
498         try {
499             return isRegistered(domainServerName, new ObjectName JavaDoc(objectName));
500         } catch (Exception JavaDoc e) {
501             ManagementEndpointException mex = new ManagementEndpointException();
502             mex.setExceptionType(e.getClass().toString());
503             mex.setMessage("Problem in isRegistered service call for objectname: " + objectName
504                     + " - the request was not completed: " + e.getMessage());
505             logger.log(BasicLevel.ERROR, mex.getMessage(), e);
506             throw (ManagementEndpointException) mex.initCause(e);
507         }
508     }
509
510     /**
511      * @see ManagementEndpoint#queryNames(String, String, String)
512      */

513     public String JavaDoc[] queryNames(String JavaDoc domainServerName, String JavaDoc objectName, String JavaDoc query) throws ManagementEndpointException {
514         try {
515
516             // TODO Determine how to use QueryExp. ATM query support disabled.
517
return getObjectValue(queryNames(domainServerName, new ObjectName JavaDoc(objectName), null));
518
519         } catch (Exception JavaDoc e) {
520             ManagementEndpointException mex = new ManagementEndpointException();
521             mex.setExceptionType(e.getClass().toString());
522             mex.setMessage("Problem in queryNames service call for objectname: " + objectName
523                     + " - the request was not completed: " + e.getMessage());
524             logger.log(BasicLevel.ERROR, mex.getMessage(), e);
525             throw (ManagementEndpointException) mex.initCause(e);
526         }
527     }
528
529     /**
530      * @see ManagementEndpoint#getAttributesList(String, String)
531      */

532     public String JavaDoc[] getAttributesList(String JavaDoc domainServerName, String JavaDoc objectName) throws ManagementEndpointException {
533         try {
534             MBeanInfo JavaDoc info = getMBeanInfo(domainServerName, new ObjectName JavaDoc(objectName));
535             MBeanAttributeInfo JavaDoc[] attrInfo = info.getAttributes();
536             String JavaDoc[] attrs = new String JavaDoc[attrInfo.length];
537             for (int i = 0; i < attrs.length; i++) {
538                 attrs[i] = attrInfo[i].getName();
539             }
540             return attrs;
541
542         } catch (Exception JavaDoc e) {
543             ManagementEndpointException mex = new ManagementEndpointException();
544             mex.setExceptionType(e.getClass().toString());
545             mex.setMessage("Problem in getAttributeList service call for objectname: " + objectName
546                 + " - the request was not completed: " + e.getMessage());
547             logger.log(BasicLevel.ERROR, mex.getMessage(), e);
548             throw (ManagementEndpointException) mex.initCause(e);
549         }
550     }
551
552     /**
553      * @see ManagementEndpoint#getDescription(String, String)
554      */

555     public String JavaDoc getDescription(String JavaDoc domainServerName, String JavaDoc objectName)
556         throws ManagementEndpointException {
557         try {
558             MBeanInfo JavaDoc info = getMBeanInfo(domainServerName, new ObjectName JavaDoc(objectName));
559             return info.getDescription();
560         } catch (Exception JavaDoc e) {
561             ManagementEndpointException mex = new ManagementEndpointException();
562             mex.setExceptionType(e.getClass().toString());
563             mex.setMessage("Problem in getDescription service call for objectname: " + objectName
564                 + " - the request was not completed: " + e.getMessage());
565             logger.log(BasicLevel.ERROR, mex.getMessage(), e);
566             throw (ManagementEndpointException) mex.initCause(e);
567         }
568
569     }
570
571     /**
572      * @see ManagementEndpoint#getOperations(String, String)
573      */

574     public String JavaDoc[] getOperations(String JavaDoc domainServerName, String JavaDoc objectName)
575         throws ManagementEndpointException {
576         try {
577             MBeanInfo JavaDoc info = getMBeanInfo(domainServerName, new ObjectName JavaDoc(objectName));
578             MBeanOperationInfo JavaDoc[] operationInfo = info.getOperations();
579             String JavaDoc[] operations = new String JavaDoc[operationInfo.length];
580             for (int i = 0; i < operationInfo.length; i++) {
581                 operations[i] = operationInfo[i].getName();
582             }
583             return operations;
584
585         } catch (Exception JavaDoc e) {
586             ManagementEndpointException mex = new ManagementEndpointException();
587             mex.setExceptionType(e.getClass().toString());
588             mex.setMessage("Problem in getOperations service call for objectname: " + objectName
589                 + " - the request was not completed: " + e.getMessage());
590             logger.log(BasicLevel.ERROR, mex.getMessage(), e);
591             throw (ManagementEndpointException) mex.initCause(e);
592         }
593     }
594
595     /*
596      * Listed below are helper methods for the ManagementEndpoint
597      *
598      * These methods are used to convert complex data types into more simpler
599      * types so that they work easier with web services. Most complex data types
600      * are converted into a string array
601      */

602
603     /**
604      * Takes an arbitrary object and returns a string array representation
605      * of the object. This methods is used to make passing values from a
606      * web service endpoint to a client easier to handle.
607      * @param objectValue An object.
608      * @return A string array representation of the object.
609      */

610     private String JavaDoc[] getObjectValue(Object JavaDoc objectValue) {
611         String JavaDoc[] value = null;
612
613         if (objectValue == null) {
614             value = new String JavaDoc[1];
615             value[0] = "null";
616         } else {
617             // Detect Array or Collection
618
if (objectValue.getClass().isArray()) {
619                 // Array
620
value = arrayToString((Object JavaDoc[]) objectValue);
621             } else {
622                 try {
623                     // Collection
624
value = collectionToString((Collection JavaDoc) objectValue);
625                 } catch (Exception JavaDoc e) {
626                     // Default
627
value = new String JavaDoc[1];
628                     value[0] = objectValue.toString();
629                 }
630             }
631         }
632         return value;
633     }
634
635     /**
636      * Takes an array of objects and returns the string array representation.
637      * @param pArray An array of objects.
638      * @return A string array representation of the object array.
639      */

640     private String JavaDoc[] arrayToString(Object JavaDoc[] pArray) {
641         String JavaDoc[] retStringArr = new String JavaDoc[pArray.length];
642         for (int i = 0; i < pArray.length; i++) {
643             if (pArray[i] == null) {
644                 retStringArr[i] = "null";
645         } else {
646                 retStringArr[i] = pArray[i].toString();
647             }
648
649         }
650         return retStringArr;
651     }
652
653     /**
654      * Takes a collection and returns the string array representation of the object.
655      * @param pCollection A collection of objects.
656      * @return A string array representation of the object.
657      */

658     private String JavaDoc[] collectionToString(Collection JavaDoc pCollection) {
659         String JavaDoc[] retStringArr = new String JavaDoc[pCollection.size()];
660         Iterator JavaDoc it = pCollection.iterator();
661         int i = 0;
662         while (it.hasNext()) {
663             retStringArr[i++] = it.next().toString();
664         }
665         return retStringArr;
666     }
667
668 }
Popular Tags