001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * SonarQube is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.api.batch;
021    
022    import com.google.common.collect.Lists;
023    import org.apache.maven.artifact.DependencyResolutionRequiredException;
024    import org.apache.maven.project.MavenProject;
025    import org.sonar.api.BatchComponent;
026    import org.sonar.api.utils.SonarException;
027    
028    import java.io.File;
029    import java.net.MalformedURLException;
030    import java.net.URL;
031    import java.net.URLClassLoader;
032    import java.util.List;
033    
034    /**
035     * @since 2.2
036     */
037    public class ProjectClasspath implements BatchComponent {
038    
039      protected MavenProject pom;
040      private List<File> elements;
041      private URLClassLoader classloader;
042    
043      public ProjectClasspath(MavenProject pom) {
044        this.pom = pom;
045      }
046    
047      public URLClassLoader getClassloader() {
048        if (classloader == null) {
049          classloader = createClassLoader();
050        }
051        return classloader;
052      }
053    
054      /**
055       * bytecode directory + JARs (dependencies)
056       */
057      public List<File> getElements() {
058        if (elements == null) {
059          elements = createElements();
060        }
061        return elements;
062      }
063    
064      protected URLClassLoader createClassLoader() {
065        try {
066          List<URL> urls = Lists.newArrayList();
067          for (File file : getElements()) {
068            urls.add(file.toURI().toURL());
069          }
070          return new URLClassLoader(urls.toArray(new URL[urls.size()]), null);
071    
072        } catch (MalformedURLException e) {
073          throw new SonarException("Fail to create the project classloader. Classpath element is unvalid.", e);
074        }
075      }
076    
077      protected List<File> createElements() {
078        try {
079          List<File> files = Lists.newArrayList();
080          if (pom.getCompileClasspathElements() != null) {
081            for (String classPathString : (List<String>) pom.getCompileClasspathElements()) {
082              files.add(new File(classPathString));
083            }
084          }
085    
086          if (pom.getBuild().getOutputDirectory() != null) {
087            File outputDirectoryFile = new File(pom.getBuild().getOutputDirectory());
088            if (outputDirectoryFile.exists()) {
089              files.add(outputDirectoryFile);
090            }
091          }
092          return files;
093        } catch (DependencyResolutionRequiredException e) {
094          throw new SonarException("Fail to create the project classloader", e);
095        }
096      }
097    }