KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > ext > logging > LoggingImpl


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.ext.logging;
25
26 import java.util.Map JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.Date JavaDoc;
33 import java.util.Properties JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.logging.LogRecord JavaDoc;
36 import java.util.logging.Formatter JavaDoc;
37
38 import java.io.File JavaDoc;
39 import java.io.FileNotFoundException JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.io.Serializable JavaDoc;
42 import java.io.PrintStream JavaDoc;
43 import java.io.File JavaDoc;
44
45
46 import javax.management.MBeanServer JavaDoc;
47 import javax.management.Attribute JavaDoc;
48 import javax.management.AttributeList JavaDoc;
49 import javax.management.MBeanInfo JavaDoc;
50 import javax.management.MBeanOperationInfo JavaDoc;
51 import javax.management.MBeanNotificationInfo JavaDoc;
52 import javax.management.NotificationListener JavaDoc;
53 import javax.management.NotificationFilter JavaDoc;
54 import javax.management.Notification JavaDoc;
55 import javax.management.NotificationEmitter JavaDoc;
56 import javax.management.ObjectName JavaDoc;
57 import javax.management.MBeanServerInvocationHandler JavaDoc;
58
59 import com.sun.appserv.management.base.AMX;
60 import com.sun.appserv.management.base.Util;
61 import com.sun.appserv.management.base.XTypes;
62 import com.sun.enterprise.management.support.AMXImplBase;
63 import com.sun.enterprise.management.support.BootUtil;
64 import com.sun.enterprise.management.support.AMXImplBase;
65
66 import com.sun.appserv.management.ext.logging.Logging;
67 import static com.sun.appserv.management.ext.logging.Logging.*;
68 import com.sun.appserv.management.ext.logging.LogQueryResult;
69 import com.sun.appserv.management.ext.logging.LogQueryResultImpl;
70 import com.sun.appserv.management.util.misc.ListUtil;
71 import com.sun.appserv.management.util.misc.MapUtil;
72 import com.sun.appserv.management.util.misc.GSetUtil;
73 import com.sun.appserv.management.util.misc.FileUtils;
74 import com.sun.appserv.management.util.misc.ThrowableMapper;
75 import com.sun.appserv.management.util.misc.ExceptionUtil;
76 import com.sun.appserv.management.util.misc.TypeCast;
77
78 import com.sun.appserv.management.util.jmx.NotificationBuilder;
79 import com.sun.appserv.management.util.jmx.JMXUtil;
80 import com.sun.appserv.management.util.jmx.NotificationEmitterSupport;
81
82 import com.sun.enterprise.server.logging.LoggingImplHook;
83
84 /**
85     Implementation of {@link Logging}.
86     <p>
87     AMX Logging MBean is hooked directly into the logging subsystem
88     via com.sun.enterprise.server.logging.FileandSyslogHandler which uses
89     com.sun.enterprise.server.logging.AMXLoggingHook to instantiate
90     and call an instance of LoggingImpl.
91  */

92 public final class LoggingImpl extends AMXImplBase
93      implements /*Logging,*/ LoggingImplHook
94 {
95     private LogMBeanIntf mLogMBean;
96     private final Map JavaDoc<Level JavaDoc,String JavaDoc> mLevelToNotificationTypeMap;
97     private final Map JavaDoc<String JavaDoc,NotificationBuilder> mNotificationTypeToNotificationBuilderMap;
98     
99     private static final String JavaDoc SERVER_LOG_NAME = "server.log";
100     private static final String JavaDoc ACCESS_LOG_NAME = "access.log";
101
102     final String JavaDoc FILE_SEP;
103     
104     private final String JavaDoc mServerName;
105     
106     /**
107        Used internally to get the Logging ObjectName for a particular server
108        Logging MBean is a special-case because it needs to load as early
109        as possible.
110      */

111         public static ObjectName JavaDoc
112     getObjectName( final String JavaDoc serverName )
113     {
114         final String JavaDoc requiredProps = Util.makeRequiredProps( XTypes.LOGGING, serverName );
115         final String JavaDoc parentProp = Util.makeProp( XTypes.SERVER_ROOT_MONITOR, serverName );
116         final String JavaDoc props = Util.concatenateProps( requiredProps, parentProp );
117         
118         return Util.newObjectName( AMX.JMX_DOMAIN, props );
119     }
120
121     private final static String JavaDoc LOGMBEAN_OBJECT_NAME_PREFIX =
122         "com.sun.appserv:name=logmanager,category=runtime,server=";
123     
124     /**
125      */

126     public LoggingImpl( final String JavaDoc serverName )
127     {
128         mServerName = serverName;
129         mLogMBean = null;
130         FILE_SEP = System.getProperty( "file.separator" );
131         
132         mLevelToNotificationTypeMap = initLevelToNotificationTypeMap();
133         mNotificationTypeToNotificationBuilderMap = new HashMap JavaDoc<String JavaDoc,NotificationBuilder>();
134     }
135     
136     
137     /**
138         Hook for subclass to modify anything in MBeanInfo.
139         @Override
140      */

141         protected MBeanInfo JavaDoc
142     modifyMBeanInfo( final MBeanInfo JavaDoc info )
143     {
144         final MBeanOperationInfo JavaDoc[] ops = info.getOperations();
145         
146         final int idx = JMXUtil.findMBeanOperationInfo( info, "queryServerLog", null);
147         
148         final MBeanOperationInfo JavaDoc op = ops[idx];
149         ops[idx] = new MBeanOperationInfo JavaDoc( op.getName(), op.getDescription(),
150                         op.getSignature(), Map JavaDoc.class.getName(),
151                         MBeanOperationInfo.INFO );
152         
153         return JMXUtil.newMBeanInfo( info, ops );
154     }
155     
156     
157     
158     private static MBeanNotificationInfo JavaDoc[] SELF_NOTIFICATION_INFOS = null;
159     /**
160         getMBeanInfo() can be called frequently. By making this static,
161         we avoid needlessly creating new Objects.
162      */

163          private static synchronized MBeanNotificationInfo JavaDoc[]
164     getSelfNotificationInfos()
165     {
166         if ( SELF_NOTIFICATION_INFOS == null )
167         {
168             final String JavaDoc[] types = GSetUtil.toStringArray( ALL_LOG_RECORD_NOTIFICATION_TYPES );
169             final MBeanNotificationInfo JavaDoc selfInfo = new MBeanNotificationInfo JavaDoc(
170                 types, Notification JavaDoc.class.getName(), "LogRecord notifications" );
171             
172             SELF_NOTIFICATION_INFOS = new MBeanNotificationInfo JavaDoc[] { selfInfo };
173         }
174         return( SELF_NOTIFICATION_INFOS );
175     }
176     
177         public MBeanNotificationInfo JavaDoc[]
178     getNotificationInfo()
179     {
180         final MBeanNotificationInfo JavaDoc[] superInfos = super.getNotificationInfo();
181         
182         final MBeanNotificationInfo JavaDoc[] all =
183             JMXUtil.mergeMBeanNotificationInfos( superInfos, getSelfNotificationInfos() );
184         
185         return all;
186     }
187     
188     
189         private Object JavaDoc
190     newProxy(
191         final ObjectName JavaDoc target,
192         final Class JavaDoc interfaceClass )
193     {
194         return( MBeanServerInvocationHandler.newProxyInstance(
195                     getMBeanServer(), target, interfaceClass, true ) );
196     }
197
198         public String JavaDoc
199     getGroup()
200     {
201         return( AMX.GROUP_MONITORING );
202     }
203     
204         private LogMBeanIntf
205     getLogMBean()
206     {
207         initLogMBean();
208         return mLogMBean;
209     }
210
211         private void
212     initLogMBean()
213     {
214         if ( mLogMBean == null )
215          synchronized( this )
216         {
217             if ( mLogMBean == null )
218             {
219                 final ObjectName JavaDoc logMBeanObjectName =
220                 Util.newObjectName( LOGMBEAN_OBJECT_NAME_PREFIX + mServerName );
221                 
222                 mLogMBean = (LogMBeanIntf)newProxy( logMBeanObjectName, LogMBeanIntf.class );
223             }
224         }
225     }
226
227         protected synchronized ObjectName JavaDoc
228     getContainerObjectName( final ObjectName JavaDoc selfObjectName )
229     {
230         ObjectName JavaDoc containerObjectName = null;
231         
232         // work is needed to flesh out the hierarchy in non-DAS server instances.
233
// return null for now if that hierarchy is missing
234
try
235         {
236             containerObjectName = super.getContainerObjectName( selfObjectName );
237         }
238         catch( Exception JavaDoc e )
239         {
240             // can occur for non-DAS instances
241
containerObjectName = null;
242         }
243         return containerObjectName;
244     }
245
246
247         public void
248     setModuleLogLevel(
249         final String JavaDoc module,
250         final String JavaDoc level )
251     {
252         getLogMBean().setLogLevel( module, level );
253     }
254
255         public String JavaDoc
256     getModuleLogLevel( final String JavaDoc module)
257     {
258         return getLogMBean().getLogLevel( module );
259     }
260
261         public int
262     getLogLevelListenerCount( final Level JavaDoc logLevel )
263     {
264         final String JavaDoc notifType = logLevelToNotificationType( logLevel );
265         
266         final int count = getNotificationEmitter().getNotificationTypeListenerCount( notifType );
267         return( count );
268     }
269
270         public String JavaDoc[]
271     getLogFileKeys()
272     {
273         return new String JavaDoc[] { SERVER_KEY, ACCESS_KEY };
274     }
275
276
277         public synchronized String JavaDoc[]
278     getLogFileNames( final String JavaDoc key )
279     {
280         String JavaDoc[] result = null;
281         
282         if ( SERVER_KEY.equals( key ) )
283         {
284             result = getLogMBean().getArchivedLogfiles();
285         }
286         else
287         {
288             throw new IllegalArgumentException JavaDoc( key );
289         }
290         
291         return result;
292     }
293
294
295         public synchronized String JavaDoc
296     getLogFile( final String JavaDoc key, final String JavaDoc fileName )
297     {
298         if ( ! SERVER_KEY.equals( key ) )
299         {
300             throw new IllegalArgumentException JavaDoc( "" + key );
301         }
302         
303         final String JavaDoc dir = getLogMBean().getLogFilesDirectory();
304         final String JavaDoc file = dir + FILE_SEP + fileName;
305         
306         try
307         {
308              return FileUtils.fileToString( new File JavaDoc( file ) );
309         }
310         catch( FileNotFoundException JavaDoc e )
311         {
312             throw new RuntimeException JavaDoc( e );
313         }
314         catch( IOException JavaDoc e )
315         {
316             throw new RuntimeException JavaDoc( e );
317         }
318     }
319
320
321         public synchronized void
322     rotateAllLogFiles()
323     {
324         getLogMBean().rotateNow( );
325     }
326
327
328
329         public synchronized void
330     rotateLogFile( final String JavaDoc key )
331     {
332         if ( ACCESS_KEY.equals( key ) )
333         {
334             throw new IllegalArgumentException JavaDoc( "not supported: " + key );
335             // getLogMBean().rotateAccessLog();
336
}
337         else if ( SERVER_KEY.equals( key ) )
338         {
339             rotateAllLogFiles();
340         }
341         else
342         {
343             throw new IllegalArgumentException JavaDoc( "" + key );
344         }
345     }
346
347
348
349
350         private Properties JavaDoc
351     attributesToProps( List JavaDoc<Attribute JavaDoc> attrs )
352     {
353         final Properties JavaDoc props = new Properties JavaDoc();
354         
355         if ( attrs != null )
356         {
357             for( Attribute JavaDoc attr: attrs)
358             {
359                 final Object JavaDoc value = attr.getValue();
360                 if ( value == null )
361                 {
362                     throw new IllegalArgumentException JavaDoc( attr.getName() + "=" + null);
363                 }
364                 
365                 props.put( attr.getName(), value.toString() );
366             }
367         }
368         
369         return( props );
370     }
371
372         private List JavaDoc<Serializable JavaDoc[]>
373     convertQueryResult( final AttributeList JavaDoc queryResult )
374     {
375         // extract field descriptions into a String[]
376
final AttributeList JavaDoc fieldAttrs = (AttributeList JavaDoc)((Attribute JavaDoc)queryResult.get( 0 )).getValue();
377         final String JavaDoc[] fieldHeaders = new String JavaDoc[ fieldAttrs.size() ];
378         for( int i = 0; i < fieldHeaders.length; ++i )
379         {
380             final Attribute JavaDoc attr = (Attribute JavaDoc)fieldAttrs.get( i );
381             fieldHeaders[ i ] = (String JavaDoc)attr.getValue();
382         }
383         
384         final List JavaDoc<List JavaDoc<Serializable JavaDoc>> srcRecords = TypeCast.asList(
385                 ((Attribute JavaDoc)queryResult.get( 1 )).getValue() );
386         
387         // create the new results, making the first Object[] be the field headers
388
final List JavaDoc<Serializable JavaDoc[]> results = new ArrayList JavaDoc<Serializable JavaDoc[]>( srcRecords.size() );
389         results.add( fieldHeaders );
390         
391         // extract every record
392
for( int recordIdx = 0; recordIdx < srcRecords.size(); ++recordIdx )
393         {
394             final List JavaDoc<Serializable JavaDoc> record = srcRecords.get( recordIdx );
395             
396             assert( record.size() == fieldHeaders.length );
397             final Serializable JavaDoc[] fieldValues = new Serializable JavaDoc[ fieldHeaders.length ];
398             for( int fieldIdx = 0; fieldIdx < fieldValues.length; ++fieldIdx )
399             {
400                 fieldValues[ fieldIdx ] = record.get( fieldIdx );
401             }
402             
403             results.add( fieldValues );
404         }
405         
406         return results;
407     }
408
409         public List JavaDoc<Serializable JavaDoc[]>
410     queryServerLog(
411         String JavaDoc name,
412         long startIndex,
413         boolean searchForward,
414         int maximumNumberOfResults,
415         Long JavaDoc fromTime,
416         Long JavaDoc toTime,
417         String JavaDoc logLevel,
418         Set JavaDoc<String JavaDoc> modules,
419         List JavaDoc<Attribute JavaDoc> nameValuePairs)
420     {
421         final List JavaDoc<Serializable JavaDoc[]> result = queryServerLogInternal(
422                  name, startIndex, searchForward, maximumNumberOfResults,
423                  fromTime, toTime, logLevel, modules, nameValuePairs );
424         return result;
425     }
426
427         private List JavaDoc<Serializable JavaDoc[]>
428     queryServerLogInternal(
429         final String JavaDoc name,
430         final long startIndex,
431         final boolean searchForward,
432         final int maximumNumberOfResults,
433         final Long JavaDoc fromTime,
434         final Long JavaDoc toTime,
435         final String JavaDoc logLevel,
436         final Set JavaDoc<String JavaDoc> modules,
437         final List JavaDoc<Attribute JavaDoc> nameValuePairs)
438     {
439         if ( name == null )
440         {
441             throw new IllegalArgumentException JavaDoc( "use MOST_RECENT_NAME, not null" );
442         }
443         
444         final boolean sortAscending = true;
445         final List JavaDoc<String JavaDoc> moduleList = ListUtil.newListFromCollection( modules );
446         final Properties JavaDoc props = attributesToProps( nameValuePairs );
447         
448         String JavaDoc actualName;
449         if ( MOST_RECENT_NAME.equals( name ) )
450         {
451             actualName = null;
452         }
453         else
454         {
455             actualName = name;
456         }
457         final AttributeList JavaDoc result = getLogMBean().getLogRecordsUsingQuery(
458             actualName,
459             new Long JavaDoc( startIndex ),
460             searchForward,
461             sortAscending,
462             maximumNumberOfResults,
463             fromTime == null ? null : new Date JavaDoc( fromTime ),
464             toTime == null ? null : new Date JavaDoc( toTime ),
465             logLevel,
466             true,
467             moduleList,
468             props) ;
469             
470         return convertQueryResult( result );
471     }
472
473         public Map JavaDoc<String JavaDoc,Number JavaDoc>[]
474     getErrorInfo()
475     {
476         final List JavaDoc<Map JavaDoc<String JavaDoc,Object JavaDoc>> infos = getLogMBean().getErrorInformation();
477         
478         final Map JavaDoc<String JavaDoc,Number JavaDoc>[] results = TypeCast.asArray( new HashMap JavaDoc[ infos.size() ] );
479         
480         for( int i = 0; i < results.length; ++i )
481         {
482             final Map JavaDoc<String JavaDoc,Object JavaDoc> info = infos.get( i );
483             
484             assert( info.keySet().size() == 3 );
485             
486             final Long JavaDoc timestamp = Long.parseLong( info.get( TIMESTAMP_KEY ).toString() );
487             final Integer JavaDoc severeCount = Integer.parseInt( info.get( SEVERE_COUNT_KEY ).toString() );
488             final Integer JavaDoc warningCount= Integer.parseInt( info.get( WARNING_COUNT_KEY ).toString() );
489             
490             final Map JavaDoc<String JavaDoc,Number JavaDoc> item = new HashMap JavaDoc<String JavaDoc,Number JavaDoc>( info.size() );
491             item.put( TIMESTAMP_KEY, timestamp);
492             item.put( SEVERE_COUNT_KEY, severeCount);
493             item.put( WARNING_COUNT_KEY, warningCount);
494             
495             results[ i ] = item;
496         }
497         
498         return results;
499     }
500
501
502     private static final Integer JavaDoc INTEGER_0 = new Integer JavaDoc( 0 );
503     
504     private static final Map JavaDoc<String JavaDoc,Integer JavaDoc> EMPTY_ERROR_DISTRIBUTION_MAP =
505         Collections.emptyMap();
506
507     private static final Set JavaDoc<String JavaDoc> LEGAL_DISTRIBUTION_LEVELS =
508             GSetUtil.newUnmodifiableStringSet(
509                 Level.SEVERE.toString(), Level.WARNING.toString() );
510         
511     
512    
513         public Map JavaDoc<String JavaDoc,Integer JavaDoc>
514     getErrorDistribution(long timestamp, String JavaDoc level)
515     {
516         if ( ! LEGAL_DISTRIBUTION_LEVELS.contains( level ) )
517         {
518             throw new IllegalArgumentException JavaDoc( level );
519         }
520         
521         Map JavaDoc<String JavaDoc,Integer JavaDoc> result =
522             getLogMBean().getErrorDistribution( timestamp, Level.parse( level ) );
523         
524         // query may return null instead of an empty Map
525
if ( result != null )
526         {
527             final Set JavaDoc<String JavaDoc> moduleIDs = result.keySet();
528             
529              // Ensure that no module has a null count
530
for( final String JavaDoc moduleID : moduleIDs )
531             {
532                 if ( result.get( moduleID ) == null )
533                 {
534                     result.put( moduleID, INTEGER_0 );
535                 }
536             }
537         }
538         else
539         {
540             // never return a null Map, only an empty one
541
result = EMPTY_ERROR_DISTRIBUTION_MAP;
542         }
543             
544         return result;
545     }
546     
547     
548         public void
549     setKeepErrorStatisticsForIntervals( final int num)
550     {
551         getLogMBean().setKeepErrorStatisticsForIntervals( num );
552     }
553     
554         public int
555     getKeepErrorStatisticsForIntervals()
556     {
557         return getLogMBean().getKeepErrorStatisticsForIntervals();
558     }
559
560         public void
561     setErrorStatisticsIntervalMinutes(final long minutes)
562     {
563         getLogMBean().setErrorStatisticsIntervalDuration( minutes );
564     }
565     
566         public long
567     getErrorStatisticsIntervalMinutes()
568     {
569         return getLogMBean().getErrorStatisticsIntervalDuration();
570     }
571     
572         public String JavaDoc[]
573     getLoggerNames()
574     {
575         final List JavaDoc<String JavaDoc> names =
576             TypeCast.checkList( getLogMBean().getLoggerNames(), String JavaDoc.class );
577         
578         return names.toArray( EMPTY_STRING_ARRAY );
579     }
580     
581         public String JavaDoc[]
582     getLoggerNamesUnder( final String JavaDoc loggerName )
583     {
584         final List JavaDoc<String JavaDoc> names = TypeCast.checkList(
585             getLogMBean().getLoggerNamesUnder( loggerName ), String JavaDoc.class );
586         
587         return names.toArray( EMPTY_STRING_ARRAY );
588     }
589     
590
591
592         public String JavaDoc[]
593     getDiagnosticCauses( final String JavaDoc messageID )
594     {
595         final List JavaDoc<String JavaDoc> causes = TypeCast.checkList(
596             getLogMBean().getDiagnosticCausesForMessageId( messageID ), String JavaDoc.class );
597         
598         String JavaDoc[] result = null;
599         if ( causes != null )
600         {
601             result = (String JavaDoc[])causes.toArray( new String JavaDoc[causes.size()] );
602         }
603         
604         return result;
605     }
606
607         public String JavaDoc[]
608     getDiagnosticChecks( final String JavaDoc messageID )
609     {
610         final List JavaDoc<String JavaDoc> checks = TypeCast.checkList(
611             getLogMBean().getDiagnosticChecksForMessageId( messageID ), String JavaDoc.class );
612         
613         String JavaDoc[] result = null;
614         if ( checks != null )
615         {
616             result = new String JavaDoc[checks.size()];
617             checks.toArray( result );
618         }
619                 
620         return result;
621     }
622
623         public String JavaDoc
624     getDiagnosticURI( final String JavaDoc messageID )
625     {
626         return getLogMBean().getDiagnosticURIForMessageId( messageID );
627     }
628
629
630     private static final Object JavaDoc[]
631     LEVELS_AND_NOTIF_TYPES = new Object JavaDoc[]
632     {
633         Level.SEVERE, LOG_RECORD_SEVERE_NOTIFICATION_TYPE,
634         Level.WARNING, LOG_RECORD_WARNING_NOTIFICATION_TYPE,
635         Level.INFO, LOG_RECORD_INFO_NOTIFICATION_TYPE,
636         Level.CONFIG, LOG_RECORD_CONFIG_NOTIFICATION_TYPE,
637         Level.FINE, LOG_RECORD_FINE_NOTIFICATION_TYPE,
638         Level.FINER, LOG_RECORD_FINER_NOTIFICATION_TYPE,
639         Level.FINEST, LOG_RECORD_FINEST_NOTIFICATION_TYPE,
640     };
641     
642         private static Map JavaDoc<Level JavaDoc,String JavaDoc>
643     initLevelToNotificationTypeMap()
644     {
645         final Map JavaDoc<Level JavaDoc,String JavaDoc> m = new HashMap JavaDoc<Level JavaDoc,String JavaDoc>();
646
647         for( int i = 0; i < LEVELS_AND_NOTIF_TYPES.length; i += 2 )
648         {
649             final Level JavaDoc level = (Level JavaDoc)LEVELS_AND_NOTIF_TYPES[ i ];
650             final String JavaDoc notifType = (String JavaDoc)LEVELS_AND_NOTIF_TYPES[ i + 1 ];
651             m.put( level, notifType );
652         }
653
654         return( Collections.unmodifiableMap( m ) );
655     }
656
657         private String JavaDoc
658     logLevelToNotificationType( final Level JavaDoc level )
659     {
660         String JavaDoc notificationType = mLevelToNotificationTypeMap.get( level );
661
662         if ( notificationType == null )
663         {
664         }
665
666         return notificationType;
667     }
668
669
670         protected void
671     preRegisterDone()
672         throws Exception JavaDoc
673     {
674         initNotificationTypeToNotificationBuilderMap( getObjectName() );
675     }
676
677         private void
678     initNotificationTypeToNotificationBuilderMap( final ObjectName JavaDoc objectName )
679     {
680         mNotificationTypeToNotificationBuilderMap.clear();
681         for( final String JavaDoc notifType : ALL_LOG_RECORD_NOTIFICATION_TYPES )
682         {
683             mNotificationTypeToNotificationBuilderMap.put(
684                 notifType,
685                 new NotificationBuilder( notifType, objectName ) );
686         }
687     }
688     
689     
690         private NotificationBuilder
691     notificationTypeToNotificationBuilder( final String JavaDoc notificationType )
692     {
693         NotificationBuilder builder =
694             mNotificationTypeToNotificationBuilderMap.get( notificationType );
695             
696         assert( builder != null );
697         
698         return builder;
699     }
700
701
702         private Map JavaDoc<String JavaDoc,Serializable JavaDoc>
703     logRecordToMap(
704         final LogRecord JavaDoc record,
705         final String JavaDoc recordAsString )
706     {
707         final Map JavaDoc<String JavaDoc,Serializable JavaDoc> m = new HashMap JavaDoc<String JavaDoc,Serializable JavaDoc>();
708         
709         m.put( LOG_RECORD_AS_STRING_KEY, recordAsString );
710         m.put( LOG_RECORD_LEVEL_KEY, record.getLevel() );
711         m.put( LOG_RECORD_LOGGER_NAME_KEY, record.getLoggerName() );
712         m.put( LOG_RECORD_MESSAGE_KEY, record.getMessage() );
713         m.put( LOG_RECORD_MILLIS_KEY, record.getMillis() );
714         m.put( LOG_RECORD_SEQUENCE_NUMBER_KEY, record.getSequenceNumber() );
715         m.put( LOG_RECORD_SOURCE_CLASS_NAME_KEY, record.getSourceClassName() );
716         m.put( LOG_RECORD_SOURCE_METHOD_NAME_KEY, record.getSourceMethodName() );
717         m.put( LOG_RECORD_THREAD_ID_KEY, record.getThreadID() );
718         final Throwable JavaDoc thrown = record.getThrown();
719         if ( thrown != null )
720         {
721             final Throwable JavaDoc mapped = new ThrowableMapper( thrown ).map();
722             m.put( LOG_RECORD_THROWN_KEY, mapped );
723             
724             final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( thrown );
725             if ( rootCause != thrown )
726             {
727                 final Throwable JavaDoc mappedRootCause = new ThrowableMapper( rootCause ).map();
728                 m.put( LOG_RECORD_ROOT_CAUSE_KEY, mappedRootCause );
729             }
730         }
731         return m;
732     }
733     
734     
735     private long mMyThreadID = -1;
736     /**
737         Internal use only, called by com.sun.enterprise.server.logging.AMXLoggingHook.
738      */

739         public void
740     privateLoggingHook(
741         final LogRecord JavaDoc logRecord,
742         final Formatter JavaDoc formatter )
743     {
744         //debug( "LoggingImpl.privateLoggingHook: " + formatter.format( logRecord ) );
745

746         if ( logRecord.getThreadID() == mMyThreadID )
747         {
748             debug( "privateLoggingHook: recusive call!!!" );
749             throw new RuntimeException JavaDoc( "recursive call" );
750         }
751         synchronized( this )
752         {
753             mMyThreadID = Thread.currentThread().getId();
754             
755             final Level JavaDoc level = logRecord.getLevel();
756             
757             try
758             {
759                 // don't construct a Notification if there are no listeners.
760
if ( getLogLevelListenerCount( level ) != 0 )
761                 {
762                     final String JavaDoc notifType = logLevelToNotificationType( level );
763                     
764                     final NotificationBuilder builder =
765                         notificationTypeToNotificationBuilder( notifType );
766                     
767                     // Notification.getMessage() will be the formatted log record
768
final String JavaDoc logRecordAsString = formatter.format( logRecord );
769                     
770                     final Map JavaDoc<String JavaDoc,Serializable JavaDoc> userData =
771                         logRecordToMap( logRecord, logRecordAsString );
772                         
773                     final Notification JavaDoc notif =
774                         builder.buildNewWithMap( logRecordAsString, userData);
775                         
776                     debug( "privateLoggingHook: sending: " + notif );
777                     sendNotification( notif );
778                 }
779                 else
780                 {
781                    // debug( "privateLogHook: no listeners for level " + level );
782
}
783             }
784             finally
785             {
786                 mMyThreadID = -1;
787             }
788         }
789     }
790
791         public void
792     testEmitLogMessage( final String JavaDoc level, final String JavaDoc message )
793     {
794         final Level JavaDoc saveLevel = getMBeanLogLevel();
795         
796         setMBeanLogLevel( Level.parse( level ) );
797         try
798         {
799             debug( "testEmitLogMessage: logging: message = " + message );
800             getLogger().log( Level.parse( level ), message );
801         }
802         finally
803         {
804             setMBeanLogLevel( saveLevel );
805         }
806     }
807     
808     /**
809         keep for debugging
810     private static String cn( Object o ) { return o == null ? "null" : o.getClass().getName(); }
811         public synchronized void
812     addNotificationListener(
813         final NotificationListener listener )
814     {
815         super.addNotificationListener( listener );
816         debug( "LoggingImpl.addNotificationListener: class = " + cn(listener) );
817     }
818     
819         public synchronized void
820     addNotificationListener(
821         final NotificationListener listener,
822         final NotificationFilter filter,
823         final Object handback)
824     {
825         super.addNotificationListener( listener, filter, handback );
826         debug( "LoggingImpl.addNotificationListener: class = " + cn(listener) +
827             ", filter = " + cn(filter) + ", handback = " + cn(handback) );
828     }
829
830         public synchronized void
831     removeNotificationListener( final NotificationListener listener)
832         throws javax.management.ListenerNotFoundException
833     {
834         super.removeNotificationListener( listener );
835         debug( "LoggingImpl.removeNotificationListener: class = " + cn(listener) );
836     }
837  
838         public synchronized void
839     removeNotificationListener(
840         final NotificationListener listener,
841         final NotificationFilter filter,
842         final Object handback)
843         throws javax.management.ListenerNotFoundException
844     {
845         super.removeNotificationListener( listener, filter, handback );
846         debug( "LoggingImpl.removeNotificationListener: class = " + cn(listener) +
847             ", filter = " + cn(filter) + ", handback = " + cn(handback) );
848     }
849     */

850 }
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
Popular Tags