incubator-aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From timothyjw...@apache.org
Subject svn commit: r896591 - in /incubator/aries/trunk/jpa: ./ jpa-container/ jpa-container/src/main/java/org/apache/aries/jpa/container/impl/ jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/ jpa-container/src/main/java/org/apache/aries/jpa...
Date Wed, 06 Jan 2010 18:16:03 GMT
Author: timothyjward
Date: Wed Jan  6 18:16:02 2010
New Revision: 896591

URL: http://svn.apache.org/viewvc?rev=896591&view=rev
Log:
ARIES-80: Add parsing logic for persistence units

Added:
    incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/PersistenceDescriptor.java
    incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/ParsedPersistenceUnit.java
    incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/PersistenceDescriptorParser.java
    incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/PersistenceDescriptorParserException.java
    incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/SchemaLocatingHandler.java
    incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/impl/
Modified:
    incubator/aries/trunk/jpa/jpa-container/pom.xml
    incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/PersistenceBundleHelper.java
    incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/PersistenceBundleManager.java
    incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/EarlyParserReturn.java
    incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/JPAHandler.java
    incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/PersistenceUnitImpl.java
    incubator/aries/trunk/jpa/pom.xml

Modified: incubator/aries/trunk/jpa/jpa-container/pom.xml
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-container/pom.xml?rev=896591&r1=896590&r2=896591&view=diff
==============================================================================
--- incubator/aries/trunk/jpa/jpa-container/pom.xml (original)
+++ incubator/aries/trunk/jpa/jpa-container/pom.xml Wed Jan  6 18:16:02 2010
@@ -21,5 +21,16 @@
       <artifactId>org.osgi.compendium</artifactId>
       <scope>provided</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.aries.testsupport</groupId>
+      <artifactId>org.apache.aries.testsupport.unit</artifactId>
+      <scope>test</scope>
+    </dependency>
+         
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 </project>
\ No newline at end of file

Modified: incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/PersistenceBundleHelper.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/PersistenceBundleHelper.java?rev=896591&r1=896590&r2=896591&view=diff
==============================================================================
--- incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/PersistenceBundleHelper.java
(original)
+++ incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/PersistenceBundleHelper.java
Wed Jan  6 18:16:02 2010
@@ -51,10 +51,10 @@
    * @param bundle
    * @return
    */
-  public static Collection<InputStream> findPersistenceXmlFiles(Bundle bundle)
+  public static Collection<PersistenceDescriptor> findPersistenceXmlFiles(Bundle bundle)
   {
     //The files we have found
-    Collection<InputStream> persistenceXmlFiles = new HashSet<InputStream>();
+    Collection<PersistenceDescriptor> persistenceXmlFiles = new HashSet<PersistenceDescriptor>();
     
     //Always search the default location
     List<String> locations = new ArrayList<String>();
@@ -71,15 +71,14 @@
     try {
       for(String location : locations) {
         InputStream file = locateFile(bundle, location.trim());
-          
         if(file != null)
-          persistenceXmlFiles.add(file);
+          persistenceXmlFiles.add(new PersistenceDescriptor(location, file));
         }
     } catch (Exception e) {
         //TODO log
-      for (InputStream is : persistenceXmlFiles) {
+      for (PersistenceDescriptor desc : persistenceXmlFiles) {
         try {
-          is.close();
+          desc.getInputStream().close();
         } catch (IOException ioe) {
           // TODO: log ioe
         }

Modified: incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/PersistenceBundleManager.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/PersistenceBundleManager.java?rev=896591&r1=896590&r2=896591&view=diff
==============================================================================
--- incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/PersistenceBundleManager.java
(original)
+++ incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/PersistenceBundleManager.java
Wed Jan  6 18:16:02 2010
@@ -19,26 +19,11 @@
 
 package org.apache.aries.jpa.container.impl;
 
-import java.io.InputStream;
 import java.util.Collection;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
 
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
-import org.osgi.framework.Filter;
-import org.osgi.framework.FrameworkUtil;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
-import org.osgi.framework.ServiceRegistration;
-import org.osgi.framework.Version;
 import org.osgi.util.tracker.BundleTracker;
 
 
@@ -49,19 +34,22 @@
 {
   /** The bundle context for this bundle */
   private BundleContext ctx = null;
-  /** A BundleTracker that locates Persistence Bundles */
-  private BundleTracker persistenceBundles = null;
   /** A map of bundles to generated fragments */
-  private final ConcurrentMap<Bundle, Bundle> hostToFragmentMap = new ConcurrentHashMap<Bundle,
Bundle>();
-  /** A map of persistence bundles to sets of persistence metadata */
-  private final ConcurrentMap<Bundle, Set<ServiceRegistration>> hostToPersistenceUnitMap
= new ConcurrentHashMap<Bundle, Set<ServiceRegistration>>();
+//  private final ConcurrentMap<Bundle, Bundle> hostToFragmentMap = new ConcurrentHashMap<Bundle,
Bundle>();
+//  /** A map of persistence bundles to sets of persistence metadata */
+//  private final ConcurrentMap<Bundle, Set<ServiceRegistration>> hostToPersistenceUnitMap
= new ConcurrentHashMap<Bundle, Set<ServiceRegistration>>();
   //TODO pull this from config
   /** The default JPA provider to use */
   public static final String DEFAULT_JPA_PROVIDER ="org.apache.openjpa.persistence.PersistenceProviderImpl";
 
+  /**
+   * Create the extender. Note that it will not start tracking 
+   * until the {@code open()} method is called
+   * @param ctx The extender bundle's context
+   */
   public PersistenceBundleManager(BundleContext ctx) 
   {
-	super(ctx, Bundle.INSTALLED | Bundle.RESOLVED | Bundle.STARTING |
+	  super(ctx, Bundle.INSTALLED | Bundle.RESOLVED | Bundle.STARTING |
 			  Bundle.ACTIVE | Bundle.STOPPING, null);
     this.ctx = ctx;
   }
@@ -100,7 +88,7 @@
       //TODO LOG WARNING HERE
     }
 
-    Collection <InputStream> persistenceXmls = PersistenceBundleHelper.findPersistenceXmlFiles(bundle);
+    Collection <PersistenceDescriptor> persistenceXmls = PersistenceBundleHelper.findPersistenceXmlFiles(bundle);
 
     //If we have no persistence units then our job is done
     if (!!!persistenceXmls.isEmpty()) {
@@ -168,59 +156,6 @@
       
   }
   
-  /**
-   * Parse the persistence.xml files referenced by the URLs in the collection
-   * @param persistenceXmls
-   * @param bundle  The bundle containing the persistence xml files
-   * @return A collection of parsed persistence units.
-   */
-//  private Collection<PersistenceUnitImpl> parseXmlFiles(Collection<PersistenceLocationData>
persistenceXmls, Bundle bundle)
-//  {
-//    Collection<PersistenceUnitImpl> persistenceUnits = new ArrayList<PersistenceUnitImpl>();
-//    //Parse each xml file in turn
-//    for(PersistenceLocationData datum : persistenceXmls) {
-//      SAXParserFactory parserFactory = SAXParserFactory.newInstance();
-//      InputStream is = null;
-//      try {
-//        SAXParser parser = parserFactory.newSAXParser();
-//        is = datum.getPersistenceXML().openStream();
-//        
-//        try{
-//          parser.parse(is, new SchemaLocatingHandler(ctx.getBundle()));
-//        } catch (EarlyParserReturn epr) {
-//          //This is not really an exception, but a way to work out which
-//          //version of the persistence schema to use in validation
-//          Schema s = epr.getSchema();
-//          
-//          if(s != null) {
-//            parserFactory.setSchema(s);
-//            parserFactory.setNamespaceAware(true);
-//            parser = parserFactory.newSAXParser();
-//           
-//            //Get back to the beginning of the stream
-//            is.close();
-//            is = datum.getPersistenceXML().openStream();
-//            
-//            JPAHandler handler = new JPAHandler(datum, epr.getVersion());
-//            parser.parse(is, handler);
-//       
-//            persistenceUnits.addAll(handler.getPersistenceUnits());
-//          }
-//        }
-//      } catch (Exception e) {
-//        //TODO Log this error in parsing
-//        e.printStackTrace();
-//      } finally {
-//        if(is != null) try {
-//          is.close();
-//        } catch (IOException e) {
-//          //TODO Log this
-//          e.printStackTrace();
-//        }
-//      }
-//    }
-//    return persistenceUnits;
-//  }
 
   /**
    * Get a persistence provider from the service registry described by the
@@ -270,119 +205,119 @@
 //    return null;
 //  }
  
-  /**
-   * Locate the best provider for the given criteria
-   * @param providerClass
-   * @param matchingCriteria
-   * @return
-   */
-  private ServiceReference getBestProvider(String providerClass, Set<Filter> matchingCriteria)
-  {
-    ServiceReference[] array = null;
-    try {
-      array = ctx.getAllServiceReferences(providerClass, null);
-    } catch (InvalidSyntaxException e) {
-      //TODO this can never happen
-    }
-    
-    if(array != null) {
-      //A linked list is faster for large numbers of ServiceReferences
-      //Note we cannot use Arrays.asList() as we need to remove items
-      //via an iterator, and this would throw UnsupportedOperationException.
-      List<ServiceReference> refs = new LinkedList<ServiceReference>();
-      
-      for(ServiceReference reference : array)
-        refs.add(reference);
-      
-      Iterator<ServiceReference> it = refs.iterator();
-      
-      //Remove anything that doesn't match the filter
-      while(it.hasNext())
-      {
-        ServiceReference ref = it.next();
-        for(Filter f : matchingCriteria)
-        {
-          if(!!!f.match(ref)) {
-            it.remove();
-            break;
-          }
-        }
-      }
-      
-      if(!!!refs.isEmpty()) {
-        //Sort the list in DESCENDING ORDER
-        Collections.sort(refs, new Comparator<ServiceReference>() {
-
-          //TODO we may wish to use Ranking, then versions for equal ranks
-          public int compare(ServiceReference object1, ServiceReference object2)
-          {
-            Version v1 = object1.getBundle().getVersion();
-            Version v2 = object2.getBundle().getVersion();
-            return v2.compareTo(v1);
-          }
-        });
-        return refs.get(0);
-      } else {
-        //TODO no matching providers for matching criteria
-      }
-    } else {
-      //TODO log no matching Providers for impl class
-    }
-    
-    return null;
-  }
-  
-  /**
-   * Create a filter for the supplied version range string
-   * @param providerVersion
-   * @return
-   * @throws InvalidSyntaxException
-   */
-  private Filter getFilter(String providerVersion)
-      throws InvalidSyntaxException
-  {
-    String toReturn = null;
-    
-    //TODO NLS enable the messages in the exceptions below (Invalid version range specified...)
-    //Create a filter to match the required provider version range
-    if(providerVersion != null) {
-      if(!!!providerVersion.contains(","))
-        toReturn = ("(osgi.jpa.provider.version>=" + providerVersion + ")");
-      else {
-        String[] versionArray = providerVersion.split(",");
-        
-        if(versionArray.length == 2) {
-          
-          versionArray[0] = versionArray[0].trim();
-          versionArray[1] = versionArray[1].trim();
-          
-          char bracket1 = versionArray[0].charAt(0);
-          char bracket2 = versionArray[1].charAt(versionArray[1].length() - 1);
-          
-          String version1 = versionArray[0].substring(1);
-          String version2 = versionArray[1].substring(0, versionArray[1].length() -1);
-
-          if(version1.compareTo(version2) > 0)
-            throw new InvalidSyntaxException("Invalid version range specified. " + providerVersion,
providerVersion);
-          
-          String compare1 = "(osgi.jpa.provider.version>=" + version1 + ")";
-          String compare2 = "(osgi.jpa.provider.version<=" + version2 + ")";
-          
-          if('(' == bracket1)
-             compare1 = compare1 + "(!(osgi.jpa.provider.version=" + version1 + "))";
-          else if('[' != bracket1) throw new InvalidSyntaxException("Invalid version range
specified. " + providerVersion, providerVersion);
-          
-
-          if(')' == bracket2)
-            compare2 = compare2 + "(!(osgi.jpa.provider.version=" + version2 + "))";
-          else if(']' != bracket2) throw new InvalidSyntaxException("Invalid version range
specified. " + providerVersion, providerVersion);
-         
-         
-          toReturn = "(&" + compare1 + compare2 + ")";
-        } else throw new InvalidSyntaxException("Invalid version range specified. " + providerVersion,
providerVersion);
-        
-      }
-    }
-    return FrameworkUtil.createFilter(toReturn);
-  }
+//  /**
+//   * Locate the best provider for the given criteria
+//   * @param providerClass
+//   * @param matchingCriteria
+//   * @return
+//   */
+//  private ServiceReference getBestProvider(String providerClass, Set<Filter> matchingCriteria)
+//  {
+//    ServiceReference[] array = null;
+//    try {
+//      array = ctx.getAllServiceReferences(providerClass, null);
+//    } catch (InvalidSyntaxException e) {
+//      //TODO this can never happen
+//    }
+//    
+//    if(array != null) {
+//      //A linked list is faster for large numbers of ServiceReferences
+//      //Note we cannot use Arrays.asList() as we need to remove items
+//      //via an iterator, and this would throw UnsupportedOperationException.
+//      List<ServiceReference> refs = new LinkedList<ServiceReference>();
+//      
+//      for(ServiceReference reference : array)
+//        refs.add(reference);
+//      
+//      Iterator<ServiceReference> it = refs.iterator();
+//      
+//      //Remove anything that doesn't match the filter
+//      while(it.hasNext())
+//      {
+//        ServiceReference ref = it.next();
+//        for(Filter f : matchingCriteria)
+//        {
+//          if(!!!f.match(ref)) {
+//            it.remove();
+//            break;
+//          }
+//        }
+//      }
+//      
+//      if(!!!refs.isEmpty()) {
+//        //Sort the list in DESCENDING ORDER
+//        Collections.sort(refs, new Comparator<ServiceReference>() {
+//
+//          //TODO we may wish to use Ranking, then versions for equal ranks
+//          public int compare(ServiceReference object1, ServiceReference object2)
+//          {
+//            Version v1 = object1.getBundle().getVersion();
+//            Version v2 = object2.getBundle().getVersion();
+//            return v2.compareTo(v1);
+//          }
+//        });
+//        return refs.get(0);
+//      } else {
+//        //TODO no matching providers for matching criteria
+//      }
+//    } else {
+//      //TODO log no matching Providers for impl class
+//    }
+//    
+//    return null;
+//  }
+//  
+//  /**
+//   * Create a filter for the supplied version range string
+//   * @param providerVersion
+//   * @return
+//   * @throws InvalidSyntaxException
+//   */
+//  private Filter getFilter(String providerVersion)
+//      throws InvalidSyntaxException
+//  {
+//    String toReturn = null;
+//    
+//    //TODO NLS enable the messages in the exceptions below (Invalid version range specified...)
+//    //Create a filter to match the required provider version range
+//    if(providerVersion != null) {
+//      if(!!!providerVersion.contains(","))
+//        toReturn = ("(osgi.jpa.provider.version>=" + providerVersion + ")");
+//      else {
+//        String[] versionArray = providerVersion.split(",");
+//        
+//        if(versionArray.length == 2) {
+//          
+//          versionArray[0] = versionArray[0].trim();
+//          versionArray[1] = versionArray[1].trim();
+//          
+//          char bracket1 = versionArray[0].charAt(0);
+//          char bracket2 = versionArray[1].charAt(versionArray[1].length() - 1);
+//          
+//          String version1 = versionArray[0].substring(1);
+//          String version2 = versionArray[1].substring(0, versionArray[1].length() -1);
+//
+//          if(version1.compareTo(version2) > 0)
+//            throw new InvalidSyntaxException("Invalid version range specified. " + providerVersion,
providerVersion);
+//          
+//          String compare1 = "(osgi.jpa.provider.version>=" + version1 + ")";
+//          String compare2 = "(osgi.jpa.provider.version<=" + version2 + ")";
+//          
+//          if('(' == bracket1)
+//             compare1 = compare1 + "(!(osgi.jpa.provider.version=" + version1 + "))";
+//          else if('[' != bracket1) throw new InvalidSyntaxException("Invalid version range
specified. " + providerVersion, providerVersion);
+//          
+//
+//          if(')' == bracket2)
+//            compare2 = compare2 + "(!(osgi.jpa.provider.version=" + version2 + "))";
+//          else if(']' != bracket2) throw new InvalidSyntaxException("Invalid version range
specified. " + providerVersion, providerVersion);
+//         
+//         
+//          toReturn = "(&" + compare1 + compare2 + ")";
+//        } else throw new InvalidSyntaxException("Invalid version range specified. " + providerVersion,
providerVersion);
+//        
+//      }
+//    }
+//    return FrameworkUtil.createFilter(toReturn);
+//  }
 }

Added: incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/PersistenceDescriptor.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/PersistenceDescriptor.java?rev=896591&view=auto
==============================================================================
--- incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/PersistenceDescriptor.java
(added)
+++ incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/PersistenceDescriptor.java
Wed Jan  6 18:16:02 2010
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.aries.jpa.container.impl;
+
+import java.io.InputStream;
+
+/**
+ * Stores the location of a persistence descriptor and
+ * a stream to its contents. Note that there is only one
+ * copy of the InputStream, only one thread should try to
+ * read from it, and it can only be closed once. 
+ */
+public class PersistenceDescriptor {
+
+  /** The location of the persistence descriptor */
+  private final String location;
+  /** The wrapped InputStream */ 
+  private final InputStream inputStream;
+
+  /**
+   * Create a PersistenceDescriptor wrapping the location and InputStream
+   * @param location
+   * @param inputStream
+   */
+  public PersistenceDescriptor(String location, InputStream inputStream) {
+    this.location = location;
+    this.inputStream = inputStream;
+  }
+
+  /**
+   * Get the location of the persistence descriptor
+   * @return
+   */
+  public String getLocation() {
+    return location;
+  }
+
+  /**
+   * Get hold of the wrapped InputStream
+   * @return
+   */
+  public InputStream getInputStream() {
+    return inputStream;
+  }
+  
+}

Modified: incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/EarlyParserReturn.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/EarlyParserReturn.java?rev=896591&r1=896590&r2=896591&view=diff
==============================================================================
--- incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/EarlyParserReturn.java
(original)
+++ incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/EarlyParserReturn.java
Wed Jan  6 18:16:02 2010
@@ -16,14 +16,14 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package com.ibm.osgi.jpa.unit.parsing;
+package org.apache.aries.jpa.container.parsing;
 
 import javax.xml.validation.Schema;
 
 import org.xml.sax.SAXException;
 
 /**
- * A convenience mechanism for finding the schema to validate with
+ * A convenience mechanism for finding the version of the schema to validate with
  */
 public class EarlyParserReturn extends SAXException
 {
@@ -59,6 +59,4 @@
     schema = s;
     jpaVersion = version;
   }
-  
-  
 }

Modified: incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/JPAHandler.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/JPAHandler.java?rev=896591&r1=896590&r2=896591&view=diff
==============================================================================
--- incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/JPAHandler.java
(original)
+++ incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/JPAHandler.java
Wed Jan  6 18:16:02 2010
@@ -16,19 +16,17 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package com.ibm.osgi.jpa.unit.parsing;
+package org.apache.aries.jpa.container.parsing;
 
 import java.util.Collection;
 import java.util.Stack;
 
+import org.osgi.framework.Bundle;
 import org.xml.sax.Attributes;
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
 import org.xml.sax.helpers.DefaultHandler;
 
-import com.ibm.osgi.jpa.unit.PersistenceUnitImpl;
-import com.ibm.osgi.jpa.util.PersistenceLocationData;
-
 /**
  *  This code is responsible for parsing the persistence.xml into PersistenceUnits
  */
@@ -38,20 +36,20 @@
   private final Stack<PersistenceUnitImpl> persistenceUnits = new Stack<PersistenceUnitImpl>();
   /** The name of the current element */
   private String elementName;
-  /** The persistence xml location data */
-  private final PersistenceLocationData xmlLocationData;
   /** The version of the persistence.xml file */
   private final String jpaVersion;
   /** A StringBuilder for caching the information from getCharacters */
   private StringBuilder builder = new StringBuilder();
+  /** The bundle that contains this persistence descriptor */
+  private Bundle bundle;
   
   /**
    * Create a new JPA Handler for the given peristence.xml
    * @param data
    * @param version  the version of the JPA schema used in the xml
    */
-  public JPAHandler(PersistenceLocationData data, String version){
-    xmlLocationData = data;
+  public JPAHandler(Bundle b, String version){
+    bundle = b;
     jpaVersion = version;
   }
   
@@ -73,7 +71,7 @@
     elementName = (localName == null || "".equals(localName))? name : localName;
 
     if("persistence-unit".equals(elementName)) {
-      persistenceUnits.push(new PersistenceUnitImpl(attributes.getValue("name"), attributes.getValue("transaction-type"),
xmlLocationData, jpaVersion));
+      persistenceUnits.push(new PersistenceUnitImpl(bundle, attributes.getValue("name"),
attributes.getValue("transaction-type"), jpaVersion));
     } else if("exclude-unlisted-classes".equals(elementName))
       persistenceUnits.peek().setExcludeUnlisted(true);
     else if("property".equals(elementName))

Added: incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/ParsedPersistenceUnit.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/ParsedPersistenceUnit.java?rev=896591&view=auto
==============================================================================
--- incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/ParsedPersistenceUnit.java
(added)
+++ incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/ParsedPersistenceUnit.java
Wed Jan  6 18:16:02 2010
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.aries.jpa.container.parsing;
+
+import java.util.Map;
+
+import org.osgi.framework.Bundle;
+
+/**
+ * The parsed information from a persistence unit
+ */
+public interface ParsedPersistenceUnit {
+  /*
+   * Keys for use in the PersistenceXml Map
+   * Stored values are Strings unless otherwise specified, and all values
+   * other than the schema version and unit name may be null.
+   */
+  
+  /** The version of the JPA schema being used */
+  public static final String SCHEMA_VERSION = "org.apache.aries.jpa.schema.version";
+  /** The name of the persistence unit */
+  public static final String UNIT_NAME = "org.apache.aries.jpa.unit.name";
+  /** The Transaction type of the persistence unit */
+  public static final String TRANSACTION_TYPE = "org.apache.aries.jpa.transaction.type";
+  /** A List of String mapping file names */
+  public static final String MAPPING_FILES = "org.apache.aries.jpa.mapping.files";
+  /** A List of String jar file names */
+  public static final String JAR_FILES = "org.apache.aries.jpa.jar.files";
+  /** A List of String managed class names */
+  public static final String MANAGED_CLASSES = "org.apache.aries.jpa.managed.classes";
+  /** A Properties object containing the properties from the persistence unit */
+  public static final String PROPERTIES = "org.apache.aries.jpa.properties";
+  /** The provider class name */
+  public static final String PROVIDER_CLASSNAME = "org.apache.aries.jpa.provider";
+  /** The jta-datasource name */
+  public static final String JTA_DATASOURCE = "org.apache.aries.jpa.jta.datasource";
+  /** The non-jta-datasource name */
+  public static final String NON_JTA_DATASOURCE = "org.apache.aries.jpa.non.jta.datasource";
+  /** A Boolean indicating whether unlisted classes should be excluded */
+  public static final String EXCLUDE_UNLISTED_CLASSES = "org.apache.aries.jpa.exclude.unlisted";
+
+  /**
+   * Return the bundle that defines the persistence unit
+   * @return
+   */
+  public Bundle getDefiningBundle();
+
+  /**
+   * Returns a deep copy of the persistence metadata. 
+   * @return
+   */
+  public Map<String, Object> getPersistenceXmlMetadata();
+}

Added: incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/PersistenceDescriptorParser.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/PersistenceDescriptorParser.java?rev=896591&view=auto
==============================================================================
--- incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/PersistenceDescriptorParser.java
(added)
+++ incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/PersistenceDescriptorParser.java
Wed Jan  6 18:16:02 2010
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.aries.jpa.container.parsing;
+
+import java.io.BufferedInputStream;
+import java.io.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+import javax.xml.validation.Schema;
+
+import org.apache.aries.jpa.container.impl.PersistenceDescriptor;
+import org.osgi.framework.Bundle;
+
+public class PersistenceDescriptorParser {
+
+  /**
+   * This class is used to prevent the first pass parse from
+   * closing the InputStream
+   */
+  private static class UnclosableInputStream extends FilterInputStream {
+
+    public UnclosableInputStream(InputStream in) {
+      super(in);
+    }
+
+    @Override
+    public void close() throws IOException {
+      //No op
+    }
+  }
+  
+  /**
+   * @param args
+   * @throws PersistenceDescriptorParserException 
+   */
+  public static Collection<ParsedPersistenceUnit> parse(Bundle b, Collection<PersistenceDescriptor>
descriptors) throws PersistenceDescriptorParserException {
+    Collection<ParsedPersistenceUnit> persistenceUnits = new ArrayList<ParsedPersistenceUnit>();
+    //Parse each xml file in turn
+    for(PersistenceDescriptor descriptor : descriptors) {
+      SAXParserFactory parserFactory = SAXParserFactory.newInstance();
+      BufferedInputStream is = null;
+      try {
+        //Buffer the InputStream so we can mark it
+        is = new BufferedInputStream(descriptor.getInputStream(), 8192);
+        is.mark(8192);
+        SAXParser parser = parserFactory.newSAXParser();
+        try{
+          parser.parse(new UnclosableInputStream(is), new SchemaLocatingHandler());
+        } catch (EarlyParserReturn epr) {
+          //This is not really an exception, but a way to work out which
+          //version of the persistence schema to use in validation
+          Schema s = epr.getSchema();
+          
+          if(s != null) {
+            parserFactory.setSchema(s);
+            parserFactory.setNamespaceAware(true);
+            parser = parserFactory.newSAXParser();
+           
+            //Get back to the beginning of the stream
+            is.reset();
+            
+            JPAHandler handler = new JPAHandler(b, epr.getVersion());
+            parser.parse(is, handler);
+            persistenceUnits.addAll(handler.getPersistenceUnits());
+          } else {
+            //TODO Should we try without validation?
+          }
+            
+        }
+      } catch (Exception e) {
+        //TODO Log this error in parsing
+        System.out.println("Error parsing " + descriptor.getLocation() + " in bundle " +
b.getSymbolicName() + "_" + b.getVersion());
+        e.printStackTrace();
+        throw new PersistenceDescriptorParserException(e);
+      } finally {
+        if(is != null) try {
+          is.close();
+        } catch (IOException e) {
+          //TODO Log this
+          e.printStackTrace();
+        }
+      }
+    }
+    return persistenceUnits;
+  }
+
+}

Added: incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/PersistenceDescriptorParserException.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/PersistenceDescriptorParserException.java?rev=896591&view=auto
==============================================================================
--- incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/PersistenceDescriptorParserException.java
(added)
+++ incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/PersistenceDescriptorParserException.java
Wed Jan  6 18:16:02 2010
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.aries.jpa.container.parsing;
+
+/**
+ * This Exception will be thrown when there was an error parsing a PersistenceDescriptor
+ * It will use the standard chaining mechanism to wrap the Exception thrown by the parser.

+ */
+public class PersistenceDescriptorParserException extends Exception {
+
+  /**
+   * Construct a PersistenceDescriptorException
+   * @param e the exception to wrap
+   */
+  public PersistenceDescriptorParserException(Exception e) {
+    super(e);
+  }
+
+  /**
+   * For Serialization
+   */
+  private static final long serialVersionUID = -8960763303021136544L;
+
+}

Modified: incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/PersistenceUnitImpl.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/PersistenceUnitImpl.java?rev=896591&r1=896590&r2=896591&view=diff
==============================================================================
--- incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/PersistenceUnitImpl.java
(original)
+++ incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/PersistenceUnitImpl.java
Wed Jan  6 18:16:02 2010
@@ -16,12 +16,9 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package com.ibm.osgi.jpa.unit;
+package org.apache.aries.jpa.container.parsing;
 
-import java.io.IOException;
-import java.net.URL;
 import java.util.ArrayList;
-import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -29,27 +26,24 @@
 
 import org.osgi.framework.Bundle;
 import org.osgi.framework.ServiceReference;
-import org.osgi.service.jpa.PersistenceUnitInfoService;
-
-import com.ibm.osgi.jpa.util.PersistenceLocationData;
 
 /**
  * An implementation of PersistenceUnit for parsed persistence unit metadata
  *
  */
 @SuppressWarnings("unchecked")
-public class PersistenceUnitImpl implements PersistenceUnitInfoService
+public class PersistenceUnitImpl implements ParsedPersistenceUnit
 {
   /** A map to hold the metadata from the xml */
   private final Map<String,Object> metadata = new HashMap<String, Object>();
-  /** Information about the location of this persistence unit */
-  private final PersistenceLocationData xmlLocationData;
+  /** The bundle defining this persistence unit */
+  private final Bundle bundle;
 
   /**
    * The Service Reference for the provider to which this persistence
    * unit is tied
    */
-  private ServiceReference provider;
+  ServiceReference provider;
 
   
   /**
@@ -61,10 +55,9 @@
    * @param location
    * @param version    The version of the JPA schema used in persistence.xml
    */
-  public PersistenceUnitImpl(String name, String transactionType, PersistenceLocationData
location, String version)
+  public PersistenceUnitImpl(Bundle b, String name, String transactionType, String version)
   {
-    xmlLocationData = location;
-    
+    this.bundle = b;
     metadata.put(SCHEMA_VERSION, version);
 
     if (name != null)metadata.put(UNIT_NAME, name);
@@ -76,13 +69,7 @@
   @Override
   public Bundle getDefiningBundle()
   {
-    return xmlLocationData.getPersistenceBundle();
-  }
-
-  @Override
-  public ServiceReference getProviderReference()
-  {
-    return provider;
+    return bundle;
   }
 
   @Override
@@ -101,18 +88,6 @@
     return data;
   }
 
-  @Override
-  public URL getPersistenceXmlLocation()
-  {
-    return xmlLocationData.getPersistenceXML();
-  }
-  
-  @Override
-  public URL getPersistenceUnitRoot()
-  {
-    return xmlLocationData.getPersistenceUnitRoot();
-  }
-
   /**
    * @param provider
    */
@@ -206,39 +181,4 @@
   {
     provider = providerRef;
   }
-
-  @Override
-  public ClassLoader getClassLoader()
-  {
-    return new BundleDelegatingClassLoader(getDefiningBundle());
-  }
-  
-  private static class BundleDelegatingClassLoader extends ClassLoader
-  {
-    private final Bundle bundle;
-    
-    public BundleDelegatingClassLoader(Bundle b)
-    {
-      super(ClassLoader.getSystemClassLoader());
-      bundle = b;
-    }
-    
-    @Override
-    protected Class<?> findClass(String name) throws ClassNotFoundException
-    {
-      return bundle.loadClass(name);
-    }
-    
-    @Override
-    protected URL findResource(String resName)
-    {
-      return bundle.getResource(resName);
-    }
-    
-    @Override
-    protected Enumeration<URL> findResources(String resName) throws IOException
-    {
-      return bundle.getResources(resName);
-    }
-  }
 }

Added: incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/SchemaLocatingHandler.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/SchemaLocatingHandler.java?rev=896591&view=auto
==============================================================================
--- incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/SchemaLocatingHandler.java
(added)
+++ incubator/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/parsing/SchemaLocatingHandler.java
Wed Jan  6 18:16:02 2010
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIESOR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.aries.jpa.container.parsing;
+
+import java.net.URL;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+import javax.xml.XMLConstants;
+import javax.xml.validation.Schema;
+import javax.xml.validation.SchemaFactory;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
+/**
+ * This parser provides a quick mechanism for determining the JPA schema level,
+ * and throws an EarlyParserReturn with the Schema to validate with
+ */
+public class SchemaLocatingHandler extends DefaultHandler
+{
+  
+  /**
+   * A static cache of schemas in use in the runtime
+   */
+  private static final ConcurrentMap<String, Schema> schemaCache = new ConcurrentHashMap<String,
Schema>();
+  
+  @Override
+  public void startElement(String uri, String localName, String name, Attributes attributes)
+      throws SAXException
+  {
+    
+    Schema s = null;
+    String version = null;
+    if("persistence".equals(name)) {
+      version = attributes.getValue(uri, "version");
+       s = validate(version);
+    }
+    throw new EarlyParserReturn(s, version);
+  }
+  
+  /**
+   * Find the schema for the version of JPA we're using
+   * @param type  The value of the version attribute in the xml
+   * @return
+   * @throws SAXException
+   */
+  private final Schema validate(String type) throws SAXException
+  {
+    Schema toReturn = (type == null)? null : schemaCache.get(type);
+    
+    if(toReturn == null) {
+      toReturn = getSchema(type);
+      if(toReturn != null) schemaCache.putIfAbsent(type, toReturn);
+    }
+    
+    return toReturn;
+  }
+
+  /**
+   * Locate the schema document
+   * @param type The schema version to find
+   * @return
+   * @throws SAXException
+   */
+  private final Schema getSchema(String type) throws SAXException
+  {
+    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+    URL schemaURL = null;
+    Schema schema = null;
+    if("1.0".equals(type)) {
+      try{
+        schemaURL = new URL("http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd");
+      }catch(Exception e){
+    	//will not occur with fixed url above.
+      }
+    }
+    //TODO handle JPA 2.0
+    if(schemaURL != null){
+      schema = schemaFactory.newSchema(schemaURL);
+    }
+    
+    return schema;
+  }
+  
+}

Modified: incubator/aries/trunk/jpa/pom.xml
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/pom.xml?rev=896591&r1=896590&r2=896591&view=diff
==============================================================================
--- incubator/aries/trunk/jpa/pom.xml (original)
+++ incubator/aries/trunk/jpa/pom.xml Wed Jan  6 18:16:02 2010
@@ -26,7 +26,18 @@
         <artifactId>org.osgi.compendium</artifactId>
         <version>4.2.0</version>
       </dependency>
+      
+      <dependency>
+        <groupId>org.apache.aries.testsupport</groupId>
+        <artifactId>org.apache.aries.testsupport.unit</artifactId>
+        <version>${version}</version>
+      </dependency>
+      
+      <dependency>
+        <groupId>junit</groupId>
+        <artifactId>junit</artifactId>
+        <version>4.5</version>
+      </dependency>
     </dependencies>
   </dependencyManagement>
-  
 </project>
\ No newline at end of file



Mime
View raw message