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.StringPool;
29  import com.liferay.util.Version;
30  
31  import java.util.ArrayList;
32  import java.util.Collections;
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         Collections.sort(
122             pluginPackages, new PluginPackageNameAndContextComparator());
123 
124         return pluginPackages;
125     }
126 
127     public void removePluginPackage(PluginPackage pluginPackage) {
128         _pluginPackages.remove(pluginPackage.getContext());
129 
130         try {
131             PluginPackageIndexer.removePluginPackage(
132                 pluginPackage.getModuleId());
133         }
134         catch (SearchException se) {
135             _log.error("Deleting index " + pluginPackage.getModuleId(), se);
136         }
137     }
138 
139     public void removePluginPackage(String context) {
140         _pluginPackages.remove(context);
141     }
142 
143     public void registerPluginPackageInstallation(PluginPackage pluginPackage) {
144         if (pluginPackage.getContext() != null) {
145             PluginPackage previousPluginPackage = _pluginPackages.get(
146                 pluginPackage.getContext());
147 
148             if (previousPluginPackage == null) {
149                 addPluginPackage(pluginPackage);
150             }
151         }
152 
153         String key = pluginPackage.getContext();
154 
155         if (key == null) {
156             key = pluginPackage.getModuleId();
157         }
158 
159         _pendingPackages.put(key, pluginPackage);
160     }
161 
162     public void registerPluginPackageInstallation(String deploymentContext) {
163         PluginPackage pluginPackage = getPluginPackage(deploymentContext);
164 
165         if (pluginPackage == null) {
166             String moduleId =
167                 deploymentContext + StringPool.SLASH + deploymentContext +
168                     StringPool.SLASH + Version.UNKNOWN + StringPool.SLASH +
169                         "war";
170 
171             pluginPackage = new PluginPackageImpl(moduleId);
172 
173             pluginPackage.setName(deploymentContext);
174             pluginPackage.setContext(deploymentContext);
175         }
176 
177         registerPluginPackageInstallation(pluginPackage);
178     }
179 
180     public void unregisterPluginPackageInstallation(String context) {
181         _pluginPackages.remove(context);
182         _pendingPackages.remove(context);
183     }
184 
185     private static Log _log =
186         LogFactory.getLog(LocalPluginPackageRepository.class);
187 
188     private Map<String, PluginPackage> _pluginPackages =
189         new HashMap<String, PluginPackage>();
190     private Map<String, PluginPackage> _pendingPackages =
191         new HashMap<String, PluginPackage>();
192 
193 }