KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > toplink > support > CommonsLoggingSessionLog


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.orm.toplink.support;
18
19 import java.lang.reflect.Method JavaDoc;
20
21 import oracle.toplink.logging.AbstractSessionLog;
22 import oracle.toplink.logging.SessionLogEntry;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 /**
27  * TopLink 10.1.3+ SessionLog implementation that logs through Commons Logging.
28  *
29  * <p>The namespace used is "oracle.toplink.xxx", with the latter part being
30  * the TopLink log category ("sql"/"transaction"/etc). In case of no category
31  * given, "session" will be used as default. This allows for fine-grained
32  * filtering of log messages, for example through Log4J configuration.
33  *
34  * <p>Maps TopLink's SEVERE level to CL ERROR, TopLink's WARNING to CL WARN,
35  * TopLink's INFO to CL INFO, TopLink's CONFIG/FINE/FINER to CL DEBUG,
36  * and TopLink's FINEST to CL TRACE. This results in common CL log behavior:
37  * INFO logging only at startup; operation logging at DEBUG level. Debug logging
38  * can be further filtered according to categories: for example, activate Log4J
39  * DEBUG logging for category "oracle.toplink.sql" to see the generated SQL.
40  *
41  * <p><b>Note:</b> This implementation will only work on TopLink 10.1.3 or higher,
42  * as it is built against TopLink's new SessionLog facilities in the
43  * <code>oracle.toplink.logging</code> package, supporting log categories.
44  *
45  * @author Juergen Hoeller
46  * @since 1.2
47  * @see CommonsLoggingSessionLog904
48  * @see oracle.toplink.logging.JavaLog
49  * @see org.springframework.orm.toplink.LocalSessionFactoryBean#setSessionLog
50  */

51 public class CommonsLoggingSessionLog extends AbstractSessionLog {
52
53     public static final String JavaDoc NAMESPACE_PREFIX = "oracle.toplink.";
54
55     public static final String JavaDoc DEFAULT_NAMESPACE = "session";
56
57     public static final String JavaDoc DEFAULT_SEPARATOR = "--";
58
59
60     private static Method JavaDoc getExceptionMethod;
61
62     static {
63         try {
64             getExceptionMethod = SessionLogEntry.class.getMethod("getException", new Class JavaDoc[0]);
65         }
66         catch (NoSuchMethodException JavaDoc ex) {
67             throw new IllegalStateException JavaDoc("Could not find method SessionLogEntry.getException()");
68         }
69     }
70
71
72     private String JavaDoc separator = DEFAULT_SEPARATOR;
73
74
75     /**
76      * Specify the separator between TopLink's supplemental details
77      * (session, connection) and the log message itself. Default is "--".
78      */

79     public void setSeparator(String JavaDoc separator) {
80         this.separator = separator;
81     }
82
83     /**
84      * Return the separator between TopLink's supplemental details
85      * (session, connection) and the log message itself. Default is "--".
86      */

87     public String JavaDoc getSeparator() {
88         return separator;
89     }
90
91
92     public void log(SessionLogEntry entry) {
93         Log logger = LogFactory.getLog(getCategory(entry));
94         switch (entry.getLevel()) {
95             case SEVERE:
96                 if (logger.isErrorEnabled()) {
97                     if (entry.hasException()) {
98                         logger.error(getMessageString(entry), getException(entry));
99                     }
100                     else {
101                         logger.error(getMessageString(entry));
102                     }
103                 }
104                 break;
105             case WARNING:
106                 if (logger.isWarnEnabled()) {
107                     if (entry.hasException()) {
108                         logger.warn(getMessageString(entry), getException(entry));
109                     }
110                     else {
111                         logger.warn(getMessageString(entry));
112                     }
113                 }
114                 break;
115             case INFO:
116                 if (logger.isInfoEnabled()) {
117                     if (entry.hasException()) {
118                         logger.info(getMessageString(entry), getException(entry));
119                     }
120                     else {
121                         logger.info(getMessageString(entry));
122                     }
123                 }
124                 break;
125             case CONFIG:
126             case FINE:
127             case FINER:
128                 if (logger.isDebugEnabled()) {
129                     if (entry.hasException()) {
130                         logger.debug(getMessageString(entry), getException(entry));
131                     }
132                     else {
133                         logger.debug(getMessageString(entry));
134                     }
135                 }
136                 break;
137             case FINEST:
138                 if (logger.isTraceEnabled()) {
139                     if (entry.hasException()) {
140                         logger.trace(getMessageString(entry), getException(entry));
141                     }
142                     else {
143                         logger.trace(getMessageString(entry));
144                     }
145                 }
146                 break;
147         }
148     }
149
150     /**
151      * Determine the log category for the given log entry.
152      * <p>If the entry carries a name space value, it will be appended
153      * to the "oracle.toplink." prefix; else, "oracle.toplink.session"
154      * will be used.
155      */

156     protected String JavaDoc getCategory(SessionLogEntry entry) {
157         if (entry.getNameSpace() != null) {
158             return NAMESPACE_PREFIX + entry.getNameSpace();
159         }
160         return NAMESPACE_PREFIX + DEFAULT_NAMESPACE;
161     }
162
163     /**
164      * Build the message String for the given log entry, including the
165      * supplemental details (session, connection) and the formatted message.
166      * @see #getSessionString(oracle.toplink.sessions.Session)
167      * @see #getConnectionString(oracle.toplink.internal.databaseaccess.Accessor)
168      * @see #formatMessage(oracle.toplink.logging.SessionLogEntry)
169      * @see #getSeparator()
170      */

171     protected String JavaDoc getMessageString(SessionLogEntry entry) {
172         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
173         if (entry.getSession() != null) {
174             buf.append(getSessionString(entry.getSession()));
175             buf.append(getSeparator());
176         }
177         if (entry.getConnection() != null) {
178             buf.append(getConnectionString(entry.getConnection()));
179             buf.append(getSeparator());
180         }
181         buf.append(formatMessage(entry));
182         return buf.toString();
183     }
184
185     /**
186      * Extract the exception from the given log entry.
187      * <p>Default implementations calls <code>SessionLogEntry.getException</code>
188      * via reflection: The return type varies between TopLink 9.0.4 and 10.1.3
189      * (<code>Exception</code> vs <code>Throwable</code>, respectively),
190      * which does not allow us to compile both CommonsLoggingSessionLog904 and
191      * CommonsLoggingSessionLog against the same <code>getException</code> method.
192      */

193     protected Throwable JavaDoc getException(SessionLogEntry entry) {
194         try {
195             return (Throwable JavaDoc) getExceptionMethod.invoke(entry, new Object JavaDoc[0]);
196         }
197         catch (Exception JavaDoc ex) {
198             throw new IllegalStateException JavaDoc(
199                     "Could not invoke method SessionLogEntry.getException(): " + ex.getMessage());
200         }
201     }
202
203
204     /**
205      * Throws an UnsupportedOperationException: This method is not
206      * called any longer as of TopLink 10.1.3, but abstract in
207      * 10.1.3 Preview 3 (which we want to be able to compile against).
208      * <p>Do not remove this method for the time being, even if the
209      * superclass method is no longer abstract in <code>toplink-api.jar</code>!
210      */

211     public void log(oracle.toplink.sessions.SessionLogEntry entry) {
212         throw new UnsupportedOperationException JavaDoc("oracle.toplink.sessions.SessionLogEntry not supported");
213     }
214
215 }
216
Popular Tags