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.geronimo.commands;
021:
022: import java.io.PrintStream;
023:
024: import org.apache.tools.ant.Project;
025: import org.apache.tools.ant.BuildLogger;
026: import org.apache.tools.ant.DefaultLogger;
027:
028: import org.slf4j.Logger;
029:
030: import org.apache.geronimo.gshell.command.IO;
031:
032: /**
033: * Custom Ant builder to setup the desired output formatting.
034: *
035: * @version $Rev: 573399 $ $Date: 2007-09-06 15:19:03 -0700 (Thu, 06 Sep 2007) $
036: */
037: public class AntBuilder extends groovy.util.AntBuilder {
038: public AntBuilder(final Logger log, final IO io) {
039: super (createProject(log, io));
040: }
041:
042: protected static Project createProject(final Logger log, final IO io) {
043: Project project = new Project();
044: project.addBuildListener(new OutputAdapter(log, io));
045: project.init();
046: project.getBaseDir();
047:
048: return project;
049: }
050:
051: private static class OutputAdapter extends DefaultLogger {
052: private Logger log;
053:
054: private IO io;
055:
056: public OutputAdapter(final Logger log, final IO io) {
057: assert log != null;
058: assert io != null;
059:
060: this .log = log;
061: this .io = io;
062:
063: setOutputPrintStream(new PrintStream(io.outputStream, true));
064: setErrorPrintStream(new PrintStream(io.errorStream, true));
065: setEmacsMode(true);
066:
067: String level = System
068: .getProperty("gshell.log.console.level");
069: if ("DEBUG".equals(level)) {
070: setMessageOutputLevel(Project.MSG_DEBUG);
071: } else {
072: setMessageOutputLevel(Project.MSG_INFO);
073: }
074: }
075:
076: protected void printMessage(final String message,
077: final PrintStream stream, final int priority) {
078: assert message != null;
079: assert stream != null;
080:
081: switch (priority) {
082: case Project.MSG_ERR:
083: log.error(message);
084: break;
085:
086: case Project.MSG_WARN:
087: log.warn(message);
088: break;
089:
090: case Project.MSG_INFO:
091: stream.println(message);
092: break;
093:
094: case Project.MSG_VERBOSE:
095: case Project.MSG_DEBUG:
096: log.debug(message);
097: break;
098:
099: default:
100: // Should never happen
101: throw new Error("Invalid priority: " + priority);
102: }
103: }
104: }
105: }
|