001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
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.plugin;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.apache.commons.lang.builder.ToStringBuilder;
024    import org.apache.commons.lang.builder.ToStringStyle;
025    import org.hibernate.annotations.Cascade;
026    import org.sonar.api.database.BaseIdentifiable;
027    
028    import java.net.URI;
029    import java.util.ArrayList;
030    import java.util.Date;
031    import java.util.List;
032    
033    import javax.persistence.CascadeType;
034    import javax.persistence.Column;
035    import javax.persistence.Entity;
036    import javax.persistence.FetchType;
037    import javax.persistence.OneToMany;
038    import javax.persistence.Table;
039    
040    /**
041     * Installed plugins
042     * 
043     * @since 2.2
044     */
045    @Entity
046    @Table(name = "plugins")
047    public class JpaPlugin extends BaseIdentifiable {
048    
049      @Column(name = "plugin_key", updatable = true, nullable = false, length = 100)
050      private String key;
051    
052      @Column(name = "version", updatable = true, nullable = true, length = 100)
053      private String version;
054    
055      @Column(name = "name", updatable = true, nullable = true, length = 100)
056      private String name;
057    
058      @Column(name = "description", updatable = true, nullable = true, length = 3000)
059      private String description;
060    
061      @Column(name = "organization", updatable = true, nullable = true, length = 100)
062      private String organization;
063    
064      @Column(name = "organization_url", updatable = true, nullable = true, length = 500)
065      private String organizationUrl;
066    
067      @Column(name = "license", updatable = true, nullable = true, length = 50)
068      private String license;
069    
070      @Column(name = "installation_date", updatable = true, nullable = true)
071      private Date installationDate;
072    
073      @Column(name = "plugin_class", updatable = true, nullable = true, length = 100)
074      private String pluginClass;
075    
076      @Column(name = "homepage", updatable = true, nullable = true, length = 500)
077      private String homepage;
078    
079      @Column(name = "core", updatable = true, nullable = true)
080      private Boolean core;
081      
082      @Column(name = "child_first_classloader", updatable = true, nullable = true)
083      private Boolean childFirstClassLoader = Boolean.FALSE;
084    
085      @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
086                org.hibernate.annotations.CascadeType.DELETE,
087                org.hibernate.annotations.CascadeType.MERGE,
088                org.hibernate.annotations.CascadeType.PERSIST,
089                org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
090      @OneToMany(mappedBy = "plugin", cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
091      private List<JpaPluginFile> files = new ArrayList<JpaPluginFile>();
092    
093      public JpaPlugin() {
094      }
095    
096      public JpaPlugin(String pluginKey) {
097        if (StringUtils.isBlank(pluginKey)) {
098          throw new IllegalArgumentException("LocalExtension.pluginKey can not be blank");
099        }
100        this.key = pluginKey;
101      }
102    
103      public String getKey() {
104        return key;
105      }
106    
107      public JpaPlugin setKey(String s) {
108        this.key = s;
109        return this;
110      }
111    
112      public String getName() {
113        return name;
114      }
115    
116      public JpaPlugin setName(String name) {
117        this.name = name;
118        return this;
119      }
120    
121      public String getDescription() {
122        return description;
123      }
124    
125      public JpaPlugin setDescription(String description) {
126        this.description = description;
127        return this;
128      }
129    
130      public String getOrganization() {
131        return organization;
132      }
133    
134      public JpaPlugin setOrganization(String organization) {
135        this.organization = organization;
136        return this;
137      }
138    
139      public String getOrganizationUrl() {
140        return organizationUrl;
141      }
142    
143      public JpaPlugin setOrganizationUrl(URI uri) {
144        this.organizationUrl = (uri != null ? uri.toString() : null);
145        return this;
146      }
147    
148      public JpaPlugin setOrganizationUrl(String s) {
149        this.organizationUrl = s;
150        return this;
151      }
152    
153      public String getLicense() {
154        return license;
155      }
156    
157      public JpaPlugin setLicense(String license) {
158        this.license = license;
159        return this;
160      }
161    
162      public String getVersion() {
163        return version;
164      }
165    
166      public JpaPlugin setVersion(String s) {
167        this.version = s;
168        return this;
169      }
170    
171      public Date getInstallationDate() {
172        return installationDate;
173      }
174    
175      public JpaPlugin setInstallationDate(Date installationDate) {
176        this.installationDate = installationDate;
177        return this;
178      }
179    
180      public String getPluginClass() {
181        return pluginClass;
182      }
183    
184      public JpaPlugin setPluginClass(String s) {
185        this.pluginClass = s;
186        return this;
187      }
188    
189      public String getHomepage() {
190        return homepage;
191      }
192    
193      public JpaPlugin setHomepage(URI uri) {
194        this.homepage = (uri != null ? uri.toString() : null);
195        return this;
196      }
197    
198      public JpaPlugin setHomepage(String s) {
199        this.homepage = s;
200        return this;
201      }
202    
203      public Boolean isCore() {
204        return core;
205      }
206    
207      public JpaPlugin setCore(Boolean b) {
208        this.core = b;
209        return this;
210      }
211    
212      public Boolean isUseChildFirstClassLoader() {
213        return childFirstClassLoader;
214      }
215    
216      public JpaPlugin setUseChildFirstClassLoader(boolean use) {
217        this.childFirstClassLoader = use;
218        return this;
219      }
220    
221      public void createFile(String filename) {
222        JpaPluginFile file = new JpaPluginFile(this, filename);
223        this.files.add(file);
224      }
225    
226      public List<JpaPluginFile> getFiles() {
227        return files;
228      }
229    
230      public void removeFiles() {
231        files.clear();
232      }
233    
234      @Override
235      public boolean equals(Object o) {
236        if (this == o) {
237          return true;
238        }
239        if (o == null || getClass() != o.getClass()) {
240          return false;
241        }
242        JpaPlugin other = (JpaPlugin) o;
243        return key.equals(other.key);
244      }
245    
246      @Override
247      public int hashCode() {
248        return key.hashCode();
249      }
250    
251      @Override
252      public String toString() {
253        return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
254            .append("id", getId())
255            .append("key", key)
256            .append("version", version)
257            .append("homepage", homepage)
258            .append("installationDate", installationDate)
259            .toString();
260      }
261    
262      public static JpaPlugin create(String pluginKey) {
263        return new JpaPlugin(pluginKey);
264      }
265    
266    }