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.GroovyClassLoader;
020    import org.codehaus.gmaven.feature.Component;
021    import org.codehaus.gmaven.feature.support.FeatureSupport;
022    import org.codehaus.gmaven.runtime.StubCompiler;
023    import org.codehaus.gmaven.runtime.support.CompilerSupport;
024    import org.codehaus.groovy.control.CompilerConfiguration;
025    
026    import java.net.URL;
027    import java.security.CodeSource;
028    import java.util.HashMap;
029    import java.util.Iterator;
030    import java.util.Map;
031    
032    /**
033     * Provides the stub compilation feature.
034     *
035     * @version $Id: StubCompilerFeature.java 52 2009-11-22 10:32:14Z user57 $
036     * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
037     */
038    public class StubCompilerFeature
039        extends FeatureSupport
040    {
041        public StubCompilerFeature() {
042            super(StubCompiler.KEY);
043        }
044    
045        @Override
046        protected Component doCreate() throws Exception {
047            return new StubCompilerImpl();
048        }
049    
050        StubCompilerImpl createInternal() throws Exception {
051            return new StubCompilerImpl();
052        }
053    
054        //
055        // StubCompilerImpl
056        //
057    
058        private class StubCompilerImpl
059            extends CompilerSupport
060            implements StubCompiler
061        {
062            private CompilerConfiguration cc = new CompilerConfiguration();
063    
064            private StubCompilerImpl() throws Exception {
065                super(StubCompilerFeature.this);
066            }
067    
068            public int compile() throws Exception {
069                if (sources.isEmpty()) {
070                    log.debug("No sources added to compile; skipping");
071                    return 0;
072                }
073    
074                Map<String,Object> options = new HashMap<String,Object>();
075                options.put("stubDir", getTargetDirectory());
076                cc.setJointCompilationOptions(options);
077                ClassLoader parent = ClassLoader.getSystemClassLoader();
078    
079                GroovyClassLoader gcl = new GroovyClassLoader(parent, cc);
080    
081                log.debug("Classpath:");
082    
083                // Append each URL to the GCL
084                URL[] classpath = getClassPath();
085    
086                for (int i=0; i<classpath.length; i++) {
087                    gcl.addURL(classpath[i]);
088                    log.debug("    {}", classpath[i]);
089                }
090    
091                CodeSource security = null;
092                GroovyClassLoader transformLoader = new GroovyClassLoader(getClass().getClassLoader());
093                for (int i=0; i<classpath.length; i++) {
094                    transformLoader.addURL(classpath[i]);
095                }
096    
097                JavaStubCompilationUnit cu = new JavaStubCompilationUnit(cc, gcl);
098    
099                log.debug("Compiling {} stubs for source(s)", sources.size());
100    
101                for (Iterator iter = sources.iterator(); iter.hasNext();) {
102                    URL url = (URL) iter.next();
103                    log.debug("    {}", url);
104                    cu.addSource(url);
105                }
106    
107                cu.compile();
108    
109                int count = cu.getStubCount();
110                log.debug("Compiled {} stubs", String.valueOf(count));
111    
112                return count;
113            }
114        }
115    }