001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.synapse.mediators;
021:
022: import org.apache.synapse.SynapseConstants;
023: import org.apache.synapse.Mediator;
024: import org.apache.synapse.MessageContext;
025: import org.apache.synapse.SynapseException;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: /**
030: * This is the superclass of all mediators, and defines common logging, tracing other aspects
031: * for all mediators who extend from this.
032: * elements of a mediator class.
033: */
034: public abstract class AbstractMediator implements Mediator {
035:
036: /** the standard log for mediators, will assign the logger for the actual subclass */
037: protected Log log;
038: /** The runtime trace log for mediators */
039: protected static final Log trace = LogFactory
040: .getLog(SynapseConstants.TRACE_LOGGER);
041:
042: /** State of tracing for this mediator */
043: protected int traceState = SynapseConstants.TRACING_UNSET;
044:
045: /**
046: * A constructor that makes subclasses pick up the correct logger
047: */
048: protected AbstractMediator() {
049: log = LogFactory.getLog(this .getClass());
050: }
051:
052: /**
053: * Returns the class name of the mediator
054: * @return the class name of the mediator
055: */
056: public String getType() {
057: String cls = getClass().getName();
058: int p = cls.lastIndexOf(".");
059: if (p == -1)
060: return cls;
061: else
062: return cls.substring(p + 1);
063: }
064:
065: /**
066: * Returns the tracing state
067: * @return the tracing state for this mediator (see SynapseConstants)
068: */
069: public int getTraceState() {
070: return traceState;
071: }
072:
073: /**
074: * Set the tracing state variable
075: * @param traceState the new tracing state for this mediator (see SynapseConstants)
076: */
077: public void setTraceState(int traceState) {
078: this .traceState = traceState;
079: }
080:
081: /**
082: * This method is used to save previous tracing state and set next the tracing
083: * state for a child mediator
084: *
085: * @param synCtx current message
086: */
087: public void setEffectiveTraceState(MessageContext synCtx) {
088: // if I have been explicitly asked to enable or disable tracing, use it and pass it on
089: // else, do nothing -> i.e. let the parents state flow
090: if (traceState != SynapseConstants.TRACING_UNSET) {
091: synCtx.setTracingState(traceState);
092: }
093: }
094:
095: /**
096: * Should this mediator perform tracing? True if its explicitly asked to
097: * trace, or its parent has been asked to trace and it does not reject it
098: * @param parentTraceState parents trace state
099: * @return true if tracing should be performed
100: */
101: public boolean shouldTrace(int parentTraceState) {
102: return (traceState == SynapseConstants.TRACING_ON)
103: || (traceState == SynapseConstants.TRACING_UNSET && parentTraceState == SynapseConstants.TRACING_ON);
104: }
105:
106: /**
107: * Should this mediator perform tracing? True if its explicitly asked to
108: * trace, or its parent has been asked to trace and it does not reject it
109: * @param msgCtx the current message
110: * @return true if tracing should be performed
111: */
112: protected boolean isTraceOn(MessageContext msgCtx) {
113: return (traceState == SynapseConstants.TRACING_ON)
114: || (traceState == SynapseConstants.TRACING_UNSET && msgCtx
115: .getTracingState() == SynapseConstants.TRACING_ON);
116: }
117:
118: /**
119: * Is tracing or debug logging on?
120: * @param isTraceOn is tracing known to be on?
121: * @return true, if either tracing or debug logging is on
122: */
123: protected boolean isTraceOrDebugOn(boolean isTraceOn) {
124: return isTraceOn || log.isDebugEnabled();
125: }
126:
127: /**
128: * Perform Trace and Debug logging of a message @INFO (trace) and DEBUG (log)
129: * @param traceOn is runtime trace on for this message?
130: * @param msg the message to log/trace
131: */
132: protected void traceOrDebug(boolean traceOn, String msg) {
133: if (traceOn) {
134: trace.info(msg);
135: }
136: if (log.isDebugEnabled()) {
137: log.debug(msg);
138: }
139: }
140:
141: /**
142: * Perform Trace and Debug logging of a message @WARN
143: * @param traceOn is runtime trace on for this message?
144: * @param msg the message to log/trace
145: */
146: protected void traceOrDebugWarn(boolean traceOn, String msg) {
147: if (traceOn) {
148: trace.warn(msg);
149: }
150: if (log.isDebugEnabled()) {
151: log.warn(msg);
152: }
153: }
154:
155: /**
156: * Perform an audit log message to all logs @ INFO. Writes to the general log, the service log
157: * and the trace log (of trace is on)
158: * @param msg the log message
159: * @param msgContext the message context
160: */
161: protected void auditLog(String msg, MessageContext msgContext) {
162: log.info(msg);
163: if (msgContext.getServiceLog() != null) {
164: msgContext.getServiceLog().info(msg);
165: }
166: if (shouldTrace(msgContext.getTracingState())) {
167: trace.info(msg);
168: }
169: }
170:
171: /**
172: * Perform an error log message to all logs @ ERROR. Writes to the general log, the service log
173: * and the trace log (of trace is on) and throws a SynapseException
174: * @param msg the log message
175: * @param msgContext the message context
176: */
177: protected void handleException(String msg, MessageContext msgContext) {
178: log.error(msg);
179: if (msgContext.getServiceLog() != null) {
180: msgContext.getServiceLog().error(msg);
181: }
182: if (shouldTrace(msgContext.getTracingState())) {
183: trace.error(msg);
184: }
185: throw new SynapseException(msg);
186: }
187:
188: /**
189: * Write an audit entry at WARN and trace and standard logs @WARN
190: * @param msg the message to log
191: * @param msgContext message context
192: */
193: protected void auditWarn(String msg, MessageContext msgContext) {
194: log.warn(msg);
195: if (msgContext.getServiceLog() != null) {
196: msgContext.getServiceLog().warn(msg);
197: }
198: if (shouldTrace(msgContext.getTracingState())) {
199: trace.warn(msg);
200: }
201: }
202:
203: /**
204: * Perform an error log message to all logs @ ERROR. Writes to the general log, the service log
205: * and the trace log (of trace is on) and throws a SynapseException
206: * @param msg the log message
207: * @param e an Exception encountered
208: * @param msgContext the message context
209: */
210: protected void handleException(String msg, Exception e,
211: MessageContext msgContext) {
212: log.error(msg, e);
213: if (msgContext.getServiceLog() != null) {
214: msgContext.getServiceLog().error(msg, e);
215: }
216: if (shouldTrace(msgContext.getTracingState())) {
217: trace.error(msg, e);
218: }
219: throw new SynapseException(msg, e);
220: }
221: }
|