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