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_7;
018    
019    import groovy.lang.Closure;
020    import groovy.util.AntBuilder;
021    import org.apache.tools.ant.BuildLogger;
022    import org.codehaus.gmaven.feature.Component;
023    import org.codehaus.gmaven.feature.ComponentException;
024    import org.codehaus.gmaven.feature.support.FeatureSupport;
025    import org.codehaus.gmaven.runtime.ClassFactory;
026    import org.codehaus.gmaven.runtime.ScriptExecutor;
027    import org.codehaus.gmaven.runtime.support.ScriptExecutorSupport;
028    import org.codehaus.gmaven.runtime.util.Callable;
029    import org.codehaus.gmaven.runtime.util.MagicAttribute;
030    
031    /**
032     * Provides the script execution feature.
033     *
034     * @version $Id: ScriptExecutorFeature.java 49 2009-10-16 14:03:56Z user57 $
035     * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
036     */
037    public class ScriptExecutorFeature
038        extends FeatureSupport
039    {
040        public ScriptExecutorFeature() {
041            super(ScriptExecutor.KEY);
042        }
043    
044        @Override
045        protected Component doCreate() throws Exception {
046            return new ScriptExecutorImpl();
047        }
048    
049        //
050        // ScriptExecutorImpl
051        //
052    
053        private class ScriptExecutorImpl
054            extends ScriptExecutorSupport
055        {
056            private ScriptExecutorImpl() throws Exception {
057                super(ScriptExecutorFeature.this);
058            }
059    
060            @Override
061            protected ClassFactory getClassFactory() {
062                try {
063                    return (ClassFactory) provider().feature(ClassFactory.KEY).create(config());
064                }
065                catch (Exception e) {
066                    throw new ComponentException(e);
067                }
068            }
069    
070            @Override
071            protected Object createClosure(final Callable target) {
072                assert target != null;
073                
074                return new Closure(this) {
075                    public Object call(final Object[] args) {
076                        try {
077                            return target.call(args);
078                        }
079                        catch (Exception e) {
080                            return throwRuntimeException(e);
081                        }
082                    }
083                };
084            }
085    
086            private AntBuilder createAntBuilder() {
087                AntBuilder ant = new AntBuilder();
088    
089                Object obj = ant.getAntProject().getBuildListeners().elementAt(0);
090    
091                if (obj instanceof BuildLogger) {
092                    BuildLogger logger = (BuildLogger)obj;
093    
094                    logger.setEmacsMode(true);
095                }
096    
097                return ant;
098            }
099    
100            @Override
101            protected Object createMagicAttribute(final MagicAttribute attr) {
102                assert attr != null;
103    
104                if (attr == MagicAttribute.ANT_BUILDER) {
105                    return createAntBuilder();
106                }
107    
108                throw new ComponentException("Unknown magic attribute: " + attr);
109            }
110        }
111    }