2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2009 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.updatecenter.common;
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.commons.lang.builder.ReflectionToStringBuilder;
26 import java.io.IOException;
27 import java.util.Date;
28 import java.util.jar.Attributes;
29 import java.util.jar.JarFile;
30 import java.util.jar.Manifest;
32 import static org.sonar.updatecenter.common.FormatUtils.toDate;
35 * This class loads Sonar plugin metadata from JAR manifest.
37 public final class PluginManifest {
39 public static final String KEY = "Plugin-Key";
40 public static final String MAIN_CLASS = "Plugin-Class";
41 public static final String NAME = "Plugin-Name";
42 public static final String DESCRIPTION = "Plugin-Description";
43 public static final String ORGANIZATION = "Plugin-Organization";
44 public static final String ORGANIZATION_URL = "Plugin-OrganizationUrl";
45 public static final String LICENSE = "Plugin-License";
46 public static final String VERSION = "Plugin-Version";
47 public static final String SONAR_VERSION = "Sonar-Version";
48 public static final String DEPENDENCIES = "Plugin-Dependencies";
49 public static final String HOMEPAGE = "Plugin-Homepage";
50 public static final String TERMS_CONDITIONS_URL = "Plugin-TermsConditionsUrl";
51 public static final String BUILD_DATE = "Plugin-BuildDate";
52 public static final String ISSUE_TRACKER_URL = "Plugin-IssueTrackerUrl";
57 public static final String USE_CHILD_FIRST_CLASSLOADER = "Plugin-ChildFirstClassLoader";
62 public static final String EXTEND_PLUGIN = "Extend-Plugin";
66 private String mainClass;
67 private String description;
68 private String organization;
69 private String organizationUrl;
70 private String license;
71 private String version;
72 private String sonarVersion;
73 private String[] dependencies = new String[0];
74 private String homepage;
75 private String termsConditionsUrl;
76 private Date buildDate;
77 private String issueTrackerUrl;
78 private boolean useChildFirstClassLoader = false;
79 private String extendPlugin;
82 * Load the manifest from a JAR file.
84 public PluginManifest(File file) throws IOException {
87 jar = new JarFile(file);
88 if (jar.getManifest() != null) {
89 loadManifest(jar.getManifest());
91 } catch (Exception e) {
92 throw new RuntimeException("Unable to read plugin manifest from jar : " + file.getAbsolutePath(), e);
101 * @param manifest can not be null
103 public PluginManifest(Manifest manifest) {
104 loadManifest(manifest);
107 public PluginManifest() {
110 private void loadManifest(Manifest manifest) {
111 Attributes attributes = manifest.getMainAttributes();
112 this.key = PluginKeyUtils.sanitize(attributes.getValue(KEY));
113 this.mainClass = attributes.getValue(MAIN_CLASS);
114 this.name = attributes.getValue(NAME);
115 this.description = attributes.getValue(DESCRIPTION);
116 this.license = attributes.getValue(LICENSE);
117 this.organization = attributes.getValue(ORGANIZATION);
118 this.organizationUrl = attributes.getValue(ORGANIZATION_URL);
119 this.version = attributes.getValue(VERSION);
120 this.homepage = attributes.getValue(HOMEPAGE);
121 this.termsConditionsUrl = attributes.getValue(TERMS_CONDITIONS_URL);
122 this.sonarVersion = attributes.getValue(SONAR_VERSION);
123 this.issueTrackerUrl = attributes.getValue(ISSUE_TRACKER_URL);
124 this.buildDate = toDate(attributes.getValue(BUILD_DATE), true);
125 this.useChildFirstClassLoader = StringUtils.equalsIgnoreCase(attributes.getValue(USE_CHILD_FIRST_CLASSLOADER), "true");
126 this.extendPlugin = attributes.getValue(EXTEND_PLUGIN);
128 String deps = attributes.getValue(DEPENDENCIES);
129 this.dependencies = StringUtils.split(StringUtils.defaultString(deps), ' ');
132 public String getKey() {
136 public PluginManifest setKey(String key) {
141 public String getName() {
145 public PluginManifest setName(String name) {
150 public String getDescription() {
154 public PluginManifest setDescription(String description) {
155 this.description = description;
159 public String getOrganization() {
163 public PluginManifest setOrganization(String organization) {
164 this.organization = organization;
168 public String getOrganizationUrl() {
169 return organizationUrl;
172 public PluginManifest setOrganizationUrl(String url) {
173 this.organizationUrl = url;
177 public String getLicense() {
181 public PluginManifest setLicense(String license) {
182 this.license = license;
186 public String getVersion() {
190 public PluginManifest setVersion(String version) {
191 this.version = version;
195 public String getSonarVersion() {
199 public PluginManifest setSonarVersion(String sonarVersion) {
200 this.sonarVersion = sonarVersion;
204 public String getMainClass() {
208 public PluginManifest setMainClass(String mainClass) {
209 this.mainClass = mainClass;
213 public String[] getDependencies() {
217 public PluginManifest setDependencies(String[] dependencies) {
218 this.dependencies = dependencies;
222 public Date getBuildDate() {
226 public PluginManifest setBuildDate(Date buildDate) {
227 this.buildDate = buildDate;
231 public String getHomepage() {
235 public PluginManifest setHomepage(String homepage) {
236 this.homepage = homepage;
240 public String getTermsConditionsUrl() {
241 return termsConditionsUrl;
244 public PluginManifest setTermsConditionsUrl(String termsConditionsUrl) {
245 this.termsConditionsUrl = termsConditionsUrl;
249 public String getIssueTrackerUrl() {
250 return issueTrackerUrl;
253 public PluginManifest setIssueTrackerUrl(String issueTrackerUrl) {
254 this.issueTrackerUrl = issueTrackerUrl;
261 public boolean isUseChildFirstClassLoader() {
262 return useChildFirstClassLoader;
268 public PluginManifest setUseChildFirstClassLoader(boolean useChildFirstClassLoader) {
269 this.useChildFirstClassLoader = useChildFirstClassLoader;
276 public String getExtendPlugin() {
283 public PluginManifest setExtendPlugin(String extendPlugin) {
284 this.extendPlugin = extendPlugin;
289 public String toString() {
290 return new ReflectionToStringBuilder(this).toString();
293 public boolean isValid() {
294 return StringUtils.isNotBlank(key) && StringUtils.isNotBlank(version);