001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.geronimo.console.car;
021:
022: import java.util.List;
023: import java.util.ArrayList;
024: import java.util.Collections;
025: import java.util.Comparator;
026:
027: import javax.portlet.RenderRequest;
028:
029: import org.apache.geronimo.system.plugin.PluginInstaller;
030: import org.apache.geronimo.system.plugin.PluginInstallerGBean;
031: import org.apache.geronimo.system.plugin.model.PluginListType;
032: import org.apache.geronimo.system.plugin.model.PluginType;
033: import org.apache.geronimo.system.plugin.model.PluginArtifactType;
034: import org.apache.geronimo.kernel.repository.Dependency;
035:
036: /**
037: * @version $Rev: 613462 $ $Date: 2008-01-19 13:46:39 -0800 (Sat, 19 Jan 2008) $
038: */
039: public abstract class AbstractListHandler extends
040: BaseImportExportHandler {
041: public AbstractListHandler(String mode, String viewName) {
042: super (mode, viewName);
043: }
044:
045: protected void listPlugins(RenderRequest request,
046: PluginInstaller pluginInstaller, PluginListType data,
047: boolean validate) {
048: List<PluginInfoBean> plugins = new ArrayList<PluginInfoBean>();
049:
050: for (PluginType metadata : data.getPlugin()) {
051:
052: // ignore plugins which have no artifacts defined
053: if (metadata.getPluginArtifact().isEmpty()) {
054: continue;
055: }
056:
057: if (metadata.getCategory() == null) {
058: metadata.setCategory("Unspecified");
059: }
060:
061: for (PluginArtifactType artifact : metadata
062: .getPluginArtifact()) {
063: PluginInfoBean plugin = new PluginInfoBean();
064: plugin.setPlugin(metadata);
065: plugin.setPluginArtifact(artifact);
066:
067: if (validate) {
068: // determine if the plugin is installable
069: PluginType holder = PluginInstallerGBean.copy(
070: metadata, artifact);
071: try {
072: pluginInstaller.validatePlugin(holder);
073: } catch (Exception e) {
074: plugin.setInstallable(false);
075: }
076: Dependency[] missingPrereqs = pluginInstaller
077: .checkPrerequisites(holder);
078: if (missingPrereqs.length > 0) {
079: plugin.setInstallable(false);
080: }
081: }
082: plugins.add(plugin);
083: }
084: }
085:
086: // sort the plugin list based on the selected table column
087: final String column = request.getParameter("column");
088: Collections.sort(plugins, new Comparator<PluginInfoBean>() {
089: public int compare(PluginInfoBean o1, PluginInfoBean o2) {
090: if ("Category".equals(column)) {
091: String category1 = o1.getCategory();
092: String category2 = o2.getCategory();
093: if (category1.equals(category2)) {
094: return o1.getName().compareTo(o2.getName());
095: }
096: return category1.compareTo(category2);
097: } else if ("Version".equals(column)) {
098: String version1 = o1.getPluginArtifact()
099: .getModuleId().getVersion();
100: String version2 = o2.getPluginArtifact()
101: .getModuleId().getVersion();
102: if (version1.equals(version2)) {
103: return o1.getName().compareTo(o2.getName());
104: }
105: return version1.compareTo(version2);
106: } else if ("Installable".equals(column)) {
107: if (o1.isInstallable() == o2.isInstallable()) {
108: return o1.getName().compareTo(o2.getName());
109: }
110: return o1.isInstallable() ? -1 : 1;
111: } else { // default sort column is Name
112: return o1.getName().compareTo(o2.getName());
113: }
114: }
115: });
116:
117: // save everything in the request
118: request.setAttribute("plugins", plugins);
119: }
120: }
|