001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.plugin;
022:
023: import com.liferay.portal.kernel.plugin.PluginPackage;
024: import com.liferay.portal.kernel.plugin.PluginPackageNameAndContextComparator;
025: import com.liferay.portal.kernel.util.StringPool;
026: import com.liferay.util.Version;
027:
028: import java.util.ArrayList;
029: import java.util.Collections;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.List;
033: import java.util.Map;
034:
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037:
038: /**
039: * <a href="LocalPluginPackageRepository.java.html"><b><i>View Source</i></b>
040: * </a>
041: *
042: * @author Jorge Ferrer
043: *
044: */
045: public class LocalPluginPackageRepository {
046:
047: public LocalPluginPackageRepository() {
048: }
049:
050: public void addPluginPackage(PluginPackage pluginPackage) {
051: if (pluginPackage.getContext() == null) {
052: if (_log.isDebugEnabled()) {
053: _log
054: .debug("Plugin package cannot be registered because it does not "
055: + "have an installation context");
056: }
057:
058: return;
059: }
060:
061: _pendingPackages.remove(pluginPackage.getContext());
062: _pendingPackages.remove(pluginPackage.getModuleId());
063:
064: _pluginPackages.remove(pluginPackage.getContext());
065: _pluginPackages.put(pluginPackage.getContext(), pluginPackage);
066: }
067:
068: public PluginPackage getInstallingPluginPackage(String context) {
069: return (PluginPackage) _pendingPackages.get(context);
070: }
071:
072: public PluginPackage getLatestPluginPackage(String groupId,
073: String artifactId) {
074:
075: PluginPackage result = null;
076:
077: Iterator itr = _pluginPackages.values().iterator();
078:
079: while (itr.hasNext()) {
080: PluginPackage pluginPackage = (PluginPackage) itr.next();
081:
082: if (pluginPackage.getGroupId().equals(groupId)
083: && pluginPackage.getArtifactId().equals(artifactId)
084: && (result == null || pluginPackage
085: .isLaterVersionThan(result))) {
086:
087: result = pluginPackage;
088: }
089: }
090:
091: return result;
092:
093: }
094:
095: public PluginPackage getPluginPackage(String context) {
096: return (PluginPackage) _pluginPackages.get(context);
097: }
098:
099: public List getPluginPackages() {
100: return new ArrayList(_pluginPackages.values());
101: }
102:
103: public List getPluginPackages(String groupId, String artifactId) {
104: List result = new ArrayList();
105:
106: Iterator itr = _pluginPackages.values().iterator();
107:
108: while (itr.hasNext()) {
109: PluginPackage pluginPackage = (PluginPackage) itr.next();
110:
111: if (pluginPackage.getGroupId().equals(groupId)
112: && pluginPackage.getArtifactId().equals(artifactId)) {
113:
114: result.add(pluginPackage);
115: }
116: }
117:
118: return result;
119: }
120:
121: public List getSortedPluginPackages() {
122: List list = new ArrayList();
123:
124: list.addAll(_pluginPackages.values());
125:
126: Collections.sort(list,
127: new PluginPackageNameAndContextComparator());
128:
129: return list;
130: }
131:
132: public void removePluginPackage(PluginPackage pluginPackage) {
133: _pluginPackages.remove(pluginPackage.getContext());
134: }
135:
136: public void removePluginPackage(String context) {
137: _pluginPackages.remove(context);
138: }
139:
140: public void registerPluginPackageInstallation(
141: PluginPackage pluginPackage) {
142: if (pluginPackage.getContext() != null) {
143: PluginPackage previousPluginPackage = (PluginPackage) _pluginPackages
144: .get(pluginPackage.getContext());
145:
146: if (previousPluginPackage == null) {
147: addPluginPackage(pluginPackage);
148: }
149: }
150:
151: String key = pluginPackage.getContext();
152:
153: if (key == null) {
154: key = pluginPackage.getModuleId();
155: }
156:
157: _pendingPackages.put(key, pluginPackage);
158: }
159:
160: public void registerPluginPackageInstallation(
161: String deploymentContext) {
162: PluginPackage pluginPackage = getPluginPackage(deploymentContext);
163:
164: if (pluginPackage == null) {
165: String moduleId = deploymentContext + StringPool.SLASH
166: + deploymentContext + StringPool.SLASH
167: + Version.UNKNOWN + StringPool.SLASH + "war";
168:
169: pluginPackage = new PluginPackageImpl(moduleId);
170:
171: pluginPackage.setName(deploymentContext);
172: pluginPackage.setContext(deploymentContext);
173: }
174:
175: registerPluginPackageInstallation(pluginPackage);
176: }
177:
178: public void unregisterPluginPackageInstallation(String context) {
179: _pluginPackages.remove(context);
180: _pendingPackages.remove(context);
181: }
182:
183: private static Log _log = LogFactory
184: .getLog(LocalPluginPackageRepository.class);
185:
186: private Map _pluginPackages = new HashMap();
187: private Map _pendingPackages = new HashMap();
188:
189: }
|