Home » geronimo-eclipse-plugin-2.2-source-release » org.apache.geronimo.st.core.jaxb » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *    http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.geronimo.st.core.jaxb;
   18   
   19   import java.io.ByteArrayInputStream;
   20   import java.io.ByteArrayOutputStream;
   21   import java.io.IOException;
   22   import java.io.InputStream;
   23   import java.io.OutputStream;
   24   import java.io.OutputStreamWriter;
   25   import java.io.UnsupportedEncodingException;
   26   import java.lang.reflect.Method;
   27   
   28   import javax.xml.bind.JAXBContext;
   29   import javax.xml.bind.JAXBElement;
   30   import javax.xml.bind.JAXBException;
   31   import javax.xml.bind.Marshaller;
   32   import javax.xml.bind.Unmarshaller;
   33   import javax.xml.parsers.DocumentBuilder;
   34   import javax.xml.parsers.DocumentBuilderFactory;
   35   import javax.xml.parsers.ParserConfigurationException;
   36   import javax.xml.parsers.SAXParser;
   37   import javax.xml.parsers.SAXParserFactory;
   38   import javax.xml.transform.OutputKeys;
   39   import javax.xml.transform.Result;
   40   import javax.xml.transform.Transformer;
   41   import javax.xml.transform.TransformerConfigurationException;
   42   import javax.xml.transform.TransformerException;
   43   import javax.xml.transform.TransformerFactory;
   44   import javax.xml.transform.dom.DOMSource;
   45   import javax.xml.transform.sax.SAXSource;
   46   import javax.xml.transform.stream.StreamResult;
   47   
   48   import org.apache.geronimo.st.core.Activator;
   49   import org.apache.geronimo.st.core.internal.Trace;
   50   import org.eclipse.core.resources.IContainer;
   51   import org.eclipse.core.resources.IFile;
   52   import org.eclipse.core.resources.IFolder;
   53   import org.eclipse.core.runtime.CoreException;
   54   import org.w3c.dom.Document;
   55   import org.xml.sax.InputSource;
   56   import org.xml.sax.SAXException;
   57   
   58   /**
   59    * @version $Rev: 817996 $ $Date: 2009-09-23 16:04:12 +0800 (Wed, 23 Sep 2009) $
   60    */
   61   public class JAXB21Utils implements IJAXBUtilsProvider{
   62   
   63       // JAXBContext instantiation is costly - must be done only once!
   64       private static final JAXBContext jaxbContext = newJAXBContext();
   65       private static final JAXBContext jaxbPluginContext = newJAXBPluginContext();
   66       private static final MarshallerListener marshallerListener = new MarshallerListener();
   67       //private static JAXB21Utils _instance = new JAXB21Utils();
   68       
   69       private static JAXBContext newJAXBContext() {
   70           try {
   71               return JAXBContext.newInstance( 
   72                       "org.apache.geronimo.jee.connector:" +
   73                       "org.apache.geronimo.jee.loginconfig:" +
   74                       "org.apache.geronimo.jee.openejb:" +
   75                       "org.apache.geronimo.jee.web:" +
   76                       "org.apache.geronimo.jee.application:" +
   77                       "org.apache.geronimo.jee.applicationclient:" +
   78                       "org.apache.geronimo.jee.deployment:" +
   79                       "org.apache.geronimo.jee.naming:" +
   80                       "org.apache.geronimo.jee.security:", Activator.class.getClassLoader() );
   81           } catch (JAXBException e) {
   82               Trace.tracePoint("JAXBException", "JAXBContext.newInstance");
   83               e.printStackTrace();
   84           }
   85           return null;
   86       }
   87       
   88       /*private JAXB21Utils(){
   89       }
   90       
   91       public static JAXB21Utils getInstance(){
   92       	return _instance;
   93       }*/
   94       
   95       public JAXBContext getJAXBContext(){
   96           return jaxbContext;
   97       }
   98   
   99       private static JAXBContext newJAXBPluginContext() {
  100           try {
  101               return JAXBContext.newInstance( 
  102                       "org.apache.geronimo.system.plugin.model", Activator.class.getClassLoader() );
  103           } catch (JAXBException e) {
  104               Trace.tracePoint("JAXBException", "JAXBContext.newInstance");
  105               e.printStackTrace();
  106           }
  107           return null;
  108       }
  109       
  110       public void marshalDeploymentPlan(JAXBElement jaxbElement, IFile file) throws Exception {
  111           try {
  112               Marshaller marshaller = jaxbContext.createMarshaller();
  113               marshaller.setListener(marshallerListener);
  114   
  115               DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  116               dbf.setNamespaceAware(true);
  117               DocumentBuilder db = dbf.newDocumentBuilder();
  118               Document doc = db.newDocument(); 
  119   
  120               marshaller.marshal(jaxbElement, doc);
  121   
  122               TransformerFactory xf = TransformerFactory.newInstance();
  123               try {
  124               	xf.setAttribute("indent-number", new Integer(4));
  125               } catch (IllegalArgumentException iae) {
  126                   //ignore this. http://forums.sun.com/thread.jspa?threadID=562510&messageID=2841867
  127               }
  128               Transformer xformer = xf.newTransformer();
  129               xformer.setOutputProperty(OutputKeys.METHOD, "xml");
  130               xformer.setOutputProperty(OutputKeys.INDENT, "yes");
  131               xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
  132               xformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "4"); 
  133   
  134               ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
  135               Result out = new StreamResult(new OutputStreamWriter(outBuffer,"UTF-8"));
  136               NamespacePrefix.processPrefix(doc);
  137   
  138               xformer.transform(new DOMSource(doc), out);
  139               ByteArrayInputStream inBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
  140               if(file.exists()) {
  141                   file.setContents(inBuffer, true, false, null);
  142               } else {
  143                   prepareFolder(file.getParent());
  144                   file.create(inBuffer, true, null);
  145               }
  146           } catch (JAXBException jaxbException) {
  147               Trace.tracePoint("JAXBException", "JAXBUtils.marshalDeploymentPlan()", file.getFullPath());
  148               throw jaxbException;
  149           } catch (CoreException coreException) {
  150               Trace.tracePoint("CoreException", "JAXBUtils.marshalDeploymentPlan()", file.getFullPath());
  151               throw coreException;
  152           } catch (ParserConfigurationException e) {
  153           	Trace.tracePoint("ParserConfigurationException", "JAXBUtils.marshalDeploymentPlan()", file.getFullPath());
  154           	throw e;
  155   		} catch (TransformerConfigurationException e) {
  156   			Trace.tracePoint("TransformerConfigurationException", "JAXBUtils.marshalDeploymentPlan()", file.getFullPath());
  157   			throw e;
  158   		} catch (UnsupportedEncodingException e) {
  159   			Trace.tracePoint("UnsupportedEncodingException", "JAXBUtils.marshalDeploymentPlan()", file.getFullPath());
  160   			throw e;
  161   		} catch (TransformerException e) {
  162   			Trace.tracePoint("TransformerException", "JAXBUtils.marshalDeploymentPlan()", file.getFullPath());
  163   			throw e;
  164   		}
  165       }
  166   
  167       public JAXBElement unmarshalFilterDeploymentPlan(IFile file) throws Exception {
  168           try {
  169               Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
  170               SAXParserFactory factory = SAXParserFactory.newInstance();
  171               factory.setNamespaceAware(true);
  172               factory.setValidating(false);
  173               SAXParser parser = factory.newSAXParser();
  174               NamespaceFilter xmlFilter = new NamespaceFilter(parser.getXMLReader());
  175               SAXSource source = new SAXSource(xmlFilter, new InputSource(file.getContents()));
  176               JAXBElement plan = (JAXBElement) unmarshaller.unmarshal(source);
  177               return plan;
  178           } catch (JAXBException e) {
  179               Trace.tracePoint("JAXBException", "JAXBUtils.unmarshalFilterDeploymentPlan()", file.getFullPath());
  180               throw e;
  181           } catch (CoreException e) {
  182               Trace.tracePoint("CoreException", "JAXBUtils.unmarshalFilterDeploymentPlan()", file.getFullPath());
  183               throw e;
  184           } catch (ParserConfigurationException e) {
  185               Trace.tracePoint("ParserConfigurationException", "JAXBUtils.unmarshalFilterDeploymentPlan()", file.getFullPath());
  186               throw e;
  187           } catch (SAXException e) {
  188               Trace.tracePoint("SAXException", "JAXBUtils.unmarshalFilterDeploymentPlan()", file.getFullPath());
  189               throw e;
  190           }
  191       }
  192   
  193       public void marshalPlugin(JAXBElement jaxbElement, OutputStream outputStream) throws Exception {
  194           try {
  195               Marshaller marshaller = jaxbPluginContext.createMarshaller();
  196               marshaller.setListener(marshallerListener);
  197   
  198               DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  199               DocumentBuilder db = dbf.newDocumentBuilder();
  200               Document doc = db.newDocument(); 
  201   
  202               marshaller.marshal(jaxbElement, doc);
  203   
  204               TransformerFactory xf = TransformerFactory.newInstance();
  205               try {
  206                   xf.setAttribute("indent-number", new Integer(4));
  207               } catch (IllegalArgumentException iae) {
  208                   //ignore this. http://forums.sun.com/thread.jspa?threadID=562510&messageID=2841867
  209               }
  210               Transformer xformer = xf.newTransformer();
  211               xformer.setOutputProperty(OutputKeys.METHOD, "xml");
  212               xformer.setOutputProperty(OutputKeys.INDENT, "yes");
  213               xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
  214               xformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "4"); 
  215   
  216               ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
  217               Result out = new StreamResult(new OutputStreamWriter(outBuffer,"UTF-8"));
  218   
  219               xformer.transform(new DOMSource(doc), out);
  220               ByteArrayInputStream inBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
  221               outputStream.write(outBuffer.toByteArray());
  222           } catch (JAXBException jaxbException) {
  223               Trace.tracePoint("JAXBException", "JAXBUtils.marshalDeploymentPlan()");
  224               throw jaxbException;
  225           } catch (IOException coreException) {
  226               Trace.tracePoint("IOException", "JAXBUtils.marshalDeploymentPlan()");
  227               throw coreException;
  228           } catch (ParserConfigurationException e) {
  229               Trace.tracePoint("ParserConfigurationException", "JAXBUtils.marshalDeploymentPlan()");
  230               throw e;
  231           } catch (TransformerConfigurationException e) {
  232               Trace.tracePoint("TransformerConfigurationException", "JAXBUtils.marshalDeploymentPlan()");
  233               throw e;
  234           } catch (TransformerException e) {
  235               Trace.tracePoint("TransformerException", "JAXBUtils.marshalDeploymentPlan()");
  236               throw e;
  237           }
  238       }
  239   
  240       public JAXBElement unmarshalPlugin(InputStream inputStream) {
  241           try {
  242               Unmarshaller unmarshaller = jaxbPluginContext.createUnmarshaller();
  243               SAXParserFactory factory = SAXParserFactory.newInstance();
  244               factory.setNamespaceAware(true);
  245               factory.setValidating(false);
  246               SAXParser parser = factory.newSAXParser();
  247               NamespaceFilter xmlFilter = new NamespaceFilter(parser.getXMLReader());
  248               SAXSource source = new SAXSource(xmlFilter, new InputSource(inputStream));
  249               JAXBElement plan = (JAXBElement) unmarshaller.unmarshal(source);
  250               return plan;
  251           } catch (JAXBException e) {
  252               Trace.tracePoint("JAXBException", "JAXBUtils.unmarshalFilterDeploymentPlan()");
  253               e.printStackTrace();
  254           } catch (ParserConfigurationException e) {
  255               Trace.tracePoint("ParserConfigurationException", "JAXBUtils.unmarshalFilterDeploymentPlan()");
  256               e.printStackTrace();
  257           } catch (SAXException e) {
  258               Trace.tracePoint("SAXException", "JAXBUtils.unmarshalFilterDeploymentPlan()");
  259               e.printStackTrace();
  260           }
  261           return null;
  262       }
  263   
  264       private void prepareFolder(IContainer folder) throws CoreException {
  265           if (folder.exists() || !(folder instanceof IFolder)) {
  266               return;
  267           }
  268           // prepare the upper level folders recursively
  269           prepareFolder(folder.getParent());
  270           ((IFolder) folder).create(true, true, null);
  271       }
  272   
  273       public Object getValue( Object element, String name ) throws Exception {
  274           try {
  275               if (String.class.isInstance(element))
  276                   return (String)element;
  277               Method method = element.getClass().getMethod( "get" + name, null);
  278               return method.invoke(element, null);
  279           } catch ( Exception e ) {
  280               throw e;
  281           }
  282       }
  283       
  284       public void setValue( Object element, String name, Object value ) throws Exception {
  285           try {
  286               Method[] methods = element.getClass().getMethods();
  287               for ( Method method: methods) {
  288                   if ( method.getName().equals( "set" + name ) ) {
  289                       method.invoke( element, value );
  290                       return;
  291                   }
  292               }
  293           } catch (Exception e) {
  294               throw e;
  295           }
  296           System.out.println( "============== No such method set" + name + " in class " + element.getClass().getName() );
  297       }
  298   }

Home » geronimo-eclipse-plugin-2.2-source-release » org.apache.geronimo.st.core.jaxb » [javadoc | source]