001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.pipeline.stage;
019
020 import org.apache.commons.pipeline.StageException;
021 import org.apache.commons.pipeline.stage.BaseStage;
022 import java.util.Queue;
023 import org.apache.commons.logging.Log;
024 import org.apache.commons.logging.LogFactory;
025
026
027 /**
028 * A do-nothing implementation of Stage that simply logs the state of processing.
029 * and each object seen by its {@link #process(Object)} method.
030 * Useful for debugging purposes.
031 */
032 public class LogStage extends BaseStage {
033 private Log log = LogFactory.getLog(LogStage.class);
034
035 /**
036 * Creates a new LogStage.
037 */
038 public LogStage() {
039 }
040
041 /**
042 * Logs the point at which preprocessing runs.
043 */
044 public void preprocess() throws StageException {
045 super.preprocess();
046 log.info("Stage " + this.getClass().getName() + " preprocessing.");
047 }
048
049 /**
050 * Logs the current state of an object on the queue and passes the
051 * object unchanged to the next stage in the pipeline.
052 */
053 public void process(Object obj) throws StageException {
054 log.info("Processing object " + obj);
055 this.emit(obj);
056 }
057
058 /**
059 * Logs tht point at which postprocessing runs
060 */
061 public void postprocess() throws StageException {
062 log.info("Stage " + this.getClass().getName() + " postprocessing.");
063 }
064
065 /**
066 * Logs the point at which stage resources are released.
067 */
068 public void release() {
069 log.info("Stage " + this.getClass().getName() + " released.");
070 }
071
072 /**
073 * Sets the logger.
074 */
075 public synchronized void setLog(Log log) {
076 this.log = log;
077 }
078
079 /**
080 * Sets the logger based upon the log name.
081 */
082 public synchronized void setLog(String logName) {
083 this.log = LogFactory.getLog(logName);
084 }
085
086 /**
087 * Sets the logger based upon the specified class.
088 */
089 public synchronized void setLog(Class clazz) {
090 this.log = LogFactory.getLog(clazz);
091 }
092 }