KickJava   Java API By Example, From Geeks To Geeks.

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


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 oracle.toplink.sessions.DefaultSessionLog;
20 import oracle.toplink.sessions.Session;
21 import oracle.toplink.sessions.SessionLogEntry;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 /**
26  * TopLink 9.0.4 SessionLog implementation that logs through Commons Logging.
27  *
28  * <p>The namespace used is "oracle.toplink.session". Fine-grained filtering
29  * of log messages, for example through Log4J configuration, is <i>not</i>
30  * available on TopLink 9.0.4: Consider upgrading to TopLink 10.1.3 and
31  * using the CommonsLoggingSessionLog class instead.
32  *
33  * <p>TopLink log entries with exceptions are logged at CL WARN level,
34  * TopLink debug log entries at CL TRACE level, and any other log entry
35  * at CL DEBUG level. Finer-grained mapping to log levels is unfortunately
36  * not possible on TopLink 9.0.4.
37  *
38  * <p><b>Note:</b> This implementation will only actually work on TopLink 9.0.4,
39  * as it is built against TopLink's old SessionLog facilities in the
40  * <code>oracle.toplink.sessions</code> package, which are effectively
41  * obsolete (deprecated and bypassed) as of TopLink 10.1.3.
42  *
43  * @author Juergen Hoeller
44  * @since 1.2
45  * @see CommonsLoggingSessionLog
46  * @see oracle.toplink.sessions.DefaultSessionLog
47  * @see org.springframework.orm.toplink.LocalSessionFactoryBean#setSessionLog
48  */

49 public class CommonsLoggingSessionLog904 extends DefaultSessionLog {
50
51     public static final String JavaDoc NAMESPACE = "oracle.toplink.session";
52
53     public static final String JavaDoc DEFAULT_SEPARATOR = "--";
54
55
56     private final Log logger = LogFactory.getLog(NAMESPACE);
57
58     private String JavaDoc separator = DEFAULT_SEPARATOR;
59
60
61     /**
62      * Specify the separator between TopLink's supplemental details
63      * (session, connection) and the log message itself. Default is "--".
64      */

65     public void setSeparator(String JavaDoc separator) {
66         this.separator = separator;
67     }
68
69     /**
70      * Return the separator between TopLink's supplemental details
71      * (session, connection) and the log message itself. Default is "--".
72      */

73     public String JavaDoc getSeparator() {
74         return separator;
75     }
76
77
78     public void log(SessionLogEntry entry) {
79         if (entry.hasException()) {
80             if (shouldLogExceptions() && logger.isWarnEnabled()) {
81                 this.logger.warn(getMessageString(entry), entry.getException());
82             }
83         }
84         else if (entry.isDebug()) {
85             if (shouldLogDebug() && logger.isTraceEnabled()) {
86                 this.logger.trace(getMessageString(entry));
87             }
88         }
89         else {
90             if (logger.isDebugEnabled()) {
91                 this.logger.debug(getMessageString(entry));
92             }
93         }
94     }
95
96     /**
97      * Build the message String for the given log entry, including the
98      * supplemental details (session, connection) and the message text.
99      * @see #getSeparator()
100      */

101     protected String JavaDoc getMessageString(SessionLogEntry entry) {
102         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
103         if (shouldPrintSession()) {
104             buf.append(getSessionName(entry.getSession()));
105             buf.append("(");
106             buf.append(String.valueOf(System.identityHashCode(entry.getSession())));
107             buf.append(")");
108             buf.append(getSeparator());
109         }
110         if (shouldPrintConnection() && entry.getConnection() != null) {
111             buf.append("Connection");
112             buf.append("(");
113             buf.append(String.valueOf(System.identityHashCode(entry.getConnection())));
114             buf.append(")");
115             buf.append(getSeparator());
116         }
117         buf.append(entry.getMessage());
118         return buf.toString();
119     }
120
121     /**
122      * Return the name to be used for the given Session
123      * ("UnitOfWork"/"ServerSession"/"ClientSession"/etc).
124      */

125     protected String JavaDoc getSessionName(Session session) {
126         if (session.isUnitOfWork()) {
127             return "UnitOfWork";
128         }
129         if (session.isServerSession()) {
130             return "ServerSession";
131         }
132         if (session.isClientSession()) {
133             return "ClientSession";
134         }
135         if (session.isSessionBroker()) {
136             return "SessionBroker";
137         }
138         if (session.isRemoteSession()) {
139             return "RemoteSession";
140         }
141         if (session.isDatabaseSession()) {
142             return "DatabaseSession";
143         }
144         return "Session";
145     }
146
147 }
148
Popular Tags