KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > offline > AMXLoader


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.management.offline;
25
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.Arrays JavaDoc;
33
34 import java.lang.reflect.Constructor JavaDoc;
35 import java.lang.reflect.Proxy JavaDoc;
36
37 import javax.management.ObjectName JavaDoc;
38 import javax.management.MBeanServer JavaDoc;
39 import javax.management.MBeanInfo JavaDoc;
40 import javax.management.MBeanAttributeInfo JavaDoc;
41 import javax.management.Notification JavaDoc;
42 import javax.management.MBeanServerNotification JavaDoc;
43 import javax.management.AttributeNotFoundException JavaDoc;
44 import javax.management.InstanceNotFoundException JavaDoc;
45 import javax.management.MBeanRegistrationException JavaDoc;
46 import javax.management.InstanceAlreadyExistsException JavaDoc;
47 import javax.management.NotCompliantMBeanException JavaDoc;
48 import javax.management.MBeanRegistration JavaDoc;
49 import javax.management.NotificationListener JavaDoc;
50 import javax.management.relation.MBeanServerNotificationFilter JavaDoc;
51
52
53 import com.sun.appserv.management.DomainRoot;
54 import com.sun.appserv.management.base.AMX;
55 import com.sun.appserv.management.base.Util;
56 import com.sun.appserv.management.base.AMXDebug;
57 import com.sun.appserv.management.base.XTypes;
58 import com.sun.appserv.management.base.Util;
59
60 import com.sun.appserv.management.util.misc.ListUtil;
61 import com.sun.appserv.management.util.misc.CollectionUtil;
62 import com.sun.appserv.management.util.misc.ExceptionUtil;
63 import com.sun.appserv.management.util.misc.StringUtil;
64 import com.sun.appserv.management.util.misc.CollectionUtil;
65 import com.sun.appserv.management.util.jmx.JMXUtil;
66
67 import com.sun.appserv.management.client.ProxyFactory;
68
69 import com.sun.appserv.management.config.ConfigElement;
70
71 import com.sun.enterprise.management.config.AMXConfigImplBase;
72 import com.sun.enterprise.management.support.OldConfigTypes;
73 import com.sun.enterprise.management.support.ObjectNames;
74 import com.sun.enterprise.management.support.BootUtil;
75 import com.sun.enterprise.management.support.Delegate;
76 import com.sun.enterprise.management.support.TypeInfo;
77 import com.sun.enterprise.management.support.TypeInfos;
78 import com.sun.enterprise.management.support.SystemInfoImpl;
79 import com.sun.enterprise.management.support.AMXDebugSupport;
80
81 import com.sun.appserv.management.util.jmx.MBeanProxyHandler;
82 import com.sun.appserv.management.util.jmx.MBeanServerConnectionSource;
83 import com.sun.appserv.management.util.jmx.stringifier.MBeanInfoStringifier;
84 import com.sun.appserv.management.util.jmx.stringifier.NotificationStringifier;
85
86
87 import com.sun.enterprise.config.ConfigBean;
88 import com.sun.enterprise.config.ConfigContext;
89 import com.sun.enterprise.config.ConfigException;
90             
91 import com.sun.enterprise.config.serverbeans.ServerXPathHelper;
92
93 /**
94     Loads AMX MBeans based on ConfigBeans.
95  */

96 final class AMXLoader implements NotificationListener JavaDoc
97 {
98     private final MBeanServer JavaDoc mServer;
99     private final String JavaDoc mJMXDomain;
100     
101     private final ConfigDelegateFactory mDelegateFactory;
102         
103     private ObjectNames mObjectNames;
104     
105     private final Map JavaDoc<ObjectName JavaDoc,ConfigBean> mObjectNameToConfigBean;
106     
107     
108         public
109     AMXLoader(
110         final MBeanServer JavaDoc server,
111         final ConfigDelegateFactory delegateFactory )
112         throws Exception JavaDoc
113     {
114         mServer = server;
115         
116         new AMXDebugSupport( mServer );
117         
118         mDelegateFactory = delegateFactory;
119         mJMXDomain = BootUtil.getInstance().getAMXJMXDomainName();
120         
121         mObjectNames = ObjectNames.getInstance( getAMXJMXDomainName() );
122     
123         mObjectNameToConfigBean = Collections.synchronizedMap( new HashMap JavaDoc<ObjectName JavaDoc,ConfigBean>() );
124         
125         loadSystemInfo( server );
126             
127         final MBeanServerNotificationFilter JavaDoc filter =
128             new MBeanServerNotificationFilter JavaDoc();
129         filter.enableAllObjectNames();
130         JMXUtil.listenToMBeanServerDelegate( mServer, this, filter, null );
131     }
132     
133         protected void
134     sdebug( Object JavaDoc o )
135     {
136         debug( o );
137         System.out.println( "" + o );
138     }
139     
140         public String JavaDoc
141     getAMXJMXDomainName()
142     {
143         return( BootUtil.getInstance().getAMXJMXDomainName() );
144     }
145     
146         private ConfigContext
147     getConfigContext()
148     {
149         return mDelegateFactory.getConfigContext();
150     }
151     
152         private void
153     debug( final Object JavaDoc o )
154     {
155         AMXDebug.getInstance().getOutput( "AMXLoader" ).println( o );
156     }
157     
158         public void
159     handleNotification(
160         final Notification JavaDoc notifIn,
161         final Object JavaDoc handback)
162     {
163         final String JavaDoc type = notifIn.getType();
164         
165         if ( notifIn instanceof MBeanServerNotification JavaDoc )
166         {
167             final ObjectName JavaDoc objectName = ((MBeanServerNotification JavaDoc)notifIn).getMBeanName();
168             
169             if ( type.equals( MBeanServerNotification.REGISTRATION_NOTIFICATION ) )
170             {
171                 handleMBeanRegistered( objectName );
172             }
173             else if ( type.equals( MBeanServerNotification.UNREGISTRATION_NOTIFICATION ) )
174             {
175                 handleMBeanUnregistered( objectName );
176             }
177         }
178     }
179     
180     
181         public void
182     handleMBeanRegistered( final ObjectName JavaDoc objectName )
183     {
184     }
185     
186         public void
187     handleMBeanUnregistered( final ObjectName JavaDoc objectName )
188     {
189         mObjectNameToConfigBean.remove( objectName );
190     }
191      
192     
193         protected Class JavaDoc
194     getImplClass( final String JavaDoc j2eeType)
195     {
196         final TypeInfo info = TypeInfos.getInstance().getInfo( j2eeType );
197         assert( info != null );
198         
199         return( info.getImplClass() );
200     }
201     
202     private static final Class JavaDoc[] DELEGATE_CONSTRUCTOR_SIG = new Class JavaDoc[]
203     {
204         Delegate.class,
205     };
206     
207         protected Object JavaDoc
208     newImpl(
209         final String JavaDoc j2eeType,
210         final Delegate delegate )
211         throws Exception JavaDoc
212     {
213         Object JavaDoc impl = null;
214         
215         final Class JavaDoc implClass = getImplClass( j2eeType );
216         
217         try
218         {
219             Constructor JavaDoc constructor = implClass.getConstructor( DELEGATE_CONSTRUCTOR_SIG );
220             
221             if ( constructor != null )
222             {
223                 impl = constructor.newInstance( new Object JavaDoc[] { delegate } );
224             }
225         }
226         catch( Exception JavaDoc e )
227         {
228             final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( e );
229             debug( "newImpl: exception creating new impl: " + e + "\n" +
230                 ExceptionUtil.getStackTrace( rootCause ) );
231             throw e;
232         }
233         
234         return( impl );
235     }
236     
237
238         private String JavaDoc
239     oldTypeToJ2EEType( final String JavaDoc type )
240     {
241         return OldConfigTypes.getInstance().oldTypeToJ2EEType( type );
242     }
243     
244         public ObjectName JavaDoc
245     loadAMX( final ConfigBean configBean )
246         throws Exception JavaDoc
247     {
248         final ConfigBeanHelperFactory factory =
249             ConfigBeanHelperFactory.getInstance( mDelegateFactory.getConfigContext() );
250         
251         final ConfigBeanHelper helper = factory.getHelper( configBean );
252             
253         final String JavaDoc xPath = helper.getXPath();
254         final List JavaDoc<String JavaDoc[]> propsList =
255             helper.getAllObjectNameProps( OldConfigTypes.getIgnoreTypes() );
256         
257         final String JavaDoc[] firstPair = propsList.iterator().next();
258         final String JavaDoc type = firstPair[ 0 ];
259         final String JavaDoc foundName = firstPair[ 1 ];
260         if ( type == null ||
261             OldConfigTypes.getIgnoreTypes().contains( type ) )
262         {
263             return null;
264         }
265         
266         final String JavaDoc j2eeType = oldTypeToJ2EEType( type );
267         final String JavaDoc name = foundName == null ?
268                         ObjectNames.getSingletonName( j2eeType ) : foundName;
269         String JavaDoc props = Util.makeRequiredProps( j2eeType, name);
270         //debug( type + "=" + name + " => " + props );
271

272         for( final String JavaDoc[] pair : propsList )
273         {
274             final String JavaDoc ancestorType = pair[ 0 ];
275             final String JavaDoc ancestorNameFound = pair[ 1 ];
276             if ( ! OldConfigTypes.getIgnoreTypes().contains( ancestorType ) )
277             {
278                 final String JavaDoc ancestorJ2EEType = oldTypeToJ2EEType( ancestorType );
279                 final String JavaDoc ancestorName = ancestorNameFound == null ?
280                                 ObjectNames.getSingletonName( j2eeType ) : ancestorNameFound;
281                 final String JavaDoc prop = Util.makeProp( ancestorJ2EEType, ancestorName );
282                 
283                 props = Util.concatenateProps( props, prop );
284             }
285             
286         }
287         
288        // debug( type + "=" + name + " => " + props );
289

290         ObjectName JavaDoc objectName = Util.newObjectName( mJMXDomain, props );
291         
292         final ConfigDelegate delegate =
293             mDelegateFactory.createConfigDelegate( configBean );
294             
295         final AMXConfigImplBase amx = (AMXConfigImplBase)newImpl( j2eeType, delegate );
296         
297         mObjectNameToConfigBean.put( objectName, configBean );
298         objectName = mServer.registerMBean( amx, objectName ).getObjectName();
299         
300         return objectName;
301     }
302     
303         
304     
305         private Map JavaDoc<String JavaDoc,Object JavaDoc>
306     getConfigBeanAttributes( final ConfigBean configBean )
307     {
308         final Map JavaDoc<String JavaDoc,Object JavaDoc> pairs = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
309         
310         try
311         {
312             final ConfigDelegate delegate =
313                 mDelegateFactory.createConfigDelegate( configBean );
314             
315             final MBeanAttributeInfo JavaDoc[] attrInfos = delegate.getMBeanInfo().getAttributes();
316             for( final MBeanAttributeInfo JavaDoc attrInfo : attrInfos )
317             {
318                 final String JavaDoc name = attrInfo.getName();
319                 Object JavaDoc value = null;
320                 try
321                 {
322                     value = delegate.getAttribute( name );
323                 }
324                 catch( AttributeNotFoundException JavaDoc e )
325                 {
326                     value = "<NOT FOUND>";
327                 }
328                 pairs.put( name, value );
329             }
330         }
331         catch( Exception JavaDoc e )
332         {
333             pairs.put( "EXCEPTION", e.getClass().getName() + ": " + e.getMessage() );
334         }
335         
336         return pairs;
337     }
338     
339     
340         private void
341     addBeanHierarchy(
342         final List JavaDoc<ConfigBean> configBeans,
343         final ConfigBean configBean )
344     {
345         // nulls exist in the array, strange but true
346
if ( configBean != null )
347         {
348             configBeans.add( configBean );
349
350             final ConfigBean[] children = configBean.getAllChildBeans();
351             if ( children != null )
352             {
353                 for( final ConfigBean child : children )
354                 {
355                     addBeanHierarchy( configBeans, child );
356                 }
357             }
358         }
359     }
360     
361         private String JavaDoc
362     configBeansToString( final List JavaDoc<ConfigBean> configBeans )
363     {
364         final String JavaDoc[] xPaths = new String JavaDoc[ configBeans.size() ];
365         int i = 0;
366         for( final ConfigBean configBean : configBeans )
367         {
368             xPaths[ i ] = "" + configBean.getXPath();
369             ++i;
370         }
371         Arrays.sort( xPaths );
372         
373         final String JavaDoc NEWLINE = System.getProperty( "line.separator" );
374         return StringUtil.toString( NEWLINE, (Object JavaDoc[])xPaths );
375     }
376     
377     
378         private ObjectName JavaDoc
379     configBeanToAMX( final ConfigBean configBean )
380     {
381         assert( configBean != null );
382         
383         ObjectName JavaDoc objectName = null;
384         try
385         {
386             objectName = loadAMX( configBean );
387         }
388         catch( Exception JavaDoc e )
389         {
390            final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( e );
391            
392            sdebug( "Exception of type " + rootCause.getClass().getName() +
393            " trying to create AMXConfig for " +
394             configBean.getXPath() +
395             ": " + rootCause.getMessage() );
396            sdebug( ExceptionUtil.getStackTrace( rootCause ) );
397         }
398         
399         return objectName;
400     }
401     
402         private List JavaDoc<ObjectName JavaDoc>
403     configBeansToAMX( final List JavaDoc<ConfigBean> configBeans )
404     {
405         final List JavaDoc<ObjectName JavaDoc> objectNames = new ArrayList JavaDoc<ObjectName JavaDoc>();
406         for( final ConfigBean configBean : configBeans )
407         {
408             final ObjectName JavaDoc objectName = configBeanToAMX( configBean );
409             if ( objectName != null )
410             {
411                 objectNames.add( objectName );
412             }
413         }
414         
415         return objectNames;
416     }
417     
418         public void
419     loadAll()
420         throws ConfigException
421     {
422         loadDomainRoot();
423         
424         final ConfigContext configContext = mDelegateFactory.getConfigContext();
425         assert( configContext != null );
426         
427         final ConfigBean domainConfigBean =
428             configContext.exactLookup( ServerXPathHelper.XPATH_DOMAIN );
429         assert( domainConfigBean != null );
430         
431         final List JavaDoc<ConfigBean> configBeans = new ArrayList JavaDoc<ConfigBean>();
432         addBeanHierarchy( configBeans, domainConfigBean );
433         debug( configBeansToString( configBeans ) );
434         
435         // make an AMX MBean for every applicable config bean
436
final List JavaDoc<ObjectName JavaDoc> objectNames = configBeansToAMX( configBeans );
437         
438         debug( "loadAll: loaded " + objectNames.size() + "MBeans:" );
439         debug( CollectionUtil.toString( JMXUtil.objectNamesToStrings( objectNames ), "\n") );
440     }
441
442         private void
443     loadDomainRoot()
444     {
445         try
446         {
447             final TypeInfo info =
448                 TypeInfos.getInstance().getInfo( XTypes.DOMAIN_ROOT );
449             final Class JavaDoc implClass = info.getImplClass();
450                 
451             final ObjectName JavaDoc objectName = mObjectNames.getDomainRootObjectName( );
452                 
453             final Object JavaDoc impl = implClass.newInstance();
454             mServer.registerMBean( impl, objectName );
455         }
456         catch( Exception JavaDoc e )
457         {
458             debug( ExceptionUtil.toString( e ) );
459             throw new Error JavaDoc( e );
460         }
461     }
462     
463     
464         public ObjectName JavaDoc
465     loadSystemInfo( final MBeanServer JavaDoc server )
466         throws Exception JavaDoc
467     {
468         final BootUtil bootUtil = BootUtil.getInstance();
469         
470         final SystemInfoImpl systemInfo = new SystemInfoImpl( server, bootUtil );
471         
472         final ObjectName JavaDoc tempName =
473             mObjectNames.getSingletonObjectName( systemInfo.J2EE_TYPE );
474         
475         final ObjectName JavaDoc objectName =
476             mServer.registerMBean( systemInfo, tempName ).getObjectName();
477         debug( "loaded SystemInfo as " + objectName );
478         return( objectName );
479     }
480     
481         private ProxyFactory
482     getProxyFactory()
483     {
484         return ProxyFactory.getInstance( mServer );
485     }
486     
487         public DomainRoot
488     getDomainRoot()
489     {
490         return( getProxyFactory().getDomainRoot() );
491     }
492 }
493
494
495
496
497
498
499
500
501
Popular Tags