[jboss-cvs] JBossAS SVN: r82860 - in projects/jpa/trunk/deployers: src/main/java/org/jboss/jpa/resolvers and 3 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Jan 14 06:28:13 EST 2009


Author: wolfc
Date: 2009-01-14 06:28:13 -0500 (Wed, 14 Jan 2009)
New Revision: 82860

Added:
   projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/BasePersistenceUnitDependencyResolver.java
   projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/DymanicPersistenceUnitDependencyResolverMBean.java
   projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/DynamicPersistenceUnitDependencyResolver.java
   projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/strategy/
   projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/strategy/BaseSearchStrategy.java
   projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/strategy/JBossSearchStrategy.java
   projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/strategy/SearchStrategy.java
   projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/strategy/SpecCompliantSearchStrategy.java
Modified:
   projects/jpa/trunk/deployers/pom.xml
   projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/DefaultPersistenceUnitDependencyResolver.java
   projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/InterApplicationPersistenceUnitDependencyResolver.java
   projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/deployers/test/resolvers/DefaultPersistenceUnitDependencyResolverTestCase.java
   projects/jpa/trunk/deployers/src/test/resources/org/jboss/jpa/deployers/test/common/jpa-deployers-beans.xml
Log:
JBAS-6382: refactored persistence unit resolvers to allow for configuration

Modified: projects/jpa/trunk/deployers/pom.xml
===================================================================
--- projects/jpa/trunk/deployers/pom.xml	2009-01-14 11:04:33 UTC (rev 82859)
+++ projects/jpa/trunk/deployers/pom.xml	2009-01-14 11:28:13 UTC (rev 82860)
@@ -69,7 +69,13 @@
       <artifactId>jboss-metadata</artifactId>
       <version>1.0.0.CR1</version>
     </dependency>
+    <!-- For @JMX -->
     <dependency>
+      <groupId>org.jboss.microcontainer</groupId>
+      <artifactId>jboss-aop-mc-int</artifactId>
+      <version>2.0.0.CR2</version>
+    </dependency>
+    <dependency>
       <groupId>org.jboss.naming</groupId>
       <artifactId>jnpserver</artifactId>
       <version>5.0.0.CR3</version>

Copied: projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/BasePersistenceUnitDependencyResolver.java (from rev 82852, projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/DefaultPersistenceUnitDependencyResolver.java)
===================================================================
--- projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/BasePersistenceUnitDependencyResolver.java	                        (rev 0)
+++ projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/BasePersistenceUnitDependencyResolver.java	2009-01-14 11:28:13 UTC (rev 82860)
@@ -0,0 +1,124 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jpa.resolvers;
+
+import org.jboss.beans.metadata.api.annotations.Inject;
+import org.jboss.deployers.structure.spi.DeploymentUnit;
+import org.jboss.jpa.javaee.JavaEEModuleInformer;
+import org.jboss.jpa.resolvers.strategy.SearchStrategy;
+
+/**
+ * A configurable PersistenceUnitDependencyResolver.
+ * 
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public abstract class BasePersistenceUnitDependencyResolver implements PersistenceUnitDependencyResolver
+{
+   private JavaEEModuleInformer javaEEModuleInformer;
+   
+   private SearchStrategy strategy;
+   
+   /**
+    * Make sure to set the strategy before using!
+    */
+   protected BasePersistenceUnitDependencyResolver()
+   {
+      
+   }
+   
+   protected BasePersistenceUnitDependencyResolver(SearchStrategy strategy)
+   {
+      assert strategy != null : "strategy is null";
+      
+      this.strategy = strategy;
+   }
+   
+   public String createBeanName(DeploymentUnit deploymentUnit, String persistenceUnitName)
+   {
+      // persistenceUnitName must be a simple name
+      assert persistenceUnitName.indexOf('/') == -1;
+      assert persistenceUnitName.indexOf('#') == -1;
+      
+      String appName = javaEEModuleInformer.getApplicationName(deploymentUnit);
+      String modulePath = javaEEModuleInformer.getModulePath(deploymentUnit);
+      String unitName = (appName != null ? appName + "/" : "") + modulePath + "#" + persistenceUnitName;
+      return "persistence.unit:unitName=" + unitName;
+   }
+
+   protected static DeploymentUnit getDeploymentUnit(DeploymentUnit current, String path)
+   {
+      if(path.startsWith("/"))
+         return getDeploymentUnit(current.getTopLevel(), path.substring(1));
+      if(path.startsWith("./"))
+         return getDeploymentUnit(current, path.substring(2));
+      if(path.startsWith("../"))
+         return getDeploymentUnit(current.getParent(), path.substring(3));
+      int i = path.indexOf('/');
+      String name;
+      if(i == -1)
+         name = path;
+      else
+         name = path.substring(0, i);
+      for(DeploymentUnit child : current.getChildren())
+      {
+         if(child.getSimpleName().equals(name))
+            return child;
+      }
+      throw new IllegalArgumentException("Can't find a deployment unit named " + name + " at " + current);
+   }
+   
+   public String resolvePersistenceUnitSupplier(DeploymentUnit deploymentUnit, String persistenceUnitName)
+   {
+      int i = (persistenceUnitName == null ? -1 : persistenceUnitName.indexOf('#'));
+      if(i != -1)
+      {
+         String path = persistenceUnitName.substring(0, i);
+         String unitName = persistenceUnitName.substring(i + 1);
+         DeploymentUnit targetDeploymentUnit = getDeploymentUnit(deploymentUnit, path);
+         // TODO: verify the existence of PersistenceUnitMetaData?
+         return createBeanName(targetDeploymentUnit, unitName);
+      }
+      else
+      {
+         if(strategy == null)
+            throw new IllegalStateException("No strategy was set on " + this);
+         String name = strategy.findPersistenceUnitSupplier(this, deploymentUnit, persistenceUnitName);
+         if(name == null)
+            throw new IllegalArgumentException("Can't find a persistence unit named '" + persistenceUnitName + "' in " + deploymentUnit);
+         return name;
+      }
+   }
+   
+   @Inject
+   public void setJavaEEModuleInformer(JavaEEModuleInformer informer)
+   {
+      this.javaEEModuleInformer = informer;
+   }
+   
+   protected void setSearchStrategy(SearchStrategy strategy)
+   {
+      assert strategy != null : "strategy is null";
+      
+      this.strategy = strategy;
+   }
+}

Modified: projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/DefaultPersistenceUnitDependencyResolver.java
===================================================================
--- projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/DefaultPersistenceUnitDependencyResolver.java	2009-01-14 11:04:33 UTC (rev 82859)
+++ projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/DefaultPersistenceUnitDependencyResolver.java	2009-01-14 11:28:13 UTC (rev 82860)
@@ -22,130 +22,20 @@
 package org.jboss.jpa.resolvers;
 
 import org.jboss.beans.metadata.api.annotations.Inject;
-import org.jboss.deployers.structure.spi.DeploymentUnit;
-import org.jboss.jpa.javaee.JavaEEModuleInformer;
-import org.jboss.metadata.jpa.spec.PersistenceMetaData;
-import org.jboss.metadata.jpa.spec.PersistenceUnitMetaData;
+import org.jboss.jpa.resolvers.strategy.SpecCompliantSearchStrategy;
 
+
 /**
  * The default implementation of a PersistenceUnitDependencyResolver.
  * 
  * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
  * @version $Revision: $
  */
-public class DefaultPersistenceUnitDependencyResolver implements PersistenceUnitDependencyResolver
+public class DefaultPersistenceUnitDependencyResolver extends BasePersistenceUnitDependencyResolver
 {
-   private JavaEEModuleInformer javaEEModuleInformer;
-   
-   public String createBeanName(DeploymentUnit deploymentUnit, String persistenceUnitName)
-   {
-      // persistenceUnitName must be a simple name
-      assert persistenceUnitName.indexOf('/') == -1;
-      assert persistenceUnitName.indexOf('#') == -1;
-      
-      String appName = javaEEModuleInformer.getApplicationName(deploymentUnit);
-      String modulePath = javaEEModuleInformer.getModulePath(deploymentUnit);
-      String unitName = (appName != null ? appName + "/" : "") + modulePath + "#" + persistenceUnitName;
-      return "persistence.unit:unitName=" + unitName;
-   }
-
-   protected String findWithinApplication(DeploymentUnit unit, String persistenceUnitName)
-   {
-      String name = findWithinModule(unit, persistenceUnitName, false);
-      if(name != null)
-         return name;
-      
-      for(DeploymentUnit child : unit.getChildren())
-      {
-         name = findWithinApplication(child, persistenceUnitName);
-         if(name != null)
-            return name;
-      }
-      return null;
-   }
-   
-   /*
-    * When finding the default persistence unit, the first persistence unit encountered is returned.
-    * TODO: Maybe the name of unscoped persistence units should be changed, so only one can be deployed anyway.
-    */
-   protected String findWithinModule(DeploymentUnit unit, String persistenceUnitName, boolean allowScoped)
-   {
-      if(!allowScoped && isScoped(unit))
-         return null;
-      
-      PersistenceMetaData persistenceMetaData = unit.getAttachment(PersistenceMetaData.class);
-      if(persistenceMetaData == null)
-         return null;
-      for(PersistenceUnitMetaData persistenceUnit : persistenceMetaData.getPersistenceUnits())
-      {
-         if(persistenceUnitName == null || persistenceUnitName.length() == 0 || persistenceUnit.getName().equals(persistenceUnitName))
-            return createBeanName(unit, persistenceUnit.getName());
-      }
-      return null;
-   }
-   
-   protected static DeploymentUnit getDeploymentUnit(DeploymentUnit current, String path)
-   {
-      if(path.startsWith("/"))
-         return getDeploymentUnit(current.getTopLevel(), path.substring(1));
-      if(path.startsWith("./"))
-         return getDeploymentUnit(current, path.substring(2));
-      if(path.startsWith("../"))
-         return getDeploymentUnit(current.getParent(), path.substring(3));
-      int i = path.indexOf('/');
-      String name;
-      if(i == -1)
-         name = path;
-      else
-         name = path.substring(0, i);
-      for(DeploymentUnit child : current.getChildren())
-      {
-         if(child.getSimpleName().equals(name))
-            return child;
-      }
-      throw new IllegalArgumentException("Can't find a deployment unit named " + name + " at " + current);
-   }
-   
-   /*
-    * EJB3 JPA 6.2.2: Persistence Unit Scope
-    */
-   private boolean isScoped(DeploymentUnit unit)
-   {
-      JavaEEModuleInformer.ModuleType type = javaEEModuleInformer.getModuleType(unit);
-      if(type == JavaEEModuleInformer.ModuleType.APP_CLIENT)
-         return true;
-      if(type == JavaEEModuleInformer.ModuleType.EJB)
-         return true;
-      if(type == JavaEEModuleInformer.ModuleType.WEB)
-         return true;
-      return false;
-   }
-   
-   public String resolvePersistenceUnitSupplier(DeploymentUnit deploymentUnit, String persistenceUnitName)
-   {
-      int i = (persistenceUnitName == null ? -1 : persistenceUnitName.indexOf('#'));
-      if(i != -1)
-      {
-         String path = persistenceUnitName.substring(0, i);
-         String unitName = persistenceUnitName.substring(i + 1);
-         DeploymentUnit targetDeploymentUnit = getDeploymentUnit(deploymentUnit, path);
-         // TODO: verify the existence of PersistenceUnitMetaData?
-         return createBeanName(targetDeploymentUnit, unitName);
-      }
-      else
-      {
-         String name = findWithinModule(deploymentUnit, persistenceUnitName, true);
-         if(name == null)
-            name = findWithinApplication(deploymentUnit.getTopLevel(), persistenceUnitName);
-         if(name == null)
-            throw new IllegalArgumentException("Can't find a persistence unit named '" + persistenceUnitName + "' in " + deploymentUnit);
-         return name;
-      }
-   }
-   
    @Inject
-   public void setJavaEEModuleInformer(JavaEEModuleInformer informer)
+   public void setSearchStrategy(SpecCompliantSearchStrategy strategy)
    {
-      this.javaEEModuleInformer = informer;
+      super.setSearchStrategy(strategy);
    }
 }

Added: projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/DymanicPersistenceUnitDependencyResolverMBean.java
===================================================================
--- projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/DymanicPersistenceUnitDependencyResolverMBean.java	                        (rev 0)
+++ projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/DymanicPersistenceUnitDependencyResolverMBean.java	2009-01-14 11:28:13 UTC (rev 82860)
@@ -0,0 +1,33 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jpa.resolvers;
+
+/**
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public interface DymanicPersistenceUnitDependencyResolverMBean
+{
+   boolean getSpecCompliant();
+   
+   void setSpecCompliant(boolean specCompliant);
+}

Added: projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/DynamicPersistenceUnitDependencyResolver.java
===================================================================
--- projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/DynamicPersistenceUnitDependencyResolver.java	                        (rev 0)
+++ projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/DynamicPersistenceUnitDependencyResolver.java	2009-01-14 11:28:13 UTC (rev 82860)
@@ -0,0 +1,76 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jpa.resolvers;
+
+import org.jboss.aop.microcontainer.aspects.jmx.JMX;
+import org.jboss.beans.metadata.api.annotations.Inject;
+import org.jboss.beans.metadata.api.annotations.Start;
+import org.jboss.jpa.resolvers.strategy.JBossSearchStrategy;
+import org.jboss.jpa.resolvers.strategy.SpecCompliantSearchStrategy;
+
+/**
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+ at JMX(exposedInterface=DymanicPersistenceUnitDependencyResolverMBean.class)
+public class DynamicPersistenceUnitDependencyResolver extends BasePersistenceUnitDependencyResolver
+   implements DymanicPersistenceUnitDependencyResolverMBean
+{
+   private boolean specCompliant = false;
+   
+   private JBossSearchStrategy jbossSearchStrategy;
+   
+   private SpecCompliantSearchStrategy specCompliantStrategy;
+   
+   public boolean getSpecCompliant()
+   {
+      return specCompliant;
+   }
+   
+   @Inject
+   public void setJBossSearchStrategy(JBossSearchStrategy strategy)
+   {
+      this.jbossSearchStrategy = strategy;
+   }
+   
+   public void setSpecCompliant(boolean specCompliant)
+   {
+      this.specCompliant = specCompliant;
+      if(specCompliant)
+         setSearchStrategy(specCompliantStrategy);
+      else
+         setSearchStrategy(jbossSearchStrategy);
+   }
+   
+   @Inject
+   public void setSpecCompliantSearchStrategy(SpecCompliantSearchStrategy strategy)
+   {
+      this.specCompliantStrategy = strategy;
+   }
+   
+   @Start
+   public void start()
+   {
+      // make sure we actually obey any overrides
+      setSpecCompliant(this.specCompliant);
+   }
+}

Modified: projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/InterApplicationPersistenceUnitDependencyResolver.java
===================================================================
--- projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/InterApplicationPersistenceUnitDependencyResolver.java	2009-01-14 11:04:33 UTC (rev 82859)
+++ projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/InterApplicationPersistenceUnitDependencyResolver.java	2009-01-14 11:28:13 UTC (rev 82860)
@@ -21,78 +21,18 @@
  */
 package org.jboss.jpa.resolvers;
 
-import java.util.Collection;
-
 import org.jboss.beans.metadata.api.annotations.Inject;
-import org.jboss.deployers.client.spi.Deployment;
-import org.jboss.deployers.client.spi.main.MainDeployer;
-import org.jboss.deployers.structure.spi.DeploymentUnit;
-import org.jboss.deployers.structure.spi.main.MainDeployerStructure;
+import org.jboss.jpa.resolvers.strategy.JBossSearchStrategy;
 
 /**
  * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
  * @version $Revision: $
  */
-public class InterApplicationPersistenceUnitDependencyResolver extends DefaultPersistenceUnitDependencyResolver
+public class InterApplicationPersistenceUnitDependencyResolver extends BasePersistenceUnitDependencyResolver
 {
-   private MainDeployer mainDeployer;
-   
-   private MainDeployerStructure mainDeployerStructure;
-   
-   protected String findWithinMainDeployer(String persistenceUnitName)
-   {
-      Collection<Deployment> topLevelDeployments = mainDeployer.getTopLevel();
-      if(topLevelDeployments == null)
-         return null;
-      
-      for(Deployment deployment : topLevelDeployments)
-      {
-         String name = deployment.getName();
-         DeploymentUnit deploymentUnit = mainDeployerStructure.getDeploymentUnit(name);
-         if(deploymentUnit == null)
-            continue;
-         String beanName = findWithinApplication(deploymentUnit, persistenceUnitName);
-         if(beanName != null)
-            return beanName;
-      }
-      
-      return null;
-   }
-   
-   // TODO: consilidate with DefaultPersistenceUnitDependencyResolver.resolvePersistenceUnitSupplier
-   public String resolvePersistenceUnitSupplier(DeploymentUnit deploymentUnit, String persistenceUnitName)
-   {
-      int i = (persistenceUnitName == null ? -1 : persistenceUnitName.indexOf('#'));
-      if(i != -1)
-      {
-         String path = persistenceUnitName.substring(0, i);
-         String unitName = persistenceUnitName.substring(i + 1);
-         DeploymentUnit targetDeploymentUnit = getDeploymentUnit(deploymentUnit, path);
-         // TODO: verify the existence of PersistenceUnitMetaData?
-         return createBeanName(targetDeploymentUnit, unitName);
-      }
-      else
-      {
-         String name = findWithinModule(deploymentUnit, persistenceUnitName, true);
-         if(name == null)
-            name = findWithinApplication(deploymentUnit.getTopLevel(), persistenceUnitName);
-         if(name == null)
-            name = findWithinMainDeployer(persistenceUnitName);
-         if(name == null)
-            throw new IllegalArgumentException("Can't find a persistence unit named '" + persistenceUnitName + "' in " + deploymentUnit);
-         return name;
-      }
-   }
-   
    @Inject
-   public void setMainDeployer(MainDeployer mainDeployer)
+   public void setSearchStrategy(JBossSearchStrategy strategy)
    {
-      this.mainDeployer = mainDeployer;
+      super.setSearchStrategy(strategy);
    }
-   
-   @Inject
-   public void setMainDeployerStructure(MainDeployerStructure mainDeployerStructure)
-   {
-      this.mainDeployerStructure = mainDeployerStructure;
-   }
 }

Copied: projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/strategy/BaseSearchStrategy.java (from rev 82857, projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/DefaultPersistenceUnitDependencyResolver.java)
===================================================================
--- projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/strategy/BaseSearchStrategy.java	                        (rev 0)
+++ projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/strategy/BaseSearchStrategy.java	2009-01-14 11:28:13 UTC (rev 82860)
@@ -0,0 +1,130 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jpa.resolvers.strategy;
+
+import org.jboss.beans.metadata.api.annotations.Inject;
+import org.jboss.deployers.structure.spi.DeploymentUnit;
+import org.jboss.jpa.javaee.JavaEEModuleInformer;
+import org.jboss.jpa.resolvers.PersistenceUnitDependencyResolver;
+import org.jboss.metadata.jpa.spec.PersistenceMetaData;
+import org.jboss.metadata.jpa.spec.PersistenceUnitMetaData;
+
+/**
+ * The spec compliant persistence unit search stragegy.
+ * 
+ * To allow injection by class it has a base name.
+ * 
+ * See EJB 3.0 6.2.2.
+ * 
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public abstract class BaseSearchStrategy implements SearchStrategy
+{
+   private JavaEEModuleInformer javaEEModuleInformer;
+   
+   public String findPersistenceUnitSupplier(PersistenceUnitDependencyResolver resolver, DeploymentUnit deploymentUnit, String persistenceUnitName)
+   {
+      String name = findWithinModule(resolver, deploymentUnit, persistenceUnitName, true);
+      if(name == null)
+         name = findWithinApplication(resolver, deploymentUnit.getTopLevel(), persistenceUnitName);
+      return name;
+   }
+   
+   protected String findWithinApplication(PersistenceUnitDependencyResolver resolver, DeploymentUnit unit, String persistenceUnitName)
+   {
+      String name = findWithinModule(resolver, unit, persistenceUnitName, false);
+      if(name != null)
+         return name;
+      
+      for(DeploymentUnit child : unit.getChildren())
+      {
+         name = findWithinApplication(resolver, child, persistenceUnitName);
+         if(name != null)
+            return name;
+      }
+      return null;
+   }
+   
+   /*
+    * When finding the default persistence unit, the first persistence unit encountered is returned.
+    * TODO: Maybe the name of unscoped persistence units should be changed, so only one can be deployed anyway.
+    */
+   protected String findWithinModule(PersistenceUnitDependencyResolver resolver, DeploymentUnit unit, String persistenceUnitName, boolean allowScoped)
+   {
+      if(!allowScoped && isScoped(unit))
+         return null;
+      
+      PersistenceMetaData persistenceMetaData = unit.getAttachment(PersistenceMetaData.class);
+      if(persistenceMetaData == null)
+         return null;
+      for(PersistenceUnitMetaData persistenceUnit : persistenceMetaData.getPersistenceUnits())
+      {
+         if(persistenceUnitName == null || persistenceUnitName.length() == 0 || persistenceUnit.getName().equals(persistenceUnitName))
+            return resolver.createBeanName(unit, persistenceUnit.getName());
+      }
+      return null;
+   }
+   
+   protected static DeploymentUnit getDeploymentUnit(DeploymentUnit current, String path)
+   {
+      if(path.startsWith("/"))
+         return getDeploymentUnit(current.getTopLevel(), path.substring(1));
+      if(path.startsWith("./"))
+         return getDeploymentUnit(current, path.substring(2));
+      if(path.startsWith("../"))
+         return getDeploymentUnit(current.getParent(), path.substring(3));
+      int i = path.indexOf('/');
+      String name;
+      if(i == -1)
+         name = path;
+      else
+         name = path.substring(0, i);
+      for(DeploymentUnit child : current.getChildren())
+      {
+         if(child.getSimpleName().equals(name))
+            return child;
+      }
+      throw new IllegalArgumentException("Can't find a deployment unit named " + name + " at " + current);
+   }
+   
+   /*
+    * EJB3 JPA 6.2.2: Persistence Unit Scope
+    */
+   private boolean isScoped(DeploymentUnit unit)
+   {
+      JavaEEModuleInformer.ModuleType type = javaEEModuleInformer.getModuleType(unit);
+      if(type == JavaEEModuleInformer.ModuleType.APP_CLIENT)
+         return true;
+      if(type == JavaEEModuleInformer.ModuleType.EJB)
+         return true;
+      if(type == JavaEEModuleInformer.ModuleType.WEB)
+         return true;
+      return false;
+   }
+   
+   @Inject
+   public void setJavaEEModuleInformer(JavaEEModuleInformer informer)
+   {
+      this.javaEEModuleInformer = informer;
+   }
+}

Copied: projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/strategy/JBossSearchStrategy.java (from rev 82852, projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/InterApplicationPersistenceUnitDependencyResolver.java)
===================================================================
--- projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/strategy/JBossSearchStrategy.java	                        (rev 0)
+++ projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/strategy/JBossSearchStrategy.java	2009-01-14 11:28:13 UTC (rev 82860)
@@ -0,0 +1,88 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jpa.resolvers.strategy;
+
+import java.util.Collection;
+
+import org.jboss.beans.metadata.api.annotations.Inject;
+import org.jboss.deployers.client.spi.Deployment;
+import org.jboss.deployers.client.spi.main.MainDeployer;
+import org.jboss.deployers.structure.spi.DeploymentUnit;
+import org.jboss.deployers.structure.spi.main.MainDeployerStructure;
+import org.jboss.jpa.resolvers.PersistenceUnitDependencyResolver;
+
+/**
+ * A search strategy which allows for persistence units in other (unrelated)
+ * deployments.
+ * 
+ * Note that this violates spec EJB 3.0 6.2.2.
+ * 
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class JBossSearchStrategy extends BaseSearchStrategy
+{
+   private MainDeployer mainDeployer;
+   
+   private MainDeployerStructure mainDeployerStructure;
+   
+   @Override
+   public String findPersistenceUnitSupplier(PersistenceUnitDependencyResolver resolver, DeploymentUnit deploymentUnit, String persistenceUnitName)
+   {
+      String name = super.findPersistenceUnitSupplier(resolver, deploymentUnit, persistenceUnitName);
+      if(name != null)
+         return name;
+      return findWithinMainDeployer(resolver, persistenceUnitName);
+   }
+   
+   protected String findWithinMainDeployer(PersistenceUnitDependencyResolver resolver, String persistenceUnitName)
+   {
+      Collection<Deployment> topLevelDeployments = mainDeployer.getTopLevel();
+      if(topLevelDeployments == null)
+         return null;
+      
+      for(Deployment deployment : topLevelDeployments)
+      {
+         String name = deployment.getName();
+         DeploymentUnit deploymentUnit = mainDeployerStructure.getDeploymentUnit(name);
+         if(deploymentUnit == null)
+            continue;
+         String beanName = findWithinApplication(resolver, deploymentUnit, persistenceUnitName);
+         if(beanName != null)
+            return beanName;
+      }
+      
+      return null;
+   }
+   
+   @Inject
+   public void setMainDeployer(MainDeployer mainDeployer)
+   {
+      this.mainDeployer = mainDeployer;
+   }
+   
+   @Inject
+   public void setMainDeployerStructure(MainDeployerStructure mainDeployerStructure)
+   {
+      this.mainDeployerStructure = mainDeployerStructure;
+   }
+}

Added: projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/strategy/SearchStrategy.java
===================================================================
--- projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/strategy/SearchStrategy.java	                        (rev 0)
+++ projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/strategy/SearchStrategy.java	2009-01-14 11:28:13 UTC (rev 82860)
@@ -0,0 +1,44 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jpa.resolvers.strategy;
+
+import org.jboss.deployers.structure.spi.DeploymentUnit;
+import org.jboss.jpa.resolvers.PersistenceUnitDependencyResolver;
+
+/**
+ * Allow for different methods to find persistence units.
+ * 
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public interface SearchStrategy
+{
+   /**
+    * Find the persistence unit supplier bean given a persistence unit name.
+    * 
+    * @param resolver the resolver which we're working on behalf of
+    * @param deploymentUnit the deployment unit that has a persistence unit reference
+    * @param persistenceUnitName the (relative) name of a persistence unit or null for the default persistence unit
+    * @return the bean name of the persistence unit or null if not found
+    */
+   String findPersistenceUnitSupplier(PersistenceUnitDependencyResolver resolver, DeploymentUnit deploymentUnit, String persistenceUnitName);
+}

Copied: projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/strategy/SpecCompliantSearchStrategy.java (from rev 82852, projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/DefaultPersistenceUnitDependencyResolver.java)
===================================================================
--- projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/strategy/SpecCompliantSearchStrategy.java	                        (rev 0)
+++ projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/resolvers/strategy/SpecCompliantSearchStrategy.java	2009-01-14 11:28:13 UTC (rev 82860)
@@ -0,0 +1,35 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jpa.resolvers.strategy;
+
+
+/**
+ * The spec compliant persistence unit search stragegy.
+ * 
+ * See EJB 3.0 6.2.2.
+ * 
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class SpecCompliantSearchStrategy extends BaseSearchStrategy
+{
+}

Modified: projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/deployers/test/resolvers/DefaultPersistenceUnitDependencyResolverTestCase.java
===================================================================
--- projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/deployers/test/resolvers/DefaultPersistenceUnitDependencyResolverTestCase.java	2009-01-14 11:04:33 UTC (rev 82859)
+++ projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/deployers/test/resolvers/DefaultPersistenceUnitDependencyResolverTestCase.java	2009-01-14 11:28:13 UTC (rev 82860)
@@ -34,6 +34,7 @@
 import org.jboss.jpa.deployers.PersistenceParsingDeployer;
 import org.jboss.jpa.deployers.test.common.SimpleJavaEEModuleInformer;
 import org.jboss.jpa.resolvers.DefaultPersistenceUnitDependencyResolver;
+import org.jboss.jpa.resolvers.strategy.SpecCompliantSearchStrategy;
 import org.jboss.metadata.jpa.spec.PersistenceMetaData;
 import org.jboss.virtual.VFS;
 import org.jboss.virtual.VirtualFile;
@@ -54,6 +55,7 @@
    {
       resolver = new DefaultPersistenceUnitDependencyResolver();
       resolver.setJavaEEModuleInformer(new SimpleJavaEEModuleInformer());
+      resolver.setSearchStrategy(new SpecCompliantSearchStrategy());
       
       String common = "/org/jboss/jpa/deployers/test";
       URL url = DefaultPersistenceUnitDependencyResolverTestCase.class.getResource(common);

Modified: projects/jpa/trunk/deployers/src/test/resources/org/jboss/jpa/deployers/test/common/jpa-deployers-beans.xml
===================================================================
--- projects/jpa/trunk/deployers/src/test/resources/org/jboss/jpa/deployers/test/common/jpa-deployers-beans.xml	2009-01-14 11:04:33 UTC (rev 82859)
+++ projects/jpa/trunk/deployers/src/test/resources/org/jboss/jpa/deployers/test/common/jpa-deployers-beans.xml	2009-01-14 11:28:13 UTC (rev 82860)
@@ -4,6 +4,8 @@
    
    <bean name="JavaEEModuleInformer" class="org.jboss.jpa.deployers.test.common.SimpleJavaEEModuleInformer"/>
    
+   <bean name="SpecCompliantSearchStrategy" class="org.jboss.jpa.resolvers.strategy.SpecCompliantSearchStrategy"/>
+   
    <bean name="PersistenceUnitDependencyResolver" class="org.jboss.jpa.resolvers.DefaultPersistenceUnitDependencyResolver"/>
    
    <!-- Do not use this one in a JavaEE environment -->




More information about the jboss-cvs-commits mailing list