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.support;
018    
019    import org.codehaus.gmaven.feature.ComponentException;
020    import org.codehaus.gmaven.feature.Configuration;
021    import org.codehaus.gmaven.feature.Feature;
022    import org.codehaus.gmaven.feature.support.ComponentSupport;
023    import org.codehaus.gmaven.runtime.util.Compiler;
024    
025    import java.io.File;
026    import java.net.MalformedURLException;
027    import java.net.URL;
028    import java.util.Collection;
029    import java.util.Collections;
030    import java.util.HashSet;
031    import java.util.Set;
032    
033    /**
034     * Support for {@link Compiler} component implementations.
035     *
036     * @version $Id: CompilerSupport.java 49 2009-10-16 14:03:56Z user57 $
037     * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
038     */
039    public abstract class CompilerSupport
040        extends ComponentSupport
041        implements Compiler
042    {
043        protected final Set sources = new HashSet();
044    
045        protected File targetDir;
046    
047        private URL[] classPath;
048    
049        protected CompilerSupport(final Feature feature, final Configuration config) {
050            super(feature, config);
051        }
052    
053        protected CompilerSupport(final Feature feature) {
054            super(feature);
055        }
056    
057        public void setTargetDirectory(final File dir) {
058            assert dir != null;
059    
060            this.targetDir = dir;
061        }
062    
063        public File getTargetDirectory() {
064            if (targetDir == null) {
065                throw new IllegalStateException("Target directory not bound");
066            }
067    
068            return targetDir;
069        }
070    
071        public void add(final URL source) {
072            assert source != null;
073    
074            sources.add(source);
075            log.debug("Added: {}", source);
076        }
077    
078        public void add(final File source) {
079            assert source != null;
080    
081            try {
082                add(source.toURI().toURL());
083            }
084            catch (MalformedURLException e) {
085                throw new ComponentException("Failed to coerce File to URL: " + source, e);
086            }
087        }
088    
089        public Collection sources() {
090            return Collections.unmodifiableCollection(sources);
091        }
092    
093        public void setClassPath(final URL[] urls) {
094            assert urls != null && urls.length > 0;
095    
096            this.classPath = urls;
097        }
098    
099        public URL[] getClassPath() {
100            if (classPath == null) {
101                return new URL[0];
102            }
103    
104            return classPath;
105        }
106    }