Author: timothyjward
Date: Mon Jan  4 12:11:45 2010
New Revision: 895610

URL: http://svn.apache.org/viewvc?rev=895610&view=rev
Log:
ARIES-79: Add basic extender code to locate persistence descriptors

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

Modified: incubator/aries/trunk/jpa/jpa-container/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-container/pom.xml?rev=895610&r1=895609&r2=895610&view=diff
==============================================================================
--- incubator/aries/trunk/jpa/jpa-container/pom.xml (original)
+++ incubator/aries/trunk/jpa/jpa-container/pom.xml Mon Jan  4 12:11:45 2010
@@ -10,4 +10,16 @@
   <packaging>bundle</packaging>
   <name>Aries JPA Container</name>
   <version>1.0.0-incubating-SNAPSHOT</version>
+  <dependencies>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.core</artifactId>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.compendium</artifactId>
+      <scope>provided</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=895610&r1=895609&r2=895610&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
 Mon Jan  4 12:11:45 2010
@@ -16,19 +16,21 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package com.ibm.osgi.jpa.util;
+package org.apache.aries.jpa.container.impl;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.net.URL;
-import java.net.URLClassLoader;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.Enumeration;
-import java.util.Iterator;
+import java.util.HashSet;
 import java.util.List;
+import java.util.jar.JarEntry;
+import java.util.jar.JarInputStream;
 
 import org.osgi.framework.Bundle;
-import org.osgi.framework.Constants;
 
 /**
  * This helper can be used to locate persistence.xml files in a bundle
@@ -36,102 +38,106 @@
 public class PersistenceBundleHelper
 {
   /** The persistence xml location */
-  private static final String PERSISTENCE_XML = "META-INF/persistence.xml";
+  public static final String PERSISTENCE_XML = "META-INF/persistence.xml";
+  public static final String PERSISTENCE_UNIT_HEADER = "Meta-Persistence";
 
   /**
-   * This method locates persistence xml files in the following
-   * locations:
-   * META-INF
-   * WEB-INF/classes
-   * the META-INF of any jar in WEB-INF/lib
-   * the META-INF of any jar on the Bundle-ClassPath
+   * This method locates persistence descriptor files based on a combination of
+   * the default location "META-INF/persistence.xml" and the Meta-Persistence
+   * header.
    * 
-   * Note that getEntry and getEntryPaths are used to ensure
-   * we do not transition the bundle to RESOLVED
+   * Note that getEntry is used to ensure we do not alter the state of the 
bundle
    * 
    * @param bundle
    * @return
    */
-  public static Collection<PersistenceLocationData> 
findPersistenceXmlFiles(Bundle bundle)
+  public static Collection<InputStream> findPersistenceXmlFiles(Bundle bundle)
   {
+    //The files we have found
+    Collection<InputStream> persistenceXmlFiles = new HashSet<InputStream>();
     
-    Collection<PersistenceLocationData> persistenceXmlFiles = new 
ArrayList<PersistenceLocationData>();
+    //Always search the default location
+    List<String> locations = new ArrayList<String>();
+    locations.add(PERSISTENCE_XML);
     
-    addLocationToCollectionIfFound(persistenceXmlFiles, "", bundle);
+    String header = (String) bundle.getHeaders().get(PERSISTENCE_UNIT_HEADER);
     
-    addLocationToCollectionIfFound(persistenceXmlFiles, "WEB-INF/classes", 
bundle);
-   
-    @SuppressWarnings("unchecked")
-    Enumeration<String> webInfLibJars = bundle.getEntryPaths("WEB-INF/lib");
-    
-    if(webInfLibJars != null) {
-      
-      List<String> paths = Collections.list(webInfLibJars);
-      Iterator<String> it = paths.iterator();
-      
-      while(it.hasNext()){
-        String s = it.next();
-        // We want to process jars in WEB-INF/lib/ so it should end
-        // .jar, and not contain any / separators after character 11
-        if(s.endsWith(".jar") && s.lastIndexOf('/') < 12) {
-          processNestedJar(bundle, persistenceXmlFiles, s);
-        }
-      }
+    if(header != null) {
+      //Split apart the header to get the individual entries
+      List<String> headerLocations = Arrays.asList(header.split(","));
+      locations.addAll(headerLocations);
     }
     
-    String bundleClassPath = (String) 
bundle.getHeaders().get(Constants.BUNDLE_CLASSPATH);
-
-    if(bundleClassPath != null) {
-      String[] cpEntries = bundleClassPath.split(",");
-      
-      for (String s : cpEntries) {
-        s = s.trim();
-        if(s.endsWith(".jar")) {
-          processNestedJar(bundle, persistenceXmlFiles, s);
+    try {
+      for(String location : locations) {
+        InputStream file = locateFile(bundle, location.trim());
+          
+        if(file != null)
+          persistenceXmlFiles.add(file);
+        }
+    } catch (Exception e) {
+        //TODO log
+      for (InputStream is : persistenceXmlFiles) {
+        try {
+          is.close();
+        } catch (IOException ioe) {
+          // TODO: log ioe
         }
       }
+      persistenceXmlFiles = Collections.emptySet();
     }
-      return persistenceXmlFiles;
-  }
+   return persistenceXmlFiles;
+ }
 
   /**
-   * Check to see if a nested jar contains a "META-INF/persistence.xml" file
-   * and add it to the list if it does
+   * Locate a persistence descriptor file in a bundle
+   * based on a String name.
    * 
    * @param bundle
    * @param persistenceXmlFiles
    * @param jarLocation
    */
-  private static void processNestedJar(Bundle bundle,
-      Collection<PersistenceLocationData> persistenceXmlFiles, String 
jarLocation)
+  private static InputStream locateFile(Bundle bundle, String location)
   {
-    URL jar = bundle.getEntry(jarLocation);
-    if(jar != null) {
-    ClassLoader cl = new URLClassLoader(new URL[] {jar});
-    URL xml = cl.getResource(PERSISTENCE_XML);
-
-    if(xml != null)
-      persistenceXmlFiles.add(new PersistenceLocationData(xml, jar, bundle));
+    InputStream is = null;
+    if(location != "") {
+      return null;
     }
-  }
-  
-  /**
-   * This method will attempt to find an entry for a given path in a given 
bundle
-   * and add it to the collection if the entry exists
-   * @param collection
-   * @param rootPath
-   * @param bundle
-   */
-  private static void 
addLocationToCollectionIfFound(Collection<PersistenceLocationData> collection, 
String rootPath, Bundle bundle)
-  {
-    rootPath = (rootPath.endsWith("/")) ? rootPath : rootPath + "/";
-    URL root = bundle.getEntry(rootPath);
-    if(root != null) {
-      String xmlPath = rootPath + PERSISTENCE_XML;
-      URL xml = bundle.getEntry(xmlPath);
-      if(xml != null)
-        collection.add(new PersistenceLocationData(xml, root, bundle));
+      
+    int bangIndex = location.indexOf('!');
+    
+    if(bangIndex == -1) {
+      URL url = bundle.getEntry(location);
+      
+      if(url != null) {
+        try {
+          is = url.openStream();
+        } catch (IOException e) {
+          // TODO log this
+        }
+      }
+    } else {
+      URL url = bundle.getEntry(location.substring(0, bangIndex));
+      
+      if(url != null) {
+        String toLocate = location.substring(bangIndex + 1);
+        
+        try {
+          JarInputStream jis = new JarInputStream(url.openStream());
+          JarEntry entry = jis.getNextJarEntry();
+          
+          while(entry != null) {
+            if(entry.getName().equals(toLocate)) {
+              is = jis;
+              break;
+            }
+            entry = jis.getNextJarEntry();
+          }
+        } catch (IOException ioe) {
+          //TODO log this
+        }
+      }
     }
+    return is;
   }
-  
 }

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=895610&r1=895609&r2=895610&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
 Mon Jan  4 12:11:45 2010
@@ -17,60 +17,40 @@
  * under the License.
  */
 
-package com.ibm.osgi.jpa.unit;
+package org.apache.aries.jpa.container.impl;
 
-import java.io.IOException;
 import java.io.InputStream;
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
-import java.util.HashSet;
-import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
-import java.util.Map;
-import java.util.Properties;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
-import javax.xml.parsers.SAXParser;
-import javax.xml.parsers.SAXParserFactory;
-import javax.xml.validation.Schema;
-
 import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
-import org.osgi.framework.BundleException;
-import org.osgi.framework.Constants;
 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.SynchronousBundleListener;
 import org.osgi.framework.Version;
-import org.osgi.service.jpa.PersistenceUnitInfoService;
-
-import com.ibm.osgi.jpa.unit.parsing.EarlyParserReturn;
-import com.ibm.osgi.jpa.unit.parsing.JPAHandler;
-import com.ibm.osgi.jpa.unit.parsing.SchemaLocatingHandler;
-import com.ibm.osgi.jpa.util.PersistenceBundleHelper;
-import com.ibm.osgi.jpa.util.PersistenceLocationData;
+import org.osgi.util.tracker.BundleTracker;
 
-import com.ibm.osgi.util.FragmentBuilder;
 
 /**
- * This class manages the fragments that need to be added to persistence 
bundles, and
- * also registers the metadata for persistence units in the service registry.
+ * This class locates, parses and manages persistence units defined in OSGi 
bundles.
  */
-public class PersistenceBundleManager implements SynchronousBundleListener, 
BundleActivator
+public class PersistenceBundleManager extends BundleTracker
 {
   /** 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 */
@@ -79,198 +59,168 @@
   /** The default JPA provider to use */
   public static final String DEFAULT_JPA_PROVIDER 
="org.apache.openjpa.persistence.PersistenceProviderImpl";
 
-  public void start(BundleContext ctx)
+  public PersistenceBundleManager(BundleContext ctx) 
   {
+       super(ctx, Bundle.INSTALLED | Bundle.RESOLVED | Bundle.STARTING |
+                         Bundle.ACTIVE | Bundle.STOPPING, null);
     this.ctx = ctx;
-    ctx.addBundleListener(this);
   }
   
-  public void stop(BundleContext arg0) throws Exception {
-    
-    ctx.removeBundleListener(this);
-    //When we stop, tidy up all the fragments and services, the
-    //fragments only get detached when the host is unresolved,
-    //so JPA can continue gracefully in active bundles
-    //if the container wishes
-    for(Bundle b : hostToFragmentMap.keySet())
-      tidyUpPersistenceBundle(b);
-  }
+//  /**
+//   * If we have generated a resources for the supplied bundle, then
+//   * tidy them  up.
+//   * @param host
+//   */
+//  private void tidyUpPersistenceBundle(Bundle host)
+//  {
+//    
+//    Bundle fragment = hostToFragmentMap.remove(host);
+//    Set<ServiceRegistration> services = 
hostToPersistenceUnitMap.remove(host);
+//    
+//    if(services != null) {
+//      for(ServiceRegistration reg : services)
+//        reg.unregister();
+//    }
+//    
+//    if(fragment != null){
+//      try {
+//        fragment.uninstall();
+//      } catch (BundleException be) {
+//        //TODO log this error, then hope that we don't try to
+//        //recreate the fragment before restarting the framework!
+//      }
+//    }
+//  }
   
-  public void bundleChanged(BundleEvent event)
+  
+  public Object addingBundle(Bundle bundle, BundleEvent event) 
   {
-    Bundle b = event.getBundle();
     
-    //We don't look at fragments
-    if (b.getHeaders().get(Constants.FRAGMENT_HOST) == null)
-    {
-      // We need to remove fragments that are no longer useful
-      // and register fragments for those that need them.
-      // No action is needed on UNRESOLVED (a package refresh)
-      // as the bundle hasn't changed
-      // We shouldn't process multiple events on different
-      // threads for the same bundle at the same time, but the
-      // framework should protect us from this. Testing may also show
-      // that we need to prevent this call being reentrant.
-      // We absolutely cannot lock when calling these methods as
-      // it can cause deadlocks if listeners hold locks whilst
-      // interacting with the framework in a manner that fires
-      // events.
-      switch(event.getType()) {
-        case BundleEvent.UNINSTALLED : {
-          tidyUpPersistenceBundle(b);
-          break;
-        }
-        //This case should fall through to add a new
-        //fragment after update/refresh
-        case BundleEvent.UNRESOLVED :
-        case BundleEvent.UPDATED :
-          tidyUpPersistenceBundle(b);
-        case BundleEvent.INSTALLED :
-          processBundle(b);
-      }
+    if(bundle.getState() == Bundle.ACTIVE) {
+      //TODO LOG WARNING HERE
     }
-  }
 
-  /**
-   * If we have generated a resources for the supplied bundle, then
-   * tidy them  up.
-   * @param host
-   */
-  private void tidyUpPersistenceBundle(Bundle host)
-  {
-    
-    Bundle fragment = hostToFragmentMap.remove(host);
-    Set<ServiceRegistration> services = hostToPersistenceUnitMap.remove(host);
-    
-    if(services != null) {
-      for(ServiceRegistration reg : services)
-        reg.unregister();
-    }
-    
-    if(fragment != null){
-      try {
-        fragment.uninstall();
-      } catch (BundleException be) {
-        //TODO log this error, then hope that we don't try to
-        //recreate the fragment before restarting the framework!
-      }
+    Collection <InputStream> persistenceXmls = 
PersistenceBundleHelper.findPersistenceXmlFiles(bundle);
+
+    //If we have no persistence units then our job is done
+    if (!!!persistenceXmls.isEmpty()) {
+      //TODO parse these
     }
+    return null;
   }
-  
-  /**
-   * Process the supplied bundle for persistence units. If any are
-   * found then the bundle will be tied to a provider using a fragment
-   * @param b
-   */
-  private void processBundle(Bundle b)
-  {
-    Bundle fragment = null;
-    Collection <PersistenceLocationData> persistenceXmls = 
PersistenceBundleHelper.findPersistenceXmlFiles(b);
-    //If we have no persistence units then our job is done
-    if (!!!persistenceXmls.isEmpty())
-    {
-      //Get the persistence units defined, and a provider for them to use
-      Collection<PersistenceUnitImpl> parsedPersistenceUnits = 
parseXmlFiles(persistenceXmls, b);
-      ServiceReference providerRef = 
getProviderServiceReference(parsedPersistenceUnits);
+//      //Get the persistence units defined, and a provider for them to use
+//      Collection<PersistenceUnitImpl> parsedPersistenceUnits = 
parseXmlFiles(persistenceXmls, b);
+//      ServiceReference providerRef = 
getProviderServiceReference(parsedPersistenceUnits);
+//      
+//      //If we can't find a provider then bomb out
+//      if (providerRef != null)
+//      {
+//        try {
+//          FragmentBuilder builder = new FragmentBuilder(b, ".jpa.fragment");
+//          builder.addImportsFromExports(providerRef.getBundle());
+//          fragment = builder.install(ctx);
+//        
+//          
+//          hostToFragmentMap.put(b, fragment);
+//          // If we successfully got a fragment then
+//          // set the provider reference and register the units
+//          Set<ServiceRegistration> registrations = new 
HashSet<ServiceRegistration>();
+//          Hashtable<String, Object> props = new Hashtable<String, Object>();
+//          
+//          
props.put(PersistenceUnitInfoService.PERSISTENCE_BUNDLE_SYMBOLIC_NAME, 
b.getSymbolicName());
+//          props.put(PersistenceUnitInfoService.PERSISTENCE_BUNDLE_VERSION, 
b.getVersion());
+//          
+//          for(PersistenceUnitImpl unit : parsedPersistenceUnits){
+//            Hashtable<String, Object> serviceProps = new Hashtable<String, 
Object>(props);
+//            
+//            String unitName = (String) 
unit.getPersistenceXmlMetadata().get(PersistenceUnitInfoService.UNIT_NAME);
+//            if(unitName != null)
+//              
serviceProps.put(PersistenceUnitInfoService.PERSISTENCE_UNIT_NAME, unitName);
+//            
+//            unit.setProviderReference(providerRef);
+//            
registrations.add(ctx.registerService(PersistenceUnitInfoService.class.getName(),
 unit, serviceProps));
+//          }
+//          hostToPersistenceUnitMap.put(b, registrations);
+//        }
+//        catch (IOException e)
+//        {
+//          // TODO Fragment generation failed, log the error
+//          // No clean up because we didn't register the bundle yet
+//          e.printStackTrace();
+//        }
+//        catch (BundleException be) {
+//          //TODO log the failure to install the fragment, but return null
+//          // to show we didn't get a fragment installed
+//          // No clean up because we didn't register the bundle yet
+//        }
+//      }
+//    }
+//  }
+
+
+  public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
+      // TODO Auto-generated method stub
       
-      //If we can't find a provider then bomb out
-      if (providerRef != null)
-      {
-        try {
-          FragmentBuilder builder = new FragmentBuilder(b, ".jpa.fragment");
-          builder.addImportsFromExports(providerRef.getBundle());
-          fragment = builder.install(ctx);
-        
-          
-          hostToFragmentMap.put(b, fragment);
-          // If we successfully got a fragment then
-          // set the provider reference and register the units
-          Set<ServiceRegistration> registrations = new 
HashSet<ServiceRegistration>();
-          Hashtable<String, Object> props = new Hashtable<String, Object>();
-          
-          
props.put(PersistenceUnitInfoService.PERSISTENCE_BUNDLE_SYMBOLIC_NAME, 
b.getSymbolicName());
-          props.put(PersistenceUnitInfoService.PERSISTENCE_BUNDLE_VERSION, 
b.getVersion());
-          
-          for(PersistenceUnitImpl unit : parsedPersistenceUnits){
-            Hashtable<String, Object> serviceProps = new Hashtable<String, 
Object>(props);
-            
-            String unitName = (String) 
unit.getPersistenceXmlMetadata().get(PersistenceUnitInfoService.UNIT_NAME);
-            if(unitName != null)
-              
serviceProps.put(PersistenceUnitInfoService.PERSISTENCE_UNIT_NAME, unitName);
-            
-            unit.setProviderReference(providerRef);
-            
registrations.add(ctx.registerService(PersistenceUnitInfoService.class.getName(),
 unit, serviceProps));
-          }
-          hostToPersistenceUnitMap.put(b, registrations);
-        }
-        catch (IOException e)
-        {
-          // TODO Fragment generation failed, log the error
-          // No clean up because we didn't register the bundle yet
-          e.printStackTrace();
-        }
-        catch (BundleException be) {
-          //TODO log the failure to install the fragment, but return null
-          // to show we didn't get a fragment installed
-          // No clean up because we didn't register the bundle yet
-        }
-      }
-    }
   }
 
+  public void removedBundle(Bundle bundle, BundleEvent event, Object object) {
+      // TODO Auto-generated method stub
+      
+  }
+  
   /**
    * 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;
-  }
+//  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
@@ -278,47 +228,47 @@
    * @param parsedPersistenceUnits
    * @return A service reference or null if no suitable reference is available
    */
-  private ServiceReference 
getProviderServiceReference(Collection<PersistenceUnitImpl> 
parsedPersistenceUnits)
-  {
-    Set<String> ppClassNames = new HashSet<String>();
-    Set<Filter> versionFilters = new HashSet<Filter>();
-    //Fill the set of class names and version Filters
-    for(PersistenceUnitImpl unit : parsedPersistenceUnits)
-    {
-      Map<String, Object> metadata = unit.getPersistenceXmlMetadata();
-      String provider = (String) 
metadata.get(PersistenceUnitInfoService.PROVIDER_CLASSNAME);
-      //get providers specified in the persistence units
-      if(provider != null && !!!provider.equals(""))
-      {
-        ppClassNames.add(provider);
-        
-        Properties props = (Properties) 
metadata.get(PersistenceUnitInfoService.PROPERTIES);
-        
-        if(props != null && 
props.containsKey(PersistenceUnitInfoService.JPA_PROVIDER_VERSION)) {
-         
-          try {
-            Filter f = 
getFilter(props.getProperty(PersistenceUnitInfoService.JPA_PROVIDER_VERSION, 
"0.0.0"));
-            versionFilters.add(f);
-          } catch (InvalidSyntaxException e) {
-            // TODO Log error and ignore, This should never happen
-            e.printStackTrace();
-          }
-        }
-      }
-    }
-    
-    //If we have too many provider class names specified then blow up
-    if(ppClassNames.size() > 1)
-    {
-      //TODO log this error (too many persistence providers specified)
-    } else {
-      //Get the best provider for the given filters
-      String provider = (ppClassNames.isEmpty()) ?
-          DEFAULT_JPA_PROVIDER : ppClassNames.iterator().next();
-          return getBestProvider(provider, versionFilters);
-    }
-    return null;
-  }
+//  private ServiceReference 
getProviderServiceReference(Collection<PersistenceUnitImpl> 
parsedPersistenceUnits)
+//  {
+//    Set<String> ppClassNames = new HashSet<String>();
+//    Set<Filter> versionFilters = new HashSet<Filter>();
+//    //Fill the set of class names and version Filters
+//    for(PersistenceUnitImpl unit : parsedPersistenceUnits)
+//    {
+//      Map<String, Object> metadata = unit.getPersistenceXmlMetadata();
+//      String provider = (String) 
metadata.get(PersistenceUnitInfoService.PROVIDER_CLASSNAME);
+//      //get providers specified in the persistence units
+//      if(provider != null && !!!provider.equals(""))
+//      {
+//        ppClassNames.add(provider);
+//        
+//        Properties props = (Properties) 
metadata.get(PersistenceUnitInfoService.PROPERTIES);
+//        
+//        if(props != null && 
props.containsKey(PersistenceUnitInfoService.JPA_PROVIDER_VERSION)) {
+//         
+//          try {
+//            Filter f = 
getFilter(props.getProperty(PersistenceUnitInfoService.JPA_PROVIDER_VERSION, 
"0.0.0"));
+//            versionFilters.add(f);
+//          } catch (InvalidSyntaxException e) {
+//            // TODO Log error and ignore, This should never happen
+//            e.printStackTrace();
+//          }
+//        }
+//      }
+//    }
+//    
+//    //If we have too many provider class names specified then blow up
+//    if(ppClassNames.size() > 1)
+//    {
+//      //TODO log this error (too many persistence providers specified)
+//    } else {
+//      //Get the best provider for the given filters
+//      String provider = (ppClassNames.isEmpty()) ?
+//          DEFAULT_JPA_PROVIDER : ppClassNames.iterator().next();
+//          return getBestProvider(provider, versionFilters);
+//    }
+//    return null;
+//  }
  
   /**
    * Locate the best provider for the given criteria
@@ -364,7 +314,6 @@
         Collections.sort(refs, new Comparator<ServiceReference>() {
 
           //TODO we may wish to use Ranking, then versions for equal ranks
-          @Override
           public int compare(ServiceReference object1, ServiceReference 
object2)
           {
             Version v1 = object1.getBundle().getVersion();


Reply via email to