Home » openejb-3.1.2-src » org.apache » openejb » jee » jpa » unit » [javadoc | source]

    1   /**
    2    *
    3    * Licensed to the Apache Software Foundation (ASF) under one or more
    4    * contributor license agreements.  See the NOTICE file distributed with
    5    * this work for additional information regarding copyright ownership.
    6    * The ASF licenses this file to You under the Apache License, Version 2.0
    7    * (the "License"); you may not use this file except in compliance with
    8    * the License.  You may obtain a copy of the License at
    9    *
   10    *     http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    *  Unless required by applicable law or agreed to in writing, software
   13    *  distributed under the License is distributed on an "AS IS" BASIS,
   14    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15    *  See the License for the specific language governing permissions and
   16    *  limitations under the License.
   17    */
   18   package org.apache.openejb.jee.jpa.unit;
   19   
   20   import org.xml.sax.InputSource;
   21   import org.xml.sax.SAXException;
   22   import org.xml.sax.XMLReader;
   23   import org.xml.sax.helpers.XMLFilterImpl;
   24   import org.apache.openejb.jee.JAXBContextFactory;
   25   
   26   import javax.xml.bind.JAXBContext;
   27   import javax.xml.bind.Unmarshaller;
   28   import javax.xml.bind.UnmarshallerHandler;
   29   import javax.xml.parsers.SAXParser;
   30   import javax.xml.parsers.SAXParserFactory;
   31   import javax.xml.transform.sax.SAXSource;
   32   import java.io.InputStream;
   33   import java.io.ByteArrayInputStream;
   34   import java.io.IOException;
   35   import java.net.URL;
   36   
   37   /**
   38    * @version $Revision: 662143 $ $Date: 2008-05-31 20:59:55 -0700 (Sat, 31 May 2008) $
   39    */
   40   public class JaxbPersistenceFactory {
   41       public static final String PERSISTENCE_SCHEMA = "http://java.sun.com/xml/ns/persistence";
   42       public static Persistence getPersistence(URL url) throws Exception {
   43           InputStream persistenceDescriptor = null;
   44   
   45           try {
   46   
   47               persistenceDescriptor = url.openStream();
   48   
   49               JAXBContext jc = JAXBContextFactory.newInstance(Persistence.class);
   50               Unmarshaller u = jc.createUnmarshaller();
   51               UnmarshallerHandler uh = u.getUnmarshallerHandler();
   52   
   53               // create a new XML parser
   54               SAXParserFactory factory = SAXParserFactory.newInstance();
   55               factory.setNamespaceAware(true);
   56               factory.setValidating(true);
   57               SAXParser parser = factory.newSAXParser();
   58   
   59               XMLReader xmlReader = parser.getXMLReader();
   60   
   61               // Create a filter to intercept events
   62               PersistenceFilter xmlFilter = new PersistenceFilter(xmlReader);
   63   
   64               // Be sure the filter has the JAXB content handler set (or it wont
   65               // work)
   66               xmlFilter.setContentHandler(uh);
   67               SAXSource source = new SAXSource(xmlFilter, new InputSource(persistenceDescriptor));
   68   
   69               return (Persistence) u.unmarshal(source);
   70   
   71           } finally {
   72               if (persistenceDescriptor != null) persistenceDescriptor.close();
   73           }
   74       }
   75   
   76       // Inject the proper namespace
   77       public static class PersistenceFilter extends XMLFilterImpl {
   78           private static final InputSource EMPTY_INPUT_SOURCE = new InputSource(new ByteArrayInputStream(new byte[0]));
   79   
   80           public PersistenceFilter(XMLReader xmlReader) {
   81               super(xmlReader);
   82           }
   83   
   84           public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
   85               return EMPTY_INPUT_SOURCE;
   86           }
   87       }
   88   }

Home » openejb-3.1.2-src » org.apache » openejb » jee » jpa » unit » [javadoc | source]