1   /**
2    * Copyright (c) 2000-2007 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.plugin.PluginPackage;
26  import com.liferay.portal.kernel.plugin.PluginPackageNameAndContextComparator;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.util.Version;
29  
30  import java.util.ArrayList;
31  import java.util.Collections;
32  import java.util.HashMap;
33  import java.util.Iterator;
34  import java.util.List;
35  import java.util.Map;
36  
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  
40  /**
41   * <a href="LocalPluginPackageRepository.java.html"><b><i>View Source</i></b>
42   * </a>
43   *
44   * @author Jorge Ferrer
45   *
46   */
47  public class LocalPluginPackageRepository {
48  
49      public LocalPluginPackageRepository() {
50      }
51  
52      public void addPluginPackage(PluginPackage pluginPackage) {
53          if (pluginPackage.getContext() == null) {
54              if (_log.isDebugEnabled()) {
55                  _log.debug(
56                      "Plugin package cannot be registered because it does not " +
57                          "have an installation context");
58              }
59  
60              return;
61          }
62  
63          _pendingPackages.remove(pluginPackage.getContext());
64          _pendingPackages.remove(pluginPackage.getModuleId());
65  
66          _pluginPackages.remove(pluginPackage.getContext());
67          _pluginPackages.put(pluginPackage.getContext(), pluginPackage);
68      }
69  
70      public PluginPackage getInstallingPluginPackage(String context) {
71          return (PluginPackage)_pendingPackages.get(context);
72      }
73  
74      public PluginPackage getLatestPluginPackage(
75          String groupId, String artifactId) {
76  
77          PluginPackage result = null;
78  
79          Iterator itr = _pluginPackages.values().iterator();
80  
81          while (itr.hasNext()) {
82              PluginPackage pluginPackage = (PluginPackage)itr.next();
83  
84              if (pluginPackage.getGroupId().equals(groupId) &&
85                  pluginPackage.getArtifactId().equals(artifactId) &&
86                  (result == null ||
87                      pluginPackage.isLaterVersionThan(result))) {
88  
89                  result = pluginPackage;
90              }
91          }
92  
93          return result;
94  
95      }
96  
97      public PluginPackage getPluginPackage(String context) {
98          return (PluginPackage)_pluginPackages.get(context);
99      }
100 
101     public List getPluginPackages() {
102         return new ArrayList(_pluginPackages.values());
103     }
104 
105     public List getPluginPackages(String groupId, String artifactId) {
106         List result = new ArrayList();
107 
108         Iterator itr = _pluginPackages.values().iterator();
109 
110         while (itr.hasNext()) {
111             PluginPackage pluginPackage = (PluginPackage)itr.next();
112 
113             if (pluginPackage.getGroupId().equals(groupId) &&
114                 pluginPackage.getArtifactId().equals(artifactId)) {
115 
116                 result.add(pluginPackage);
117             }
118         }
119 
120         return result;
121     }
122 
123     public List getSortedPluginPackages() {
124         List list = new ArrayList();
125 
126         list.addAll(_pluginPackages.values());
127 
128         Collections.sort(list, new PluginPackageNameAndContextComparator());
129 
130         return list;
131     }
132 
133     public void removePluginPackage(PluginPackage pluginPackage) {
134         _pluginPackages.remove(pluginPackage.getContext());
135     }
136 
137     public void removePluginPackage(String context) {
138         _pluginPackages.remove(context);
139     }
140 
141     public void registerPluginPackageInstallation(PluginPackage pluginPackage) {
142         if (pluginPackage.getContext() != null) {
143             PluginPackage previousPluginPackage =
144                 (PluginPackage)_pluginPackages.get(pluginPackage.getContext());
145 
146             if (previousPluginPackage == null) {
147                 addPluginPackage(pluginPackage);
148             }
149         }
150 
151         String key = pluginPackage.getContext();
152 
153         if (key == null) {
154             key = pluginPackage.getModuleId();
155         }
156 
157         _pendingPackages.put(key, pluginPackage);
158     }
159 
160     public void registerPluginPackageInstallation(String deploymentContext) {
161         PluginPackage pluginPackage = getPluginPackage(deploymentContext);
162 
163         if (pluginPackage == null) {
164             String moduleId =
165                 deploymentContext + StringPool.SLASH + deploymentContext +
166                     StringPool.SLASH + Version.UNKNOWN + StringPool.SLASH +
167                         "war";
168 
169             pluginPackage = new PluginPackageImpl(moduleId);
170 
171             pluginPackage.setName(deploymentContext);
172             pluginPackage.setContext(deploymentContext);
173         }
174 
175         registerPluginPackageInstallation(pluginPackage);
176     }
177 
178     public void unregisterPluginPackageInstallation(String context) {
179         _pluginPackages.remove(context);
180         _pendingPackages.remove(context);
181     }
182 
183     private static Log _log =
184         LogFactory.getLog(LocalPluginPackageRepository.class);
185 
186     private Map _pluginPackages = new HashMap();
187     private Map _pendingPackages = new HashMap();
188 
189 }