KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Collections JavaDoc;
30
31 import java.io.Serializable JavaDoc;
32
33
34 import javax.management.Attribute JavaDoc;
35
36 import com.sun.appserv.management.ext.logging.Logging;
37 import static com.sun.appserv.management.ext.logging.Logging.*;
38 import com.sun.appserv.management.ext.logging.LogQuery;
39 import com.sun.appserv.management.ext.logging.LogModuleNames;
40 import com.sun.appserv.management.ext.logging.LogQueryResult;
41 import com.sun.appserv.management.ext.logging.LogQueryEntry;
42 import com.sun.appserv.management.ext.logging.LogQueryResultImpl;
43
44 import com.sun.appserv.management.util.misc.GSetUtil;
45
46
47
48
49 /**
50     Helper class for simplifying querying the log files.
51     Unlike {@link LoggingHelper}, state is maintained from
52     call-to-call, which may be helpful in performing
53     repeated queries.
54     <p>
55     Some combinations of parameters may not be useful; it is
56     up to the caller to ensure reasonable parameters.
57     <p>
58     A typical use for this helper would be periodic queries
59     for new log records and/or retrieving log records in
60     batches.
61     
62     @since AppServer 9.0
63     @see LoggingHelper
64  */

65 public final class StatefulLoggingHelper extends Helper
66 {
67     private Logging mLogging;
68     
69     private String JavaDoc mLogFile;
70     private long mStartIndex;
71     private Set JavaDoc<String JavaDoc> mModules;
72     private boolean mSearchForward;
73     private int mMaxRecords;
74     private Long JavaDoc mStartTime;
75     private Long JavaDoc mStopTime;
76     private String JavaDoc mLogLevel;
77     private List JavaDoc<Attribute JavaDoc> mAttrs;
78     
79     /**
80         Create with default parameters.
81      */

82         public
83     StatefulLoggingHelper( final Logging logging )
84     {
85         super( logging.getDomainRoot() );
86         mLogging = logging;
87         
88         mLogFile = MOST_RECENT_NAME;
89         mStartIndex = FIRST_RECORD;
90         mSearchForward = true;
91         mLogLevel = LOWEST_SUPPORTED_QUERY_LEVEL;
92         mModules = LogModuleNames.ALL_NAMES;
93         mMaxRecords = ALL_RECORDS;
94         mStartTime = null;
95         mStopTime = null;
96         mAttrs = new ArrayList JavaDoc<Attribute JavaDoc>();
97     }
98     
99         public Logging
100     getLogging()
101     {
102         return mLogging;
103     }
104     
105     public String JavaDoc getLogFile() { return mLogFile; }
106     public long getStartIndex() { return mStartIndex; }
107     public Set JavaDoc<String JavaDoc> getModules() { return mModules; }
108     public List JavaDoc<Attribute JavaDoc> getAttrs() { return mAttrs; }
109     public boolean getSearchForward() { return mSearchForward; }
110     public long getStartTime() { return mStartTime; }
111     public long getStopTime() { return mStopTime; }
112     public String JavaDoc getLogLevel() { return mLogLevel; }
113     public int getMaxRecords() { return mMaxRecords; }
114     
115     
116     /**
117         If the specified log file is different, the startIndex
118         is reset appropriately.
119      */

120         public void
121     setLogFile( final String JavaDoc name )
122     {
123         if ( ! mLogFile.equals( name ) )
124         {
125             mLogFile = name;
126             setStartIndex( getSearchForward() ? FIRST_RECORD : LAST_RECORD );
127         }
128
129     }
130     
131         public void
132     setStartIndex( final int startIndex )
133     {
134         mStartIndex = startIndex;
135     }
136     
137         public void
138     setLogLevel( final String JavaDoc logLevel )
139     {
140         mLogLevel = logLevel;
141     }
142     
143         public void
144     setMaxRecords( final int maxRecords )
145     {
146         mMaxRecords = maxRecords;
147     }
148     
149     
150         public void
151     setModules( final Set JavaDoc<String JavaDoc> modules )
152     {
153         mModules = modules;
154     }
155     
156         public void
157     setModule( final String JavaDoc module )
158     {
159         mModules = GSetUtil.newSet( module );
160     }
161     
162         public void
163     setAttrs( final List JavaDoc<Attribute JavaDoc> attrs )
164     {
165         mAttrs.clear();
166         mAttrs.addAll( attrs );
167     }
168     
169         public void
170     setSearchForward( final boolean searchForward )
171     {
172         mSearchForward = true;
173     }
174     
175         public void
176     setStartTime( final Long JavaDoc startTime )
177     {
178         mStartTime = startTime;
179     }
180     
181         public void
182     setStopTime( final Long JavaDoc stopTime )
183     {
184         mStopTime = stopTime;
185     }
186     
187     /**
188         Query for LogRecords based upon the current settings.
189         The startIndex is updated appropriately following the query,
190         depending on the search direction. A subsequent
191         query will begin at the next available index.
192      */

193         public LogQueryResult
194     query()
195     {
196         final Logging logging = getLogging();
197         assert( logging != null );
198         
199         final LogQueryResult result = logging.queryServerLog(
200             mLogFile,
201             mStartIndex,
202             mSearchForward,
203             mMaxRecords,
204             mStartTime,
205             mStopTime,
206             mLogLevel,
207             mModules,
208             mAttrs );
209        
210        final LogQueryEntry[] entries = result.getEntries();
211        if ( entries.length != 0 )
212        {
213             // update start index
214
if ( mSearchForward )
215             {
216                 mStartIndex = entries[ entries.length - 1 ].getRecordNumber() + 1;
217             }
218             else
219             {
220                 mStartIndex = entries[ 0 ].getRecordNumber();
221             }
222        }
223        
224        return result;
225     }
226 }
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
Popular Tags