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_6;
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        @Override
047        protected Component doCreate() throws Exception {
048            return new ShellImpl();
049        }
050    
051        //
052        // ShellImpl
053        //
054        
055        private class ShellImpl
056            extends ComponentSupport
057            implements Shell, Shell.Keys
058        {
059            private ShellImpl() throws Exception {
060                super(ShellFeature.this);
061            }
062    
063            public void execute(final ClassLoader classLoader) throws Exception {
064                assert classLoader != null;
065    
066                boolean legacy = config().get(LEGACY, false);
067    
068                final StreamSet streams = StreamSet.system();
069    
070                // Put a nice blank before and after we run the shell
071                streams.out.println();
072    
073                SecurityManager sm = System.getSecurityManager();
074                System.setSecurityManager(new NoExitSecurityManager());
075    
076                try {
077                    if (!legacy) {
078                        new DefaultTask(config()).run(classLoader);
079                    }
080                    else {
081                        new LegacyTask().run(classLoader);
082                    }
083                }
084                finally {
085                    System.setSecurityManager(sm);
086    
087                    StreamSet.system(streams);
088                }
089    
090                // The blank after
091                streams.out.println();
092            }
093        }
094    
095        //
096        // Task
097        //
098    
099        private interface Task
100        {
101            void run(ClassLoader classLoader) throws Exception;
102        }
103    
104        //
105        // DefaultTask
106        //
107    
108        private class DefaultTask
109            implements Shell.Keys, Task
110        {
111            private final IO io;
112    
113            private final String args;
114    
115            public DefaultTask(final Configuration config) {
116                assert config != null;
117    
118                io = new IO();
119    
120                Logger.io = io;
121    
122                if (config.get(VERBOSE, false)) {
123                    io.setVerbosity(IO.Verbosity.VERBOSE);
124                }
125    
126                if (config.get(DEBUG, false)) {
127                    io.setVerbosity(IO.Verbosity.DEBUG);
128                }
129    
130                if (config.get(QUIET, false)) {
131                    io.setVerbosity(IO.Verbosity.QUIET);
132                }
133    
134                String color = config.get(COLOR, Boolean.TRUE.toString());
135                if (color != null) {
136                    Main.setColor(color);
137                }
138    
139                String term = config.get(TERMINAL, (String)null);
140                if (term != null) {
141                    Main.setTerminalType(term);
142                }
143    
144                args = config.get(ARGS, (String)null);
145            }
146    
147            public void run(final ClassLoader classLoader) throws Exception {
148                assert classLoader != null;
149    
150                Groovysh shell = new Groovysh(classLoader, new Binding(), io);
151    
152                shell.run(args);
153            }
154        }
155    
156        //
157        // LegacyTask
158        //
159    
160        private class LegacyTask
161            implements Task
162        {
163            public void run(final ClassLoader classLoader) throws Exception {
164                assert classLoader != null;
165    
166                InteractiveShell shell = new InteractiveShell(classLoader, new Binding(), System.in, System.out, System.err);
167                shell.run();
168            }
169        }
170    }