001    /*
002     * Copyright (C) 2006-2007 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.codehaus.gmaven.runtime.v1_5;
018    
019    import groovy.lang.Binding;
020    import groovy.ui.InteractiveShell;
021    import org.codehaus.gmaven.feature.Component;
022    import org.codehaus.gmaven.feature.Configuration;
023    import org.codehaus.gmaven.feature.support.ComponentSupport;
024    import org.codehaus.gmaven.feature.support.FeatureSupport;
025    import org.codehaus.gmaven.runtime.Shell;
026    import org.codehaus.gmaven.runtime.support.util.NoExitSecurityManager;
027    import org.codehaus.groovy.tools.shell.Groovysh;
028    import org.codehaus.groovy.tools.shell.IO;
029    import org.codehaus.groovy.tools.shell.Main;
030    import org.codehaus.groovy.tools.shell.util.Logger;
031    import org.sonatype.gshell.util.io.StreamSet;
032    
033    /**
034     * Provides the command-line shell feature.
035     *
036     * @version $Id: ShellFeature.java 58 2009-11-26 10:15:40Z user57 $
037     * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
038     */
039    public class ShellFeature
040        extends FeatureSupport
041    {
042        public ShellFeature() {
043            super(Shell.KEY);
044        }
045    
046        protected Component doCreate() throws Exception {
047            return new ShellImpl();
048        }
049    
050        //
051        // ShellImpl
052        //
053        
054        private class ShellImpl
055            extends ComponentSupport
056            implements Shell, Shell.Keys
057        {
058            private ShellImpl() throws Exception {
059                super(ShellFeature.this);
060            }
061    
062            public void execute(final ClassLoader classLoader) throws Exception {
063                assert classLoader != null;
064    
065                boolean legacy = config().get(LEGACY, false);
066    
067                final StreamSet streams = StreamSet.system();
068    
069                // Put a nice blank before and after we run the shell
070                streams.out.println();
071    
072                SecurityManager sm = System.getSecurityManager();
073                System.setSecurityManager(new NoExitSecurityManager());
074    
075                try {
076                    if (!legacy) {
077                        new DefaultTask(config()).run(classLoader);
078                    }
079                    else {
080                        new LegacyTask().run(classLoader);
081                    }
082                }
083                finally {
084                    System.setSecurityManager(sm);
085    
086                    StreamSet.system(streams);
087                }
088    
089                // The blank after
090                streams.out.println();
091            }
092        }
093    
094        //
095        // Task
096        //
097    
098        private interface Task
099        {
100            void run(ClassLoader classLoader) throws Exception;
101        }
102    
103        //
104        // DefaultTask
105        //
106    
107        private class DefaultTask
108            implements Shell.Keys, Task
109        {
110            private final IO io;
111    
112            private final String args;
113    
114            public DefaultTask(final Configuration config) {
115                assert config != null;
116    
117                io = new IO();
118                
119                Logger.io = io;
120    
121                if (config.get(VERBOSE, false)) {
122                    io.setVerbosity(IO.Verbosity.VERBOSE);
123                }
124    
125                if (config.get(DEBUG, false)) {
126                    io.setVerbosity(IO.Verbosity.DEBUG);
127                }
128    
129                if (config.get(QUIET, false)) {
130                    io.setVerbosity(IO.Verbosity.QUIET);
131                }
132    
133                String color = config.get(COLOR, Boolean.TRUE.toString());
134                if (color != null) {
135                    Main.setColor(color);
136                }
137    
138                String term = config.get(TERMINAL, (String)null);
139                if (term != null) {
140                    Main.setTerminalType(term);
141                }
142    
143                args = config.get(ARGS, (String)null);
144            }
145            
146            public void run(final ClassLoader classLoader) throws Exception {
147                assert classLoader != null;
148    
149                Groovysh shell = new Groovysh(classLoader, new Binding(), io);
150                
151                shell.run(args);
152            }
153        }
154    
155        //
156        // LegacyTask
157        //
158        
159        private class LegacyTask
160            implements Task
161        {
162            public void run(final ClassLoader classLoader) throws Exception {
163                assert classLoader != null;
164    
165                InteractiveShell shell = new InteractiveShell(classLoader, new Binding(), System.in, System.out, System.err);
166                shell.run();
167            }
168        }
169    }