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 org.codehaus.gmaven.feature.Component;
021    import org.codehaus.gmaven.feature.support.ComponentSupport;
022    import org.codehaus.gmaven.feature.support.FeatureSupport;
023    import org.codehaus.gmaven.runtime.Console;
024    import org.codehaus.gmaven.runtime.support.util.NoExitSecurityManager;
025    import org.sonatype.gshell.util.io.StreamSet;
026    
027    import java.util.EventObject;
028    
029    /**
030     * Provides the GUI console feature.
031     *
032     * @version $Id: ConsoleFeature.java 58 2009-11-26 10:15:40Z user57 $
033     * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
034     */
035    public class ConsoleFeature
036        extends FeatureSupport
037    {
038        public ConsoleFeature() {
039            super(Console.KEY);
040        }
041    
042        @Override
043        protected Component doCreate() throws Exception {
044            return new ConsoleImpl();
045        }
046    
047        //
048        // ConsoleImpl
049        //
050    
051        private class ConsoleImpl
052            extends ComponentSupport
053            implements Console
054        {
055            private final Object lock = new Object();
056    
057            private ConsoleImpl() throws Exception {
058                super(ConsoleFeature.this);
059            }
060    
061            public void execute(final ClassLoader classLoader) throws Exception {
062                assert classLoader != null;
063    
064                final StreamSet streams = StreamSet.system();
065    
066                final SecurityManager sm = System.getSecurityManager();
067    
068                System.setSecurityManager(new NoExitSecurityManager());
069    
070                try {
071                    final groovy.ui.Console console = new groovy.ui.Console(classLoader, new Binding()) {
072                        public void exit(final EventObject event) {
073                            try {
074                                super.exit(event);
075                            }
076                            finally {
077                                synchronized (lock) {
078                                    lock.notifyAll();
079                                }
080                            }
081                        }
082                    };
083    
084                    console.run();
085    
086                    synchronized (lock) {
087                        lock.wait();
088                    }
089                }
090                finally {
091                    System.setSecurityManager(sm);
092                    StreamSet.system(streams);
093                }
094            }
095        }
096    }