001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.system.plugin;
018    
019    import java.io.Serializable;
020    import java.net.URL;
021    import java.util.List;
022    import java.util.ArrayList;
023    import java.util.Set;
024    import java.util.HashSet;
025    import org.apache.geronimo.kernel.repository.Artifact;
026    
027    /**
028     * Metadata on a list of configurations available in a remote server.
029     *
030     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
031     */
032    public class PluginList implements Serializable {
033        private final URL[] repositories;
034        private final PluginMetadata[] plugins;
035    
036        public PluginList(URL[] repositories, PluginMetadata[] plugins) {
037            this.repositories = repositories;
038            this.plugins = plugins;
039        }
040    
041        public static PluginList createInstallList(PluginList all, Artifact[] selectedConfigIDs) {
042            URL[] repositories = all.repositories;
043            List list = new ArrayList();
044            Set set = new HashSet();
045            for (int i = 0; i < selectedConfigIDs.length; i++) {
046                Artifact artifact = selectedConfigIDs[i];
047                set.add(artifact);
048            }
049            for (int i = 0; i < all.getPlugins().length; i++) {
050                PluginMetadata metadata = all.getPlugins()[i];
051                if(set.contains(metadata.getModuleId())) {
052                    if(metadata.isInstalled() || !metadata.isEligible()) {
053                        throw new IllegalArgumentException("Cannot install "+metadata.getModuleId());
054                    }
055                    list.add(metadata);
056                }
057            }
058            if(list.size() == 0) {
059                return null;
060            }
061            PluginMetadata[] configurations = (PluginMetadata[]) list.toArray(new PluginMetadata[list.size()]);
062            return new PluginList(repositories, configurations);
063        }
064    
065        public static PluginList createInstallList(PluginList all, Artifact selectedConfigID) {
066            URL[] repositories = all.repositories;
067            PluginMetadata target = null;
068            for (int i = 0; i < all.getPlugins().length; i++) {
069                PluginMetadata metadata = all.getPlugins()[i];
070                if(selectedConfigID.equals(metadata.getModuleId())) {
071                    if(metadata.isInstalled() || !metadata.isEligible()) {
072                        throw new IllegalArgumentException("Cannot install "+metadata.getModuleId());
073                    }
074                    target = metadata;
075                    break;
076                }
077            }
078            if(target == null) {
079                return null;
080            }
081            return new PluginList(repositories, new PluginMetadata[]{target});
082        }
083    
084        public URL[] getRepositories() {
085            return repositories;
086        }
087    
088        public PluginMetadata[] getPlugins() {
089            return plugins;
090        }
091    }