KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > logging > JavaLog


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
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
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 in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.logging;
23
24
25 /** Java class "JavaLog.java" generated from Poseidon for UML.
26  * Poseidon for UML is developed by <A HREF="http://www.gentleware.com">Gentleware</A>.
27  * Generated with <A HREF="http://jakarta.apache.org/velocity/">velocity</A> template engine.
28  */

29 import java.util.*;
30 import java.util.logging.*;
31 import java.security.AccessController JavaDoc;
32 import java.security.PrivilegedAction JavaDoc;
33 import oracle.toplink.essentials.sessions.Session;
34 import oracle.toplink.essentials.internal.localization.i18n.*;
35
36 /**
37  * PUBLIC:
38  * <p>
39  * This is a wrapper class for java.util.logging. It is used when messages need to
40  * be logged through java.util.logging.
41  * </p>
42  * @see SessionLog
43  * @see AbstractSessionLog
44  * @see SessionLogEntry
45  * @see Session
46  */

47 public class JavaLog extends AbstractSessionLog {
48
49     /**
50      * Stores the default session name in case there is the session name is missing.
51      */

52     protected static final String JavaDoc DEFAULT_SESSION_NAME = "defaultsessionname";
53     public static final String JavaDoc DEFAULT_TOPLINK_NAMESPACE = "oracle.toplink.essentials";
54     protected static final String JavaDoc LOGGING_LOCALIZATION_STRING = "oracle.toplink.essentials.internal.localization.i18n.LoggingLocalizationResource";
55     protected static final String JavaDoc TRACE_LOCALIZATION_STRING = "oracle.toplink.essentials.internal.localization.i18n.TraceLocalizationResource";
56
57     /**
58      * Stores all the java.util.logging.Levels. The indexes are TopLink logging levels.
59      */

60     private static final Level[] levels = new Level[] { Level.ALL, Level.FINEST, Level.FINER, Level.FINE, Level.CONFIG, Level.INFO, Level.WARNING, Level.SEVERE, Level.OFF };
61
62     /**
63      * Stores the Logger for default TopLink namespace, i.e. "oracle.toplink.essentials".
64      */

65     private static Logger defaultTopLinkLogger = Logger.getLogger(DEFAULT_TOPLINK_NAMESPACE);;
66
67     /**
68      * Represents the HashMap that stores all the name space strings.
69      * The keys are category names. The values are namespace strings.
70      */

71     private Map nameSpaceMap = new HashMap();
72
73     /**
74      * Stores the namespace for session, i.e."oracle.toplink.essentials.<sessionname>".
75      */

76     private String JavaDoc sessionNameSpace;
77
78     /**
79      * Stores the Logger for session namespace, i.e. "oracle.toplink.essentials.<sessionname>".
80      */

81     private Logger sessionLogger;
82
83     private Map catagoryloggers = new HashMap<String JavaDoc, Logger>();
84
85
86     /**
87      * PUBLIC:
88      * <p>
89      * Return the effective log level for the name space extracted from session and category.
90      * If a Logger's level is set to be null then the Logger will use an effective Level that will
91      * be obtained by walking up the parent tree and using the first non-null Level.
92      * </p><p>
93      *
94      * @return the effective log level.
95      * </p>
96      */

97     public int getLevel(String JavaDoc category) {
98         Logger logger = getLogger(category);
99         while ((logger != null) && (logger.getLevel() == null)) {
100             logger = logger.getParent();
101         }
102
103         if (logger == null) {
104             return OFF;
105         }
106
107         //For a given java.util.logging.Level, return the index (ie, TopLink logging level)
108
int logLevel = logger.getLevel().intValue();
109         for (int i = 0; i < levels.length ; i++) {
110             if (logLevel == levels[i].intValue()) {
111                 return i;
112             }
113         }
114         return OFF;
115     }
116
117     /**
118      * PUBLIC:
119      * <p>
120      * Set the log level to a logger with name space extracted from the given category.
121      * </p>
122      */

123     public void setLevel(final int level, String JavaDoc category) {
124         final Logger logger = getLogger(category);
125         AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
126             public Object JavaDoc run() {
127                 logger.setLevel(getJavaLevel(level));
128                 return null; // nothing to return
129
}
130         });
131     }
132
133     /**
134      * INTERNAL:
135      * Return the name space for the given category from the map.
136      */

137     protected String JavaDoc getNameSpaceString(String JavaDoc category) {
138         if (session == null) {
139             return DEFAULT_TOPLINK_NAMESPACE;
140         } else if ((category == null) || (category.length() == 0)) {
141             return sessionNameSpace;
142         } else {
143             return (String JavaDoc)nameSpaceMap.get(category);
144         }
145     }
146
147     /**
148      * INTERNAL:
149      * Return the Logger for the given category
150      */

151     protected Logger getLogger(String JavaDoc category) {
152         if (session == null) {
153             return defaultTopLinkLogger;
154         } else if ((category == null) || (category.length() == 0)) {
155             return sessionLogger;
156         } else {
157             Logger logger = (Logger) catagoryloggers.get(category);
158             // If session != null, catagoryloggers should have an entry for this category
159
assert logger != null;
160             return logger;
161         }
162     }
163
164
165     /**
166      * PUBLIC:
167      * <p>
168      * Set the session and session namespace.
169      * </p>
170      *
171      * @param session a Session
172      * </p>
173      */

174     public void setSession(Session session) {
175         super.setSession(session);
176         if (session != null) {
177             String JavaDoc sessionName;
178             if ((session.getName() != null) && (session.getName().length() != 0)) {
179                 sessionName = session.getName();
180             } else {
181                 sessionName = DEFAULT_SESSION_NAME;
182             }
183             sessionNameSpace = DEFAULT_TOPLINK_NAMESPACE + "." + sessionName;
184
185             //Initialize loggers eagerly
186
sessionLogger = Logger.getLogger(sessionNameSpace);
187             for (int i = 0; i < loggerCatagories.length; i++) {
188                 String JavaDoc loggerCatagory = loggerCatagories[i];
189                 String JavaDoc loggerNameSpace = sessionNameSpace + "." + loggerCatagory;
190                 nameSpaceMap.put(loggerCatagory, loggerNameSpace);
191                 catagoryloggers.put(loggerCatagory, Logger.getLogger(loggerNameSpace));
192             }
193         }
194     }
195
196     /**
197      * INTERNAL:
198      * Return the corresponding java.util.logging.Level for a given TopLink level.
199      */

200     private Level getJavaLevel(int level) {
201         return levels[level];
202     }
203
204     /**
205      * PUBLIC:
206      * <p>
207      * Check if a message of the given level would actually be logged by the logger
208      * with name space built from the given session and category.
209      * Return the shouldLog for the given category from
210      * </p><p>
211      * @return true if the given message level will be logged
212      * </p>
213      */

214     public boolean shouldLog(int level, String JavaDoc category) {
215         Logger logger = getLogger(category);
216         return logger.isLoggable(getJavaLevel(level));
217     }
218
219     /**
220      * PUBLIC:
221      * <p>
222      * Log a SessionLogEntry
223      * </p><p>
224      * @param entry SessionLogEntry that holds all the information for a TopLink logging event
225      * </p>
226      */

227     public void log(SessionLogEntry entry) {
228         if (!shouldLog(entry.getLevel(), entry.getNameSpace())) {
229             return;
230         }
231
232         Logger logger = getLogger(entry.getNameSpace());
233         Level javaLevel = getJavaLevel(entry.getLevel());
234
235         internalLog(entry, javaLevel, logger);
236     }
237
238     /**
239      * INTERNAL:
240      * <p>
241      * Build a LogRecord
242      * </p><p>
243      * @param entry SessionLogEntry that holds all the information for a TopLink logging event
244      * @param javaLevel the message level
245      * </p>
246      */

247     protected void internalLog(SessionLogEntry entry, Level javaLevel, Logger logger) {
248         TopLinkLogRecord lr = new TopLinkLogRecord(javaLevel, entry.getMessage());
249         lr.setSourceClassName(null);
250         lr.setSourceMethodName(null);
251         lr.setParameters(entry.getParameters());
252         lr.setLoggerName(getNameSpaceString(entry.getNameSpace()));
253         if (entry.shouldTranslate()) {
254             String JavaDoc bundleName;
255             ResourceBundle resourceBundle;
256             if (entry.getLevel() > FINE) {
257                 bundleName = LOGGING_LOCALIZATION_STRING;
258                 resourceBundle = new LoggingLocalizationResource();
259             } else {
260                 bundleName = TRACE_LOCALIZATION_STRING;
261                 resourceBundle = new TraceLocalizationResource();
262             }
263             lr.setResourceBundleName(bundleName);
264             lr.setResourceBundle(resourceBundle);
265         }
266         if (shouldPrintSession()) {
267             lr.setSessionString(getSessionString(entry.getSession()));
268         }
269         if (shouldPrintConnection()) {
270             lr.setConnection(entry.getConnection());
271         }
272         lr.setThrown(entry.getException());
273         lr.setShouldLogExceptionStackTrace(shouldLogExceptionStackTrace());
274         lr.setShouldPrintDate(shouldPrintDate());
275         lr.setShouldPrintThread(shouldPrintThread());
276         logger.log(lr);
277     }
278
279     /**
280      * PUBLIC:
281      * <p>
282      * Log a throwable.
283      * </p><p>
284      * @param throwable a throwable
285      * </p>
286      */

287     public void throwing(Throwable JavaDoc throwable) {
288         getLogger(null).throwing(null, null, throwable);
289     }
290
291     /**
292      * INTERNAL:
293      * Each session owns its own session log because session is stored in the session log
294      */

295     public Object JavaDoc clone() {
296         // There is no special treatment required for cloning here
297
// The state of this object is described by member variables sessionLogger and catagoryLoggers.
298
// This state depends on session.
299
// If session for the clone is going to be the same as session for this there is no
300
// need to do "deep" cloning.
301
// If not, the session being cloned should call setSession() on its JavaLog object to initialize it correctly.
302
JavaLog cloneLog = (JavaLog)super.clone();
303         return cloneLog;
304     }
305 }
306
307
Popular Tags