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.system.plugin; 022 023 import java.io.OutputStream; 024 import java.io.Writer; 025 import java.io.InputStream; 026 import java.io.Reader; 027 import java.io.IOException; 028 import java.io.StringWriter; 029 import java.net.MalformedURLException; 030 031 import javax.xml.stream.XMLInputFactory; 032 import javax.xml.stream.XMLStreamException; 033 import javax.xml.stream.XMLStreamReader; 034 import javax.xml.bind.JAXBContext; 035 import javax.xml.bind.JAXBException; 036 import javax.xml.bind.Marshaller; 037 import javax.xml.bind.JAXBElement; 038 import javax.xml.bind.Unmarshaller; 039 import javax.xml.parsers.ParserConfigurationException; 040 import javax.xml.parsers.SAXParserFactory; 041 import javax.xml.parsers.SAXParser; 042 import javax.xml.transform.sax.SAXSource; 043 044 import org.xml.sax.helpers.XMLFilterImpl; 045 import org.xml.sax.XMLReader; 046 import org.xml.sax.Attributes; 047 import org.xml.sax.SAXException; 048 import org.xml.sax.InputSource; 049 import org.apache.geronimo.system.plugin.model.PluginType; 050 import org.apache.geronimo.system.plugin.model.ObjectFactory; 051 import org.apache.geronimo.system.plugin.model.PluginListType; 052 import org.apache.geronimo.system.plugin.model.PluginArtifactType; 053 import org.apache.geronimo.system.plugin.model.AttributeType; 054 import org.apache.geronimo.system.plugin.model.AttributesType; 055 import org.apache.geronimo.system.plugin.model.ModuleType; 056 import org.apache.geronimo.system.plugin.model.GbeanType; 057 058 /** 059 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $ 060 */ 061 public class PluginXmlUtil { 062 public static final XMLInputFactory XMLINPUT_FACTORY = XMLInputFactory.newInstance(); 063 public static final JAXBContext PLUGIN_CONTEXT; 064 public static final JAXBContext PLUGIN_LIST_CONTEXT; 065 public static final JAXBContext PLUGIN_ARTIFACT_CONTEXT; 066 067 static { 068 try { 069 PLUGIN_CONTEXT = JAXBContext.newInstance(PluginType.class); 070 PLUGIN_LIST_CONTEXT = JAXBContext.newInstance(PluginListType.class); 071 PLUGIN_ARTIFACT_CONTEXT = JAXBContext.newInstance(PluginArtifactType.class); 072 } catch (JAXBException e) { 073 throw new RuntimeException("Could not create jaxb contexts for plugin types"); 074 } 075 } 076 077 public static void writePluginMetadata(PluginType metadata, OutputStream out) throws XMLStreamException, JAXBException { 078 Marshaller marshaller = PLUGIN_CONTEXT.createMarshaller(); 079 marshaller.setProperty("jaxb.formatted.output", true); 080 JAXBElement<PluginType> element = new ObjectFactory().createGeronimoPlugin(metadata); 081 marshaller.marshal(element, out); 082 } 083 084 public static void writePluginList(PluginListType metadata, Writer out) throws XMLStreamException, JAXBException { 085 Marshaller marshaller = PLUGIN_LIST_CONTEXT.createMarshaller(); 086 marshaller.setProperty("jaxb.formatted.output", true); 087 JAXBElement<PluginListType> element = new ObjectFactory().createGeronimoPluginList(metadata); 088 marshaller.marshal(element, out); 089 } 090 091 092 /** 093 * Read a set of plugin metadata from a DOM document. 094 */ 095 public static PluginType loadPluginMetadata(InputStream in) throws SAXException, MalformedURLException, JAXBException, XMLStreamException { 096 XMLStreamReader xmlStream = XMLINPUT_FACTORY.createXMLStreamReader(in); 097 Unmarshaller unmarshaller = PLUGIN_CONTEXT.createUnmarshaller(); 098 JAXBElement<PluginType> element = unmarshaller.unmarshal(xmlStream, PluginType.class); 099 PluginType plugin = element.getValue(); 100 return plugin; 101 } 102 103 public static PluginArtifactType loadPluginArtifactMetadata(Reader in) throws SAXException, MalformedURLException, JAXBException, XMLStreamException, ParserConfigurationException { 104 InputSource inputSource = new InputSource(in); 105 106 SAXParserFactory factory = SAXParserFactory.newInstance(); 107 factory.setNamespaceAware(true); 108 factory.setValidating(false); 109 SAXParser parser = factory.newSAXParser(); 110 111 Unmarshaller unmarshaller = PLUGIN_ARTIFACT_CONTEXT.createUnmarshaller(); 112 // unmarshaller.setEventHandler(new ValidationEventHandler(){ 113 // public boolean handleEvent(ValidationEvent validationEvent) { 114 // System.out.println(validationEvent); 115 // return false; 116 // } 117 // }); 118 119 120 NamespaceFilter xmlFilter = new NamespaceFilter(parser.getXMLReader()); 121 xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler()); 122 123 SAXSource source = new SAXSource(xmlFilter, inputSource); 124 // XMLStreamReader xmlStream = XMLINPUT_FACTORY.createXMLStreamReader(in); 125 JAXBElement<PluginArtifactType> element = unmarshaller.unmarshal(source, PluginArtifactType.class); 126 PluginArtifactType plugin = element.getValue(); 127 return plugin; 128 } 129 130 /** 131 * Loads the list of all available plugins from the specified stream 132 * (representing geronimo-plugins.xml at the specified repository). 133 */ 134 public static PluginListType loadPluginList(InputStream in) throws ParserConfigurationException, IOException, SAXException, JAXBException, XMLStreamException { 135 Unmarshaller unmarshaller = PLUGIN_LIST_CONTEXT.createUnmarshaller(); 136 XMLStreamReader xmlStream = XMLINPUT_FACTORY.createXMLStreamReader(in); 137 JAXBElement<PluginListType> element = unmarshaller.unmarshal(xmlStream, PluginListType.class); 138 PluginListType pluginList = element.getValue(); 139 return pluginList; 140 } 141 142 public static class NamespaceFilter extends XMLFilterImpl { 143 private static String PLUGIN_NS = "http://geronimo.apache.org/xml/ns/plugins-1.3"; 144 private static String GBEAN_NS = "http://geronimo.apache.org/xml/ns/attributes-1.2"; 145 private static String ENVIRONMENT_NS = "http://geronimo.apache.org/xml/ns/deployment-1.2"; 146 147 private String namespace; 148 149 public NamespaceFilter(XMLReader xmlReader) { 150 super(xmlReader); 151 } 152 153 @Override 154 public void startElement(String uri, String localName, String qname, Attributes atts) throws SAXException { 155 if ("plugin-artifact".equals(localName)) { 156 namespace = PLUGIN_NS; 157 } else if ("gbean".equals(localName)) { 158 namespace = GBEAN_NS; 159 } else if ("environment".equals(localName)) { 160 namespace = ENVIRONMENT_NS; 161 } 162 super.startElement(namespace, localName, qname, atts); 163 } 164 165 @Override 166 public void endElement(String uri, String localName, String qName) throws SAXException { 167 super.endElement(namespace, localName, qName); 168 if ("plugin-artifact".equals(localName)) { 169 namespace = null; 170 } else if ("gbean".equals(localName)) { 171 namespace = PLUGIN_NS; 172 } else if ("environment".equals(localName)) { 173 namespace = GBEAN_NS; 174 } 175 } 176 } 177 }