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   */
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<PluginPackage> 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<String> getTags() {
70          return _tags;
71      }
72  
73      public void addPluginPackage(PluginPackage pluginPackage) {
74  
75          // Avoid duplicates
76  
77          PluginPackage existingPackage = _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 _artifactURLIndex.get(artifactURL);
95      }
96  
97      public PluginPackage findPluginPackageByModuleId(String moduleId) {
98          return _moduleIdIndex.get(moduleId);
99      }
100 
101     public List<PluginPackage> findPluginsByGroupIdAndArtifactId(
102         String groupId, String artifactId) {
103 
104         return _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<PluginPackage> pluginPackages = findPluginsByGroupIdAndArtifactId(
121             groupId, artifactId);
122 
123         if (pluginPackages == null) {
124             pluginPackages = new ArrayList<PluginPackage>();
125 
126             _groupAndArtifactIndex.put(
127                 groupId+ StringPool.SLASH + artifactId, pluginPackages);
128         }
129 
130         pluginPackages.add(pluginPackage);
131     }
132 
133     private void _removeFromGroupAndArtifactIndex(
134         String groupId, String artifactId, String moduleId) {
135 
136         List<PluginPackage> pluginPackages = findPluginsByGroupIdAndArtifactId(
137             groupId, artifactId);
138 
139         if (pluginPackages != null) {
140             Iterator<PluginPackage> itr = pluginPackages.iterator();
141 
142             while (itr.hasNext()) {
143                 PluginPackage pluginPackage = itr.next();
144 
145                 if (pluginPackage.getModuleId().equals(moduleId)) {
146                     itr.remove();
147 
148                     break;
149                 }
150             }
151         }
152     }
153 
154     private String _repositoryURL;
155     private Map<String, PluginPackage> _artifactURLIndex =
156         new HashMap<String, PluginPackage>();
157     private Map<String, PluginPackage> _moduleIdIndex =
158         new HashMap<String, PluginPackage>();
159     private Map<String, List<PluginPackage>> _groupAndArtifactIndex =
160         new HashMap<String, List<PluginPackage>>();
161     private List<PluginPackage> _pluginPackages =
162         new ArrayList<PluginPackage>();
163     private Properties _settings = null;
164     private Set<String> _tags = new TreeSet<String>();
165 
166 }