geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From djen...@apache.org
Subject svn commit: r945810 - in /geronimo/server/trunk/plugins/openjpa2/geronimo-persistence-jpa20-builder/src/main/java/org/apache/geronimo/persistence/builder: PersistenceContextRefBuilder.java PersistenceUnitBuilder.java PersistenceUnitRefBuilder.java
Date Tue, 18 May 2010 18:42:27 GMT
Author: djencks
Date: Tue May 18 18:42:26 2010
New Revision: 945810

URL: http://svn.apache.org/viewvc?rev=945810&view=rev
Log:
GERONIMO-4680 Use bundle resource finder to find persistence.xmls.  A bit of cleanup

Modified:
    geronimo/server/trunk/plugins/openjpa2/geronimo-persistence-jpa20-builder/src/main/java/org/apache/geronimo/persistence/builder/PersistenceContextRefBuilder.java
    geronimo/server/trunk/plugins/openjpa2/geronimo-persistence-jpa20-builder/src/main/java/org/apache/geronimo/persistence/builder/PersistenceUnitBuilder.java
    geronimo/server/trunk/plugins/openjpa2/geronimo-persistence-jpa20-builder/src/main/java/org/apache/geronimo/persistence/builder/PersistenceUnitRefBuilder.java

Modified: geronimo/server/trunk/plugins/openjpa2/geronimo-persistence-jpa20-builder/src/main/java/org/apache/geronimo/persistence/builder/PersistenceContextRefBuilder.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/openjpa2/geronimo-persistence-jpa20-builder/src/main/java/org/apache/geronimo/persistence/builder/PersistenceContextRefBuilder.java?rev=945810&r1=945809&r2=945810&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/openjpa2/geronimo-persistence-jpa20-builder/src/main/java/org/apache/geronimo/persistence/builder/PersistenceContextRefBuilder.java
(original)
+++ geronimo/server/trunk/plugins/openjpa2/geronimo-persistence-jpa20-builder/src/main/java/org/apache/geronimo/persistence/builder/PersistenceContextRefBuilder.java
Tue May 18 18:42:26 2010
@@ -33,6 +33,8 @@ import org.apache.geronimo.gbean.Abstrac
 import org.apache.geronimo.gbean.GBeanData;
 import org.apache.geronimo.gbean.GBeanInfo;
 import org.apache.geronimo.gbean.GBeanInfoBuilder;
+import org.apache.geronimo.gbean.annotation.GBean;
+import org.apache.geronimo.gbean.annotation.ParamAttribute;
 import org.apache.geronimo.j2ee.deployment.Module;
 import org.apache.geronimo.j2ee.deployment.NamingBuilder;
 import org.apache.geronimo.j2ee.deployment.annotation.PersistenceContextAnnotationHelper;
@@ -58,6 +60,7 @@ import org.apache.xmlbeans.XmlObject;
 /**
  * @version $Rev$ $Date$
  */
+@GBean(j2eeType = NameFactory.MODULE_BUILDER)
 public class PersistenceContextRefBuilder extends AbstractNamingBuilder {
     private static final QName PERSISTENCE_CONTEXT_REF_QNAME = new QName(JEE_NAMESPACE, "persistence-context-ref");
     private static final QNameSet PERSISTENCE_CONTEXT_REF_QNAME_SET = QNameSet.singleton(PERSISTENCE_CONTEXT_REF_QNAME);
@@ -67,7 +70,9 @@ public class PersistenceContextRefBuilde
     private final AbstractNameQuery defaultPersistenceUnitAbstractNameQuery;
     private final boolean strictMatching;
 
-    public PersistenceContextRefBuilder(Environment defaultEnvironment, AbstractNameQuery
defaultPersistenceUnitAbstractNameQuery, boolean strictMatching) {
+    public PersistenceContextRefBuilder(@ParamAttribute(name = "defaultEnvironment") Environment
defaultEnvironment,
+                                     @ParamAttribute(name = "defaultPersistenceUnitAbstractNameQuery")
AbstractNameQuery defaultPersistenceUnitAbstractNameQuery,
+                                     @ParamAttribute(name = "strictMatching") boolean strictMatching)
{
         super(defaultEnvironment);
         this.defaultPersistenceUnitAbstractNameQuery = defaultPersistenceUnitAbstractNameQuery;
         this.strictMatching = strictMatching;
@@ -255,19 +260,4 @@ public class PersistenceContextRefBuilde
         return GER_PERSISTENCE_CONTEXT_REF_QNAME_SET;
     }
 
-    public static final GBeanInfo GBEAN_INFO;
-
-    static {
-        GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(PersistenceContextRefBuilder.class,
NameFactory.MODULE_BUILDER);
-        infoBuilder.addAttribute("defaultEnvironment", Environment.class, true, true);
-        infoBuilder.addAttribute("defaultPersistenceUnitAbstractNameQuery", AbstractNameQuery.class,
true, true);
-        infoBuilder.addAttribute("strictMatching", boolean.class, true, true);
-
-        infoBuilder.setConstructor(new String[]{"defaultEnvironment", "defaultPersistenceUnitAbstractNameQuery",
"strictMatching"});
-        GBEAN_INFO = infoBuilder.getBeanInfo();
-    }
-
-    public static GBeanInfo getGBeanInfo() {
-        return GBEAN_INFO;
-    }
 }

Modified: geronimo/server/trunk/plugins/openjpa2/geronimo-persistence-jpa20-builder/src/main/java/org/apache/geronimo/persistence/builder/PersistenceUnitBuilder.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/openjpa2/geronimo-persistence-jpa20-builder/src/main/java/org/apache/geronimo/persistence/builder/PersistenceUnitBuilder.java?rev=945810&r1=945809&r2=945810&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/openjpa2/geronimo-persistence-jpa20-builder/src/main/java/org/apache/geronimo/persistence/builder/PersistenceUnitBuilder.java
(original)
+++ geronimo/server/trunk/plugins/openjpa2/geronimo-persistence-jpa20-builder/src/main/java/org/apache/geronimo/persistence/builder/PersistenceUnitBuilder.java
Tue May 18 18:42:26 2010
@@ -16,8 +16,7 @@
  */
 package org.apache.geronimo.persistence.builder;
 
-import java.io.File;
-import java.io.IOException;
+import java.io.InputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
@@ -28,6 +27,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.jar.JarFile;
+import java.util.zip.ZipEntry;
 
 import javax.xml.namespace.QName;
 import org.apache.geronimo.common.DeploymentException;
@@ -39,8 +39,10 @@ import org.apache.geronimo.deployment.xm
 import org.apache.geronimo.gbean.AbstractName;
 import org.apache.geronimo.gbean.AbstractNameQuery;
 import org.apache.geronimo.gbean.GBeanData;
-import org.apache.geronimo.gbean.GBeanInfo;
-import org.apache.geronimo.gbean.GBeanInfoBuilder;
+import org.apache.geronimo.gbean.annotation.GBean;
+import org.apache.geronimo.gbean.annotation.ParamAttribute;
+import org.apache.geronimo.gbean.annotation.ParamSpecial;
+import org.apache.geronimo.gbean.annotation.SpecialAttributeType;
 import org.apache.geronimo.j2ee.deployment.EARContext;
 import org.apache.geronimo.j2ee.deployment.Module;
 import org.apache.geronimo.j2ee.deployment.ModuleBuilderExtension;
@@ -48,22 +50,26 @@ import org.apache.geronimo.j2ee.j2eeobje
 import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
 import org.apache.geronimo.kernel.Naming;
 import org.apache.geronimo.kernel.config.ConfigurationStore;
-import org.apache.xbean.osgi.bundle.util.BundleClassLoader;
 import org.apache.geronimo.kernel.repository.Environment;
 import org.apache.geronimo.naming.ResourceSource;
 import org.apache.geronimo.persistence.PersistenceUnitGBean;
 import org.apache.geronimo.schema.SchemaConversionUtils;
 import org.apache.geronimo.xbeans.persistence20.PersistenceDocument;
-import org.apache.xbean.finder.ResourceFinder;
+import org.apache.xbean.osgi.bundle.util.BundleClassLoader;
+import org.apache.xbean.osgi.bundle.util.BundleResourceFinder;
 import org.apache.xmlbeans.QNameSet;
 import org.apache.xmlbeans.XmlCursor;
 import org.apache.xmlbeans.XmlException;
 import org.apache.xmlbeans.XmlObject;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.packageadmin.PackageAdmin;
 
 /**
  * @version $Rev$ $Date$
  */
+@GBean(j2eeType = NameFactory.MODULE_BUILDER)
 public class PersistenceUnitBuilder implements ModuleBuilderExtension {
 
     private static final EARContext.Key<List<URL>> PERSISTENCE_URL_LIST_KEY =
new EARContext.Key<List<URL>>() {
@@ -89,18 +95,23 @@ public class PersistenceUnitBuilder impl
     private final AbstractNameQuery extendedEntityManagerRegistryName;
     private static final String ANON_PU_NAME = "AnonymousPersistenceUnit";
     private static final String RESOURCE_SOURCE_CLASS_NAME = ResourceSource.class.getName();
+    private final PackageAdmin packageAdmin;
 
-    public PersistenceUnitBuilder(Environment defaultEnvironment,
-                                  String defaultPersistenceProviderClassName,
-                                  String defaultJtaDataSourceName,
-                                  String defaultNonJtaDataSourceName,
-                                  AbstractNameQuery extendedEntityManagerRegistryName, Properties
defaultPersistenceUnitProperties) throws URISyntaxException {
+    public PersistenceUnitBuilder(@ParamAttribute(name = "defaultEnvironment") Environment
defaultEnvironment,
+                                  @ParamAttribute(name = "defaultPersistenceProviderClassName")
String defaultPersistenceProviderClassName,
+                                  @ParamAttribute(name = "defaultJtaDataSourceName") String
defaultJtaDataSourceName,
+                                  @ParamAttribute(name = "defaultNonJtaDataSourceName") String
defaultNonJtaDataSourceName,
+                                  @ParamAttribute(name = "extendedEntityManagerRegistryName")
AbstractNameQuery extendedEntityManagerRegistryName,
+                                  @ParamAttribute(name = "defaultPersistenceUnitProperties")
Properties defaultPersistenceUnitProperties,
+                                  @ParamSpecial(type = SpecialAttributeType.bundleContext)
BundleContext bundleContext) throws URISyntaxException {
         this.defaultEnvironment = defaultEnvironment;
         this.defaultPersistenceProviderClassName = defaultPersistenceProviderClassName;
         this.defaultJtaDataSourceName = defaultJtaDataSourceName == null ? null : getAbstractNameQuery(defaultJtaDataSourceName);
         this.defaultNonJtaDataSourceName = defaultNonJtaDataSourceName == null ? null : getAbstractNameQuery(defaultNonJtaDataSourceName);
         this.extendedEntityManagerRegistryName = extendedEntityManagerRegistryName;
         this.defaultPersistenceUnitProperties = defaultPersistenceUnitProperties == null
? new Properties() : defaultPersistenceUnitProperties;
+        ServiceReference sr = bundleContext.getServiceReference(PackageAdmin.class.getName());
+        packageAdmin = (PackageAdmin) bundleContext.getService(sr);
     }
 
     public void createModule(Module module, Bundle bundle, Naming naming, ModuleIDBuilder
idBuilder) throws DeploymentException {
@@ -133,68 +144,56 @@ public class PersistenceUnitBuilder impl
             throw new DeploymentException("Parse Persistence configuration file failed",
e);
         }
         try {
-            File rootBaseFile = module.getRootEarContext().getConfiguration().getConfigurationDir();
-            String rootBase = rootBaseFile.toURI().normalize().toString();
             URI moduleBaseURI = moduleContext.getBaseDir().toURI();
-            Map rootGeneralData = module.getRootEarContext().getGeneralData();
             ClassPathList manifestcp = EARContext.CLASS_PATH_LIST_KEY.get(module.getEarContext().getGeneralData());
             if (manifestcp == null) {
                 manifestcp = new ClassPathList();
                 manifestcp.add(module.getTargetPath());
             }
-            URL[] urls = new URL[manifestcp.size()];
-            int i = 0;
-            for (String path : manifestcp) {
-                path = path.replaceAll(" ", "%20");
-                URL url = moduleBaseURI.resolve(path).toURL();
-                urls[i++] = url;
-            }
-            ResourceFinder finder = new ResourceFinder("", null, urls);
-            List<URL> knownPersistenceUrls = PERSISTENCE_URL_LIST_KEY.get(rootGeneralData);
-            List<URL> persistenceUrls = finder.findAll("META-INF/persistence.xml");
-            persistenceUrls.removeAll(knownPersistenceUrls);
-            if (raws.length > 0 || persistenceUrls.size() > 0) {
+//            URL[] urls = new URL[manifestcp.size()];
+//            int i = 0;
+//            for (String path : manifestcp) {
+//                //TODO either this encoding is unnecessary or incomplete
+//                path = path.replaceAll(" ", "%20");
+//                URL url = moduleBaseURI.resolve(path).toURL();
+//                urls[i++] = url;
+//            }
+            BundleResourceFinder finder = new BundleResourceFinder(packageAdmin, bundle,
"", "META-INF/persistence.xml");
+            List<URL> knownPersistenceUrls = PERSISTENCE_URL_LIST_KEY.get(module.getRootEarContext().getGeneralData());
+            final Map<URL, String> persistenceURLs = new HashMap<URL, String>();
+            finder.find(new BundleResourceFinder.ResourceFinderCallback() {
+
+                @Override
+                public void foundInDirectory(Bundle bundle, String baseDir, URL url) throws
Exception {
+                    persistenceURLs.put(url, baseDir);
+                }
+
+                @Override
+                public void foundInJar(Bundle bundle, String jarName, ZipEntry entry, InputStream
inputStream) throws Exception {
+                    URL jarURL = bundle.getEntry(jarName);
+                    URL url = new URL("jar:" + jarURL.toString() + "!/" + entry.getName());
+                    persistenceURLs.put(url, jarName);
+                }
+            });
+            persistenceURLs.keySet().removeAll(knownPersistenceUrls);
+            if (raws.length > 0 || persistenceURLs.size() > 0) {
                 EnvironmentBuilder.mergeEnvironments(module.getEnvironment(), defaultEnvironment);
             }
-            for (URL persistenceUrl : persistenceUrls) {
-                String persistenceLocation;
+            for (Map.Entry<URL, String> entry : persistenceURLs.entrySet()) {
+                URL persistenceUrl = entry.getKey();
+                String persistenceLocation = entry.getValue();
+                PersistenceDocument persistenceDocument;
                 try {
-                    persistenceLocation = persistenceUrl.toURI().toString();
-                } catch (URISyntaxException e) {
-                    //????
-                    continue;
-                }
-                int pos = persistenceLocation.indexOf(rootBase);
-                if (pos < 0) {
-                    //not in the ear
-                    continue;
-                }
-                int endPos = persistenceLocation.lastIndexOf("!/");
-                if (endPos < 0) {
-                    // if unable to find the '!/' marker, try to see if this is
-                    // a war file with the persistence.xml directly embeded - no ejb-jar
-                    endPos = persistenceLocation.lastIndexOf("META-INF");
-                }
-                if (endPos >= 0) {
-                    //path relative to ear base uri
-                    String relative = persistenceLocation.substring(pos + rootBase.length(),
endPos);
-                    //find path relative to module base uri
-                    relative = module.getRelativePath(relative);
-                    PersistenceDocument persistenceDocument;
-                    try {
-                        XmlObject xmlObject = XmlBeansUtil.parse(persistenceUrl, new BundleClassLoader(moduleContext.getDeploymentBundle()));
-                        persistenceDocument = convertToPersistenceDocument(xmlObject);
-                    } catch (XmlException e) {
-                        throw new DeploymentException("Could not parse persistence.xml file:
" + persistenceUrl, e);
-                    }
-                    PersistenceDocument.Persistence persistence = persistenceDocument.getPersistence();
-                    buildPersistenceUnits(persistence, overrides, module, relative);
-                    knownPersistenceUrls.add(persistenceUrl);
-                } else {
-                    throw new DeploymentException("Could not find persistence.xml file: "
+ persistenceUrl);
+                    XmlObject xmlObject = XmlBeansUtil.parse(persistenceUrl, new BundleClassLoader(moduleContext.getDeploymentBundle()));
+                    persistenceDocument = convertToPersistenceDocument(xmlObject);
+                } catch (XmlException e) {
+                    throw new DeploymentException("Could not parse persistence.xml file:
" + persistenceUrl, e);
                 }
+                PersistenceDocument.Persistence persistence = persistenceDocument.getPersistence();
+                buildPersistenceUnits(persistence, overrides, module, persistenceLocation);
+                knownPersistenceUrls.add(persistenceUrl);
             }
-        } catch (IOException e) {
+        } catch (Exception e) {
             throw new DeploymentException("Could not look for META-INF/persistence.xml files",
e);
         }
 
@@ -222,6 +221,7 @@ public class PersistenceUnitBuilder impl
                 try {
                     cursor.dispose();
                 } catch (Exception e) {
+                    //ignore
                 }
             }
         }
@@ -234,9 +234,9 @@ public class PersistenceUnitBuilder impl
 //            cursor.toStartDoc();
 //            cursor.toFirstChild();
 //            SchemaConversionUtils.convertSchemaVersion(cursor, SchemaConversionUtils.JPA_PERSISTENCE_NAMESPACE,
"http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd", "2.0");
-            XmlObject result = xmlObject.changeType(PersistenceDocument.Persistence.type);
-            XmlBeansUtil.validateDD(result);
-            return (PersistenceDocument.Persistence) result;
+        XmlObject result = xmlObject.changeType(PersistenceDocument.Persistence.type);
+        XmlBeansUtil.validateDD(result);
+        return (PersistenceDocument.Persistence) result;
 //        } finally {
 //            if (cursor != null) {
 //                try {
@@ -392,34 +392,4 @@ public class PersistenceUnitBuilder impl
         return QNameSet.singleton(PERSISTENCE_QNAME);
     }
 
-    public static final GBeanInfo GBEAN_INFO;
-
-    static {
-        GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(PersistenceUnitBuilder.class,
NameFactory.MODULE_BUILDER);
-
-        infoBuilder.addAttribute("defaultEnvironment", Environment.class, true, true);
-        infoBuilder.addAttribute("defaultPersistenceProviderClassName", String.class, true,
true);
-        infoBuilder.addAttribute("defaultJtaDataSourceName", String.class, true, true);
-        infoBuilder.addAttribute("defaultNonJtaDataSourceName", String.class, true, true);
-        infoBuilder.addAttribute("extendedEntityManagerRegistryName", AbstractNameQuery.class,
true, true);
-        infoBuilder.addAttribute("defaultPersistenceUnitProperties", Properties.class, true,
true);
-
-        infoBuilder.setConstructor(new String[]{
-                "defaultEnvironment",
-                "defaultPersistenceProviderClassName",
-                "defaultJtaDataSourceName",
-                "defaultNonJtaDataSourceName",
-                "extendedEntityManagerRegistryName",
-                "defaultPersistenceUnitProperties"
-        });
-
-        GBEAN_INFO = infoBuilder.getBeanInfo();
-
-    }
-
-    public static GBeanInfo getGBeanInfo() {
-        return GBEAN_INFO;
-    }
-
-
 }

Modified: geronimo/server/trunk/plugins/openjpa2/geronimo-persistence-jpa20-builder/src/main/java/org/apache/geronimo/persistence/builder/PersistenceUnitRefBuilder.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/openjpa2/geronimo-persistence-jpa20-builder/src/main/java/org/apache/geronimo/persistence/builder/PersistenceUnitRefBuilder.java?rev=945810&r1=945809&r2=945810&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/openjpa2/geronimo-persistence-jpa20-builder/src/main/java/org/apache/geronimo/persistence/builder/PersistenceUnitRefBuilder.java
(original)
+++ geronimo/server/trunk/plugins/openjpa2/geronimo-persistence-jpa20-builder/src/main/java/org/apache/geronimo/persistence/builder/PersistenceUnitRefBuilder.java
Tue May 18 18:42:26 2010
@@ -32,6 +32,8 @@ import org.apache.geronimo.gbean.GBeanIn
 import org.apache.geronimo.gbean.GBeanInfoBuilder;
 import org.apache.geronimo.gbean.GBeanData;
 import org.apache.geronimo.gbean.AbstractName;
+import org.apache.geronimo.gbean.annotation.GBean;
+import org.apache.geronimo.gbean.annotation.ParamAttribute;
 import org.apache.geronimo.j2ee.deployment.Module;
 import org.apache.geronimo.j2ee.deployment.NamingBuilder;
 import org.apache.geronimo.j2ee.deployment.annotation.PersistenceUnitAnnotationHelper;
@@ -53,6 +55,7 @@ import org.apache.xmlbeans.XmlObject;
 /**
  * @version $Rev$ $Date$
  */
+@GBean(j2eeType = NameFactory.MODULE_BUILDER)
 public class PersistenceUnitRefBuilder extends AbstractNamingBuilder {
     private static final QName PERSISTENCE_UNIT_REF_QNAME = new QName(JEE_NAMESPACE, "persistence-unit-ref");
     private static final QNameSet PERSISTENCE_UNIT_REF_QNAME_SET = QNameSet.singleton(PERSISTENCE_UNIT_REF_QNAME);
@@ -63,7 +66,9 @@ public class PersistenceUnitRefBuilder e
     private final boolean strictMatching;
 
 
-    public PersistenceUnitRefBuilder(Environment defaultEnvironment, AbstractNameQuery defaultPersistenceUnitAbstractNameQuery,
boolean strictMatching) {
+    public PersistenceUnitRefBuilder(@ParamAttribute(name = "defaultEnvironment") Environment
defaultEnvironment,
+                                     @ParamAttribute(name = "defaultPersistenceUnitAbstractNameQuery")
AbstractNameQuery defaultPersistenceUnitAbstractNameQuery,
+                                     @ParamAttribute(name = "strictMatching") boolean strictMatching)
{
         super(defaultEnvironment);
         this.defaultPersistenceUnitAbstractNameQuery = defaultPersistenceUnitAbstractNameQuery;
         this.strictMatching = strictMatching;
@@ -224,19 +229,4 @@ public class PersistenceUnitRefBuilder e
         return map;
     }
 
-    public static final GBeanInfo GBEAN_INFO;
-
-    static {
-        GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(PersistenceUnitRefBuilder.class,
NameFactory.MODULE_BUILDER);
-        infoBuilder.addAttribute("defaultEnvironment", Environment.class, true, true);
-        infoBuilder.addAttribute("defaultPersistenceUnitAbstractNameQuery", AbstractNameQuery.class,
true, true);
-        infoBuilder.addAttribute("strictMatching", boolean.class, true, true);
-
-        infoBuilder.setConstructor(new String[]{"defaultEnvironment", "defaultPersistenceUnitAbstractNameQuery",
"strictMatching"});
-        GBEAN_INFO = infoBuilder.getBeanInfo();
-    }
-
-    public static GBeanInfo getGBeanInfo() {
-        return PersistenceUnitRefBuilder.GBEAN_INFO;
-    }
 }



Mime
View raw message