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