1   /**
2    * Copyright (c) 2000-2007 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.kernel.plugin;
24  
25  import com.liferay.portal.kernel.util.StringPool;
26  
27  import java.util.ArrayList;
28  import java.util.HashMap;
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.Properties;
33  import java.util.Set;
34  import java.util.TreeSet;
35  
36  /**
37   * <a href="RemotePluginPackageRepository.java.html"><b><i>View Source</i></b>
38   * </a>
39   *
40   * @author Jorge Ferrer
41   *
42   */
43  public class RemotePluginPackageRepository {
44  
45      public static final String LOCAL_URL = "LOCAL_URL";
46  
47      public static final String SETTING_USE_DOWNLOAD_URL = "use-download-url";
48  
49      public RemotePluginPackageRepository(String repositoryURL) {
50          _repositoryURL = repositoryURL;
51      }
52  
53      public String getRepositoryURL() {
54          return _repositoryURL;
55      }
56  
57      public List getPluginPackages() {
58          return _pluginPackages;
59      }
60  
61      public Properties getSettings() {
62          return _settings;
63      }
64  
65      public void setSettings(Properties settings) {
66          _settings = settings;
67      }
68  
69      public Set getTags() {
70          return _tags;
71      }
72  
73      public void addPluginPackage(PluginPackage pluginPackage) {
74  
75          // Avoid duplicates
76  
77          PluginPackage existingPackage = (PluginPackage)_moduleIdIndex.get(
78              pluginPackage.getModuleId());
79  
80          if (existingPackage != null) {
81             return;
82          }
83  
84          _artifactURLIndex.put(pluginPackage.getArtifactURL(), pluginPackage);
85          _moduleIdIndex.put(pluginPackage.getModuleId(), pluginPackage);
86          _addToGroupAndArtifactIndex(
87              pluginPackage.getGroupId(), pluginPackage.getArtifactId(),
88              pluginPackage);
89          _pluginPackages.add(pluginPackage);
90          _tags.addAll(pluginPackage.getTags());
91      }
92  
93      public PluginPackage findPluginByArtifactURL(String artifactURL) {
94          return (PluginPackage)_artifactURLIndex.get(artifactURL);
95      }
96  
97      public PluginPackage findPluginPackageByModuleId(String moduleId) {
98          return (PluginPackage)_moduleIdIndex.get(moduleId);
99      }
100 
101     public List findPluginsByGroupIdAndArtifactId(
102         String groupId, String artifactId) {
103 
104         return (List)_groupAndArtifactIndex.get(
105             groupId + StringPool.SLASH + artifactId);
106     }
107 
108     public void removePlugin(PluginPackage pluginPackage) {
109         _artifactURLIndex.remove(pluginPackage.getArtifactURL());
110         _moduleIdIndex.remove(pluginPackage.getModuleId());
111         _removeFromGroupAndArtifactIndex(
112             pluginPackage.getGroupId(), pluginPackage.getArtifactId(),
113             pluginPackage.getModuleId());
114         _pluginPackages.remove(pluginPackage);
115     }
116 
117     private void _addToGroupAndArtifactIndex(
118         String groupId, String artifactId, PluginPackage pluginPackage) {
119 
120         List plugins = findPluginsByGroupIdAndArtifactId(groupId, artifactId);
121 
122         if (plugins == null) {
123             plugins = new ArrayList();
124 
125             _groupAndArtifactIndex.put(
126                 groupId+ StringPool.SLASH + artifactId, plugins);
127         }
128 
129         plugins.add(pluginPackage);
130     }
131 
132     private void _removeFromGroupAndArtifactIndex(
133         String groupId, String artifactId, String moduleId) {
134 
135         List plugins = findPluginsByGroupIdAndArtifactId(groupId, artifactId);
136 
137         if (plugins != null) {
138             Iterator itr = plugins.iterator();
139 
140             while (itr.hasNext()) {
141                 PluginPackage pluginPackage = (PluginPackage)itr.next();
142 
143                 if (pluginPackage.getModuleId().equals(moduleId)) {
144                     itr.remove();
145 
146                     break;
147                 }
148             }
149         }
150     }
151 
152     private String _repositoryURL;
153     private Map _artifactURLIndex = new HashMap();
154     private Map _moduleIdIndex = new HashMap();
155     private Map _groupAndArtifactIndex = new HashMap();
156     private List _pluginPackages = new ArrayList();
157     private Properties _settings = null;
158     private Set _tags = new TreeSet();
159 
160 }