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