001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.orm.toplink.support;
018:
019: import oracle.toplink.sessions.DefaultSessionLog;
020: import oracle.toplink.sessions.Session;
021: import oracle.toplink.sessions.SessionLogEntry;
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: /**
026: * TopLink 9.0.4 SessionLog implementation that logs through Commons Logging.
027: *
028: * <p>The namespace used is "oracle.toplink.session". Fine-grained filtering
029: * of log messages, for example through Log4J configuration, is <i>not</i>
030: * available on TopLink 9.0.4: Consider upgrading to TopLink 10.1.3 and
031: * using the CommonsLoggingSessionLog class instead.
032: *
033: * <p>TopLink log entries with exceptions are logged at CL WARN level,
034: * TopLink debug log entries at CL TRACE level, and any other log entry
035: * at CL DEBUG level. Finer-grained mapping to log levels is unfortunately
036: * not possible on TopLink 9.0.4.
037: *
038: * <p><b>Note:</b> This implementation will only actually work on TopLink 9.0.4,
039: * as it is built against TopLink's old SessionLog facilities in the
040: * <code>oracle.toplink.sessions</code> package, which are effectively
041: * obsolete (deprecated and bypassed) as of TopLink 10.1.3.
042: *
043: * @author Juergen Hoeller
044: * @since 1.2
045: * @see CommonsLoggingSessionLog
046: * @see oracle.toplink.sessions.DefaultSessionLog
047: * @see org.springframework.orm.toplink.LocalSessionFactoryBean#setSessionLog
048: */
049: public class CommonsLoggingSessionLog904 extends DefaultSessionLog {
050:
051: public static final String NAMESPACE = "oracle.toplink.session";
052:
053: public static final String DEFAULT_SEPARATOR = "--";
054:
055: private final Log logger = LogFactory.getLog(NAMESPACE);
056:
057: private String separator = DEFAULT_SEPARATOR;
058:
059: /**
060: * Specify the separator between TopLink's supplemental details
061: * (session, connection) and the log message itself. Default is "--".
062: */
063: public void setSeparator(String separator) {
064: this .separator = separator;
065: }
066:
067: /**
068: * Return the separator between TopLink's supplemental details
069: * (session, connection) and the log message itself. Default is "--".
070: */
071: public String getSeparator() {
072: return separator;
073: }
074:
075: public void log(SessionLogEntry entry) {
076: if (entry.hasException()) {
077: if (shouldLogExceptions() && logger.isWarnEnabled()) {
078: this .logger.warn(getMessageString(entry), entry
079: .getException());
080: }
081: } else if (entry.isDebug()) {
082: if (shouldLogDebug() && logger.isTraceEnabled()) {
083: this .logger.trace(getMessageString(entry));
084: }
085: } else {
086: if (logger.isDebugEnabled()) {
087: this .logger.debug(getMessageString(entry));
088: }
089: }
090: }
091:
092: /**
093: * Build the message String for the given log entry, including the
094: * supplemental details (session, connection) and the message text.
095: * @see #getSeparator()
096: */
097: protected String getMessageString(SessionLogEntry entry) {
098: StringBuffer buf = new StringBuffer();
099: if (shouldPrintSession()) {
100: buf.append(getSessionName(entry.getSession()));
101: buf.append("(");
102: buf.append(String.valueOf(System.identityHashCode(entry
103: .getSession())));
104: buf.append(")");
105: buf.append(getSeparator());
106: }
107: if (shouldPrintConnection() && entry.getConnection() != null) {
108: buf.append("Connection");
109: buf.append("(");
110: buf.append(String.valueOf(System.identityHashCode(entry
111: .getConnection())));
112: buf.append(")");
113: buf.append(getSeparator());
114: }
115: buf.append(entry.getMessage());
116: return buf.toString();
117: }
118:
119: /**
120: * Return the name to be used for the given Session
121: * ("UnitOfWork"/"ServerSession"/"ClientSession"/etc).
122: */
123: protected String getSessionName(Session session) {
124: if (session.isUnitOfWork()) {
125: return "UnitOfWork";
126: }
127: if (session.isServerSession()) {
128: return "ServerSession";
129: }
130: if (session.isClientSession()) {
131: return "ClientSession";
132: }
133: if (session.isSessionBroker()) {
134: return "SessionBroker";
135: }
136: if (session.isRemoteSession()) {
137: return "RemoteSession";
138: }
139: if (session.isDatabaseSession()) {
140: return "DatabaseSession";
141: }
142: return "Session";
143: }
144:
145: }
|