KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > support > LoaderOfOldMonitor


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 package com.sun.enterprise.management.support;
24
25 import java.util.Set JavaDoc;
26 import java.util.Collections JavaDoc;
27
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.MBeanRegistrationException JavaDoc;
31 import javax.management.InstanceAlreadyExistsException JavaDoc;
32 import javax.management.InstanceNotFoundException JavaDoc;
33
34
35 import com.sun.enterprise.management.support.oldconfig.OldProps;
36 import com.sun.appserv.management.base.Util;
37 import com.sun.appserv.management.base.XTypes;
38 import com.sun.appserv.management.base.AMX;
39 import com.sun.appserv.management.util.jmx.JMXUtil;
40 import com.sun.appserv.management.util.misc.GSetUtil;
41 import com.sun.appserv.management.util.misc.StringUtil;
42 import com.sun.appserv.management.util.stringifier.ArrayStringifier;
43 import com.sun.appserv.management.j2ee.J2EETypes;
44 import com.sun.enterprise.config.serverbeans.ApplicationHelper;
45 import com.sun.enterprise.config.serverbeans.WebModule;
46 import com.sun.enterprise.config.ConfigBean;
47 import com.sun.enterprise.admin.server.core.AdminService;
48 import com.sun.enterprise.config.ConfigContext;
49
50
51 /**
52     Loads MBeans.
53  */

54 final class LoaderOfOldMonitor extends LoaderOfOld
55 {
56     LoaderOfOldMonitor( final Loader loader )
57     {
58         super( loader );
59     }
60
61         public Set JavaDoc<ObjectName JavaDoc>
62     findAllOldCandidates()
63     {
64         final ObjectName JavaDoc pattern = JMXUtil.newObjectName( "com.sun.appserv:category=monitor,*" );
65         final Set JavaDoc<ObjectName JavaDoc> all = JMXUtil.queryNames( getMBeanServer(), pattern, null );
66         
67         return( all );
68     }
69     
70
71     private final class MyOldTypes
72         implements OldTypeToJ2EETypeMapper
73     {
74         private final OldMonitorTypes mOldTypes;
75         
76             public
77         MyOldTypes()
78         {
79             mOldTypes = OldMonitorTypes.getInstance();
80         }
81         
82             public String JavaDoc
83         j2eeTypeToOldType( final String JavaDoc j2eeType )
84         {
85             String JavaDoc result = null;
86             
87             if ( j2eeType.endsWith( "BeanMonitor" ) )
88             {
89                 result = "ejb";
90             }
91             else
92             {
93                 result = mOldTypes.j2eeTypeToOldType( j2eeType );
94             }
95
96             return( result );
97         }
98         
99         
100             public String JavaDoc
101         oldTypeToJ2EEType( final String JavaDoc oldType, final ObjectName JavaDoc oldObjectName )
102         {
103             String JavaDoc j2eeType = null;
104             
105             final String JavaDoc EJB = "ejb";
106             
107             /*
108                 The type "ejb" is a bug--there are 4 types of ejbs, and they should have
109                 four distinct types. Our headache here is to deduce the proper type.
110              */

111             if ( oldType.equals( EJB ) )
112             {
113                 /**
114                     type=ejb
115                  */

116                 if ( oldObjectName.getKeyProperty( "type" ).equals( EJB ) )
117                 {
118                     // "StatisticNames" is incorrectly implemented as method (should be an Attribute)
119
String JavaDoc[] statisticNames = null;
120                     try
121                     {
122                         statisticNames = (String JavaDoc[])getMBeanServer().invoke( oldObjectName, "getStatisticNames", null, null);
123                     }
124                     catch ( Exception JavaDoc e )
125                     {
126                         throw new RuntimeException JavaDoc( "Can't invoke getStatisticNames() on " + oldObjectName, e );
127                     }
128                     
129                     // determine, using the names of the Statistics, which type of bean monitor it is
130
final Set JavaDoc<String JavaDoc> names = GSetUtil.newSet( statisticNames );
131                     assert( names.contains( "CreateCount" ) ) :
132                         "MBean " + oldObjectName + " does not contain 'RemoveCount' as a Statistic name: all = " +
133                         ArrayStringifier.stringify( statisticNames, ", " );
134                     assert( names.contains( "RemoveCount" ) );
135                     if ( names.contains( "MethodReadyCount" ) )
136                     {
137                         if ( names.contains( "PassiveCount" ) )
138                         {
139                             j2eeType = XTypes.STATEFUL_SESSION_BEAN_MONITOR;
140                         }
141                         else
142                         {
143                             j2eeType = XTypes.STATELESS_SESSION_BEAN_MONITOR;
144                         }
145                     }
146                     else if ( names.contains( "MessageCount" ) )
147                     {
148                         j2eeType = XTypes.MESSAGE_DRIVEN_BEAN_MONITOR;
149                     }
150                     else if ( names.contains( "ReadyCount" ) && names.contains( "PooledCount" ) )
151                     {
152                         j2eeType = XTypes.ENTITY_BEAN_MONITOR;
153                     }
154                     else
155                     {
156                         throw new RuntimeException JavaDoc( "Unknown type of ejb monitor: " + oldObjectName );
157                     }
158                 }
159                 else
160                 {
161                     /*
162                         It's one of the property keys, referring to its parent eg:
163                         ejb=<name>
164                         
165                         Find the bean containing it, then determine the j2eeType of that bean
166                      */

167                     final String JavaDoc parentName = oldObjectName.getKeyProperty( EJB );
168                     final String JavaDoc parentModuleName = oldObjectName.getKeyProperty( "ejb-module" );
169                     final String JavaDoc serverName = oldObjectName.getKeyProperty( "server" );
170                     final String JavaDoc applicationName = oldObjectName.getKeyProperty( "application" );
171                     
172                     final String JavaDoc parentTypeProp = Util.makeProp( "type", EJB );
173                     final String JavaDoc parentNameProp = Util.makeProp( "name", parentName );
174                     final String JavaDoc parentModuleNameProp = Util.makeProp( "ejb-module", parentModuleName );
175                     final String JavaDoc parentProps = Util.concatenateProps( parentTypeProp, parentNameProp, parentModuleNameProp );
176                     final String JavaDoc serverProp = Util.makeProp( "server", serverName );
177                     
178                     String JavaDoc props = Util.concatenateProps( serverProp, parentProps, "category=monitor" );
179                         
180                     if ( applicationName != null )
181                     {
182                         final String JavaDoc applicationProp = Util.makeProp( "application", applicationName );
183                         props = Util.concatenateProps( props, applicationProp );
184                     }
185                     
186                     
187                     final ObjectName JavaDoc pattern = JMXUtil.newObjectNamePattern( "com.sun.appserv", props );
188                     
189                     final Set JavaDoc<ObjectName JavaDoc> candidates =
190                         JMXUtil.queryNames( getMBeanServer(), pattern, null );
191
192                     if ( candidates.size() == 0 )
193                     {
194                         getLogger().fine( "Can't find MBean for pattern " +
195                             StringUtil.quote( pattern ) +
196                             " corresponding to " + StringUtil.quote( oldObjectName ) );
197                         throw new RuntimeException JavaDoc( new DeferRegistrationException( pattern ) );
198                     }
199
200                     final ObjectName JavaDoc beanObjectName = GSetUtil.getSingleton( candidates );
201                     j2eeType = oldTypeToJ2EEType( EJB, beanObjectName );
202                 }
203             }
204             else
205             {
206                 j2eeType = mOldTypes.oldTypeToJ2EEType( oldType, oldObjectName );
207             }
208             return( j2eeType );
209         }
210
211             public String JavaDoc
212         oldObjectNameToJ2EEType( final ObjectName JavaDoc oldObjectName )
213         {
214             final String JavaDoc oldType = oldObjectName.getKeyProperty( "type" );
215             
216             return( oldTypeToJ2EEType( oldType, oldObjectName ) );
217         }
218     }
219     
220         private static void
221     mySleep( final long millis )
222     {
223         try
224         {
225             Thread.sleep( millis );
226         }
227         catch( InterruptedException JavaDoc e )
228         {
229         }
230     }
231     
232         private final String JavaDoc
233     formApplicationAndServerProps( final ObjectName JavaDoc oldObjectName )
234     {
235         final String JavaDoc serverName = oldObjectName.getKeyProperty( "server" );
236         final String JavaDoc serverProp = Util.makeProp( XTypes.SERVER_ROOT_MONITOR, serverName );
237         
238         String JavaDoc props = serverProp;
239         
240         String JavaDoc applicationName = oldObjectName.getKeyProperty( "application" );
241         if ( applicationName == null )
242         {
243             applicationName = AMX.NULL_NAME;
244         }
245         final String JavaDoc applicationProp = Util.makeProp( XTypes.APPLICATION_MONITOR, applicationName );
246         
247         props = Util.concatenateProps( props, applicationProp );
248         
249         return( props );
250     }
251     
252     /**
253         Finding the monitoring peer for a WebModule is very complex as of version 8.1, due
254         to the inconsistent names used for JSR 77 MBeans versus their monitoring equivalents.
255         Mix in virtual servers, and context root versus module name, and it's a real
256         witches-brew of naming--Lloyd Chambers 27 Oct 2004
257         
258         Here are some facts:
259         (1) the name property of a j2eeType=WebModule ObjectName
260         is formed as //<virtual-server-name><context-root>.
261         <context-root> always starts with "/". <context-root> by default is the same
262         as the name of the web module, but need not be. A typical name is thus:
263         //server/MyModule
264         
265         (2) the name property of a type=standalone-web-module ObjectName is always the module name, never
266         the context root. The name property of a type=web-module (embedded war) is the module name + ".war".
267         The virtual server is not specified, since a type=standalone-web-module or type=web-module MBean
268         is only a holder for webmodule-virtual-server mbeans. Thus, the usual parallel structure is malformed.
269         in the case of monitoring mbeans for web modules.
270         
271         Examples:
272         
273         (a) j2eeType=WebModule,name=//server/bookstore,J2EEServer=server,J2EEApplication=null
274         (b) type=standalone-web-module,name=bookstore,category=monitor,server=server
275         (c) type=web-module,name=logging-helloworld.war,application=logging-helloworld,category=monitor,server=server
276         
277         (3) type=servlet is a monitoring mbean which contains properties standalone-web-module or
278         web-module and the webmodule-virtual-server in which it resides. For example:
279         
280         (a) type=servlet,name=LoggingServlet,application=logging-helloworld,category=monitor,
281             server=server,web-module=logging-helloworld.war,webmodule-virtual-server=server
282             
283         (b) type=servlet,name=jsp,category=monitor,server=server,standalone-web-module=bookstore,webmodule-virtual-server=server
284         
285         @see ServletImpl #getMonitoringPeerObjectName
286      */

287      
288         protected ObjectName JavaDoc
289     oldToNewObjectName( final ObjectName JavaDoc oldObjectName )
290     {
291         final String JavaDoc oldType = oldObjectName.getKeyProperty( "type" );
292         final String JavaDoc domainName = mLoader.getAMXJMXDomainName();
293         
294         ObjectName JavaDoc newObjectName = null;
295         
296         /*
297         if ( oldType.equals( "webmodule-virtual-server" ) )
298         {
299         *
300             Make the peer of a WebModule be a WebModuleVirtualServerMonitor
301          *
302             final String virtualServerName = oldObjectName.getKeyProperty( "name" );
303             final String webModuleName = WebModuleSupport.getWebModuleName( oldObjectName );
304             final String compositeName = WebModuleSupport.formCompositeName( virtualServerName, webModuleName );
305             
306             final String requiredProps =
307                 Util.makeRequiredProps( XTypes.WEB_MODULE_VIRTUAL_SERVER_MONITOR, compositeName );
308             
309             final String containmentProps = formApplicationAndServerProps( oldObjectName );
310             final String props = Util.concatenateProps( requiredProps, containmentProps );
311             
312             newObjectName = JMXUtil.newObjectName( domainName, props );
313         }
314         else
315         */
if ( oldType.equals( "servlet" ) )
316         {
317             // A ServletMonitor is contained by its WebModuleVirtualServerMonitor
318
// a monitor for a Servlet
319
final String JavaDoc webModuleVirtualServerMonitorName = oldObjectName.getKeyProperty( "webmodule-virtual-server" );
320             
321             final String JavaDoc requiredProps =
322                 Util.makeRequiredProps( XTypes.SERVLET_MONITOR, oldObjectName.getKeyProperty( "name" ) );
323             
324             final String JavaDoc containerProp =
325                 Util.makeProp( XTypes.WEB_MODULE_VIRTUAL_SERVER_MONITOR, webModuleVirtualServerMonitorName );
326             final String JavaDoc containmentProps = formApplicationAndServerProps( oldObjectName );
327             
328             final String JavaDoc props = Util.concatenateProps( requiredProps, containerProp, containmentProps);
329             
330             newObjectName = JMXUtil.newObjectName( domainName, props );
331         }
332         else
333         if ( oldType.equals( "webservice-endpoint" ))
334         {
335             // a monitor for a web service endpoint
336
// first look for a EJB web service endpoint
337
String JavaDoc ejbModName =
338             oldObjectName.getKeyProperty("standalone-ejb-module");
339             if ( ejbModName == null)
340             {
341                 // try if this is a ejb module in an application
342
ejbModName = oldObjectName.getKeyProperty("ejb-module");
343             }
344             if ( ejbModName != null)
345             {
346                 final String JavaDoc requiredProps =
347                     Util.makeRequiredProps( XTypes.WEBSERVICE_ENDPOINT_MONITOR, oldObjectName.getKeyProperty( "name" ) );
348             
349                 final String JavaDoc modProp = Util.makeProp( XTypes.EJB_MODULE_MONITOR, ejbModName );
350                 final String JavaDoc containmentProps = formApplicationAndServerProps( oldObjectName );
351             
352                 final String JavaDoc props =
353                     Util.concatenateProps( requiredProps, modProp, containmentProps);
354             
355                 newObjectName = JMXUtil.newObjectName( domainName, props );
356             }
357             else
358             {
359                 /*
360                 String virtualServerName = oldObjectName.getKeyProperty( "webmodule-virtual-server" );
361                 if ( virtualServerName == null)
362                 {
363                     virtualServerName = "server";
364                 }
365
366                 final String webModuleName= WebModuleSupport.getWebModuleName( oldObjectName );
367                 
368                 final String vsMonitorCompositeName =
369                     WebModuleSupport.formCompositeName( virtualServerName, webModuleName );
370             
371                 final String requiredProps =
372                     Util.makeRequiredProps( XTypes.WEBSERVICE_ENDPOINT_MONITOR, oldObjectName.getKeyProperty( "name" ) );
373             
374                 final String virtualServerProp = Util.makeProp( XTypes.WEB_MODULE_VIRTUAL_SERVER_MONITOR, vsMonitorCompositeName );
375                 final String containmentProps = formApplicationAndServerProps( oldObjectName );
376             
377                 final String props = Util.concatenateProps( requiredProps, virtualServerProp, containmentProps);
378             
379                 newObjectName = JMXUtil.newObjectName( domainName, props );
380                 */

381                 final String JavaDoc requiredProps =
382                     Util.makeRequiredProps( XTypes.WEBSERVICE_ENDPOINT_MONITOR, oldObjectName.getKeyProperty( "name" ) );
383             
384                 final String JavaDoc webModuleVirtualServerMonitorName =
385                     oldObjectName.getKeyProperty( "webmodule-virtual-server" );
386                 final String JavaDoc modProp =
387                     Util.makeProp( XTypes.WEB_MODULE_VIRTUAL_SERVER_MONITOR, webModuleVirtualServerMonitorName );
388                 final String JavaDoc containmentProps = formApplicationAndServerProps( oldObjectName );
389             
390                 final String JavaDoc props =
391                     Util.concatenateProps( requiredProps, modProp, containmentProps);
392             
393                 newObjectName = JMXUtil.newObjectName( domainName, props );
394             }
395
396         }
397         else
398         {
399             final OldTypeToJ2EETypeMapper mapper = new MyOldTypes();
400             final OldProps oldProps = new OldProps( oldObjectName, mapper);
401
402             String JavaDoc props = oldProps.getNewProps();
403             newObjectName = JMXUtil.newObjectName( domainName, props );
404
405             /*
406                 If it's containment hierarchy includes an APPLICATION_MONITOR, then if one
407                 is not present (eg a standalone web or ejb monitor),
408                 then insert one with name AMX.NULL_NAME.
409              */

410             final String JavaDoc j2eeType = newObjectName.getKeyProperty( AMX.J2EE_TYPE_KEY );
411             final String JavaDoc[] fullType = TypeInfos.getInstance().getJ2EETypeChain( newObjectName );
412             for( int i = 0; i < fullType.length - 1; ++i )
413             {
414                 if ( fullType[ i ].equals( XTypes.APPLICATION_MONITOR ) &&
415                     newObjectName.getKeyProperty( XTypes.APPLICATION_MONITOR ) == null )
416                 {
417                     final String JavaDoc prop = Util.makeProp( XTypes.APPLICATION_MONITOR, AMX.NULL_NAME );
418                     newObjectName = JMXUtil.newObjectName( newObjectName, prop );
419                     break;
420                 }
421             }
422         }
423         return( newObjectName );
424     }
425
426
427         // remove the forward slash
428
private String JavaDoc
429     getContextRoot( final String JavaDoc webModuleName ) {
430
431         ConfigBean cb = null;
432         ConfigContext cCtx = AdminService.getAdminService().
433                 getAdminContext().getAdminConfigContext();
434
435         try {
436             cb = ApplicationHelper.findApplication(cCtx, webModuleName);
437         } catch(Exception JavaDoc e) {
438             // ignore
439
return null;
440         }
441         if (cb instanceof WebModule) {
442             String JavaDoc ctxRoot = ((WebModule) cb).getContextRoot();
443             if ((ctxRoot != null) && (ctxRoot.length() > 0)) {
444                if (ctxRoot.charAt(0) == '/'){
445                     ctxRoot = ctxRoot.substring(1,ctxRoot.length()) ;
446                }
447             }
448             return ctxRoot;
449         } else {
450             return null;
451         }
452     }
453
454         private boolean
455     isOldMonitorObjectName( final ObjectName JavaDoc objectName )
456     {
457         boolean isOldMonitor = false;
458         
459         if ( objectName.getDomain().equals( "com.sun.appserv" ) &&
460                 "monitor".equals( objectName.getKeyProperty( "category" ) ) )
461         {
462             final String JavaDoc type = objectName.getKeyProperty( "type" );
463             
464             isOldMonitor = ! getIgnoreTypes().contains( type );
465         }
466         
467         return( isOldMonitor );
468     }
469
470     /**
471         Do not attempt to create corresponding "new" mbeans for these types
472      */

473     private static final Set JavaDoc<String JavaDoc> IGNORE_TYPES = GSetUtil.newUnmodifiableStringSet(
474             "connection-factories",
475             "connector-modules",
476             "connector-service",
477             "connection-factory",
478             "connection-pools",
479             "resources",
480             "thread-pools",
481             "applications",
482             "orb",
483             "connection-managers",
484             "bean-methods",
485             
486             // we deal with these two specially
487
"web-module",
488             "standalone-web-module",
489             
490             // these are JDK 5.0 MBeans. See bug (RFE)
491
"operating-system",
492             "threadinfo",
493             "memory",
494             "thread-system",
495             "garbage-collectors",
496             "garbage-collector",
497             "runtime",
498             "compilation-system",
499             "class-loading-system",
500             
501             // native web core types that have not yet been removed
502
"request",
503             "pwc-thread-pool",
504             
505             "jndi", //completely non-functional as of 27 June 2005.
506

507             "jms-service",
508             "jndi",
509             
510             "GMSClientMBean",
511             
512             // support these at some point?
513
"DomainDiagnostics",
514             "JVMInformation",
515             "JVMInformationCollector",
516             "work-management"
517         );
518
519     /**
520         These types need to be supported.
521      */

522     private static final Set JavaDoc<String JavaDoc> NEEDS_SUPPORT = GSetUtil.newUnmodifiableStringSet(
523             // not for 9.0
524
// "DomainDiagnostics",
525
// "JVMInformation",
526
// "JVMInformationCollector",
527
// "work-management",
528
);
529
530
531         protected Set JavaDoc<String JavaDoc>
532     getNeedsSupport()
533     {
534         return( NEEDS_SUPPORT );
535     }
536
537         protected Set JavaDoc<String JavaDoc>
538     getIgnoreTypes()
539     {
540         return( IGNORE_TYPES );
541     }
542
543
544         public boolean
545     isOldMBean( final ObjectName JavaDoc oldObjectName )
546     {
547         return isOldMonitorObjectName( oldObjectName );
548     }
549     
550 }
551
552
553
554
555
556
557
558
559
Popular Tags