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    
021    package org.apache.geronimo.mavenplugins.car;
022    
023    import java.io.File;
024    import java.io.FileInputStream;
025    import java.io.FileWriter;
026    import java.io.InputStream;
027    import java.io.Writer;
028    import java.util.Iterator;
029    
030    import org.apache.geronimo.kernel.config.NoSuchStoreException;
031    import org.apache.geronimo.kernel.repository.Artifact;
032    import org.apache.geronimo.system.plugin.PluginInstallerGBean;
033    import org.apache.geronimo.system.plugin.PluginXmlUtil;
034    import org.apache.geronimo.system.plugin.model.PluginArtifactType;
035    import org.apache.geronimo.system.plugin.model.PluginListType;
036    import org.apache.geronimo.system.plugin.model.PluginType;
037    
038    /**
039     * Maintain the geronimo-plugins.xml catalog in the local maven repository by merging in the geronimo-plugin.xml from the current project.
040     *
041     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
042     * @goal update-pluginlist
043     */
044    public class UpdatePluginListMojo extends AbstractCarMojo {
045    
046        /**
047         * Location of the (just generated) plugin metadata file to merge into the geronimo-plugins.xml catalog in the local maven repository.
048         *
049         * @parameter expression="${project.build.directory}/resources/META-INF/geronimo-plugin.xml"
050         * @required
051         */
052        protected File targetFile;
053    
054        protected void doExecute() throws Exception {
055    
056            InputStream min = new FileInputStream(targetFile);
057            PluginType plugin;
058            try {
059                plugin = PluginXmlUtil.loadPluginMetadata(min);
060            } finally {
061                min.close();
062            }
063    
064            String path = getArtifactRepository().getBasedir();
065            File baseDir = new File(path);
066    
067            File outFile = new File(baseDir, "geronimo-plugins.xml");
068            PluginListType pluginList;
069            if (outFile.exists()) {
070                InputStream in = new FileInputStream(outFile);
071                try {
072                    pluginList = PluginXmlUtil.loadPluginList(in);
073                } finally {
074                    in.close();
075                }
076            } else {
077                pluginList = new PluginListType();
078                pluginList.getDefaultRepository().add(path);
079            }
080    
081            updatePluginList(plugin, pluginList);
082            Writer out = new FileWriter(outFile, false);
083            try {
084                PluginXmlUtil.writePluginList(pluginList, out);
085            } finally {
086                out.close();
087            }
088        }
089    
090        public void updatePluginList(PluginType plugin, PluginListType pluginList) throws NoSuchStoreException {
091            PluginType key = PluginInstallerGBean.toKey(plugin);
092            PluginArtifactType instance = plugin.getPluginArtifact().get(0);
093            Artifact id = PluginInstallerGBean.toArtifact(instance.getModuleId());
094            boolean foundKey = false;
095            for (PluginType test : pluginList.getPlugin()) {
096                for (Iterator<PluginArtifactType> it = test.getPluginArtifact().iterator(); it.hasNext();) {
097                    PluginArtifactType testInstance = it.next();
098                    Artifact testId = PluginInstallerGBean.toArtifact(testInstance.getModuleId());
099                    if (id.equals(testId)) {
100                        //if the module id appears anywhere, remove that instance
101                        it.remove();
102                    }
103                }
104                PluginType testKey = PluginInstallerGBean.toKey(test);
105                if (key.equals(testKey)) {
106                    foundKey = true;
107                    //if the name, group, author, licences, url match, then add this instance to current pluginType
108                    test.getPluginArtifact().add(instance);
109                }
110            }
111            if (!foundKey) {
112                //did not find a matching key
113                pluginList.getPlugin().add(plugin);
114            }
115        }
116    
117    }