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.FileWriter;
024 import java.io.IOException;
025 import java.io.InputStream;
026 import java.io.Writer;
027 import java.util.HashMap;
028 import java.util.Map;
029 import java.util.SortedSet;
030 import java.util.jar.JarFile;
031 import java.util.zip.ZipEntry;
032 import java.util.zip.ZipException;
033
034 import javax.xml.bind.JAXBException;
035 import javax.xml.stream.XMLStreamException;
036
037 import org.apache.geronimo.kernel.config.NoSuchStoreException;
038 import org.apache.geronimo.kernel.repository.Artifact;
039 import org.apache.geronimo.kernel.repository.ListableRepository;
040 import org.apache.geronimo.kernel.repository.Maven2Repository;
041 import org.apache.geronimo.system.plugin.PluginInstallerGBean;
042 import org.apache.geronimo.system.plugin.PluginXmlUtil;
043 import org.apache.geronimo.system.plugin.model.PluginListType;
044 import org.apache.geronimo.system.plugin.model.PluginType;
045 import org.xml.sax.SAXException;
046
047 /**
048 * Creates or replaces a geronimo-plugins.xml catalog of geronimo plugins in the local maven repository. Although geronimo-plugins.xml is
049 * maintained automatically when you build geronimo plugins locally, this is useful if you have downloaded plugins from elsewhere or
050 * corrupted the geronimo-plugins.xml file. This must be run explcitly using the command line
051 * mvn org.apache.geronimo.buildsupport:car-maven-plugin:create-pluginlist
052 *
053 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
054 * @goal create-pluginlist
055 */
056 public class CreatePluginListMojo extends AbstractCarMojo {
057
058 protected void doExecute() throws Exception {
059 String path = getArtifactRepository().getBasedir();
060 File baseDir = new File(path);
061
062 ListableRepository repository = new Maven2Repository(baseDir);
063 PluginListType pluginList = createPluginListForRepositories(repository, path);
064 File outFile = new File(baseDir, "geronimo-plugins.xml");
065 Writer out = new FileWriter(outFile, false);
066 try {
067 PluginXmlUtil.writePluginList(pluginList, out);
068 } finally {
069 out.close();
070 }
071 }
072
073 public PluginListType createPluginListForRepositories(ListableRepository repository, String repoName) throws NoSuchStoreException {
074 Map<PluginType, PluginType> pluginMap = new HashMap<PluginType, PluginType>();
075 SortedSet<Artifact> configs = repository.list();
076 for (Artifact configId : configs) {
077 PluginType data = getPluginMetadata(repository, configId);
078
079 if (data != null) {
080 PluginType key = PluginInstallerGBean.toKey(data);
081 PluginType existing = pluginMap.get(key);
082 if (existing == null) {
083 pluginMap.put(key, data);
084 } else {
085 existing.getPluginArtifact().addAll(data.getPluginArtifact());
086 }
087 }
088 }
089 PluginListType pluginList = new PluginListType();
090 pluginList.getPlugin().addAll(pluginMap.values());
091 pluginList.getDefaultRepository().add(repoName);
092 return pluginList;
093 }
094
095 private PluginType getPluginMetadata(ListableRepository repository, Artifact configId) {
096 File dir = repository.getLocation(configId);
097 if (!dir.isFile() || !dir.canRead()) {
098 log.error("Cannot read artifact dir " + dir.getAbsolutePath());
099 throw new IllegalStateException("Cannot read artifact dir " + dir.getAbsolutePath());
100 }
101 if (dir.toString().endsWith(".pom")) {
102 return null;
103 }
104 try {
105 JarFile jar = new JarFile(dir);
106 try {
107 ZipEntry entry = jar.getEntry("META-INF/geronimo-plugin.xml");
108 if (entry == null) {
109 return null;
110 }
111 InputStream in = jar.getInputStream(entry);
112 try {
113 PluginType pluginType = PluginXmlUtil.loadPluginMetadata(in);
114 if (pluginType.getPluginArtifact().isEmpty()) {
115 return null;
116 }
117 return pluginType;
118 } finally {
119 in.close();
120 }
121 } finally {
122 jar.close();
123 }
124 } catch (ZipException e) {
125 //not a zip file, ignore
126 } catch (SAXException e) {
127 log.error("Unable to read JAR file " + dir.getAbsolutePath(), e);
128 } catch (XMLStreamException e) {
129 log.error("Unable to read JAR file " + dir.getAbsolutePath(), e);
130 } catch (JAXBException e) {
131 log.error("Unable to read JAR file " + dir.getAbsolutePath(), e);
132 } catch (IOException e) {
133 log.error("Unable to read JAR file " + dir.getAbsolutePath(), e);
134 }
135 return null;
136 }
137
138
139 }