1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.plugin;
24  
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.util.Version;
27  
28  import java.util.Map;
29  import java.util.StringTokenizer;
30  import java.util.concurrent.ConcurrentHashMap;
31  
32  /**
33   * <a href="ModuleId.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Jorge Ferrer
36   *
37   */
38  public class ModuleId {
39  
40      public static ModuleId getInstance(String moduleId) {
41          ModuleId moduleIdObj = _moduleIds.get(moduleId);
42  
43          if (moduleIdObj == null) {
44              moduleIdObj =  new ModuleId(moduleId);
45  
46              _moduleIds.put(moduleId, moduleIdObj);
47          }
48  
49          return moduleIdObj;
50      }
51  
52      public static String toString(
53          String groupId, String artifactId, String version, String type) {
54  
55          return groupId + StringPool.SLASH + artifactId + StringPool.SLASH +
56              version + StringPool.SLASH + type;
57      }
58  
59      public String getGroupId() {
60          return _groupId;
61      }
62  
63      public String getArtifactId() {
64          return _artifactId;
65      }
66  
67      public String getPackageId() {
68          return _groupId + StringPool.SLASH + _artifactId;
69      }
70  
71      public String getVersion() {
72          return _pluginVersion.toString();
73      }
74  
75      public String getType() {
76          return _type;
77      }
78  
79      public String getArtifactPath() {
80          return StringPool.SLASH + _groupId + StringPool.SLASH + _artifactId +
81              StringPool.SLASH + _pluginVersion + StringPool.SLASH +
82                  getArtifactWARName();
83      }
84  
85      public String getArtifactWARName() {
86          return _artifactId + StringPool.DASH + _pluginVersion +
87              StringPool.PERIOD + _type;
88      }
89  
90      public boolean isLaterVersionThan(String version) {
91          return _pluginVersion.isLaterVersionThan(version);
92      }
93  
94      public boolean isPreviousVersionThan(String version) {
95          return _pluginVersion.isPreviousVersionThan(version);
96      }
97  
98      public boolean isSameVersionAs(String version) {
99          return _pluginVersion.isSameVersionAs(version);
100     }
101 
102     public boolean equals(Object obj) {
103         if (!(obj instanceof ModuleId)) {
104             return false;
105         }
106 
107         ModuleId moduleId = (ModuleId)obj;
108 
109         return toString().equals(moduleId.toString());
110     }
111 
112     public int hashCode() {
113         return toString().hashCode();
114     }
115 
116     public String toString() {
117         return toString(
118             _groupId, _artifactId, _pluginVersion.toString(), _type);
119     }
120 
121     protected ModuleId(
122         String groupId, String artifactId, Version pluginVersion, String type) {
123 
124         _groupId = groupId;
125         _artifactId = artifactId;
126         _pluginVersion = pluginVersion;
127         _type = type;
128     }
129 
130     protected ModuleId(String moduleId) {
131         StringTokenizer st = new StringTokenizer(moduleId, StringPool.SLASH);
132 
133         if (st.countTokens() < 4) {
134             throw new RuntimeException(
135                 "The moduleId " + moduleId + " is not correct");
136         }
137 
138         _groupId = st.nextToken();
139         _artifactId = st.nextToken();
140         _pluginVersion = Version.getInstance(st.nextToken());
141         _type = st.nextToken();
142     }
143 
144     private static Map<String, ModuleId> _moduleIds =
145         new ConcurrentHashMap<String, ModuleId>();
146 
147     private String _artifactId;
148     private String _groupId;
149     private Version _pluginVersion;
150     private String _type;
151 
152 }