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