KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > phoenix > components > manager > AbstractJMXManager


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.phoenix.components.manager;
9
10 import java.lang.reflect.Constructor JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Set JavaDoc;
13 import javax.management.MBeanServer JavaDoc;
14 import javax.management.MalformedObjectNameException JavaDoc;
15 import javax.management.ObjectName JavaDoc;
16 import javax.management.modelmbean.ModelMBean JavaDoc;
17 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
18 import org.apache.avalon.excalibur.i18n.ResourceManager;
19 import org.apache.avalon.excalibur.i18n.Resources;
20 import org.apache.avalon.phoenix.interfaces.ManagerException;
21
22 /**
23  * An abstract class via which JMX Managers can extend.
24  *
25  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
26  * @author <a HREF="mailto:Huw@mmlive.com">Huw Roberts</a>
27  * @version $Revision: 1.5 $ $Date: 2002/08/06 11:57:40 $
28  */

29 public abstract class AbstractJMXManager
30     extends AbstractSystemManager
31 {
32     private static final Resources REZ =
33         ResourceManager.getPackageResources( AbstractJMXManager.class );
34     private MBeanInfoBuilder topicBuilder;
35     private MBeanServer JavaDoc m_mBeanServer;
36     private String JavaDoc m_domain = "Phoenix";
37
38     public void initialize()
39         throws Exception JavaDoc
40     {
41         super.initialize();
42
43         final MBeanServer JavaDoc mBeanServer = createMBeanServer();
44         setMBeanServer( mBeanServer );
45
46         topicBuilder = new MBeanInfoBuilder();
47         setupLogger( topicBuilder );
48     }
49
50     public void start()
51         throws Exception JavaDoc
52     {
53     }
54
55     public void stop()
56         throws Exception JavaDoc
57     {
58     }
59
60     public void dispose()
61     {
62         setMBeanServer( null );
63     }
64
65     /**
66      * Export the object to the particular management medium using
67      * the supplied object and interfaces.
68      * This needs to be implemented by subclasses.
69      *
70      * @param name the name of object
71      * @param object the object
72      * @param interfaces the interfaces
73      * @return the exported object
74      * @throws ManagerException if an error occurs
75      */

76     protected Object JavaDoc export( final String JavaDoc name,
77                              final Object JavaDoc object,
78                              final Class JavaDoc[] interfaces )
79         throws ManagerException
80     {
81         try
82         {
83             final Target target = createTarget( name, object, interfaces );
84             exportTarget( target );
85             return target;
86         }
87         catch( final Exception JavaDoc e )
88         {
89             final String JavaDoc message = REZ.getString( "jmxmanager.error.export.fail", name );
90             getLogger().error( message, e );
91             throw new ManagerException( message, e );
92         }
93     }
94
95     /**
96      * Stop the exported object from being managed.
97      *
98      * @param name the name of object
99      * @param exportedObject the object return by export
100      * @throws ManagerException if an error occurs
101      */

102     protected void unexport( final String JavaDoc name,
103                              final Object JavaDoc exportedObject )
104         throws ManagerException
105     {
106         try
107         {
108             final Target target = (Target) exportedObject;
109             final Set JavaDoc topicNames = target.getTopicNames();
110             final Iterator JavaDoc i = topicNames.iterator();
111
112             while( i.hasNext() )
113             {
114                 final ObjectName JavaDoc objectName =
115                     createObjectName( name, target.getTopic( ( String JavaDoc ) i.next() ) );
116
117                 getMBeanServer().unregisterMBean( objectName );
118             }
119         }
120         catch( final Exception JavaDoc e )
121         {
122             final String JavaDoc message =
123                 REZ.getString( "jmxmanager.error.unexport.fail", name );
124             getLogger().error( message, e );
125             throw new ManagerException( message, e );
126         }
127     }
128
129     /**
130      * Verify that an interface conforms to the requirements of management medium.
131      *
132      * @param clazz the interface class
133      * @throws ManagerException if verification fails
134      */

135     protected void verifyInterface( final Class JavaDoc clazz )
136         throws ManagerException
137     {
138         //TODO: check it extends all right things and that it
139
//has all the right return types etc. Blocks must have
140
//interfaces extending Service (or Manageable)
141
}
142
143     protected MBeanServer JavaDoc getMBeanServer()
144     {
145         return m_mBeanServer;
146     }
147
148     protected void setMBeanServer( MBeanServer JavaDoc mBeanServer )
149     {
150         m_mBeanServer = mBeanServer;
151     }
152
153     protected String JavaDoc getDomain()
154     {
155         return m_domain;
156     }
157
158     protected void setDomain( final String JavaDoc domain )
159     {
160         m_domain = domain;
161     }
162
163     /**
164      * Creates a new MBeanServer.
165      * The subclass should implement this to create specific MBeanServer.
166      */

167     protected abstract MBeanServer JavaDoc createMBeanServer()
168         throws Exception JavaDoc;
169
170     /**
171      * Creates a target that can then be exported for management. A topic is created
172      * for each interface and for topics specified in the mxinfo file, if present
173      *
174      * @param name name of the target
175      * @param object managed object
176      * @param interfaces interfaces to be exported
177      * @return the management target
178      */

179     protected Target createTarget( final String JavaDoc name,
180                                    final Object JavaDoc object,
181                                    Class JavaDoc[] interfaces )
182     {
183         final Target target = new Target( name, object );
184         try
185         {
186             topicBuilder.build( target, object.getClass(), interfaces );
187         }
188         catch( final Exception JavaDoc e )
189         {
190             getLogger().debug( e.getMessage(), e );
191         }
192
193         return target;
194     }
195
196     /**
197      * Exports the target to the management repository. This is done by exporting
198      * each topic in the target.
199      *
200      * @param target the management target
201      */

202     protected void exportTarget( final Target target )
203         throws Exception JavaDoc
204     {
205         // loop through each topic and export it
206
final Set JavaDoc topicNames = target.getTopicNames();
207         final Iterator JavaDoc i = topicNames.iterator();
208         while( i.hasNext() )
209         {
210             final String JavaDoc topicName = (String JavaDoc)i.next();
211             final ModelMBeanInfo JavaDoc topic = target.getTopic( topicName );
212             final String JavaDoc targetName = target.getName();
213             final Object JavaDoc managedResource = target.getManagedResource();
214             Object JavaDoc targetObject = managedResource;
215             if( topic.getMBeanDescriptor().getFieldValue( "proxyClassName" ) != null )
216             {
217                 targetObject = createManagementProxy( topic, managedResource );
218             }
219
220             // use a proxy adapter class to manage object
221
exportTopic( topic,
222                          targetObject,
223                          targetName );
224         }
225     }
226
227     /**
228      * Exports the topic to the management repository. The name of the topic in the
229      * management repository will be the target name + the topic name
230      *
231      * @param topic the descriptor for the topic
232      * @param target to be managed
233      * @param targetName the target's name
234      */

235     protected Object JavaDoc exportTopic( final ModelMBeanInfo JavaDoc topic,
236                                   final Object JavaDoc target,
237                                   final String JavaDoc targetName )
238         throws Exception JavaDoc
239     {
240         final Object JavaDoc mBean = createMBean( topic, target );
241         final ObjectName JavaDoc objectName = createObjectName( targetName, topic );
242         getMBeanServer().registerMBean( mBean, objectName );
243
244         // debugging stuff.
245
/*
246         ModelMBean modelMBean = (ModelMBean)mBean;
247         ModelMBeanInfo modelMBeanInfo = (ModelMBeanInfo)modelMBean.getMBeanInfo();
248         MBeanAttributeInfo[] attList = modelMBeanInfo.getAttributes();
249         for( int i = 0; i < attList.length; i++ )
250         {
251             ModelMBeanAttributeInfo mbai = (ModelMBeanAttributeInfo)attList[ i ];
252             Descriptor d = mbai.getDescriptor();
253             String[] fieldNames = d.getFieldNames();
254             for( int j = 0; j < fieldNames.length; j++ )
255             {
256                 String fieldName = fieldNames[ j ];
257                 System.out.println( "Field name = " + fieldName +
258                                     " / value = " + d.getFieldValue( fieldName ) +
259                                     "::" +mbai.getType() + " value " +
260                 modelMBean.getAttribute( mbai.getName() ) + " for " + mbai.getName() );
261             }
262         }
263         */

264
265         return mBean;
266     }
267
268     /**
269      * Create a MBean for specified object.
270      * The following policy is used top create the MBean...
271      *
272      * @param target the object to create MBean for
273      * @return the MBean to be exported
274      * @throws ManagerException if an error occurs
275      */

276     private Object JavaDoc createMBean( final ModelMBeanInfo JavaDoc topic,
277                                 final Object JavaDoc target )
278         throws ManagerException
279     {
280         final String JavaDoc className = topic.getClassName();
281         // Load the ModelMBean implementation class
282
Class JavaDoc clazz;
283         try
284         {
285             clazz = Class.forName( className );
286         }
287         catch( Exception JavaDoc e )
288         {
289             final String JavaDoc message =
290                 REZ.getString( "jmxmanager.error.mbean.load.class",
291                                className );
292             getLogger().error( message, e );
293             throw new ManagerException( message, e );
294         }
295
296         // Create a new ModelMBean instance
297
ModelMBean JavaDoc mbean = null;
298         try
299         {
300             mbean = (ModelMBean JavaDoc)clazz.newInstance();
301             mbean.setModelMBeanInfo( topic );
302         }
303         catch( final Exception JavaDoc e )
304         {
305             final String JavaDoc message =
306                 REZ.getString( "jmxmanager.error.mbean.instantiate",
307                                className );
308             getLogger().error( message, e );
309             throw new ManagerException( message, e );
310         }
311
312         // Set the managed resource (if any)
313
try
314         {
315             if( null != target )
316             {
317                 mbean.setManagedResource( target, "ObjectReference" );
318             }
319         }
320         catch( Exception JavaDoc e )
321         {
322             final String JavaDoc message =
323                 REZ.getString( "jmxmanager.error.mbean.set.resource",
324                                className );
325             getLogger().error( message, e );
326             throw new ManagerException( message, e );
327         }
328
329         return mbean;
330     }
331
332     /**
333      * Create JMX name for object.
334      *
335      * @param name the name of object
336      * @return the {@link ObjectName} representing object
337      * @throws MalformedObjectNameException if malformed name
338      */

339     private ObjectName JavaDoc createObjectName( final String JavaDoc name, final ModelMBeanInfo JavaDoc topic )
340         throws MalformedObjectNameException JavaDoc
341     {
342         return new ObjectName JavaDoc( getDomain() + ":" + name + ",topic=" + topic.getDescription() );
343     }
344
345     /**
346      * Instantiates a proxy management object for the target object
347      *
348      * this should move out of bridge and into Registry, it isn't specifically for jmx
349      */

350     private Object JavaDoc createManagementProxy( final ModelMBeanInfo JavaDoc topic,
351                                           final Object JavaDoc managedObject )
352         throws Exception JavaDoc
353     {
354         final String JavaDoc proxyClassname = (String JavaDoc)topic.getMBeanDescriptor().getFieldValue( "proxyClassName" );
355         final ClassLoader JavaDoc classLoader = managedObject.getClass().getClassLoader();
356         final Class JavaDoc proxyClass = classLoader.loadClass( proxyClassname );
357         final Class JavaDoc[] argTypes = new Class JavaDoc[]{Object JavaDoc.class};
358         final Object JavaDoc[] argValues = new Object JavaDoc[]{managedObject};
359         final Constructor JavaDoc constructor = proxyClass.getConstructor( argTypes );
360         return constructor.newInstance( argValues );
361     }
362 }
363
Popular Tags