View Javadoc
1   /**
2    * This file Copyright (c) 2003-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.model;
35  
36  import info.magnolia.module.ModuleVersionHandler;
37  
38  import java.util.ArrayList;
39  import java.util.Collection;
40  
41  import org.apache.commons.lang3.StringUtils;
42  
43  /**
44   * Describes a module. Bean representation of a module's xml descriptor.
45   */
46  public class ModuleDefinition {
47      private String name;
48      private String displayName;
49      private String description;
50      private String className;
51      private Class<? extends ModuleVersionHandler> versionHandler;
52      private Version version;
53      private Collection<DependencyDefinition> dependencies = new ArrayList<>();
54      private Collection<ServletDefinition> servlets = new ArrayList<>();
55      private Collection<RepositoryDefinition> repositories = new ArrayList<>();
56      private Collection<PropertyDefinition> properties = new ArrayList<>();
57      private Collection<ComponentsDefinition> components = new ArrayList<>();
58  
59      public ModuleDefinition() {
60      }
61  
62      public ModuleDefinition(String name, Version version, String className, Class<? extends ModuleVersionHandler> versionHandler) {
63          this.name = name;
64          this.version = version;
65          this.className = className;
66          this.versionHandler = versionHandler;
67      }
68  
69      public String getName() {
70          return this.name;
71      }
72  
73      public void setName(String name) {
74          this.name = name;
75      }
76  
77      /**
78       * Returns the displayName or the name if displayName wasn't set.
79       */
80      public String getDisplayName() {
81          if (StringUtils.isEmpty(this.displayName)) {
82              return this.name;
83          }
84  
85          return this.displayName;
86      }
87  
88      public void setDisplayName(String displayName) {
89          this.displayName = displayName;
90      }
91  
92      public String getDescription() {
93          return this.description;
94      }
95  
96      public void setDescription(String description) {
97          this.description = description;
98      }
99  
100     public String getClassName() {
101         return this.className;
102     }
103 
104     public void setClassName(String className) {
105         this.className = className;
106     }
107 
108     public Class<? extends ModuleVersionHandler> getVersionHandler() {
109         return versionHandler;
110     }
111 
112     public void setVersionHandler(Class<? extends ModuleVersionHandler> versionHandler) {
113         this.versionHandler = versionHandler;
114     }
115 
116     public void setVersion(Version version) {
117         this.version = version;
118     }
119 
120     public Version getVersion() {
121         return version;
122     }
123 
124     public Collection<DependencyDefinition> getDependencies() {
125         return this.dependencies;
126     }
127 
128     public void setDependencies(Collection<DependencyDefinition> dependencies) {
129         this.dependencies = dependencies;
130     }
131 
132     public void addDependency(DependencyDefinition dep) {
133         dependencies.add(dep);
134     }
135 
136     public Collection<ServletDefinition> getServlets() {
137         return this.servlets;
138     }
139 
140     public void setServlets(Collection<ServletDefinition> servlets) {
141         for (ServletDefinition def : servlets) {
142             this.addServlet(def);
143         }
144     }
145 
146     public void addServlet(ServletDefinition def) {
147         if (StringUtils.isEmpty(def.getComment())) {
148             def.setComment("a servlet used by the " + this.getName() + " module");
149         }
150         this.servlets.add(def);
151     }
152 
153     public Collection<RepositoryDefinition> getRepositories() {
154         return this.repositories;
155     }
156 
157     public void setRepositories(Collection<RepositoryDefinition> repositories) {
158         this.repositories = repositories;
159     }
160 
161     public void addRepository(RepositoryDefinition repository) {
162         this.repositories.add(repository);
163     }
164 
165     public Collection<PropertyDefinition> getProperties() {
166         return properties;
167     }
168 
169     public void setProperties(Collection<PropertyDefinition> properties) {
170         this.properties = properties;
171     }
172 
173     public void addProperty(PropertyDefinition property) {
174         properties.add(property);
175     }
176 
177     public Collection<ComponentsDefinition> getComponents() {
178         return components;
179     }
180 
181     public void setComponents(Collection<ComponentsDefinition> components) {
182         this.components = components;
183     }
184 
185     public boolean addComponents(ComponentsDefinition components) {
186         return this.components.add(components);
187     }
188 
189     /**
190      * Convenience method which returns the value of the given property,
191      * or null if it does not exist.
192      */
193     public String getProperty(String propertyName) {
194         for (PropertyDefinition p : properties) {
195             if (propertyName.equals(p.getName())) {
196                 return p.getValue();
197             }
198         }
199         return null;
200     }
201 
202     @Override
203     public String toString() {
204         return getDisplayName() + " (version " + version + ")";
205     }
206 }