1   /**
2    * Copyright (c) 2000-2008 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.search.SearchException;
28  import com.liferay.portal.kernel.util.ListUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.util.Version;
31  
32  import java.util.ArrayList;
33  import java.util.HashMap;
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 _pendingPackages.get(context);
72      }
73  
74      public PluginPackage getLatestPluginPackage(
75          String groupId, String artifactId) {
76  
77          PluginPackage latestPluginPackage = null;
78  
79          for (PluginPackage pluginPackage : _pluginPackages.values()) {
80              if ((pluginPackage.getGroupId().equals(groupId)) &&
81                  (pluginPackage.getArtifactId().equals(artifactId)) &&
82                  ((latestPluginPackage == null) ||
83                      pluginPackage.isLaterVersionThan(latestPluginPackage))) {
84  
85                  latestPluginPackage = pluginPackage;
86              }
87          }
88  
89          return latestPluginPackage;
90      }
91  
92      public PluginPackage getPluginPackage(String context) {
93          return _pluginPackages.get(context);
94      }
95  
96      public List<PluginPackage> getPluginPackages() {
97          return new ArrayList<PluginPackage>(_pluginPackages.values());
98      }
99  
100     public List<PluginPackage> getPluginPackages(
101         String groupId, String artifactId) {
102 
103         List<PluginPackage> pluginPackages = new ArrayList<PluginPackage>();
104 
105         for (PluginPackage pluginPackage : _pluginPackages.values()) {
106             if (pluginPackage.getGroupId().equals(groupId) &&
107                 pluginPackage.getArtifactId().equals(artifactId)) {
108 
109                 pluginPackages.add(pluginPackage);
110             }
111         }
112 
113         return pluginPackages;
114     }
115 
116     public List<PluginPackage> getSortedPluginPackages() {
117         List<PluginPackage> pluginPackages = new ArrayList<PluginPackage>();
118 
119         pluginPackages.addAll(_pluginPackages.values());
120 
121         return ListUtil.sort(
122             pluginPackages, new PluginPackageNameAndContextComparator());
123     }
124 
125     public void removePluginPackage(PluginPackage pluginPackage) {
126         _pluginPackages.remove(pluginPackage.getContext());
127 
128         try {
129             PluginPackageIndexer.removePluginPackage(
130                 pluginPackage.getModuleId());
131         }
132         catch (SearchException se) {
133             _log.error("Deleting index " + pluginPackage.getModuleId(), se);
134         }
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 = _pluginPackages.get(
144                 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<String, PluginPackage> _pluginPackages =
187         new HashMap<String, PluginPackage>();
188     private Map<String, PluginPackage> _pendingPackages =
189         new HashMap<String, PluginPackage>();
190 
191 }