001: // Copyright 2006 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.ioc.internal.services;
016:
017: import static java.lang.String.format;
018:
019: import java.util.Iterator;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.tapestry.ioc.internal.util.Defense;
023: import org.apache.tapestry.ioc.services.ExceptionTracker;
024:
025: /**
026: * Used by {@link org.apache.tapestry.ioc.internal.services.LoggingDecoratorImpl} to delegate out
027: * logging behavior to a seperate object (helps ensure no naming conflicts).
028: */
029: public final class ServiceLogger {
030: private final Log _log;
031:
032: private final ExceptionTracker _exceptionTracker;
033:
034: private static final String ENTER = "ENTER";
035:
036: private static final String EXIT = " EXIT";
037:
038: private static final String FAIL = " FAIL";
039:
040: public ServiceLogger(Log log, ExceptionTracker exceptionTracker) {
041: _log = log;
042: _exceptionTracker = exceptionTracker;
043: }
044:
045: /** Returns true if the debugging is enabled for the underlying Log. */
046: public boolean isDebugEnabled() {
047: return _log.isDebugEnabled();
048: }
049:
050: /**
051: * Invoked when a method is first entered
052: *
053: * @param name
054: * of the method
055: * @param arguments
056: */
057: public void entry(String name, Object[] arguments) {
058: StringBuilder buffer = new StringBuilder();
059:
060: buffer.append(format("[%s] %s(", ENTER, name));
061:
062: for (int i = 0; i < arguments.length; i++) {
063: if (i > 0)
064: buffer.append(", ");
065:
066: convert(buffer, arguments[i]);
067: }
068:
069: buffer.append(")");
070:
071: _log.debug(buffer.toString());
072: }
073:
074: private void convert(StringBuilder buffer, Object object) {
075: if (object == null) {
076: buffer.append("null");
077: return;
078: }
079:
080: // Minimal, alas: Doesn't handle embedded quotes and other
081: // characters. Really want to convert the string back to what it
082: // would look like as source code.
083:
084: if (object instanceof String) {
085: buffer.append("\"");
086: buffer.append(object.toString());
087: buffer.append("\"");
088: return;
089: }
090:
091: if (object instanceof Object[]) {
092: Object[] values = (Object[]) object;
093: buffer.append('{');
094:
095: for (int i = 0; i < values.length; i++) {
096: if (i > 0)
097: buffer.append(", ");
098:
099: convert(buffer, values[i]);
100: }
101:
102: buffer.append('}');
103: return;
104: }
105:
106: if (object instanceof Iterable) {
107: Iterable itr = (Iterable) object;
108: boolean first = true;
109:
110: buffer.append('[');
111: Iterator i = itr.iterator();
112: while (i.hasNext()) {
113: if (!first)
114: buffer.append(", ");
115:
116: convert(buffer, i.next());
117: first = false;
118: }
119: buffer.append(']');
120: return;
121: }
122:
123: // Might need to add a few more, for things like character values ...
124:
125: buffer.append(object.toString());
126: }
127:
128: /**
129: * Invoked when a method returns a value
130: *
131: * @param name
132: * of the method
133: * @param result
134: * the return value for the method invocation
135: */
136: public void exit(String name, Object result) {
137: Defense.notNull(name, "name");
138:
139: StringBuilder buffer = new StringBuilder();
140:
141: buffer.append(format("[%s] %s [", EXIT, name));
142:
143: convert(buffer, result);
144:
145: buffer.append(']');
146:
147: _log.debug(buffer.toString());
148: }
149:
150: /** Invoked when void method finishes succesfully. */
151: public void voidExit(String name) {
152: _log.debug(format("[%s] %s", EXIT, name));
153: }
154:
155: /** Invoked when method invocation instead throws an exception. */
156: public void fail(String name, Throwable t) {
157: if (_log.isDebugEnabled()) {
158: _log.debug(format("[%s] %s -- %s", FAIL, name, t.getClass()
159: .getName()),
160: _exceptionTracker.exceptionLogged(t) ? null : t);
161: }
162: }
163: }
|