001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 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 */
020package org.sonar.api.batch;
021
022import com.google.common.collect.Lists;
023import org.apache.maven.artifact.DependencyResolutionRequiredException;
024import org.apache.maven.project.MavenProject;
025import org.sonar.api.BatchComponent;
026import org.sonar.api.utils.SonarException;
027
028import java.io.File;
029import java.net.MalformedURLException;
030import java.net.URL;
031import java.net.URLClassLoader;
032import java.util.List;
033
034/**
035 * @since 2.2
036 */
037public 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}