[jboss-cvs] JBossAS SVN: r110929 - in projects/jboss-deployers/trunk: deployers-spi/src/main/java/org/jboss/deployers/spi/deployer/helpers and 3 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Mar 16 11:30:44 EDT 2011


Author: alesj
Date: 2011-03-16 11:30:44 -0400 (Wed, 16 Mar 2011)
New Revision: 110929

Added:
   projects/jboss-deployers/trunk/deployers-core-spi/src/main/java/org/jboss/deployers/spi/attachments/LocalAttachments.java
   projects/jboss-deployers/trunk/deployers-structure-spi/src/test/java/org/jboss/test/deployers/structure/attachments/test/AbstractLocalAttachmentsUnitTestCase.java
Modified:
   projects/jboss-deployers/trunk/deployers-spi/src/main/java/org/jboss/deployers/spi/deployer/helpers/AbstractSimpleRealDeployer.java
   projects/jboss-deployers/trunk/deployers-structure-spi/src/main/java/org/jboss/deployers/structure/spi/helpers/AbstractDeploymentUnit.java
   projects/jboss-deployers/trunk/deployers-structure-spi/src/test/java/org/jboss/test/deployers/structure/attachments/StructureAttachmentsTestSuite.java
Log:
[JBDEPLOY-271]; add local attachment lookup.


Copied: projects/jboss-deployers/trunk/deployers-core-spi/src/main/java/org/jboss/deployers/spi/attachments/LocalAttachments.java (from rev 109912, projects/jboss-deployers/trunk/deployers-core-spi/src/main/java/org/jboss/deployers/spi/attachments/Attachments.java)
===================================================================
--- projects/jboss-deployers/trunk/deployers-core-spi/src/main/java/org/jboss/deployers/spi/attachments/LocalAttachments.java	                        (rev 0)
+++ projects/jboss-deployers/trunk/deployers-core-spi/src/main/java/org/jboss/deployers/spi/attachments/LocalAttachments.java	2011-03-16 15:30:44 UTC (rev 110929)
@@ -0,0 +1,107 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.deployers.spi.attachments;
+
+import java.io.Serializable;
+import java.util.Map;
+
+/**
+ * Local attachments
+ * 
+ * Represents a set of local attachments
+ * 
+ * @author <a href="ales.justin at jboss.org">Ales Justin</a>
+ */
+public interface LocalAttachments extends Serializable
+{
+   /**
+    * Get all the local attachments
+    * 
+    * @return the unmodifiable attachments
+    */
+   Map<String, Object> getLocalAttachments();
+
+   /**
+    * Get local attachment
+    * 
+    * @param name the name of the attachment
+    * @return the attachment or null if not present
+    * @throws IllegalArgumentException for a null name
+    */
+   Object getLocalAttachment(String name);
+
+   /**
+    * Get local attachment
+    * 
+    * @param <T> the expected type
+    * @param name the name of the attachment
+    * @param expectedType the expected type
+    * @return the attachment or null if not present
+    * @throws IllegalArgumentException for a null name or expectedType
+    */
+   <T> T getLocalAttachment(String name, Class<T> expectedType);
+
+   /**
+    * Get local attachment
+    * 
+    * @param <T> the expected type
+    * @param type the type
+    * @return the attachment or null if not present
+    * @throws IllegalArgumentException for a null name or type
+    */
+   <T> T getLocalAttachment(Class<T> type);
+   
+   /**
+    * Is the local attachment present
+    * 
+    * @param name the name of the attachment
+    * @return true when the attachment is present
+    * @throws IllegalArgumentException for a null name
+    */
+   boolean isLocalAttachmentPresent(String name);
+   
+   /**
+    * Is the local attachment present
+    * 
+    * @param name the name of the attachment
+    * @param expectedType the expected type
+    * @return true when the attachment is present
+    * @throws IllegalArgumentException for a null name or expectedType
+    */
+   boolean isLocalAttachmentPresent(String name, Class<?> expectedType);
+   
+   /**
+    * Is the local attachment present
+    * 
+    * @param type the type
+    * @return true when the attachment is present
+    * @throws IllegalArgumentException for a null name or type
+    */
+   boolean isLocalAttachmentPresent(Class<?> type);
+
+   /**
+    * Are there any local attachments
+    * 
+    * @return true if there are any attachments, false otherwise.
+    */
+   boolean hasLocalAttachments();
+}

Modified: projects/jboss-deployers/trunk/deployers-spi/src/main/java/org/jboss/deployers/spi/deployer/helpers/AbstractSimpleRealDeployer.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-spi/src/main/java/org/jboss/deployers/spi/deployer/helpers/AbstractSimpleRealDeployer.java	2011-03-16 14:48:29 UTC (rev 110928)
+++ projects/jboss-deployers/trunk/deployers-spi/src/main/java/org/jboss/deployers/spi/deployer/helpers/AbstractSimpleRealDeployer.java	2011-03-16 15:30:44 UTC (rev 110929)
@@ -22,6 +22,7 @@
 package org.jboss.deployers.spi.deployer.helpers;
 
 import org.jboss.deployers.spi.DeploymentException;
+import org.jboss.deployers.spi.attachments.LocalAttachments;
 import org.jboss.deployers.structure.spi.DeploymentUnit;
 
 /**
@@ -29,10 +30,14 @@
  * 
  * @param <T> the input type
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @author <a href="ales.justin at jboss.org">Ales Justin</a>
  * @version $Revision: 1.1 $
  */
 public abstract class AbstractSimpleRealDeployer<T> extends AbstractRealDeployer
 {
+   /** The force hierarchy lookup flag */
+   private boolean forceHierarchyLookup;
+
    /**
     * Create a new AbstractSimpleRealDeployer
     *  
@@ -55,16 +60,32 @@
       return (Class<? extends T>) input;
    }
 
+   /**
+    * Get attachment.
+    *
+    * @param unit the current deployment unit
+    * @return attachment or null
+    */
+   protected T getAttachment(DeploymentUnit unit)
+   {
+      if (forceHierarchyLookup == false && isComponentsOnly() && (unit instanceof LocalAttachments))
+      {
+         LocalAttachments la = (LocalAttachments) unit;
+         return la.getLocalAttachment(getInput());
+      }
+      return unit.getAttachment(getInput());
+   }
+
    public void internalDeploy(DeploymentUnit unit) throws DeploymentException
    {
-      T deployment = unit.getAttachment(getInput());
+      T deployment = getAttachment(unit);
       if (deployment != null)
          deploy(unit, deployment);
    }
 
    public void internalUndeploy(DeploymentUnit unit)
    {
-      T deployment = unit.getAttachment(getInput());
+      T deployment = getAttachment(unit);
       if (deployment != null)
          undeploy(unit, deployment);
    }
@@ -75,4 +96,14 @@
    {
       // Nothing
    }
+
+   /**
+    * Set hierarchy flag.
+    *
+    * @param forceHierarchyLookup the flag
+    */
+   public void setForceHierarchyLookup(boolean forceHierarchyLookup)
+   {
+      this.forceHierarchyLookup = forceHierarchyLookup;
+   }
 }

Modified: projects/jboss-deployers/trunk/deployers-structure-spi/src/main/java/org/jboss/deployers/structure/spi/helpers/AbstractDeploymentUnit.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-structure-spi/src/main/java/org/jboss/deployers/structure/spi/helpers/AbstractDeploymentUnit.java	2011-03-16 14:48:29 UTC (rev 110928)
+++ projects/jboss-deployers/trunk/deployers-structure-spi/src/main/java/org/jboss/deployers/structure/spi/helpers/AbstractDeploymentUnit.java	2011-03-16 15:30:44 UTC (rev 110929)
@@ -21,6 +21,25 @@
 */
 package org.jboss.deployers.structure.spi.helpers;
 
+import org.jboss.dependency.spi.DependencyInfo;
+import org.jboss.dependency.spi.DependencyItem;
+import org.jboss.deployers.client.spi.main.MainDeployer;
+import org.jboss.deployers.spi.DeploymentException;
+import org.jboss.deployers.spi.attachments.LocalAttachments;
+import org.jboss.deployers.spi.attachments.MutableAttachments;
+import org.jboss.deployers.spi.attachments.helpers.AbstractMutableAttachments;
+import org.jboss.deployers.spi.deployer.DeploymentStage;
+import org.jboss.deployers.structure.spi.ClassLoaderFactory;
+import org.jboss.deployers.structure.spi.DeploymentContext;
+import org.jboss.deployers.structure.spi.DeploymentContextExt;
+import org.jboss.deployers.structure.spi.DeploymentResourceLoader;
+import org.jboss.deployers.structure.spi.DeploymentUnit;
+import org.jboss.deployers.structure.spi.DeploymentUnitExt;
+import org.jboss.deployers.structure.spi.DeploymentUnitVisitor;
+import org.jboss.metadata.spi.MetaData;
+import org.jboss.metadata.spi.MutableMetaData;
+import org.jboss.metadata.spi.scope.ScopeKey;
+
 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
@@ -32,18 +51,6 @@
 import java.util.Map;
 import java.util.Set;
 
-import org.jboss.dependency.spi.DependencyInfo;
-import org.jboss.dependency.spi.DependencyItem;
-import org.jboss.deployers.client.spi.main.MainDeployer;
-import org.jboss.deployers.spi.DeploymentException;
-import org.jboss.deployers.spi.deployer.DeploymentStage;
-import org.jboss.deployers.spi.attachments.MutableAttachments;
-import org.jboss.deployers.spi.attachments.helpers.AbstractMutableAttachments;
-import org.jboss.deployers.structure.spi.*;
-import org.jboss.metadata.spi.MetaData;
-import org.jboss.metadata.spi.MutableMetaData;
-import org.jboss.metadata.spi.scope.ScopeKey;
-
 /**
  * AbstractDeploymentUnit.<p>
  * 
@@ -55,7 +62,7 @@
  * @author <a href="ales.justin at jboss.com">Ales Justin</a>
  * @version $Revision: 1.1 $
  */
-public class AbstractDeploymentUnit extends AbstractMutableAttachments implements DeploymentUnit, DeploymentUnitExt
+public class AbstractDeploymentUnit extends AbstractMutableAttachments implements DeploymentUnit, DeploymentUnitExt, LocalAttachments
 {
    /** The serialVersionUID */
    private static final long serialVersionUID = 1513962148798298768L;
@@ -354,9 +361,14 @@
 
    public Object getAttachment(String name)
    {
+      return getAttachmentInternal(name, getDeploymentContext().isComponent());
+   }
+
+   private Object getAttachmentInternal(String name, boolean checkParent)
+   {
       DeploymentContext deploymentContext = getDeploymentContext();
       DeploymentContext parent = deploymentContext.getParent();
-      if (deploymentContext.isComponent() == false)
+      if (checkParent == false)
          parent = null;
       Object result = deploymentContext.getPredeterminedManagedObjects().getAttachment(name);
       if (result != null)
@@ -408,9 +420,14 @@
 
    public Map<String, Object> getAttachments()
    {
+      return getAttachmentsInternal(getDeploymentContext().isComponent());
+   }
+
+   private Map<String, Object> getAttachmentsInternal(boolean checkParent)
+   {
       DeploymentContext deploymentContext = getDeploymentContext();
       DeploymentContext parent = deploymentContext.getParent();
-      if (deploymentContext.isComponent() == false)
+      if (checkParent == false)
          parent = null;
       HashMap<String, Object> result = new HashMap<String, Object>();
       if (parent != null)
@@ -429,6 +446,11 @@
 
    public boolean hasAttachments()
    {
+      return hasAttachmentsInternal(getDeploymentContext().isComponent());
+   }
+
+   private boolean hasAttachmentsInternal(boolean checkParent)
+   {
       DeploymentContext deploymentContext = getDeploymentContext();
       if (deploymentContext.getTransientAttachments().hasAttachments())
          return true;
@@ -436,17 +458,77 @@
          return true;
       else if (deploymentContext.getPredeterminedManagedObjects().hasAttachments())
          return true;
-      
-      if (deploymentContext.isComponent())
-         return deploymentContext.getParent().getDeploymentUnit().hasAttachments();
-      return false;
+
+      return checkParent && deploymentContext.getParent().getDeploymentUnit().hasAttachments();
    }
 
    public boolean isAttachmentPresent(String name)
    {
       return getAttachment(name) != null;
    }
-   
+
+   public Map<String, Object> getLocalAttachments()
+   {
+      return getAttachmentsInternal(false);
+   }
+
+   public Object getLocalAttachment(String name)
+   {
+      return getAttachmentInternal(name, false);
+   }
+
+   public <T> T getLocalAttachment(String name, Class<T> expectedType)
+   {
+      if (expectedType == null)
+         throw new IllegalArgumentException("Null expectedType");
+      Object result = getLocalAttachment(name);
+      if (result == null)
+         return null;
+      return expectedType.cast(result);
+   }
+
+   public <T> T getLocalAttachment(Class<T> type)
+   {
+      if (type == null)
+         throw new IllegalArgumentException("Null type");
+      return getLocalAttachment(type.getName(), type);
+   }
+
+   public boolean isLocalAttachmentPresent(String name)
+   {
+      return getLocalAttachment(name) != null;
+   }
+
+   public boolean isLocalAttachmentPresent(String name, Class<?> expectedType)
+   {
+      if (expectedType == null)
+         throw new IllegalArgumentException("Null expectedType");
+      Object result = getLocalAttachment(name);
+      if (result == null)
+         return false;
+      try
+      {
+         expectedType.cast(result);
+      }
+      catch (ClassCastException e)
+      {
+         return false;
+      }
+      return true;
+   }
+
+   public boolean isLocalAttachmentPresent(Class<?> type)
+   {
+      if (type == null)
+         throw new IllegalArgumentException("Null type");
+      return isLocalAttachmentPresent(type.getName(), type);
+   }
+
+   public boolean hasLocalAttachments()
+   {
+      return hasAttachmentsInternal(false);
+   }
+
    public ClassLoader getResourceClassLoader()
    {
       return getDeploymentContext().getResourceClassLoader();

Modified: projects/jboss-deployers/trunk/deployers-structure-spi/src/test/java/org/jboss/test/deployers/structure/attachments/StructureAttachmentsTestSuite.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-structure-spi/src/test/java/org/jboss/test/deployers/structure/attachments/StructureAttachmentsTestSuite.java	2011-03-16 14:48:29 UTC (rev 110928)
+++ projects/jboss-deployers/trunk/deployers-structure-spi/src/test/java/org/jboss/test/deployers/structure/attachments/StructureAttachmentsTestSuite.java	2011-03-16 15:30:44 UTC (rev 110929)
@@ -24,7 +24,6 @@
 import junit.framework.Test;
 import junit.framework.TestSuite;
 import junit.textui.TestRunner;
-
 import org.jboss.test.deployers.structure.attachments.test.AbstractDeploymentContextPredeterminedManagedObjectsUnitTestCase;
 import org.jboss.test.deployers.structure.attachments.test.AbstractDeploymentContextTransientAttachmentsUnitTestCase;
 import org.jboss.test.deployers.structure.attachments.test.AbstractDeploymentContextTransientManagedObjectsUnitTestCase;
@@ -33,11 +32,13 @@
 import org.jboss.test.deployers.structure.attachments.test.AbstractDeploymentUnitPredeterminedManagedObjectsUnitTestCase;
 import org.jboss.test.deployers.structure.attachments.test.AbstractDeploymentUnitTransientAttachmentsUnitTestCase;
 import org.jboss.test.deployers.structure.attachments.test.AbstractDeploymentUnitTransientManagedObjectsUnitTestCase;
+import org.jboss.test.deployers.structure.attachments.test.AbstractLocalAttachmentsUnitTestCase;
 
 /**
  * StructureAttachmentsTestSuite.
  * 
  * @author <a href="adrian at jboss.org">Adrian Brock</a>
+ * @author <a href="ales.justin at jboss.org">Ales Justin</a>
  * @version $Revision: 1.1 $
  */
 public class StructureAttachmentsTestSuite extends TestSuite
@@ -59,6 +60,7 @@
       suite.addTest(AbstractDeploymentUnitTransientAttachmentsUnitTestCase.suite());
       suite.addTest(AbstractDeploymentUnitAttachmentsUnitTestCase.suite());
       suite.addTest(AbstractDeploymentUnitAttachmentHierarchyUnitTestCase.suite());
+      suite.addTest(AbstractLocalAttachmentsUnitTestCase.suite());
 
       return suite;
    }

Copied: projects/jboss-deployers/trunk/deployers-structure-spi/src/test/java/org/jboss/test/deployers/structure/attachments/test/AbstractLocalAttachmentsUnitTestCase.java (from rev 109912, projects/jboss-deployers/trunk/deployers-structure-spi/src/test/java/org/jboss/test/deployers/structure/attachments/test/AbstractDeploymentUnitAttachmentsUnitTestCase.java)
===================================================================
--- projects/jboss-deployers/trunk/deployers-structure-spi/src/test/java/org/jboss/test/deployers/structure/attachments/test/AbstractLocalAttachmentsUnitTestCase.java	                        (rev 0)
+++ projects/jboss-deployers/trunk/deployers-structure-spi/src/test/java/org/jboss/test/deployers/structure/attachments/test/AbstractLocalAttachmentsUnitTestCase.java	2011-03-16 15:30:44 UTC (rev 110929)
@@ -0,0 +1,106 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.test.deployers.structure.attachments.test;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import org.jboss.deployers.spi.attachments.Attachments;
+import org.jboss.deployers.spi.attachments.LocalAttachments;
+import org.jboss.deployers.spi.attachments.MutableAttachments;
+import org.jboss.deployers.structure.spi.DeploymentUnit;
+import org.jboss.deployers.structure.spi.helpers.AbstractDeploymentContext;
+import org.jboss.deployers.structure.spi.helpers.AbstractDeploymentUnit;
+import org.jboss.test.deployers.attachments.test.AttachmentsTest;
+import org.jboss.test.deployers.attachments.test.ExpectedAttachments;
+
+import java.util.Date;
+
+/**
+ * AbstractLocalAttachmentsUnitTestCase.
+ * 
+ * @author <a href="ales.justin at jboss.org">Ales Justin</a>
+ */
+public class AbstractLocalAttachmentsUnitTestCase extends AttachmentsTest
+{
+   public static Test suite()
+   {
+      return new TestSuite(AbstractLocalAttachmentsUnitTestCase.class);
+   }
+
+   private DeploymentUnit unit;
+
+   public AbstractLocalAttachmentsUnitTestCase(String name)
+   {
+      super(name);
+      AbstractDeploymentContext context = new AbstractDeploymentContext("attachments", "");
+      unit = new AbstractDeploymentUnit(context);
+      context.setDeploymentUnit(unit);
+   }
+
+   public void testLocal() throws Exception
+   {
+      DeploymentUnit component = unit.addComponent("component");
+
+      Date date1 = new Date();
+      Date date2 = new Date();
+      Integer i1 = 1;
+
+      unit.addAttachment(Date.class, date1);
+      unit.addAttachment("date1", date1, Date.class);
+      component.addAttachment(Date.class, date2);
+      component.addAttachment("date2", date2, Date.class);
+      unit.addAttachment(Integer.class, i1);
+
+      ExpectedAttachments ea = new ExpectedAttachments();
+      ea.add("date2", date2);
+      ea.add(Date.class.getName(), date2);
+
+      assertEquals(i1, unit.getAttachment(Integer.class));
+      assertEquals(i1, component.getAttachment(Integer.class));
+
+      LocalAttachments la = assertInstanceOf(component, LocalAttachments.class);
+      assertEquals(date2, la.getLocalAttachment(Date.class));
+      assertEquals(date2, la.getLocalAttachment("date2"));
+      assertEquals(date2, la.getLocalAttachment("date2", Date.class));
+      assertTrue(la.isLocalAttachmentPresent(Date.class));
+      assertTrue(la.isLocalAttachmentPresent("date2"));
+      assertTrue(la.isLocalAttachmentPresent("date2", Date.class));
+      assertTrue(la.hasLocalAttachments());
+      assertEquals(ea.expected, la.getLocalAttachments());
+   }
+
+   @Override
+   public void testSerialization() throws Exception
+   {
+      // ignore
+   }
+
+   protected Attachments getAttachments()
+   {
+      return unit;
+   }
+
+   protected MutableAttachments getMutable()
+   {
+      return unit;
+   }
+}



More information about the jboss-cvs-commits mailing list