1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.plugin;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.plugin.PluginPackage;
21  import com.liferay.portal.kernel.plugin.PluginPackageNameAndContextComparator;
22  import com.liferay.portal.kernel.plugin.Version;
23  import com.liferay.portal.kernel.search.Indexer;
24  import com.liferay.portal.kernel.search.IndexerRegistryUtil;
25  import com.liferay.portal.kernel.util.ListUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  
28  import java.util.ArrayList;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  /**
34   * <a href="LocalPluginPackageRepository.java.html"><b><i>View Source</i></b>
35   * </a>
36   *
37   * @author Jorge Ferrer
38   */
39  public class LocalPluginPackageRepository {
40  
41      public LocalPluginPackageRepository() {
42      }
43  
44      public void addPluginPackage(PluginPackage pluginPackage) {
45          if (pluginPackage.getContext() == null) {
46              if (_log.isDebugEnabled()) {
47                  _log.debug(
48                      "Plugin package cannot be registered because it does not " +
49                          "have an installation context");
50              }
51  
52              return;
53          }
54  
55          _pendingPackages.remove(pluginPackage.getContext());
56          _pendingPackages.remove(pluginPackage.getModuleId());
57  
58          _pluginPackages.remove(pluginPackage.getContext());
59          _pluginPackages.put(pluginPackage.getContext(), pluginPackage);
60      }
61  
62      public PluginPackage getInstallingPluginPackage(String context) {
63          return _pendingPackages.get(context);
64      }
65  
66      public PluginPackage getLatestPluginPackage(
67          String groupId, String artifactId) {
68  
69          PluginPackage latestPluginPackage = null;
70  
71          for (PluginPackage pluginPackage : _pluginPackages.values()) {
72              if ((pluginPackage.getGroupId().equals(groupId)) &&
73                  (pluginPackage.getArtifactId().equals(artifactId)) &&
74                  ((latestPluginPackage == null) ||
75                      pluginPackage.isLaterVersionThan(latestPluginPackage))) {
76  
77                  latestPluginPackage = pluginPackage;
78              }
79          }
80  
81          return latestPluginPackage;
82      }
83  
84      public PluginPackage getPluginPackage(String context) {
85          return _pluginPackages.get(context);
86      }
87  
88      public List<PluginPackage> getPluginPackages() {
89          return new ArrayList<PluginPackage>(_pluginPackages.values());
90      }
91  
92      public List<PluginPackage> getPluginPackages(
93          String groupId, String artifactId) {
94  
95          List<PluginPackage> pluginPackages = new ArrayList<PluginPackage>();
96  
97          for (PluginPackage pluginPackage : _pluginPackages.values()) {
98              if (pluginPackage.getGroupId().equals(groupId) &&
99                  pluginPackage.getArtifactId().equals(artifactId)) {
100 
101                 pluginPackages.add(pluginPackage);
102             }
103         }
104 
105         return pluginPackages;
106     }
107 
108     public List<PluginPackage> getSortedPluginPackages() {
109         List<PluginPackage> pluginPackages = new ArrayList<PluginPackage>();
110 
111         pluginPackages.addAll(_pluginPackages.values());
112 
113         return ListUtil.sort(
114             pluginPackages, new PluginPackageNameAndContextComparator());
115     }
116 
117     public void removePluginPackage(PluginPackage pluginPackage)
118         throws PortalException {
119 
120         _pluginPackages.remove(pluginPackage.getContext());
121 
122         Indexer indexer = IndexerRegistryUtil.getIndexer(PluginPackage.class);
123 
124         indexer.delete(pluginPackage);
125     }
126 
127     public void removePluginPackage(String context) {
128         _pluginPackages.remove(context);
129     }
130 
131     public void registerPluginPackageInstallation(PluginPackage pluginPackage) {
132         if (pluginPackage.getContext() != null) {
133             PluginPackage previousPluginPackage = _pluginPackages.get(
134                 pluginPackage.getContext());
135 
136             if (previousPluginPackage == null) {
137                 addPluginPackage(pluginPackage);
138             }
139         }
140 
141         String key = pluginPackage.getContext();
142 
143         if (key == null) {
144             key = pluginPackage.getModuleId();
145         }
146 
147         _pendingPackages.put(key, pluginPackage);
148     }
149 
150     public void registerPluginPackageInstallation(String deploymentContext) {
151         PluginPackage pluginPackage = getPluginPackage(deploymentContext);
152 
153         if (pluginPackage == null) {
154             String moduleId =
155                 deploymentContext + StringPool.SLASH + deploymentContext +
156                     StringPool.SLASH + Version.UNKNOWN + StringPool.SLASH +
157                         "war";
158 
159             pluginPackage = new PluginPackageImpl(moduleId);
160 
161             pluginPackage.setName(deploymentContext);
162             pluginPackage.setContext(deploymentContext);
163         }
164 
165         registerPluginPackageInstallation(pluginPackage);
166     }
167 
168     public void unregisterPluginPackageInstallation(String context) {
169         _pluginPackages.remove(context);
170         _pendingPackages.remove(context);
171     }
172 
173     private static Log _log = LogFactoryUtil.getLog(
174         LocalPluginPackageRepository.class);
175 
176     private Map<String, PluginPackage> _pluginPackages =
177         new HashMap<String, PluginPackage>();
178     private Map<String, PluginPackage> _pendingPackages =
179         new HashMap<String, PluginPackage>();
180 
181 }