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 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        protected Component doCreate() throws Exception {
043            return new ConsoleImpl();
044        }
045    
046        //
047        // ConsoleImpl
048        //
049    
050        private class ConsoleImpl
051            extends ComponentSupport
052            implements Console
053        {
054            private final Object lock = new Object();
055    
056            private ConsoleImpl() throws Exception {
057                super(ConsoleFeature.this);
058            }
059    
060            public void execute(final ClassLoader classLoader) throws Exception {
061                assert classLoader != null;
062    
063                final StreamSet streams = StreamSet.system();
064    
065                final SecurityManager sm = System.getSecurityManager();
066    
067                System.setSecurityManager(new NoExitSecurityManager());
068    
069                try {
070                    final groovy.ui.Console console = new groovy.ui.Console(classLoader, new Binding()) {
071                        public void exit(final EventObject event) {
072                            try {
073                                super.exit(event);
074                            }
075                            finally {
076                                synchronized (lock) {
077                                    lock.notifyAll();
078                                }
079                            }
080                        }
081                    };
082    
083                    console.run();
084    
085                    synchronized (lock) {
086                        lock.wait();
087                    }
088                }
089                finally {
090                    System.setSecurityManager(sm);
091                    StreamSet.system(streams);
092                }
093            }
094        }
095    }