KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > server > logging > AMXLoggingHook


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.server.logging;
24
25 import java.util.Set JavaDoc;
26
27 import java.util.logging.LogRecord JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Formatter JavaDoc;
30
31 import javax.management.ObjectName JavaDoc;
32 import javax.management.MBeanServer JavaDoc;
33
34 import java.lang.reflect.Method JavaDoc;
35 import java.lang.reflect.Constructor JavaDoc;
36
37 /**
38     Hook used for AMX logging by FileandSysLogHandler.
39     <b>This class assumes that the caller is holding a lock that
40     will prevent more than one thread from calling {@link #publish}.</b>
41  */

42 final class AMXLoggingHook // don't make this class public
43
{
44     private ObjectName JavaDoc mLoggingObjectName;
45     private MBeanServer JavaDoc mMBeanServer;
46     private boolean mLoggingAvailable;
47     private LoggingImplHook mLoggingImplHook;
48     
49     /**
50         The minimum log level for which LogRecords will be passed
51         to the AMX Logging MBean. Generally speaking, all LogRecords
52         should be passed along, letting the AMX Logging MBean decide
53         their disposition.
54      */

55     private Level JavaDoc mMinimumLogLevel;
56     
57    // /** signature for LOGGING_HOOK_METHOD_NAME method */
58
// private static final String[] LOGGING_HOOK_SIG = new String[]
59
// { LogRecord.class.getName(), Formatter.class.getName() };
60

61    // /** name of the operation called on the AMX Logging MBean */
62
// private static final String LOGGING_HOOK_NAME = "privateLoggingHook";
63

64     private static final boolean DEBUG = false;
65     private final Output mOut;
66     private final String JavaDoc mServerName;
67     
68         private void
69     dumpSystemProps( final Output output )
70     {
71         final java.util.Properties JavaDoc props = System.getProperties();
72         
73         final String JavaDoc[] keys = (String JavaDoc[])props.keySet().toArray( new String JavaDoc[0] );
74         java.util.Arrays.sort( keys );
75         for( final String JavaDoc key : keys )
76         {
77             debug( key + "=" + props.getProperty( key ) );
78         }
79         
80     }
81     
82     AMXLoggingHook()
83     {
84         mServerName = System.getProperty(
85             com.sun.enterprise.util.SystemPropertyConstants.SERVER_NAME);
86         final String JavaDoc instanceRoot = System.getProperty(
87             com.sun.enterprise.util.SystemPropertyConstants.INSTANCE_ROOT_PROPERTY );
88             
89         mOut = createOutput( DEBUG, instanceRoot + "/AMXLoggingHook-" + mServerName + ".debug" );
90         // debug( "\n_______________________________________________________________________________");
91
// debug( "AMXLoggingHook: REV = 1");
92
// debug( "AMXLoggingHook: started at " + new java.util.Date() + " for server " + mServerName);
93

94         // dumpSystemProps( mOut );
95

96         mLoggingObjectName = null;
97         mMBeanServer = getMBeanServer(); // may or may not be available
98

99         mMinimumLogLevel = Level.FINEST;
100         mLoggingImplHook = null;
101         
102     }
103     
104         private static Output
105    createOutput( final boolean debug, final String JavaDoc fileName )
106    {
107         try
108         {
109            final java.io.File JavaDoc f = new java.io.File JavaDoc( fileName );
110            
111            return debug ? new FileOutput( f ) : new NullOutput();
112         }
113         catch( Throwable JavaDoc t )
114         {
115             // nothing can be done; fall through and return NullOutput
116
}
117         
118         return new NullOutput();
119    }
120     
121     private final void debug( final Object JavaDoc o ) { mOut.println( o.toString() ); }
122
123         private MBeanServer JavaDoc
124     getMBeanServer()
125     {
126         if ( mMBeanServer == null )
127         {
128             mMBeanServer =
129                 com.sun.enterprise.admin.common.MBeanServerFactory.getMBeanServer();
130             // may still be null if not yet available
131
if ( mMBeanServer != null )
132             {
133                 debug( "MBeanServer NOW EXISTS, creating LoggingImpl" );
134                 initLogging( mMBeanServer );
135             }
136         }
137         
138         return mMBeanServer;
139     }
140     
141     
142      private static final String JavaDoc LOGGING_IMPL_CLASSNAME =
143         "com.sun.enterprise.management.ext.logging.LoggingImpl";
144     /**
145         Called once to install the Logging MBean
146      */

147         private void
148     initLogging( final MBeanServer JavaDoc mbeanServer )
149     {
150         try
151         {
152             final Class JavaDoc loggingClass = Class.forName( LOGGING_IMPL_CLASSNAME );
153             
154             final Constructor JavaDoc constructor = loggingClass.getConstructor( String JavaDoc.class );
155             mLoggingImplHook = (LoggingImplHook)constructor.newInstance( mServerName );
156             
157             final Method JavaDoc getObjectNameMethod =
158                 loggingClass.getMethod( "getObjectName", String JavaDoc.class );
159             mLoggingObjectName = (ObjectName JavaDoc)getObjectNameMethod.invoke( mLoggingImplHook, mServerName );
160             debug( "registering Logging as: " + mLoggingObjectName );
161             mbeanServer.registerMBean( mLoggingImplHook, mLoggingObjectName );
162          }
163          catch ( Exception JavaDoc e )
164          {
165             final String JavaDoc msg = "Can't load " + LOGGING_IMPL_CLASSNAME + ", caught: " + e;
166             debug( msg );
167             throw new Error JavaDoc( msg );
168          }
169     }
170     
171         public Level JavaDoc
172     getMinimumLogLevel( )
173     {
174         return mMinimumLogLevel;
175     }
176     
177         public void
178     setMinimumLogLevel( final Level JavaDoc level )
179     {
180         mMinimumLogLevel = level;
181     }
182     
183         // call the Logging MBean
184
void
185     publish(
186         final LogRecord JavaDoc record,
187         final Formatter JavaDoc theFormatter )
188     {
189         if ( record.getLevel().intValue() < mMinimumLogLevel.intValue() )
190         {
191             return;
192         }
193
194         debug( "publish: " + theFormatter.format( record ) );
195
196         final MBeanServer JavaDoc server = getMBeanServer();
197         if ( server != null )
198         {
199             if ( ! mLoggingAvailable )
200             {
201                 mLoggingAvailable = server.isRegistered( mLoggingObjectName );
202                 if ( mLoggingAvailable )
203                 {
204                     debug( "Logging MBean is now available: " + mLoggingObjectName );
205                 }
206             }
207             
208             if ( mLoggingAvailable )
209             {
210                 try
211                 {
212                     mLoggingImplHook.privateLoggingHook( record, theFormatter );
213                     //final Object[] args = new Object[] { record, theFormatter };
214
//server.invoke( mLoggingObjectName, LOGGING_HOOK_NAME , args, LOGGING_HOOK_SIG );
215
}
216                 catch( Throwable JavaDoc t )
217                 {
218                     debug(
219                         "AMXLoggingHook.publish: Exception calling privateLoggingHook: " + t );
220                     // squelch--we can't log it or we'll have a recursive call
221
}
222             }
223             else
224             {
225                 debug( "publish: no Logging yet." );
226             }
227         }
228         else
229         {
230             debug( "publish: no MBeanServer yet." );
231         }
232     }
233     
234
235     
236     /**
237         Maintains an ordered list of LogRecords which
238         could not be sent to the not-yet-existing AMX Logging MBean.
239     private static final class StartupRecords {
240         private final List<LogRecord> mList;
241         
242         public StartupRecords() {
243             mList = new ArrayList<LogRecord>();
244         }
245         
246         public void add( final LogRecord r ){mList.add( r );}
247         public void clear(){ mList.clear(); }
248         public List getAll() { return Collections.unmodifiableList( mList ); }
249     };
250     
251      */

252     
253     private interface Output
254     {
255         public void println( Object JavaDoc o );
256     }
257
258     /**
259         Directs output to a file; used internally for debugging to avoid
260         infinite recursion between logging and this hook.
261      */

262     private static final class FileOutput implements Output
263     {
264         private java.io.PrintStream JavaDoc mOut;
265         
266         // passing null means output is not emitted
267
public
268         FileOutput( final java.io.File JavaDoc f)
269             throws java.io.IOException JavaDoc
270         {
271             mOut = new java.io.PrintStream JavaDoc( new java.io.FileOutputStream JavaDoc( f ) );
272         }
273         
274             public void
275         println( Object JavaDoc o )
276         {
277             mOut.println( o.toString() );
278         }
279         
280             public void
281         close( )
282         {
283             if ( mOut != null )
284             try
285             {
286                 mOut.close();
287             }
288             finally
289             {
290                 mOut = null;
291             }
292         }
293     };
294     
295     // ignores all output
296
private static final class NullOutput implements Output
297     {
298         NullOutput() {}
299         public void println( Object JavaDoc o ) {}
300     }
301 }
302
303
304
305
306
Popular Tags