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.plugin;
018    
019    import java.io.File;
020    import java.io.IOException;
021    import java.util.Collections;
022    import java.util.HashMap;
023    import java.util.Iterator;
024    import java.util.Map;
025    import java.util.Set;
026    import java.util.TreeSet;
027    
028    import org.apache.maven.project.MavenProject;
029    
030    /**
031     * Support for communication between stub generation and compilation.
032     *
033     * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
034     * @version $Id: CompileState.java 8 2009-07-16 09:15:04Z user57 $
035     */
036    public class CompileState
037    {
038        private Map forceCompile = new HashMap();
039    
040        private Map forceCompileTest = new HashMap();
041    
042        public synchronized void addForcedCompilationSource(final MavenProject project, final File file) {
043            String projectKey = projectKey(project);
044    
045            if (!forceCompile.containsKey(projectKey)) {
046                forceCompile.put(projectKey, new TreeSet());
047            }
048    
049            ((Set) forceCompile.get(projectKey)).add(file);
050        }
051    
052        public synchronized Set getForcedCompilationSources(final MavenProject project) {
053            String projectKey = projectKey(project);
054    
055            if (forceCompile.containsKey(projectKey)) {
056                Set files = new TreeSet();
057    
058                for (Iterator i = ((Set) forceCompile.get(projectKey)).iterator(); i.hasNext();) {
059                    File file = (File) i.next();
060    
061                    if (file.isFile()) {
062                        files.add(file);
063                    }
064                }
065    
066                return Collections.unmodifiableSet(files);
067            }
068            else {
069                return Collections.unmodifiableSet(new TreeSet());
070            }
071        }
072    
073        public synchronized void addForcedCompilationTestSource(final MavenProject project, final File file) {
074            String projectKey = projectKey(project);
075    
076            if (!forceCompileTest.containsKey(projectKey)) {
077                forceCompileTest.put(projectKey, new TreeSet());
078            }
079    
080            ((Set) forceCompileTest.get(projectKey)).add(file);
081        }
082    
083        public synchronized Set getForcedCompilationTestSources(final MavenProject project) {
084            String projectKey = projectKey(project);
085    
086            if (forceCompileTest.containsKey(projectKey)) {
087                Set files = new TreeSet();
088                for (Iterator i = ((Set) forceCompileTest.get(projectKey)).iterator(); i.hasNext();) {
089                    File file = (File) i.next();
090                    if (file.isFile()) {
091                        files.add(file);
092                    }
093                }
094    
095                return Collections.unmodifiableSet(files);
096            }
097            else {
098                return Collections.unmodifiableSet(new TreeSet());
099            }
100        }
101    
102        private String projectKey(final MavenProject project) {
103            assert project != null;
104    
105            if (project.getBasedir() == null || !project.getBasedir().isDirectory()) {
106                throw new IllegalStateException("Project " + project.getId() + " does not define a base directory: " + project.getBasedir());
107            }
108    
109            try {
110                return project.getBasedir().getCanonicalPath();
111            }
112            catch (IOException e) {
113                throw new RuntimeException(e);
114            }
115        }
116    }