KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28  
29 /*
30  * $Header: /cvs/glassfish/admin/mbeanapi-impl/src/java/com/sun/enterprise/management/support/MBeanImplBase.java,v 1.9 2006/03/09 20:30:47 llc Exp $
31  * $Revision: 1.9 $
32  * $Date: 2006/03/09 20:30:47 $
33  */

34
35 package com.sun.enterprise.management.support;
36
37 import java.util.logging.Logger JavaDoc;
38 import java.util.logging.Level JavaDoc;
39 import java.util.Map JavaDoc;
40 import java.util.HashMap JavaDoc;
41
42 import java.io.Serializable JavaDoc;
43
44 import javax.management.ObjectName JavaDoc;
45 import javax.management.DynamicMBean JavaDoc;
46 import javax.management.MBeanServer JavaDoc;
47 import javax.management.MBeanRegistration JavaDoc;
48 import javax.management.InstanceNotFoundException JavaDoc;
49 import javax.management.IntrospectionException JavaDoc;
50 import javax.management.ReflectionException JavaDoc;
51
52 import javax.management.AttributeChangeNotification JavaDoc;
53 import javax.management.Notification JavaDoc;
54 import javax.management.NotificationFilter JavaDoc;
55 import javax.management.NotificationListener JavaDoc;
56 import javax.management.ListenerNotFoundException JavaDoc;
57
58 import com.sun.appserv.management.util.misc.StringUtil;
59 import com.sun.appserv.management.base.AMXLoggerBase;
60 import com.sun.appserv.management.base.LoggerSupport;
61 import com.sun.appserv.management.base.AMXDebug;
62 import com.sun.appserv.management.base.AMXMBeanLogging;
63
64 import com.sun.appserv.management.util.stringifier.SmartStringifier;
65 import com.sun.appserv.management.util.jmx.NotificationBuilder;
66 import com.sun.appserv.management.util.jmx.NotificationSender;
67 import com.sun.appserv.management.util.jmx.NotificationEmitterSupport;
68 import com.sun.appserv.management.util.jmx.AttributeChangeNotificationBuilder;
69 import com.sun.appserv.management.util.jmx.stringifier.NotificationStringifier;
70
71 import com.sun.appserv.management.util.misc.Output;
72 import com.sun.appserv.management.util.misc.FileOutput;
73 import com.sun.appserv.management.util.misc.OutputIgnore;
74
75 import com.sun.logging.LogDomains;
76
77 /**
78     Absolute base impl class. Should contain only core functionality,
79     nothing to do with appserver specifics.
80  */

81 public abstract class MBeanImplBase
82     implements MBeanRegistration JavaDoc, NotificationSender, AMXMBeanLogging
83 {
84     protected static final String JavaDoc[] EMPTY_STRING_ARRAY = new String JavaDoc[0];
85     
86     /**
87         The MBeanServer in which this object is registered (if any)
88      */

89     protected MBeanServer JavaDoc mServer;
90     
91     /**
92         The ObjectName by which this object is registered (if registered).
93         Multiple registrations, which are possible, overwrite this; the last registration
94         wins. If the MBean has not been registered, this name will be null.
95      */

96     protected ObjectName JavaDoc mSelfObjectName;
97     
98     
99     private NotificationEmitterSupport mNotificationEmitter;
100     
101     protected final Logger JavaDoc mLogger;
102     
103     private Map JavaDoc<String JavaDoc,NotificationBuilder> mNotificationBuilders;
104     
105     private final Output mDebug;
106     
107         public
108     MBeanImplBase()
109     {
110         mLogger = Logger.getLogger( LogDomains.ADMIN_LOGGER );
111         mNotificationEmitter = null;
112         
113         /**
114             We need debugging before the MBean is registered, so our
115             only choice is to use an ID based on something other than
116             the as-yet-unknown ObjectName, namely the classname.
117          */

118         mDebug = AMXDebug.getInstance().getOutput( getDebugID() );
119     }
120     
121     
122         public final int
123     getListenerCount()
124     {
125         return( getNotificationEmitter().getListenerCount() );
126     }
127     
128         public final int
129     getNotificationTypeListenerCount( final String JavaDoc type )
130     {
131         return( getNotificationEmitter().getNotificationTypeListenerCount( type ) );
132     }
133     
134     /**
135         @return an empty array
136         Subclass may wish to override this.
137     */

138         synchronized protected final NotificationEmitterSupport
139     getNotificationEmitter()
140     {
141         if ( mNotificationEmitter == null )
142         {
143             mNotificationEmitter = new NotificationEmitterSupport( true );
144         }
145         
146         return( mNotificationEmitter );
147     }
148     
149         public synchronized void
150     addNotificationListener(
151         final NotificationListener JavaDoc listener )
152     {
153         getNotificationEmitter().addNotificationListener( listener, null, null );
154     }
155     
156         public synchronized void
157     addNotificationListener(
158         final NotificationListener JavaDoc listener,
159         final NotificationFilter JavaDoc filter,
160         final Object JavaDoc handback)
161     {
162         getNotificationEmitter().addNotificationListener( listener, filter, handback );
163     }
164
165         public synchronized void
166     removeNotificationListener( final NotificationListener JavaDoc listener)
167         throws ListenerNotFoundException JavaDoc
168     {
169         getNotificationEmitter().removeNotificationListener( listener );
170     }
171  
172         public synchronized void
173     removeNotificationListener(
174         final NotificationListener JavaDoc listener,
175         final NotificationFilter JavaDoc filter,
176         final Object JavaDoc handback)
177         throws ListenerNotFoundException JavaDoc
178     {
179         getNotificationEmitter().removeNotificationListener( listener, filter, handback );
180     }
181
182         public void
183     sendNotification( final Notification JavaDoc notification)
184     {
185         getNotificationEmitter().sendNotification( notification );
186     }
187     
188         protected NotificationBuilder
189     createNotificationBuilder( final String JavaDoc notificationType )
190     {
191         NotificationBuilder builder = null;
192         
193         if ( notificationType.equals( AttributeChangeNotification.ATTRIBUTE_CHANGE ) )
194         {
195             builder = new AttributeChangeNotificationBuilder( getObjectName() );
196         }
197         else
198         {
199             builder = new NotificationBuilder( notificationType, getObjectName() );
200         }
201         
202         return( builder );
203     }
204     
205     /**
206         Get a NotificationBuilder for the specified type of Notification
207         whose source is this object.
208      */

209         protected synchronized NotificationBuilder
210     getNotificationBuilder( final String JavaDoc notificationType )
211     {
212         if ( mNotificationBuilders == null )
213         {
214             mNotificationBuilders = new HashMap JavaDoc<String JavaDoc,NotificationBuilder>();
215         }
216         
217         NotificationBuilder builder =
218             (NotificationBuilder)mNotificationBuilders.get( notificationType );
219         
220         if ( builder == null )
221         {
222             builder = createNotificationBuilder( notificationType );
223             mNotificationBuilders.put( notificationType, builder );
224         }
225         
226         return( builder );
227     }
228     
229     /**
230         Send a Notification of the specified type containing no data.
231      */

232         protected void
233     sendNotification( final String JavaDoc notificationType )
234     {
235         sendNotification( notificationType, notificationType, null, null );
236     }
237     
238     
239     /**
240         Send a Notification of the specified type containing a single
241         key/value pair for data.
242      */

243         protected void
244     sendNotification(
245         final String JavaDoc notificationType,
246         final String JavaDoc key,
247         final Serializable JavaDoc value)
248     {
249         final String JavaDoc message = "no message specified";
250         
251         sendNotification( notificationType, message, key, value );
252     }
253     
254     /**
255         Send a Notification of the specified type containing a single
256         key/value pair for data.
257      */

258         protected void
259     sendNotification(
260         final String JavaDoc notificationType,
261         final String JavaDoc message,
262         final String JavaDoc key,
263         final Serializable JavaDoc value)
264     {
265         final NotificationBuilder builder =
266             getNotificationBuilder( notificationType );
267             
268         final Notification JavaDoc notif = builder.buildNew( message );
269         NotificationBuilder.putMapData( notif, key, value );
270             
271         sendNotification( notif );
272     }
273     
274     
275         public final ObjectName JavaDoc
276     getObjectName()
277     {
278         return( mSelfObjectName );
279     }
280     
281         public String JavaDoc
282     getJMXDomain()
283     {
284         return( getObjectName().getDomain() );
285     }
286     
287         public final MBeanServer JavaDoc
288     getMBeanServer()
289     {
290         return( mServer );
291     }
292     
293         protected static String JavaDoc
294     toString( Object JavaDoc o )
295     {
296         if ( o == null )
297         {
298             return( "" + o );
299         }
300         
301         return( SmartStringifier.toString( o ) );
302     }
303     
304         protected final void
305     trace( Object JavaDoc o )
306     {
307         debug( o );
308     }
309     
310         protected final void
311     logSevere( Object JavaDoc o )
312     {
313         final String JavaDoc msg = toString( o );
314         debug( msg );
315         
316         if ( getMBeanLogLevelInt() <= Level.SEVERE.intValue() )
317         {
318             getMBeanLogger().severe( msg );
319         }
320     }
321     
322         protected final void
323     logWarning( Object JavaDoc o )
324     {
325         final String JavaDoc msg = toString( o );
326         debug( msg );
327         
328         if ( getMBeanLogLevelInt() <= Level.WARNING.intValue() )
329         {
330             getMBeanLogger().warning( msg );
331         }
332     }
333     
334         protected final void
335     logInfo( Object JavaDoc o )
336     {
337         final String JavaDoc msg = toString( o );
338         debug( msg );
339         
340         if ( getMBeanLogLevelInt() <= Level.INFO.intValue() )
341         {
342             getMBeanLogger().info( msg );
343         }
344     }
345     
346         protected final void
347     logFine( Object JavaDoc o )
348     {
349         final String JavaDoc msg = toString( o );
350         debug( msg );
351         
352         if ( getMBeanLogLevelInt() <= Level.FINE.intValue() )
353         {
354             getMBeanLogger().fine( msg );
355         }
356     }
357     
358         protected final void
359     logFiner( Object JavaDoc o )
360     {
361         final String JavaDoc msg = toString( o );
362         debug( msg );
363         
364         if ( getMBeanLogLevelInt() <= Level.FINER.intValue() )
365         {
366             getMBeanLogger().finer( msg );
367         }
368     }
369     
370         protected final void
371     logFinest( Object JavaDoc o )
372     {
373         final String JavaDoc msg = toString( o );
374         debug( msg );
375         
376         if ( getMBeanLogLevelInt() <= Level.FINEST.intValue() )
377         {
378             getMBeanLogger().finest( msg );
379         }
380     }
381     
382         protected final Logger JavaDoc
383     getMBeanLogger()
384     {
385         return mLogger;
386     }
387     
388     
389         public final Level JavaDoc
390     getMBeanLogLevel()
391     {
392         Logger JavaDoc logger = getMBeanLogger();
393         assert( logger != null );
394         
395         Level JavaDoc level = logger.getLevel();
396         while ( level == null )
397         {
398             logger = logger.getParent();
399             level = logger.getLevel();
400         }
401         
402         return( level );
403     }
404     
405     
406         public final void
407     setMBeanLogLevel( final Level JavaDoc level )
408     {
409         getMBeanLogger().setLevel( level );
410     }
411     
412         protected final int
413     getMBeanLogLevelInt()
414     {
415         return( getMBeanLogLevel().intValue() );
416     }
417     
418         public final String JavaDoc
419     getMBeanLoggerName()
420     {
421         return( getMBeanLogger().getName() );
422     }
423     
424     private static final Level JavaDoc[] LOG_LEVELS =
425     {
426         Level.ALL,
427         Level.SEVERE,
428         Level.WARNING,
429         Level.INFO,
430         Level.FINE,
431         Level.FINER,
432         Level.FINEST,
433         Level.OFF,
434     };
435     
436         public final void
437     setMBeanLogLevelString( final String JavaDoc levelString )
438     {
439         Level JavaDoc level = null;
440         for( int i = 0; i < LOG_LEVELS.length; ++i )
441         {
442             if ( levelString.equals( LOG_LEVELS[ i ].getName() ) )
443             {
444                 level = LOG_LEVELS[ i ];
445                 break;
446             }
447         }
448         
449         if ( level == null )
450         {
451             throw new IllegalArgumentException JavaDoc( levelString );
452         }
453
454         setMBeanLogLevel( level );
455     }
456     
457         protected static String JavaDoc
458     quote( final Object JavaDoc o )
459     {
460         return( StringUtil.quote( "" + o ) );
461     }
462     
463         public ObjectName JavaDoc
464     preRegister(
465         final MBeanServer JavaDoc server,
466         final ObjectName JavaDoc nameIn)
467         throws Exception JavaDoc
468     {
469         assert( nameIn != null );
470         mServer = server;
471         mSelfObjectName = nameIn;
472         
473         // ObjectName could still be modified by subclass
474
return( mSelfObjectName );
475     }
476     
477         public void
478     postRegister( Boolean JavaDoc registrationDone )
479     {
480         if ( registrationDone.booleanValue() )
481         {
482             getMBeanLogger().finest( "postRegister: " + getObjectName());
483         }
484         else
485         {
486             getMBeanLogger().finest( "postRegister: FAILURE: " + getObjectName());
487         }
488     }
489     
490         public void
491     preDeregister()
492         throws Exception JavaDoc
493     {
494         getMBeanLogger().finest( "preDeregister: " + getObjectName() );
495     }
496     
497         public void
498     postDeregister()
499     {
500         getMBeanLogger().finest( "postDeregister: " + getObjectName() );
501         
502         if ( mNotificationEmitter != null )
503         {
504             mNotificationEmitter.cleanup();
505             mNotificationEmitter = null;
506         }
507         
508         if ( mNotificationBuilders != null )
509         {
510             mNotificationBuilders.clear();
511             mNotificationBuilders = null;
512         }
513         
514         
515         mServer = null;
516         mSelfObjectName = null;
517         
518     }
519     
520         protected String JavaDoc
521     getDebugID()
522     {
523         return this.getClass().getName();
524     }
525     
526     
527     /**
528         Although unusual, a subclass may override the debug state.
529         Generally it is faster to NOT call getAMXDebug() before calling
530         debug( x ) if 'x' is just a string. If it is expensive to
531         construct 'x', then preflighting with getAMXDebug() may
532         be worthwhile.
533       */

534         public final boolean
535     getAMXDebug()
536     {
537         return AMXDebug.getInstance().getDebug( getDebugID() );
538     }
539     
540         public boolean
541     enableAMXDebug( final boolean enabled )
542     {
543         final boolean formerValue = getAMXDebug();
544         if ( formerValue != enabled )
545         {
546             setAMXDebug( enabled );
547         }
548         return formerValue;
549     }
550     
551         protected Output
552     getDebugOutput()
553     {
554         return AMXDebug.getInstance().getOutput( getDebugID() );
555     }
556     
557         public final void
558     setAMXDebug( final boolean debug )
559     {
560         AMXDebug.getInstance().setDebug( getDebugID(), debug);
561     }
562     
563         protected boolean
564     shouldOmitObjectNameForDebug()
565     {
566         return mSelfObjectName == null;
567     }
568     
569         protected final void
570     debug(final Object JavaDoc o)
571     {
572         if ( getAMXDebug() )
573         {
574             final String JavaDoc newline = System.getProperty( "line.separator" );
575             
576             if ( shouldOmitObjectNameForDebug() )
577             {
578                 mDebug.println( o );
579             }
580             else
581             {
582                 mDebug.println( mSelfObjectName.toString() );
583                 mDebug.println( "===> " + o );
584             }
585             mDebug.println( newline );
586         }
587     }
588     
589         protected void
590     debugMethod( final String JavaDoc methodName, final Object JavaDoc... args )
591     {
592         if ( getAMXDebug() )
593         {
594             debug( AMXDebug.methodString( methodName, args ) );
595         }
596     }
597     
598         protected void
599     debugMethod(
600         final String JavaDoc msg,
601         final String JavaDoc methodName,
602         final Object JavaDoc... args )
603     {
604         if ( getAMXDebug() )
605         {
606             debug( AMXDebug.methodString( methodName, args ) + ": " + msg );
607         }
608     }
609     
610         protected void
611     debug( final Object JavaDoc... args )
612     {
613         if ( getAMXDebug() )
614         {
615             debug( StringUtil.toString( "", args) );
616         }
617     }
618     
619     
620         protected boolean
621     sleepMillis( final long millis )
622     {
623         boolean interrupted = false;
624         
625         try
626         {
627             Thread.sleep( millis );
628         }
629         catch( InterruptedException JavaDoc e )
630         {
631             Thread.interrupted();
632             interrupted = true;
633         }
634         
635         return interrupted;
636     }
637 }
638
639
640
641
642
643
644
645
646
Popular Tags