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.plugin;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.plugin.PluginPackage;
021    import com.liferay.portal.kernel.plugin.PluginPackageNameAndContextComparator;
022    import com.liferay.portal.kernel.plugin.Version;
023    import com.liferay.portal.kernel.search.Indexer;
024    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
025    import com.liferay.portal.kernel.util.ListUtil;
026    import com.liferay.portal.kernel.util.StringPool;
027    
028    import java.util.ArrayList;
029    import java.util.HashMap;
030    import java.util.List;
031    import java.util.Map;
032    
033    /**
034     * @author Jorge Ferrer
035     */
036    public class LocalPluginPackageRepository {
037    
038            public LocalPluginPackageRepository() {
039            }
040    
041            public void addPluginPackage(PluginPackage pluginPackage) {
042                    if (pluginPackage.getContext() == null) {
043                            if (_log.isDebugEnabled()) {
044                                    _log.debug(
045                                            "Plugin package cannot be registered because it does not " +
046                                                    "have an installation context");
047                            }
048    
049                            return;
050                    }
051    
052                    _pendingPackages.remove(pluginPackage.getContext());
053                    _pendingPackages.remove(pluginPackage.getModuleId());
054    
055                    _pluginPackages.remove(pluginPackage.getContext());
056                    _pluginPackages.put(pluginPackage.getContext(), pluginPackage);
057            }
058    
059            public PluginPackage getInstallingPluginPackage(String context) {
060                    return _pendingPackages.get(context);
061            }
062    
063            public PluginPackage getLatestPluginPackage(
064                    String groupId, String artifactId) {
065    
066                    PluginPackage latestPluginPackage = null;
067    
068                    for (PluginPackage pluginPackage : _pluginPackages.values()) {
069                            if (pluginPackage.getGroupId().equals(groupId) &&
070                                    pluginPackage.getArtifactId().equals(artifactId) &&
071                                    ((latestPluginPackage == null) ||
072                                     pluginPackage.isLaterVersionThan(latestPluginPackage))) {
073    
074                                    latestPluginPackage = pluginPackage;
075                            }
076                    }
077    
078                    return latestPluginPackage;
079            }
080    
081            public PluginPackage getPluginPackage(String context) {
082                    return _pluginPackages.get(context);
083            }
084    
085            public List<PluginPackage> getPluginPackages() {
086                    return new ArrayList<PluginPackage>(_pluginPackages.values());
087            }
088    
089            public List<PluginPackage> getPluginPackages(
090                    String groupId, String artifactId) {
091    
092                    List<PluginPackage> pluginPackages = new ArrayList<PluginPackage>();
093    
094                    for (PluginPackage pluginPackage : _pluginPackages.values()) {
095                            if (pluginPackage.getGroupId().equals(groupId) &&
096                                    pluginPackage.getArtifactId().equals(artifactId)) {
097    
098                                    pluginPackages.add(pluginPackage);
099                            }
100                    }
101    
102                    return pluginPackages;
103            }
104    
105            public List<PluginPackage> getSortedPluginPackages() {
106                    List<PluginPackage> pluginPackages = new ArrayList<PluginPackage>();
107    
108                    pluginPackages.addAll(_pluginPackages.values());
109    
110                    return ListUtil.sort(
111                            pluginPackages, new PluginPackageNameAndContextComparator());
112            }
113    
114            public void registerPluginPackageInstallation(PluginPackage pluginPackage) {
115                    if (pluginPackage.getContext() != null) {
116                            PluginPackage previousPluginPackage = _pluginPackages.get(
117                                    pluginPackage.getContext());
118    
119                            if (previousPluginPackage == null) {
120                                    addPluginPackage(pluginPackage);
121                            }
122                    }
123    
124                    String key = pluginPackage.getContext();
125    
126                    if (key == null) {
127                            key = pluginPackage.getModuleId();
128                    }
129    
130                    _pendingPackages.put(key, pluginPackage);
131            }
132    
133            public void registerPluginPackageInstallation(String deploymentContext) {
134                    PluginPackage pluginPackage = getPluginPackage(deploymentContext);
135    
136                    if (pluginPackage == null) {
137                            String moduleId =
138                                    deploymentContext + StringPool.SLASH + deploymentContext +
139                                            StringPool.SLASH + Version.UNKNOWN + StringPool.SLASH +
140                                                    "war";
141    
142                            pluginPackage = new PluginPackageImpl(moduleId);
143    
144                            pluginPackage.setName(deploymentContext);
145                            pluginPackage.setContext(deploymentContext);
146                    }
147    
148                    registerPluginPackageInstallation(pluginPackage);
149            }
150    
151            public void removePluginPackage(PluginPackage pluginPackage)
152                    throws PortalException {
153    
154                    _pluginPackages.remove(pluginPackage.getContext());
155    
156                    Indexer indexer = IndexerRegistryUtil.getIndexer(PluginPackage.class);
157    
158                    indexer.delete(pluginPackage);
159            }
160    
161            public void removePluginPackage(String context) {
162                    _pluginPackages.remove(context);
163            }
164    
165            public void unregisterPluginPackageInstallation(String context) {
166                    _pluginPackages.remove(context);
167                    _pendingPackages.remove(context);
168            }
169    
170            private static Log _log = LogFactoryUtil.getLog(
171                    LocalPluginPackageRepository.class);
172    
173            private Map<String, PluginPackage> _pendingPackages =
174                    new HashMap<String, PluginPackage>();
175            private Map<String, PluginPackage> _pluginPackages =
176                    new HashMap<String, PluginPackage>();
177    
178    }