KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > j2ee > J2EEManagedObjectImplBase


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.j2ee;
24
25 import java.util.Set JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import javax.management.ObjectName JavaDoc;
33 import javax.management.MBeanInfo JavaDoc;
34 import javax.management.MBeanAttributeInfo JavaDoc;
35 import javax.management.j2ee.statistics.Stats JavaDoc;
36 import javax.management.Attribute JavaDoc;
37 import javax.management.AttributeNotFoundException JavaDoc;
38
39 import com.sun.enterprise.management.support.AMXNonConfigImplBase;
40
41
42 import com.sun.appserv.management.util.misc.GSetUtil;
43 import com.sun.appserv.management.util.misc.MapUtil;
44 import com.sun.appserv.management.util.misc.ExceptionUtil;
45 import com.sun.enterprise.management.support.AMXAttributeNameMapper;
46 import com.sun.appserv.management.util.jmx.JMXUtil;
47 import com.sun.appserv.management.util.jmx.AttributeNameManglerImpl;
48
49 import com.sun.appserv.management.base.Util;
50 import com.sun.appserv.management.base.AMX;
51 import com.sun.appserv.management.base.XTypes;
52 import com.sun.appserv.management.base.QueryMgr;
53 import com.sun.appserv.management.base.Extra;
54
55 import com.sun.appserv.management.j2ee.J2EETypes;
56 import com.sun.appserv.management.j2ee.J2EEServer;
57 import com.sun.appserv.management.j2ee.J2EEManagedObject;
58 import com.sun.appserv.management.j2ee.ConfigPeer;
59
60 import com.sun.appserv.management.monitor.MonitoringStats;
61
62 import com.sun.appserv.management.config.ServerConfig;
63
64 import com.sun.enterprise.management.support.Delegate;
65
66 import com.sun.enterprise.management.support.TypeInfos;
67
68
69 /**
70  */

71 public abstract class J2EEManagedObjectImplBase extends AMXNonConfigImplBase
72 {
73     protected long mStartTime;
74     
75         public
76     J2EEManagedObjectImplBase()
77     {
78         this( null );
79     }
80         
81         public
82     J2EEManagedObjectImplBase( final Delegate delegate )
83     {
84         super( delegate );
85         mStartTime = 0;
86     }
87
88         protected
89     J2EEManagedObjectImplBase( String JavaDoc j2eeType, Delegate delegate )
90     {
91         super( j2eeType, delegate );
92         mStartTime = 0;
93     }
94
95     /**
96         We may (or may not) want to expose some of these at some
97         point, though not necessarily with the same names.
98      */

99     static private final Set JavaDoc<String JavaDoc> IGNORE_MISSING =
100         GSetUtil.newUnmodifiableStringSet(
101             "EJBModule", "J2EEApplication", "J2EEServer", "j2eeType",
102             "ModuleName", "eventTypes", "hasWebServices", "endpointAddresses"
103         );
104         
105         protected void
106     handleMissingOriginals( final Set JavaDoc<String JavaDoc> missingOriginals )
107     {
108         missingOriginals.removeAll( IGNORE_MISSING );
109         
110         super.handleMissingOriginals( missingOriginals );
111     }
112
113     
114         public void
115     preRegisterDone()
116         throws Exception JavaDoc
117     {
118         super.preRegisterDone();
119         mStartTime = System.currentTimeMillis();
120     }
121     
122          protected final void
123     setstartTime(final long startTime )
124     {
125          mStartTime = startTime;
126     }
127
128
129         public long
130     getstartTime()
131     {
132         return( mStartTime );
133     }
134
135
136         protected String JavaDoc
137     getServerName()
138     {
139         return( getObjectName().getKeyProperty( "J2EEServer" ) );
140     }
141
142         protected String JavaDoc
143     getServerXType()
144     {
145         String JavaDoc type = null;
146         
147         final String JavaDoc serverName = getServerName();
148         if ( serverName != null )
149         {
150             final Map JavaDoc<String JavaDoc,ServerConfig> m =
151                 getDomainRoot().getDomainConfig().getServerConfigMap();
152             
153             final ServerConfig serverConfig = m.get( serverName );
154             type = serverConfig.getJ2EEType();
155         }
156         
157         return( type );
158     }
159     
160     /**
161         Map from JSR77 j2eeType to our config j2eeType.
162      */

163     private static final Map JavaDoc<String JavaDoc,String JavaDoc> ToConfigMap = MapUtil.newMap( new String JavaDoc[]
164     {
165         J2EETypes.J2EE_DOMAIN, XTypes.DOMAIN_CONFIG,
166         J2EETypes.J2EE_CLUSTER, XTypes.CLUSTER_CONFIG,
167         J2EETypes.J2EE_SERVER, XTypes.STANDALONE_SERVER_CONFIG,
168         J2EETypes.JVM, XTypes.JAVA_CONFIG,
169         
170         J2EETypes.J2EE_APPLICATION, XTypes.J2EE_APPLICATION_CONFIG,
171         J2EETypes.EJB_MODULE, XTypes.EJB_MODULE_CONFIG,
172         J2EETypes.WEB_MODULE, XTypes.WEB_MODULE_CONFIG,
173         J2EETypes.APP_CLIENT_MODULE, XTypes.APP_CLIENT_MODULE_CONFIG,
174         
175         J2EETypes.JAVA_MAIL_RESOURCE, XTypes.MAIL_RESOURCE_CONFIG,
176         J2EETypes.JDBC_RESOURCE, XTypes.JDBC_RESOURCE_CONFIG,
177         J2EETypes.JNDI_RESOURCE, XTypes.JNDI_RESOURCE_CONFIG,
178         J2EETypes.WEB_SERVICE_ENDPOINT, XTypes.WEB_SERVICE_ENDPOINT_CONFIG,
179     }
180     );
181     
182         protected String JavaDoc
183     getConfigPeerJ2EEType()
184     {
185         final String JavaDoc configPeerJ2EEType = (String JavaDoc)ToConfigMap.get( getSelfJ2EEType() );
186
187         return( configPeerJ2EEType );
188     }
189     
190     /**
191         Maps a j2eeType to its peer monitoring j2eeType
192      */

193     private static final Map JavaDoc<String JavaDoc,String JavaDoc> MON_MAP =
194         Collections.unmodifiableMap( MapUtil.newMap( new String JavaDoc[]
195     {
196         J2EETypes.J2EE_SERVER, XTypes.SERVER_ROOT_MONITOR,
197         J2EETypes.J2EE_APPLICATION, XTypes.APPLICATION_MONITOR,
198         
199         J2EETypes.WEB_MODULE, XTypes.WEB_MODULE_VIRTUAL_SERVER_MONITOR,
200         J2EETypes.SERVLET, XTypes.SERVLET_MONITOR,
201         
202         J2EETypes.EJB_MODULE, XTypes.EJB_MODULE_MONITOR,
203         J2EETypes.STATELESS_SESSION_BEAN, XTypes.STATELESS_SESSION_BEAN_MONITOR,
204         J2EETypes.STATEFUL_SESSION_BEAN, XTypes.STATEFUL_SESSION_BEAN_MONITOR,
205         J2EETypes.ENTITY_BEAN, XTypes.ENTITY_BEAN_MONITOR,
206         J2EETypes.MESSAGE_DRIVEN_BEAN, XTypes.MESSAGE_DRIVEN_BEAN_MONITOR,
207     }));
208     
209         protected String JavaDoc
210     getMonitoringPeerJ2EEType()
211     {
212         final String JavaDoc monPeerJ2EEType = (String JavaDoc)MON_MAP.get( getSelfJ2EEType() );
213
214         return( monPeerJ2EEType );
215     }
216     
217         protected Map JavaDoc<String JavaDoc,String JavaDoc>
218     getMonitoringPeerProps(
219         final String JavaDoc monitorJ2EEType,
220         final String JavaDoc monitorName )
221     {
222         final Map JavaDoc<String JavaDoc,String JavaDoc> props = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
223         
224         props.put( AMX.J2EE_TYPE_KEY, monitorJ2EEType );
225         props.put( AMX.NAME_KEY, monitorName );
226         
227         for( final String JavaDoc j2eeType : MON_MAP.keySet() )
228         {
229             final String JavaDoc j2eeMonType = (String JavaDoc)MON_MAP.get( j2eeType );
230             
231             final String JavaDoc name = getKeyProperty( j2eeType );
232             if ( name != null )
233             {
234                 props.put( j2eeMonType, name );
235             }
236         }
237         
238         return( props );
239     }
240     
241         protected Map JavaDoc<String JavaDoc,String JavaDoc>
242     getMonitoringPeerProps( )
243     {
244         Map JavaDoc<String JavaDoc,String JavaDoc> props = null;
245         
246         final String JavaDoc j2eeType = getMonitoringPeerJ2EEType();
247         if ( j2eeType != null )
248         {
249             props = getMonitoringPeerProps( j2eeType, getMonitoringPeerName() );
250         }
251         return( props );
252     }
253     
254         protected ObjectName JavaDoc
255     queryProps( final Map JavaDoc<String JavaDoc,String JavaDoc> propsMap )
256     {
257         ObjectName JavaDoc objectName = null;
258         
259         final String JavaDoc props = MapUtil.toString( propsMap, "," );
260             
261         final Set JavaDoc<ObjectName JavaDoc> candidates =
262             getQueryMgr().queryPropsObjectNameSet( props );
263         if ( candidates.size() == 1 )
264         {
265             objectName = GSetUtil.getSingleton( candidates );
266         }
267         else if ( candidates.size() > 1 )
268         {
269             throw new RuntimeException JavaDoc(
270                 "Unexpectedly found too many candidates for query pattern " + quote( props ) +
271                 " found: " + toString( candidates ) );
272         }
273         
274         return( objectName );
275     }
276     
277         public ObjectName JavaDoc
278     getMonitoringPeerObjectName()
279     {
280         ObjectName JavaDoc objectName = null;
281         
282         final Map JavaDoc<String JavaDoc,String JavaDoc> propsMap = getMonitoringPeerProps();
283         if ( propsMap != null && propsMap.keySet().size() != 0 )
284         {
285             debug( "getMonitoringPeerObjectName: my ObjectName = ", getObjectName(),
286                 ", queryProps = ", MapUtil.toString( propsMap, ", " ) );
287             objectName = queryProps( propsMap );
288
289             if ( objectName == null )
290             {
291                 objectName = queryMonitoringPeerFailed( propsMap );
292             }
293         }
294         
295         return( objectName );
296     }
297     
298     /**
299         The usual method of finding the monitoring peer has failed.
300         This is an opportunity to try again.
301      */

302         protected ObjectName JavaDoc
303     queryMonitoringPeerFailed( final Map JavaDoc<String JavaDoc,String JavaDoc> propsMap )
304     {
305         return null;
306     }
307     
308     
309     /**
310         The usual method of finding the config peer has failed.
311         This is an opportunity to try again.
312      */

313         protected ObjectName JavaDoc
314     queryConfigPeerFailed( final Map JavaDoc<String JavaDoc,String JavaDoc> propsMap )
315     {
316         debug( "queryConfigPeerFailed: " + MapUtil.toString( propsMap ) );
317         return null;
318     }
319     
320     
321     /**
322         JSR 77 impl
323         
324         @return String representation of the ObjectName
325     */

326         public final String JavaDoc
327     getobjectName()
328     {
329         return( getObjectName().toString() );
330     }
331     
332     
333         public boolean
334     isstatisticProvider()
335     {
336         return( getMonitoringPeerObjectName() != null );
337     }
338     
339         public boolean
340     isstatisticsProvider()
341     {
342         return( isstatisticProvider() );
343     }
344     
345         protected Stats JavaDoc
346     getStatsGeneric()
347     {
348         Stats JavaDoc stats = null;
349         
350         if ( isstatisticProvider() )
351         {
352             final ObjectName JavaDoc mon = getMonitoringPeerObjectName();
353             if ( mon != null )
354             {
355                 final AMX monStats = getProxyFactory().getProxy( mon, AMX.class);
356                 try
357                 {
358                     final Extra extra = Util.getExtra( monStats );
359                     stats = (Stats JavaDoc)extra.getAttribute( "stats" );
360                     
361                 }
362                 catch( Exception JavaDoc e )
363                 {
364                     // OK, ignore
365
}
366             }
367         }
368         
369         return( stats );
370     }
371         
372         public final String JavaDoc
373     getGroup()
374     {
375         return( AMX.GROUP_JSR77 );
376     }
377     
378     
379         protected String JavaDoc
380     getConfigPeerName()
381     {
382         return( getSelfName() );
383     }
384     
385         protected String JavaDoc
386     getMonitoringPeerName()
387     {
388         return( getSelfName() );
389     }
390     
391     /**
392         If there is a config peer, return properties that uniquely identify it.
393         This base implementation won't be sufficient for many MBeans, so subclasses
394         should call super.getConfigPeerProps(), then tack on any additional ones.
395      */

396         protected Map JavaDoc<String JavaDoc,String JavaDoc>
397     getConfigPeerProps()
398     {
399         HashMap JavaDoc<String JavaDoc,String JavaDoc> props = null;
400         final String JavaDoc j2eeType = getConfigPeerJ2EEType();
401         if ( j2eeType != null )
402         {
403             props = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
404             
405             props.put( AMX.J2EE_TYPE_KEY, j2eeType );
406             props.put( AMX.NAME_KEY, getConfigPeerName() );
407         }
408         return( props );
409     }
410     
411     
412     /**
413         Get the name of a config peer. Default behavior is to use the config peer
414         j2eeType, together with the name of this MBean and GROUP_CONFIGURATION to
415         locate a single MBean. A subclass not following this convention will have
416         to override this method.
417      */

418         public ObjectName JavaDoc
419     getConfigPeerObjectName()
420     {
421         ObjectName JavaDoc configPeerObjectName = null;
422         
423         if (isConfigProvider() )
424         {
425             final Map JavaDoc<String JavaDoc,String JavaDoc> propsMap = getConfigPeerProps( );
426             if ( propsMap != null )
427             {
428                 configPeerObjectName = queryProps( propsMap );
429                 if ( configPeerObjectName == null )
430                 {
431                     configPeerObjectName = queryConfigPeerFailed( propsMap );
432                 }
433             }
434         
435         }
436         
437         return( configPeerObjectName );
438     }
439     
440     
441         public boolean
442     isConfigProvider()
443     {
444         return( ToConfigMap.keySet().contains( getSelfJ2EEType() ) );
445     }
446     
447         public boolean
448     iseventProvider()
449     {
450         return( false );
451     }
452     
453         public boolean
454     isstateManageable()
455     {
456         return( false );
457     }
458     
459         protected final String JavaDoc[]
460     getContaineeObjectNameStrings( final String JavaDoc j2eeType )
461     {
462         final Set JavaDoc<ObjectName JavaDoc> objectNames = getContaineeObjectNameSet( j2eeType );
463         
464         final String JavaDoc[] names = GSetUtil.toStringArray( objectNames );
465         
466         return( names );
467     }
468     
469         public J2EEServer
470     getJ2EEServer()
471     {
472         return getProxyFactory().getProxy( getServerObjectName(), J2EEServer.class);
473     }
474     
475         public ObjectName JavaDoc
476     getServerObjectName()
477     {
478         final ObjectName JavaDoc selfObjectName = getObjectName();
479         
480         ObjectName JavaDoc serverObjectName = null;
481         
482         final String JavaDoc serverName = selfObjectName.getKeyProperty( J2EETypes.J2EE_SERVER );
483         if ( serverName != null )
484         {
485             final String JavaDoc props = Util.makeRequiredProps( J2EETypes.J2EE_SERVER, serverName );
486             final Set JavaDoc<AMX> candidates = getQueryMgr().queryPatternSet( selfObjectName.getDomain(), props );
487             
488             serverObjectName = Util.getObjectName( GSetUtil.getSingleton( candidates ) );
489         }
490         return( serverObjectName );
491     }
492     
493     
494     private static final Set JavaDoc<String JavaDoc> DEPLOYED_TYPES = GSetUtil.newUnmodifiableStringSet(
495             J2EETypes.J2EE_APPLICATION,
496             J2EETypes.WEB_MODULE,
497             J2EETypes.EJB_MODULE,
498             J2EETypes.APP_CLIENT_MODULE,
499             J2EETypes.RESOURCE_ADAPTER_MODULE
500         );
501     
502     
503         public Set JavaDoc<ObjectName JavaDoc>
504     getDeployedObjectsObjectNameSet()
505     {
506         return( getContaineeObjectNameSet( DEPLOYED_TYPES ) );
507     }
508
509         public String JavaDoc[]
510     getdeployedObjects()
511     {
512         final String JavaDoc[] names = GSetUtil.toStringArray( getDeployedObjectsObjectNameSet() );
513         return( names );
514     }
515     
516     /**
517         Return a list of Attribute names which should not be mapped.
518      */

519         protected Set JavaDoc<String JavaDoc>
520     getDontMapAttributeNames()
521     {
522         // all mapped by default
523
return( Collections.emptySet() );
524     }
525     
526     /*
527         protected void
528     addCustomMappings( final AMXAttributeNameMapper mapper )
529     {
530         super.addCustomMappings( mapper );
531         
532         for( final String name : getDontMapAttributeNames() )
533         {
534             mapper.dontMap( name );
535         }
536     }
537     */

538     
539 }
540
Popular Tags