KickJava   Java API By Example, From Geeks To Geeks.

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


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  * $Header: /cvs/glassfish/admin/mbeanapi-impl/src/java/com/sun/enterprise/management/support/AMXMBeanLogger.java,v 1.4 2006/03/09 20:30:46 llc Exp $
26  * $Revision: 1.4 $
27  * $Date: 2006/03/09 20:30:46 $
28  */

29
30 package com.sun.enterprise.management.support;
31
32 import java.util.Map JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.logging.LogRecord JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 import javax.management.ObjectName JavaDoc;
40
41
42
43
44 import com.sun.appserv.management.base.AMX;
45 import com.sun.appserv.management.base.AMXLoggerBase;
46 import com.sun.appserv.management.base.LoggerSupport;
47 import com.sun.appserv.management.base.Util;
48
49 /**
50     The logger class for all MBean loggers.
51     <p>
52     When constructing an MBean Logger,
53     we derive its name from the ObjectName
54     of the MBean. The Javadoc for the logger class states:
55     <i>Logger names can be arbitrary strings..."</i>. This convention
56     is followed in spirit; the logger name for an MBean is followed by
57     forming a dotted string which consists of a common prefix followed by
58     the hierarchy of parents of the form
59     <j2eeType> or <j2eeType>:<name>, depending on whether the
60     MBean has a name or not.
61  */

62 public final class AMXMBeanLogger extends AMXLoggerBase
63 {
64     private final ObjectName JavaDoc mObjectName;
65     
66         private static String JavaDoc
67     mangle( final String JavaDoc s )
68     {
69         // don't allow the '.' in a name part
70
return( s.replaceAll( "\\.", "_" ) );
71     }
72     
73     private static final String JavaDoc TYPE_NAME_DELIM = ":";
74     
75         private static String JavaDoc
76     formTypeName(
77         final String JavaDoc j2eeType,
78         final String JavaDoc name )
79     {
80         String JavaDoc result = null;
81         
82         if ( j2eeType == null )
83         {
84             result = name;
85         }
86         else
87         {
88             String JavaDoc pair = mangle( j2eeType );
89             
90             if ( ! name.equals( ObjectNames.getSingletonName( j2eeType ) ) )
91             {
92                 pair = pair + TYPE_NAME_DELIM + mangle( name );
93             }
94             
95             result = pair;
96         }
97         
98         return( result );
99     }
100     
101     
102     /**
103         Derive a Logger name consistent with the package hierarchy.
104      */

105         private static String JavaDoc
106     createLoggerName( final ObjectName JavaDoc objectName )
107     {
108         final String JavaDoc j2eeType = Util.getJ2EEType( objectName );
109         final String JavaDoc name = Util.getName( objectName );
110         final TypeInfos infos = TypeInfos.getInstance();
111         
112         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
113         buf.append( LoggerSupport.AMX_MBEAN_LOGGER_PREFIX );
114         buf.append( AMX.FULL_TYPE_DELIM + formTypeName( j2eeType, name ) );
115         
116         final String JavaDoc[] typeChain = infos.getJ2EETypeChain( objectName );
117         for( int i = 0; i < typeChain.length - 1; ++i )
118         {
119             final String JavaDoc type = typeChain[ i ];
120             String JavaDoc value = objectName.getKeyProperty( type );
121             if ( value == null )
122             {
123                 value = AMX.NO_NAME;
124             }
125             
126             buf.append( AMX.FULL_TYPE_DELIM + formTypeName( type, value ) );
127         }
128
129         return( buf.toString() );
130     }
131     
132         private
133     AMXMBeanLogger(
134         final ObjectName JavaDoc objectName )
135     {
136         this( objectName, null );
137         throw new IllegalArgumentException JavaDoc(); // don't use this routine
138
}
139     
140         private
141     AMXMBeanLogger(
142         final ObjectName JavaDoc objectName,
143         final String JavaDoc resourceBundleName )
144     {
145         super( createLoggerName( objectName ), resourceBundleName );
146         
147         mObjectName = objectName;
148         throw new IllegalArgumentException JavaDoc(); // don't use this routine
149
}
150     
151         public static Logger JavaDoc
152     createNew( final ObjectName JavaDoc objectName )
153     {
154         final String JavaDoc loggerName = createLoggerName( objectName );
155         
156         return( Logger.getLogger( loggerName ) );
157     }
158     
159     
160     /**
161         Override so that we can include the ObjectName in every LogRecord.
162      */

163     public static final String JavaDoc OBJECT_NAME_KEY = "ObjectName";
164         public void
165     log( final LogRecord JavaDoc record )
166     {
167         final Object JavaDoc[] existing = record.getParameters();
168         final int numExisting = existing == null ? 0 : existing.length;
169         
170         final Object JavaDoc[] params = new Object JavaDoc[ 1 + numExisting ];
171         if ( existing != null )
172         {
173             System.arraycopy( existing, 0, params, 0, existing.length );
174         }
175         
176         // follow convention by placing a Map with our stuff in the last position
177
final Map JavaDoc<String JavaDoc,ObjectName JavaDoc> m = new HashMap JavaDoc<String JavaDoc,ObjectName JavaDoc>();
178         m.put( OBJECT_NAME_KEY, mObjectName );
179         params[ params.length - 1 ] = m;
180         
181         record.setParameters( params );
182         
183         super.log( record );
184     }
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
Popular Tags