001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.plugin;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    
019    import java.util.ArrayList;
020    import java.util.HashMap;
021    import java.util.Iterator;
022    import java.util.List;
023    import java.util.Map;
024    import java.util.Properties;
025    import java.util.Set;
026    import java.util.TreeSet;
027    
028    /**
029     * @author Jorge Ferrer
030     */
031    public class RemotePluginPackageRepository {
032    
033            public static final String LOCAL_URL = "LOCAL_URL";
034    
035            public static final String SETTING_USE_DOWNLOAD_URL = "use-download-url";
036    
037            public RemotePluginPackageRepository(String repositoryURL) {
038                    _repositoryURL = repositoryURL;
039            }
040    
041            public void addPluginPackage(PluginPackage pluginPackage) {
042    
043                    // Avoid duplicates
044    
045                    PluginPackage existingPackage = _moduleIdIndex.get(
046                            pluginPackage.getModuleId());
047    
048                    if (existingPackage != null) {
049                            return;
050                    }
051    
052                    _artifactURLIndex.put(pluginPackage.getArtifactURL(), pluginPackage);
053                    _moduleIdIndex.put(pluginPackage.getModuleId(), pluginPackage);
054                    _addToGroupAndArtifactIndex(
055                            pluginPackage.getGroupId(), pluginPackage.getArtifactId(),
056                            pluginPackage);
057                    _pluginPackages.add(pluginPackage);
058                    _tags.addAll(pluginPackage.getTags());
059            }
060    
061            public PluginPackage findPluginByArtifactURL(String artifactURL) {
062                    return _artifactURLIndex.get(artifactURL);
063            }
064    
065            public PluginPackage findPluginPackageByModuleId(String moduleId) {
066                    return _moduleIdIndex.get(moduleId);
067            }
068    
069            public List<PluginPackage> findPluginsByGroupIdAndArtifactId(
070                    String groupId, String artifactId) {
071    
072                    return _groupAndArtifactIndex.get(
073                            groupId + StringPool.SLASH + artifactId);
074            }
075    
076            public List<PluginPackage> getPluginPackages() {
077                    return _pluginPackages;
078            }
079    
080            public String getRepositoryURL() {
081                    return _repositoryURL;
082            }
083    
084            public Properties getSettings() {
085                    return _settings;
086            }
087    
088            public Set<String> getTags() {
089                    return _tags;
090            }
091    
092            public void removePlugin(PluginPackage pluginPackage) {
093                    _artifactURLIndex.remove(pluginPackage.getArtifactURL());
094                    _moduleIdIndex.remove(pluginPackage.getModuleId());
095                    _removeFromGroupAndArtifactIndex(
096                            pluginPackage.getGroupId(), pluginPackage.getArtifactId(),
097                            pluginPackage.getModuleId());
098                    _pluginPackages.remove(pluginPackage);
099            }
100    
101            public void setSettings(Properties settings) {
102                    _settings = settings;
103            }
104    
105            private void _addToGroupAndArtifactIndex(
106                    String groupId, String artifactId, PluginPackage pluginPackage) {
107    
108                    List<PluginPackage> pluginPackages = findPluginsByGroupIdAndArtifactId(
109                            groupId, artifactId);
110    
111                    if (pluginPackages == null) {
112                            pluginPackages = new ArrayList<PluginPackage>();
113    
114                            _groupAndArtifactIndex.put(
115                                    groupId+ StringPool.SLASH + artifactId, pluginPackages);
116                    }
117    
118                    pluginPackages.add(pluginPackage);
119            }
120    
121            private void _removeFromGroupAndArtifactIndex(
122                    String groupId, String artifactId, String moduleId) {
123    
124                    List<PluginPackage> pluginPackages = findPluginsByGroupIdAndArtifactId(
125                            groupId, artifactId);
126    
127                    if (pluginPackages != null) {
128                            Iterator<PluginPackage> itr = pluginPackages.iterator();
129    
130                            while (itr.hasNext()) {
131                                    PluginPackage pluginPackage = itr.next();
132    
133                                    if (pluginPackage.getModuleId().equals(moduleId)) {
134                                            itr.remove();
135    
136                                            break;
137                                    }
138                            }
139                    }
140            }
141    
142            private Map<String, PluginPackage> _artifactURLIndex =
143                    new HashMap<String, PluginPackage>();
144            private Map<String, List<PluginPackage>> _groupAndArtifactIndex =
145                    new HashMap<String, List<PluginPackage>>();
146            private Map<String, PluginPackage> _moduleIdIndex =
147                    new HashMap<String, PluginPackage>();
148            private List<PluginPackage> _pluginPackages =
149                    new ArrayList<PluginPackage>();
150            private String _repositoryURL;
151            private Properties _settings = null;
152            private Set<String> _tags = new TreeSet<String>();
153    
154    }