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.ivy.util;
019:
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Stack;
024:
025: /**
026: * A {@link MessageLogger} implementation delegating the work to the current top logger on a stack.
027: * <p>
028: * When the logger stack is empty, it delegates the work to a default logger, which by default is
029: * the {@link Message#getDefaultLogger()}.
030: * </p>
031: * <p>
032: * {@link #pushLogger(MessageLogger)} should be called to delegate to a new logger, and
033: * {@link #popLogger()} should be called when the context of this logger is finished.
034: * </p>
035: */
036: public class MessageLoggerEngine implements MessageLogger {
037: private final Stack/*<MessageLogger>*/loggerStack = new Stack();
038:
039: private MessageLogger defaultLogger = null;
040:
041: private List problems = new ArrayList();
042:
043: private List warns = new ArrayList();
044:
045: private List errors = new ArrayList();
046:
047: public MessageLoggerEngine() {
048: }
049:
050: /**
051: * Sets the logger used when the stack is empty.
052: *
053: * @param defaultLogger the logger to use when the stack is empty.
054: */
055: public void setDefaultLogger(MessageLogger defaultLogger) {
056: this .defaultLogger = defaultLogger;
057: }
058:
059: /**
060: * Push a logger on the stack.
061: *
062: * @param logger
063: * the logger to push. Must not be <code>null</code>.
064: */
065: public void pushLogger(MessageLogger logger) {
066: Checks.checkNotNull(logger, "logger");
067: loggerStack.push(logger);
068: }
069:
070: /**
071: * Pops a logger from the logger stack.
072: * <p>
073: * Does nothing if the logger stack is empty
074: * </p>
075: */
076: public void popLogger() {
077: if (!loggerStack.isEmpty()) {
078: loggerStack.pop();
079: }
080: }
081:
082: /**
083: * Returns the current logger, or the default one if there is no logger in the stack
084: * @return the current logger, or the default one if there is no logger in the stack
085: */
086: private MessageLogger peekLogger() {
087: if (loggerStack.isEmpty()) {
088: return getDefaultLogger();
089: }
090: return (MessageLogger) loggerStack.peek();
091: }
092:
093: private MessageLogger getDefaultLogger() {
094: // we don't store the logger returned by Message.getDefaultLogger() to always stay in sync
095: // as long as our default logger has not been set explicitly with setDefaultLogger()
096: return defaultLogger == null ? Message.getDefaultLogger()
097: : defaultLogger;
098: }
099:
100: // consolidated methods
101: public void warn(String msg) {
102: peekLogger().warn(msg);
103: problems.add("WARN: " + msg);
104: warns.add(msg);
105: }
106:
107: public void error(String msg) {
108: peekLogger().error(msg);
109: problems.add("\tERROR: " + msg);
110: errors.add(msg);
111: }
112:
113: public List getErrors() {
114: return errors;
115: }
116:
117: public List getProblems() {
118: return problems;
119: }
120:
121: public List getWarns() {
122: return warns;
123: }
124:
125: public void sumupProblems() {
126: MessageLoggerHelper.sumupProblems(this );
127: clearProblems();
128: }
129:
130: public void clearProblems() {
131: getDefaultLogger().clearProblems();
132: for (Iterator iter = loggerStack.iterator(); iter.hasNext();) {
133: MessageLogger l = (MessageLogger) iter.next();
134: l.clearProblems();
135: }
136: problems.clear();
137: errors.clear();
138: warns.clear();
139: }
140:
141: public void setShowProgress(boolean progress) {
142: getDefaultLogger().setShowProgress(progress);
143: // updates all loggers in the stack
144: for (Iterator iter = loggerStack.iterator(); iter.hasNext();) {
145: MessageLogger l = (MessageLogger) iter.next();
146: l.setShowProgress(progress);
147: }
148: }
149:
150: public boolean isShowProgress() {
151: // testing the default logger is enough, all loggers should be in sync
152: return getDefaultLogger().isShowProgress();
153: }
154:
155: // delegation methods
156:
157: public void debug(String msg) {
158: peekLogger().debug(msg);
159: }
160:
161: public void deprecated(String msg) {
162: peekLogger().deprecated(msg);
163: }
164:
165: public void endProgress() {
166: peekLogger().endProgress();
167: }
168:
169: public void endProgress(String msg) {
170: peekLogger().endProgress(msg);
171: }
172:
173: public void info(String msg) {
174: peekLogger().info(msg);
175: }
176:
177: public void rawinfo(String msg) {
178: peekLogger().rawinfo(msg);
179: }
180:
181: public void log(String msg, int level) {
182: peekLogger().log(msg, level);
183: }
184:
185: public void progress() {
186: peekLogger().progress();
187: }
188:
189: public void rawlog(String msg, int level) {
190: peekLogger().rawlog(msg, level);
191: }
192:
193: public void verbose(String msg) {
194: peekLogger().verbose(msg);
195: }
196:
197: }
|