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 void addPluginPackage(PluginPackage pluginPackage) {
53  
54          // Avoid duplicates
55  
56          PluginPackage existingPackage = _moduleIdIndex.get(
57              pluginPackage.getModuleId());
58  
59          if (existingPackage != null) {
60             return;
61          }
62  
63          _artifactURLIndex.put(pluginPackage.getArtifactURL(), pluginPackage);
64          _moduleIdIndex.put(pluginPackage.getModuleId(), pluginPackage);
65          _addToGroupAndArtifactIndex(
66              pluginPackage.getGroupId(), pluginPackage.getArtifactId(),
67              pluginPackage);
68          _pluginPackages.add(pluginPackage);
69          _tags.addAll(pluginPackage.getTags());
70      }
71  
72      public PluginPackage findPluginByArtifactURL(String artifactURL) {
73          return _artifactURLIndex.get(artifactURL);
74      }
75  
76      public PluginPackage findPluginPackageByModuleId(String moduleId) {
77          return _moduleIdIndex.get(moduleId);
78      }
79  
80      public List<PluginPackage> findPluginsByGroupIdAndArtifactId(
81          String groupId, String artifactId) {
82  
83          return _groupAndArtifactIndex.get(
84              groupId + StringPool.SLASH + artifactId);
85      }
86  
87      public List<PluginPackage> getPluginPackages() {
88          return _pluginPackages;
89      }
90  
91      public String getRepositoryURL() {
92          return _repositoryURL;
93      }
94  
95      public Properties getSettings() {
96          return _settings;
97      }
98  
99      public Set<String> getTags() {
100         return _tags;
101     }
102 
103     public void removePlugin(PluginPackage pluginPackage) {
104         _artifactURLIndex.remove(pluginPackage.getArtifactURL());
105         _moduleIdIndex.remove(pluginPackage.getModuleId());
106         _removeFromGroupAndArtifactIndex(
107             pluginPackage.getGroupId(), pluginPackage.getArtifactId(),
108             pluginPackage.getModuleId());
109         _pluginPackages.remove(pluginPackage);
110     }
111 
112     public void setSettings(Properties settings) {
113         _settings = settings;
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 Map<String, PluginPackage> _artifactURLIndex =
154         new HashMap<String, PluginPackage>();
155     private Map<String, List<PluginPackage>> _groupAndArtifactIndex =
156         new HashMap<String, List<PluginPackage>>();
157     private Map<String, PluginPackage> _moduleIdIndex =
158         new HashMap<String, PluginPackage>();
159     private List<PluginPackage> _pluginPackages =
160         new ArrayList<PluginPackage>();
161     private String _repositoryURL;
162     private Properties _settings = null;
163     private Set<String> _tags = new TreeSet<String>();
164 
165 }