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