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.deprecated;
22 import org.apache.maven.model.Developer;
23 import org.json.simple.JSONObject;
26 import java.io.IOException;
27 import java.text.SimpleDateFormat;
28 import java.util.Date;
29 import java.util.List;
30 import java.util.jar.Attributes;
31 import java.util.jar.JarFile;
32 import java.util.jar.Manifest;
33 import java.util.zip.ZipEntry;
36 * Information about Sonar Plugin.
38 * @author Evgeny Mandrikov
40 public class Plugin implements Versioned {
43 private String description;
44 private String version;
45 private String downloadUrl;
46 private String requiredSonarVersion;
47 private String homepage;
48 private long timestamp;
50 private String pluginClass;
51 private String issueTracker;
52 private String sources;
53 private String license;
55 private List<Developer> developers;
57 public Plugin(String pluginKey) {
61 public String getKey() {
65 public void setKey(String key) {
72 public String getName() {
76 public void setName(String name) {
80 public String getDescription() {
84 public void setDescription(String description) {
85 this.description = description;
91 public String getVersion() {
95 public void setVersion(String version) {
96 this.version = version;
99 public String getReleaseDate() {
100 return (new SimpleDateFormat("d MMM yyyy")).format(new Date(timestamp));
103 private void setDate(long timestamp) {
104 this.timestamp = timestamp;
108 * @return URL for downloading
110 public String getDownloadUrl() {
114 public void setDownloadUrl(String downloadUrl) {
115 this.downloadUrl = downloadUrl;
119 * @return minimal Sonar version to run this plugin
121 public String getRequiredSonarVersion() {
122 // TODO Sonar-Version from MANIFEST.MF
123 return requiredSonarVersion;
126 public void setRequiredSonarVersion(String sonarVersion) {
127 this.requiredSonarVersion = sonarVersion;
133 public String getHomepage() {
134 // TODO Plugin-Homepage from MANIFEST.MF
138 public void setHomepage(String homepage) {
139 this.homepage = homepage;
142 public String getIssueTracker() {
146 public void setIssueTracker(String url) {
147 this.issueTracker = url;
150 public String getSources() {
154 public void setSources(String sources) {
155 this.sources = sources;
158 public String getLicense() {
162 public void setLicense(String license) {
163 this.license = license;
166 public List<Developer> getDevelopers() {
170 public void setDevelopers(List<Developer> developers) {
171 this.developers = developers;
174 public JSONObject toJsonObject() {
175 JSONObject obj = new JSONObject();
176 obj.put("id", getKey());
177 obj.put("name", getName());
178 obj.put("version", getVersion());
179 obj.put("sonarVersion", getRequiredSonarVersion());
180 if (getDownloadUrl() != null) {
181 obj.put("downloadUrl", getDownloadUrl());
183 if (getHomepage() != null) {
184 obj.put("homepage", getHomepage());
189 public static Plugin extractMetadata(File file) throws IOException {
190 JarFile jar = new JarFile(file);
191 ZipEntry entry = jar.getEntry("META-INF/MANIFEST.MF");
192 long timestamp = entry.getTime();
193 Manifest manifest = jar.getManifest();
196 Attributes attributes = manifest.getMainAttributes();
197 String pluginKey = attributes.getValue("Plugin-Key");
198 Plugin plugin = new Plugin(pluginKey);
199 plugin.setName(attributes.getValue("Plugin-Name"));
200 plugin.setVersion(attributes.getValue("Plugin-Version"));
201 plugin.setRequiredSonarVersion(attributes.getValue("Sonar-Version"));
202 plugin.setHomepage(attributes.getValue("Plugin-Homepage"));
203 plugin.setDate(timestamp);