KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
27
28 import java.util.Set JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Properties JavaDoc;
33 import java.util.Date JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.lang.reflect.Method JavaDoc;
36
37 import java.io.Serializable JavaDoc;
38
39 import javax.management.MBeanServerConnection JavaDoc;
40 import javax.management.MBeanServerInvocationHandler JavaDoc;
41 import javax.management.NotificationEmitter JavaDoc;
42 import javax.management.NotificationListener JavaDoc;
43 import javax.management.Notification JavaDoc;
44 import javax.management.NotificationFilter JavaDoc;
45 import javax.management.MBeanNotificationInfo JavaDoc;
46 import javax.management.ListenerNotFoundException JavaDoc;
47 import javax.management.JMException JavaDoc;
48 import javax.management.ObjectName JavaDoc;
49 import javax.management.MBeanInfo JavaDoc;
50 import javax.management.Attribute JavaDoc;
51 import javax.management.AttributeList JavaDoc;
52 import javax.management.MBeanAttributeInfo JavaDoc;
53
54
55 import com.sun.appserv.management.base.Util;
56
57 import com.sun.appserv.management.ext.logging.LogRecordFields;
58 import com.sun.appserv.management.ext.logging.LogQueryResult;
59 import com.sun.appserv.management.ext.logging.LogQueryResultImpl;
60 import com.sun.appserv.management.ext.logging.LogQueryEntry;
61 import com.sun.appserv.management.ext.logging.LogQueryEntryImpl;
62
63 import com.sun.appserv.management.util.stringifier.ArrayStringifier;
64 import com.sun.appserv.management.util.jmx.stringifier.AttributeStringifier;
65 import com.sun.appserv.management.util.jmx.stringifier.AttributeListStringifier;
66 import com.sun.appserv.management.util.misc.TypeCast;
67
68
69 import com.sun.enterprise.management.AMXTestBase;
70 import com.sun.enterprise.management.Capabilities;
71
72 import com.sun.enterprise.management.ext.logging.LogMBeanIntf;
73
74 /**
75     Test the LogMBean. It is not part of AMX, but we rely on it closely,
76     so ensure that it works.
77  */

78 public final class LogMBeanTest extends AMXTestBase
79 {
80     private LogMBeanIntf mLogMBean;
81     final ObjectName JavaDoc mTarget;
82     
83         public
84     LogMBeanTest( )
85     {
86         mTarget =
87             Util.newObjectName( "com.sun.appserv:name=logmanager,category=runtime,server=server" );
88
89         mLogMBean = initLogMBean();
90     }
91     
92         public static Capabilities
93     getCapabilities()
94     {
95         return getOfflineCapableCapabilities( false );
96     }
97     
98         private void
99     addListener( final LogMBeanIntf logMBean )
100     {
101         final MyLogMBeanListener listener = new MyLogMBeanListener();
102         final NotificationFilter JavaDoc filter = null;
103         logMBean.addNotificationListener( listener, filter, null );
104     }
105     
106         public LogMBeanIntf
107     initLogMBean()
108     {
109         final LogMBeanIntf logMBean = (LogMBeanIntf)
110             MBeanServerInvocationHandler.newProxyInstance(
111                 getConnection(), mTarget, LogMBeanIntf.class, true );
112         
113         return logMBean;
114     }
115     
116         public LogMBeanIntf
117     getLogMBean()
118     {
119         return mLogMBean;
120     }
121     
122     
123         public void
124     testGetArchivedLogFiles()
125     {
126         final String JavaDoc[] names = getLogMBean().getArchivedLogfiles();
127         //trace( "getArchivedLogFiles: " + ArrayStringifier.stringify( names, "\n") );
128
}
129     
130     
131         public void
132     testGetLogFilesDirectory()
133     {
134         final String JavaDoc name = getLogMBean().getLogFilesDirectory();
135         //trace( "getLogFilesDirectory: " + name );
136
}
137     
138         public void
139     testGetLoggerNames()
140     {
141         final List JavaDoc<String JavaDoc> names = TypeCast.asList( getLogMBean().getLoggerNames() );
142         TypeCast.checkList( names, String JavaDoc.class );
143         //System.out.println( "getLogFilesDirectory: " + names );
144

145         for( final String JavaDoc name : names )
146         {
147             final String JavaDoc level = getLogMBean().getLogLevel( name );
148             Level.parse( level );
149             
150             final List JavaDoc<String JavaDoc> unders = TypeCast.asList( getLogMBean().getLoggerNamesUnder( name ) );
151             TypeCast.checkList( unders, String JavaDoc.class );
152         }
153     }
154     
155         private void
156     displayAttribute( final Attribute JavaDoc attr )
157     {
158         trace( "Attribute: " + attr.getName() );
159         final Object JavaDoc value = attr.getValue();
160         trace( " Value: " + (value == null ? "null" : value.getClass().getName()) );
161     }
162     
163          private LogQueryResult
164     convertQueryResult( final AttributeList JavaDoc queryResult )
165     {
166         // extract field descriptions into a String[]
167
final AttributeList JavaDoc fieldAttrs = (AttributeList JavaDoc)((Attribute JavaDoc)queryResult.get( 0 )).getValue();
168         final String JavaDoc[] fieldHeaders = new String JavaDoc[ fieldAttrs.size() ];
169         assert( fieldHeaders.length == LogRecordFields.NUM_FIELDS );
170         for( int i = 0; i < fieldHeaders.length; ++i )
171         {
172             final Attribute JavaDoc attr = (Attribute JavaDoc)fieldAttrs.get( i );
173             fieldHeaders[ i ] = (String JavaDoc)attr.getValue();
174             //System.out.println( fieldHeaders[ i ] );
175
}
176         
177         // extract every record
178
final List JavaDoc<List JavaDoc<Serializable JavaDoc>> records =
179             TypeCast.asList( ((Attribute JavaDoc)queryResult.get( 1 )).getValue() );
180         final LogQueryEntry[] entries = new LogQueryEntry[ records.size() ];
181         for( int recordIdx = 0; recordIdx < records.size(); ++recordIdx )
182         {
183             final List JavaDoc<Serializable JavaDoc> record = records.get( recordIdx );
184             TypeCast.checkList( record, Serializable JavaDoc.class );
185             
186             assert( record.size() == fieldHeaders.length );
187             final Serializable JavaDoc[] fieldValues = new Serializable JavaDoc[ fieldHeaders.length ];
188             for( int fieldIdx = 0; fieldIdx < fieldValues.length; ++fieldIdx )
189             {
190                 fieldValues[ fieldIdx ] = record.get( fieldIdx );
191             }
192             
193             entries[ recordIdx ] = new LogQueryEntryImpl( fieldValues );
194         }
195         
196         return new LogQueryResultImpl( fieldHeaders, entries );
197     }
198     
199         public void
200     testQuery()
201     {
202         final String JavaDoc filename = "server.log";
203         final Boolean JavaDoc searchForward = Boolean.TRUE;
204         final Boolean JavaDoc sortAscending = Boolean.TRUE;
205         final int startRecord = 0;
206         final int requestedCount = 1000 * 1000;
207         final Date JavaDoc fromDate = null;
208         final Date JavaDoc toDate = null;
209         final List JavaDoc<Object JavaDoc> listOfModules = null;
210         final Boolean JavaDoc levelOnly = Boolean.FALSE;
211         final Properties JavaDoc props = null;
212         
213         final AttributeList JavaDoc attrs = getLogMBean().getLogRecordsUsingQuery(
214             filename,
215             new Long JavaDoc( startRecord ),
216             searchForward,
217             sortAscending,
218             new Integer JavaDoc( requestedCount ),
219             fromDate,
220             toDate,
221             Level.WARNING.toString(),
222             levelOnly,
223             listOfModules,
224             props );
225        
226        final LogQueryResult result = convertQueryResult( attrs );
227        
228        final LogQueryEntry[] entries = result.getEntries();
229        assert( entries.length != 0 );
230        
231        for( final LogQueryEntry entry : entries )
232        {
233             final String JavaDoc messageID = entry.getMessageID();
234             
235             final ArrayList JavaDoc causes = getLogMBean().getDiagnosticCausesForMessageId( messageID );
236             //trace( causes );
237
final ArrayList JavaDoc checks = getLogMBean().getDiagnosticChecksForMessageId( messageID );
238             //trace( checks );
239
final String JavaDoc uri = getLogMBean().getDiagnosticURIForMessageId( messageID );
240             //trace( uri );
241
}
242        
243       // trace( result );
244
}
245     
246     
247     private final class MyLogMBeanListener implements NotificationListener JavaDoc
248     {
249         public MyLogMBeanListener() {}
250         
251             public void
252         handleNotification(
253             final Notification JavaDoc notif,
254             final Object JavaDoc handback )
255         {
256             trace( "LoggingImpl.java: received Notification: " + notif );
257         }
258     }
259     
260 }
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
Popular Tags