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 static org.sonar.updatecenter.common.FormatUtils.toDate;
25 import java.io.IOException;
26 import java.util.Date;
27 import java.util.jar.Attributes;
28 import java.util.jar.JarFile;
29 import java.util.jar.Manifest;
31 import org.apache.commons.lang.StringUtils;
32 import org.apache.commons.lang.builder.ReflectionToStringBuilder;
35 * This class loads Sonar plugin metadata from JAR manifest.
39 public final class PluginManifest {
41 public static final String KEY = "Plugin-Key";
42 public static final String MAIN_CLASS = "Plugin-Class";
43 public static final String NAME = "Plugin-Name";
44 public static final String DESCRIPTION = "Plugin-Description";
45 public static final String ORGANIZATION = "Plugin-Organization";
46 public static final String ORGANIZATION_URL = "Plugin-OrganizationUrl";
47 public static final String LICENSE = "Plugin-License";
48 public static final String VERSION = "Plugin-Version";
49 public static final String SONAR_VERSION = "Sonar-Version";
50 public static final String DEPENDENCIES = "Plugin-Dependencies";
51 public static final String HOMEPAGE = "Plugin-Homepage";
52 public static final String TERMS_CONDITIONS_URL = "Plugin-TermsConditionsUrl";
53 public static final String BUILD_DATE = "Plugin-BuildDate";
54 public static final String ISSUE_TRACKER_URL = "Plugin-IssueTrackerUrl";
59 public static final String USE_CHILD_FIRST_CLASSLOADER = "Plugin-ChildFirstClassLoader";
63 private String mainClass;
64 private String description;
65 private String organization;
66 private String organizationUrl;
67 private String license;
68 private String version;
69 private String sonarVersion;
70 private String[] dependencies = new String[0];
71 private String homepage;
72 private String termsConditionsUrl;
73 private Date buildDate;
74 private String issueTrackerUrl;
75 private boolean useChildFirstClassLoader = false;
78 * Load the manifest from a JAR file.
80 public PluginManifest(File file) throws IOException {
83 jar = new JarFile(file);
84 if (jar.getManifest() != null) {
85 loadManifest(jar.getManifest());
87 } catch (Exception e) {
88 throw new RuntimeException("Unable to read plugin manifest from jar : " + file.getAbsolutePath(), e);
100 public PluginManifest(Manifest manifest) {
101 loadManifest(manifest);
104 public PluginManifest() {
107 private void loadManifest(Manifest manifest) {
108 Attributes attributes = manifest.getMainAttributes();
109 this.key = PluginKeyUtils.sanitize(attributes.getValue(KEY));
110 this.mainClass = attributes.getValue(MAIN_CLASS);
111 this.name = attributes.getValue(NAME);
112 this.description = attributes.getValue(DESCRIPTION);
113 this.license = attributes.getValue(LICENSE);
114 this.organization = attributes.getValue(ORGANIZATION);
115 this.organizationUrl = attributes.getValue(ORGANIZATION_URL);
116 this.version = attributes.getValue(VERSION);
117 this.homepage = attributes.getValue(HOMEPAGE);
118 this.termsConditionsUrl = attributes.getValue(TERMS_CONDITIONS_URL);
119 this.sonarVersion = attributes.getValue(SONAR_VERSION);
120 this.issueTrackerUrl = attributes.getValue(ISSUE_TRACKER_URL);
121 this.buildDate = toDate(attributes.getValue(BUILD_DATE), true);
122 this.useChildFirstClassLoader = StringUtils.equalsIgnoreCase(attributes.getValue(USE_CHILD_FIRST_CLASSLOADER), "true");
124 String deps = attributes.getValue(DEPENDENCIES);
125 this.dependencies = StringUtils.split(StringUtils.defaultString(deps), ' ');
128 public String getKey() {
132 public PluginManifest setKey(String key) {
137 public String getName() {
141 public PluginManifest setName(String name) {
146 public String getDescription() {
150 public PluginManifest setDescription(String description) {
151 this.description = description;
155 public String getOrganization() {
159 public PluginManifest setOrganization(String organization) {
160 this.organization = organization;
164 public String getOrganizationUrl() {
165 return organizationUrl;
168 public PluginManifest setOrganizationUrl(String url) {
169 this.organizationUrl = url;
173 public String getLicense() {
177 public PluginManifest setLicense(String license) {
178 this.license = license;
182 public String getVersion() {
186 public PluginManifest setVersion(String version) {
187 this.version = version;
191 public String getSonarVersion() {
195 public PluginManifest setSonarVersion(String sonarVersion) {
196 this.sonarVersion = sonarVersion;
200 public String getMainClass() {
204 public PluginManifest setMainClass(String mainClass) {
205 this.mainClass = mainClass;
209 public String[] getDependencies() {
213 public PluginManifest setDependencies(String[] dependencies) {
214 this.dependencies = dependencies;
218 public Date getBuildDate() {
222 public PluginManifest setBuildDate(Date buildDate) {
223 this.buildDate = buildDate;
227 public String getHomepage() {
231 public PluginManifest setHomepage(String homepage) {
232 this.homepage = homepage;
236 public String getTermsConditionsUrl() {
237 return termsConditionsUrl;
240 public PluginManifest setTermsConditionsUrl(String termsConditionsUrl) {
241 this.termsConditionsUrl = termsConditionsUrl;
245 public String getIssueTrackerUrl() {
246 return issueTrackerUrl;
249 public PluginManifest setIssueTrackerUrl(String issueTrackerUrl) {
250 this.issueTrackerUrl = issueTrackerUrl;
254 public boolean isUseChildFirstClassLoader() {
255 return useChildFirstClassLoader;
258 public PluginManifest setUseChildFirstClassLoader(boolean useChildFirstClassLoader) {
259 this.useChildFirstClassLoader = useChildFirstClassLoader;
264 public String toString() {
265 return new ReflectionToStringBuilder(this).toString();
268 public boolean isValid() {
269 return StringUtils.isNotBlank(key) && StringUtils.isNotBlank(version);