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 */
020package org.sonar.api.batch;
021
022import com.google.common.collect.Lists;
023import java.io.File;
024import java.net.MalformedURLException;
025import java.net.URL;
026import java.net.URLClassLoader;
027import java.util.List;
028import org.sonar.api.BatchComponent;
029import org.sonar.api.batch.bootstrap.ProjectDefinition;
030import org.sonar.api.resources.ProjectFileSystem;
031import org.sonar.api.utils.SonarException;
032
033/**
034 * @since 2.2
035 * @deprecated since 4.5 this is some Java specific stuff that should by handled by SQ Java plugin
036 */
037@Deprecated
038public class ProjectClasspath implements BatchComponent {
039
040  private final ProjectDefinition def;
041  private final ProjectFileSystem projectFileSystem;
042  private List<File> elements;
043  private URLClassLoader classloader;
044
045  public ProjectClasspath(ProjectDefinition def, ProjectFileSystem projectFileSystem) {
046    this.def = def;
047    this.projectFileSystem = projectFileSystem;
048  }
049
050  public URLClassLoader getClassloader() {
051    if (classloader == null) {
052      classloader = createClassLoader();
053    }
054    return classloader;
055  }
056
057  /**
058   * bytecode directory + JARs (dependencies)
059   */
060  public List<File> getElements() {
061    if (elements == null) {
062      elements = createElements();
063    }
064    return elements;
065  }
066
067  protected URLClassLoader createClassLoader() {
068    try {
069      List<URL> urls = Lists.newArrayList();
070      for (File file : getElements()) {
071        urls.add(file.toURI().toURL());
072      }
073      return new URLClassLoader(urls.toArray(new URL[urls.size()]), null);
074
075    } catch (MalformedURLException e) {
076      throw new SonarException("Fail to create the project classloader. Classpath element is unvalid.", e);
077    }
078  }
079
080  protected List<File> createElements() {
081    List<File> e = Lists.newArrayList();
082    for (String path : def.getBinaries()) {
083      e.add(projectFileSystem.resolvePath(path));
084    }
085    for (String path : def.getLibraries()) {
086      e.add(projectFileSystem.resolvePath(path));
087    }
088    return e;
089  }
090}