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: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
036     */
037    public class AntBuilder
038        extends groovy.util.AntBuilder
039    {
040        public AntBuilder(final Logger log, final IO io) {
041            super(createProject(log, io));
042        }
043    
044        protected static Project createProject(final Logger log, final IO io) {
045            Project project = new Project();
046            project.addBuildListener(new OutputAdapter(log, io));
047            project.init();
048            project.getBaseDir();
049    
050            return project;
051        }
052        
053        private static class OutputAdapter
054            extends DefaultLogger
055        {
056            private Logger log;
057            
058            private IO io;
059    
060            public OutputAdapter(final Logger log, final IO io) {
061                assert log != null;
062                assert io != null;
063                
064                this.log = log;
065                this.io = io;
066                
067                setOutputPrintStream(new PrintStream(io.outputStream, true));
068                setErrorPrintStream(new PrintStream(io.errorStream, true));
069                setEmacsMode(true);
070                
071                String level = System.getProperty("gshell.log.console.level");
072                if ("DEBUG".equals(level)) {
073                    setMessageOutputLevel(Project.MSG_DEBUG);
074                }
075                else {
076                    setMessageOutputLevel(Project.MSG_INFO);
077                }
078            }
079    
080            protected void printMessage(final String message, final PrintStream stream, final int priority) {
081                assert message != null;
082                assert stream != null;
083                
084                switch (priority) {
085                    case Project.MSG_ERR:
086                        log.error(message);
087                        break;
088    
089                    case Project.MSG_WARN:
090                        log.warn(message);
091                        break;
092    
093                    case Project.MSG_INFO:
094                        stream.println(message);
095                        break;
096    
097                    case Project.MSG_VERBOSE:
098                    case Project.MSG_DEBUG:
099                        log.debug(message);
100                        break;
101                    
102                    default:
103                        // Should never happen
104                        throw new Error("Invalid priority: " + priority);
105                }
106            }
107        }
108    }