KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > helper > LoggingHelper


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.appserv.management.helper;
24
25 import java.util.List JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.HashSet JavaDoc;
29
30 import java.util.logging.Level JavaDoc;
31
32 import javax.management.Attribute JavaDoc;
33
34 import com.sun.appserv.management.ext.logging.Logging;
35 import static com.sun.appserv.management.ext.logging.Logging.*;
36 import com.sun.appserv.management.ext.logging.LogQuery;
37 import com.sun.appserv.management.ext.logging.LogModuleNames;
38 import com.sun.appserv.management.ext.logging.LogQueryResult;
39 import com.sun.appserv.management.ext.logging.LogQueryResultImpl;
40
41 import com.sun.appserv.management.util.misc.GSetUtil;
42
43
44 /**
45     Helper class for simplifying access to logging.
46     @since AppServer 9.0
47  */

48 public final class LoggingHelper extends Helper
49 {
50     private final Logging mLogging;
51     
52     /**
53         Create with default parameters.
54      */

55         public
56     LoggingHelper( final Logging logging )
57     {
58         super( logging.getDomainRoot() );
59         mLogging = logging;
60     }
61     
62         public Logging
63     getLogging()
64     {
65         return mLogging;
66     }
67     
68     
69     /**
70         Get all log records of the specified error level or higher level for a
71         all specified modules in the most current log file.
72         
73         @see LogQuery
74         @see LogModuleNames
75      */

76         public LogQueryResult
77     queryServerLog(
78         final String JavaDoc logLevel,
79         final Set JavaDoc<String JavaDoc> modules)
80     {
81         final String JavaDoc name = MOST_RECENT_NAME;
82         final int startIndex = FIRST_RECORD;
83         final boolean searchForward = true;
84         final int maxRecords = ALL_RECORDS;
85         final Long JavaDoc startTime = null;
86         final Long JavaDoc stopTime = null;
87         final List JavaDoc<Attribute JavaDoc> attrs = null;
88         
89         assert( getLogging() != null );
90         
91         final LogQueryResult result = getLogging().queryServerLog(
92             name,
93             startIndex,
94             searchForward,
95             maxRecords,
96             startTime,
97             stopTime,
98             logLevel,
99             modules,
100             attrs );
101         
102         return result;
103     }
104     
105     /**
106         Get all log records of the specified error level or higher level for a
107         particular module in the most current log file.
108         
109         @see LogQuery
110      */

111         public LogQueryResult
112     queryServerLog(
113         final String JavaDoc logLevel,
114         final String JavaDoc moduleID)
115     {
116         return queryServerLog( logLevel, GSetUtil.newSet( moduleID ) );
117     }
118     
119     /**
120         Get all log records of the specified error level or higher level for
121         all modules in the most current log file.
122         @see LogQuery
123      */

124         public LogQueryResult
125     queryServerLog( final String JavaDoc logLevel )
126     {
127         return queryServerLog( logLevel, LogModuleNames.ALL_NAMES );
128     }
129     
130     
131     /**
132         Get all available log records for all modules in the most current log file.
133         @see LogQuery
134      */

135         public LogQueryResult
136     queryAllCurrent()
137     {
138         return queryAllInFile( MOST_RECENT_NAME );
139     }
140     
141     
142     
143     
144         private long
145     now()
146     {
147         return System.currentTimeMillis();
148     }
149     
150     /**
151         Get all available log records for all modules in the most current log file
152         which have occurred within the last number of seconds.
153         
154         @param seconds
155         @see LogQuery
156      */

157         public LogQueryResult
158     queryServerLogRecent( final long seconds )
159     {
160         return queryServerLogRecent( seconds, LogModuleNames.ALL_NAMES );
161     }
162     
163     /**
164         Get all available log records for all modules in the most current log file
165         which have occurred within the last number of seconds.
166         
167         @param seconds
168         @see LogQuery
169      */

170         public LogQueryResult
171     queryServerLogRecent(
172         final long seconds,
173         final Set JavaDoc<String JavaDoc> modules )
174     {
175         final String JavaDoc name = MOST_RECENT_NAME;
176         final int startIndex = LAST_RECORD;
177         final boolean searchForward = false;
178         final int maxRecords = ALL_RECORDS;
179         final Long JavaDoc startTime = now();
180         final Long JavaDoc stopTime = now() - (seconds * 1000);
181         final List JavaDoc<Attribute JavaDoc> attrs = null;
182         
183         final LogQueryResult result = getLogging().queryServerLog(
184             name,
185             startIndex,
186             searchForward,
187             maxRecords,
188             startTime,
189             stopTime,
190             LOWEST_SUPPORTED_QUERY_LEVEL,
191             modules,
192             attrs );
193         return result;
194     }
195     
196     
197     /**
198         Get all available log records for all modules in the specified log file.
199         @see LogQuery
200      */

201         public LogQueryResult
202     queryAllInFile( final String JavaDoc name )
203     {
204         final int startIndex = FIRST_RECORD;
205         final boolean searchForward = true;
206         final int maxRecords = ALL_RECORDS;
207         final Long JavaDoc startTime = null;
208         final Long JavaDoc stopTime = null;
209         final List JavaDoc<Attribute JavaDoc> attrs = null;
210         
211         final LogQueryResult result = getLogging().queryServerLog(
212             name,
213             startIndex,
214             searchForward,
215             maxRecords,
216             startTime,
217             stopTime,
218             LOWEST_SUPPORTED_QUERY_LEVEL,
219             LogModuleNames.ALL_NAMES,
220             attrs );
221         
222         return result;
223     }
224     
225     /**
226         Get all available log records in <i>all files</i> for all modules of all
227         available log levels.
228         
229         @see LogQuery
230      */

231         public LogQueryResult[]
232     queryAll()
233     {
234         final String JavaDoc[] names = getLogging().getLogFileNames( SERVER_KEY );
235         
236         final List JavaDoc<LogQueryResult> all = new ArrayList JavaDoc<LogQueryResult>( names.length );
237         for( final String JavaDoc name : names )
238         {
239             // a log file could disappear while querying
240
try
241             {
242                 final LogQueryResult result = queryAllInFile( name );
243                 all.add( result );
244             }
245             catch( Exception JavaDoc e )
246             {
247                 // ignore and try next one.
248
}
249         }
250         
251         final LogQueryResult[] results = new LogQueryResultImpl[ all.size() ];
252         return all.toArray( results );
253     }
254 }
255
256
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
283
284
Popular Tags