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: 613667 $ $Date: 2008-01-20 12:18:37 -0800 (Sun, 20 Jan 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(
064: repository, path);
065: File outFile = new File(baseDir, "geronimo-plugins.xml");
066: Writer out = new FileWriter(outFile, false);
067: try {
068: PluginXmlUtil.writePluginList(pluginList, out);
069: } finally {
070: out.close();
071: }
072: }
073:
074: public PluginListType createPluginListForRepositories(
075: ListableRepository repository, String repoName)
076: throws NoSuchStoreException {
077: Map<PluginType, PluginType> pluginMap = new HashMap<PluginType, PluginType>();
078: SortedSet<Artifact> configs = repository.list();
079: for (Artifact configId : configs) {
080: PluginType data = getPluginMetadata(repository, configId);
081:
082: if (data != null) {
083: PluginType key = PluginInstallerGBean.toKey(data);
084: PluginType existing = pluginMap.get(key);
085: if (existing == null) {
086: pluginMap.put(key, data);
087: } else {
088: existing.getPluginArtifact().addAll(
089: data.getPluginArtifact());
090: }
091: }
092: }
093: PluginListType pluginList = new PluginListType();
094: pluginList.getPlugin().addAll(pluginMap.values());
095: pluginList.getDefaultRepository().add(repoName);
096: return pluginList;
097: }
098:
099: private PluginType getPluginMetadata(ListableRepository repository,
100: Artifact configId) {
101: File dir = repository.getLocation(configId);
102: if (!dir.isFile() || !dir.canRead()) {
103: log.error("Cannot read artifact dir "
104: + dir.getAbsolutePath());
105: throw new IllegalStateException("Cannot read artifact dir "
106: + dir.getAbsolutePath());
107: }
108: if (dir.toString().endsWith(".pom")) {
109: return null;
110: }
111: try {
112: JarFile jar = new JarFile(dir);
113: try {
114: ZipEntry entry = jar
115: .getEntry("META-INF/geronimo-plugin.xml");
116: if (entry == null) {
117: return null;
118: }
119: InputStream in = jar.getInputStream(entry);
120: try {
121: PluginType pluginType = PluginXmlUtil
122: .loadPluginMetadata(in);
123: if (pluginType.getPluginArtifact().isEmpty()) {
124: return null;
125: }
126: return pluginType;
127: } finally {
128: in.close();
129: }
130: } finally {
131: jar.close();
132: }
133: } catch (ZipException e) {
134: //not a zip file, ignore
135: } catch (SAXException e) {
136: log.error("Unable to read JAR file "
137: + dir.getAbsolutePath(), e);
138: } catch (XMLStreamException e) {
139: log.error("Unable to read JAR file "
140: + dir.getAbsolutePath(), e);
141: } catch (JAXBException e) {
142: log.error("Unable to read JAR file "
143: + dir.getAbsolutePath(), e);
144: } catch (IOException e) {
145: log.error("Unable to read JAR file "
146: + dir.getAbsolutePath(), e);
147: }
148: return null;
149: }
150:
151: }
|