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  public class ModuleId {
38  
39      public static ModuleId getInstance(String moduleId) {
40          ModuleId moduleIdObj = _moduleIds.get(moduleId);
41  
42          if (moduleIdObj == null) {
43              moduleIdObj =  new ModuleId(moduleId);
44  
45              _moduleIds.put(moduleId, moduleIdObj);
46          }
47  
48          return moduleIdObj;
49      }
50  
51      public static String toString(
52          String groupId, String artifactId, String version, String type) {
53  
54          return groupId + StringPool.SLASH + artifactId + StringPool.SLASH +
55              version + StringPool.SLASH + type;
56      }
57  
58      public String getGroupId() {
59          return _groupId;
60      }
61  
62      public String getArtifactId() {
63          return _artifactId;
64      }
65  
66      public String getPackageId() {
67          return _groupId + StringPool.SLASH + _artifactId;
68      }
69  
70      public String getVersion() {
71          return _pluginVersion.toString();
72      }
73  
74      public String getType() {
75          return _type;
76      }
77  
78      public String getArtifactPath() {
79          return StringPool.SLASH + _groupId + StringPool.SLASH + _artifactId +
80              StringPool.SLASH + _pluginVersion + StringPool.SLASH +
81                  getArtifactWARName();
82      }
83  
84      public String getArtifactWARName() {
85          return _artifactId + StringPool.DASH + _pluginVersion +
86              StringPool.PERIOD + _type;
87      }
88  
89      public boolean isLaterVersionThan(String version) {
90          return _pluginVersion.isLaterVersionThan(version);
91      }
92  
93      public boolean isPreviousVersionThan(String version) {
94          return _pluginVersion.isPreviousVersionThan(version);
95      }
96  
97      public boolean isSameVersionAs(String version) {
98          return _pluginVersion.isSameVersionAs(version);
99      }
100 
101     public boolean equals(Object obj) {
102         if (!(obj instanceof ModuleId)) {
103             return false;
104         }
105 
106         ModuleId moduleId = (ModuleId)obj;
107 
108         return toString().equals(moduleId.toString());
109     }
110 
111     public int hashCode() {
112         return toString().hashCode();
113     }
114 
115     public String toString() {
116         return toString(
117             _groupId, _artifactId, _pluginVersion.toString(), _type);
118     }
119 
120     protected ModuleId(
121         String groupId, String artifactId, Version pluginVersion, String type) {
122 
123         _groupId = groupId;
124         _artifactId = artifactId;
125         _pluginVersion = pluginVersion;
126         _type = type;
127     }
128 
129     protected ModuleId(String moduleId) {
130         StringTokenizer st = new StringTokenizer(moduleId, StringPool.SLASH);
131 
132         if (st.countTokens() < 4) {
133             throw new RuntimeException(
134                 "The moduleId " + moduleId + " is not correct");
135         }
136 
137         _groupId = st.nextToken();
138         _artifactId = st.nextToken();
139         _pluginVersion = Version.getInstance(st.nextToken());
140         _type = st.nextToken();
141     }
142 
143     private static Map<String, ModuleId> _moduleIds =
144         new ConcurrentHashMap<String, ModuleId>();
145 
146     private String _artifactId;
147     private String _groupId;
148     private Version _pluginVersion;
149     private String _type;
150 
151 }