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 * @deprecated since 4.5 this is some Java specific stuff that should by handled by SQ Java plugin
037 */
038 @Deprecated
039 public class ProjectClasspath implements BatchComponent {
040
041 protected MavenProject pom;
042 private List<File> elements;
043 private URLClassLoader classloader;
044
045 public ProjectClasspath(MavenProject pom) {
046 this.pom = pom;
047 }
048
049 public URLClassLoader getClassloader() {
050 if (classloader == null) {
051 classloader = createClassLoader();
052 }
053 return classloader;
054 }
055
056 /**
057 * bytecode directory + JARs (dependencies)
058 */
059 public List<File> getElements() {
060 if (elements == null) {
061 elements = createElements();
062 }
063 return elements;
064 }
065
066 protected URLClassLoader createClassLoader() {
067 try {
068 List<URL> urls = Lists.newArrayList();
069 for (File file : getElements()) {
070 urls.add(file.toURI().toURL());
071 }
072 return new URLClassLoader(urls.toArray(new URL[urls.size()]), null);
073
074 } catch (MalformedURLException e) {
075 throw new SonarException("Fail to create the project classloader. Classpath element is unvalid.", e);
076 }
077 }
078
079 protected List<File> createElements() {
080 try {
081 List<File> files = Lists.newArrayList();
082 if (pom.getCompileClasspathElements() != null) {
083 for (String classPathString : (List<String>) pom.getCompileClasspathElements()) {
084 files.add(new File(classPathString));
085 }
086 }
087
088 if (pom.getBuild().getOutputDirectory() != null) {
089 File outputDirectoryFile = new File(pom.getBuild().getOutputDirectory());
090 if (outputDirectoryFile.exists()) {
091 files.add(outputDirectoryFile);
092 }
093 }
094 return files;
095 } catch (DependencyResolutionRequiredException e) {
096 throw new SonarException("Fail to create the project classloader", e);
097 }
098 }
099 }