001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2009 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar 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 * Sonar 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
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.api.batch;
021
022 import org.apache.maven.artifact.DependencyResolutionRequiredException;
023 import org.apache.maven.project.MavenProject;
024 import org.sonar.api.BatchComponent;
025 import org.sonar.api.utils.SonarException;
026
027 import java.io.File;
028 import java.net.MalformedURLException;
029 import java.net.URL;
030 import java.net.URLClassLoader;
031 import java.util.ArrayList;
032 import java.util.List;
033
034 /**
035 * @since 2.2
036 */
037 public class ProjectClasspath implements BatchComponent {
038
039 private 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 = new ArrayList<URL>();
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 = new ArrayList<File>();
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 }