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