001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2011 SonarSource
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.core.plugins;
021
022 import org.apache.commons.io.FileUtils;
023 import org.sonar.api.Plugin;
024 import org.sonar.api.utils.SonarException;
025 import org.sonar.api.utils.ZipUtils;
026 import org.sonar.updatecenter.common.PluginKeyUtils;
027 import org.sonar.updatecenter.common.PluginManifest;
028
029 import java.io.File;
030 import java.io.IOException;
031 import java.net.URL;
032 import java.net.URLClassLoader;
033 import java.util.List;
034 import java.util.zip.ZipEntry;
035
036 public class PluginFileExtractor {
037
038 public DefaultPluginMetadata installInSameLocation(File pluginFile, boolean isCore, List<File> deprecatedExtensions) {
039 return install(pluginFile, isCore, deprecatedExtensions, null);
040 }
041
042 public DefaultPluginMetadata install(File pluginFile, boolean isCore, List<File> deprecatedExtensions, File toDir) {
043 DefaultPluginMetadata metadata = extractMetadata(pluginFile, isCore);
044 metadata.setDeprecatedExtensions(deprecatedExtensions);
045 return install(metadata, toDir);
046 }
047
048 public DefaultPluginMetadata install(DefaultPluginMetadata metadata, File toDir) {
049 try {
050 File pluginFile = metadata.getFile();
051 File pluginBasedir;
052 if (toDir != null) {
053 pluginBasedir = toDir;
054 FileUtils.forceMkdir(pluginBasedir);
055 File targetFile = new File(pluginBasedir, pluginFile.getName());
056 FileUtils.copyFile(pluginFile, targetFile);
057 metadata.addDeployedFile(targetFile);
058 } else {
059 pluginBasedir = pluginFile.getParentFile();
060 metadata.addDeployedFile(pluginFile);
061 }
062
063 if (metadata.getPathsToInternalDeps().length > 0) {
064 // needs to unzip the jar
065 ZipUtils.unzip(pluginFile, pluginBasedir, new ZipUtils.ZipEntryFilter() {
066 public boolean accept(ZipEntry entry) {
067 return entry.getName().startsWith("META-INF/lib");
068 }
069 });
070 for (String depPath : metadata.getPathsToInternalDeps()) {
071 File dependency = new File(pluginBasedir, depPath);
072 if (!dependency.isFile() || !dependency.exists()) {
073 throw new IllegalArgumentException("Dependency " + depPath + " can not be found in " + pluginFile.getName());
074 }
075 metadata.addDeployedFile(dependency);
076 }
077 }
078
079 for (File extension : metadata.getDeprecatedExtensions()) {
080 File toFile = new File(pluginBasedir, extension.getName());
081 if (!toFile.equals(extension)) {
082 FileUtils.copyFile(extension, toFile);
083 }
084 metadata.addDeployedFile(toFile);
085 }
086
087 return metadata;
088
089 } catch (IOException e) {
090 throw new SonarException("Fail to install plugin: " + metadata, e);
091 }
092 }
093
094 public DefaultPluginMetadata extractMetadata(File file, boolean isCore) {
095 try {
096 PluginManifest manifest = new PluginManifest(file);
097 DefaultPluginMetadata metadata = DefaultPluginMetadata.create(file);
098 metadata.setKey(manifest.getKey());
099 metadata.setName(manifest.getName());
100 metadata.setDescription(manifest.getDescription());
101 metadata.setLicense(manifest.getLicense());
102 metadata.setOrganization(manifest.getOrganization());
103 metadata.setOrganizationUrl(manifest.getOrganizationUrl());
104 metadata.setMainClass(manifest.getMainClass());
105 metadata.setVersion(manifest.getVersion());
106 metadata.setHomepage(manifest.getHomepage());
107 metadata.setPathsToInternalDeps(manifest.getDependencies());
108 metadata.setUseChildFirstClassLoader(manifest.isUseChildFirstClassLoader());
109 metadata.setBasePlugin(manifest.getBasePlugin());
110 metadata.setCore(isCore);
111 if (metadata.isOldManifest()) {
112 completeDeprecatedMetadata(metadata);
113 }
114 return metadata;
115
116 } catch (IOException e) {
117 throw new IllegalStateException("Fail to extract plugin metadata from file: " + file, e);
118 }
119 }
120
121 private void completeDeprecatedMetadata(DefaultPluginMetadata metadata) throws IOException {
122 String mainClass = metadata.getMainClass();
123 File pluginFile = metadata.getFile();
124 try {
125 // copy file in a temp directory because Windows+Oracle JVM Classloader lock the JAR file
126 File tempFile = File.createTempFile(pluginFile.getName(), null);
127 FileUtils.copyFile(pluginFile, tempFile);
128
129 URLClassLoader pluginClassLoader = URLClassLoader.newInstance(new URL[]{tempFile.toURI().toURL()}, getClass().getClassLoader());
130 Plugin pluginInstance = (Plugin) pluginClassLoader.loadClass(mainClass).newInstance();
131 metadata.setKey(PluginKeyUtils.sanitize(pluginInstance.getKey()));
132 metadata.setDescription(pluginInstance.getDescription());
133 metadata.setName(pluginInstance.getName());
134
135 } catch (Exception e) {
136 throw new RuntimeException("The metadata main class can not be created. Plugin file=" + pluginFile.getName() + ", class=" + mainClass, e);
137 }
138 }
139 }