[jboss-cvs] JBossAS SVN: r70698 - in projects/security/security-jboss-sx/trunk/acl: src/main/java/org/jboss/security/acl and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Mar 10 16:44:51 EDT 2008


Author: sguilhen at redhat.com
Date: 2008-03-10 16:44:51 -0400 (Mon, 10 Mar 2008)
New Revision: 70698

Added:
   projects/security/security-jboss-sx/trunk/acl/src/main/java/org/jboss/security/acl/ACLResourceFactory.java
   projects/security/security-jboss-sx/trunk/acl/src/tests/java/org/jboss/test/security/acl/TestResourceFactory.java
Modified:
   projects/security/security-jboss-sx/trunk/acl/pom.xml
   projects/security/security-jboss-sx/trunk/acl/src/main/java/org/jboss/security/acl/ACLImpl.java
   projects/security/security-jboss-sx/trunk/acl/src/main/java/org/jboss/security/acl/JPAPersistenceStrategy.java
   projects/security/security-jboss-sx/trunk/acl/src/tests/java/org/jboss/test/security/acl/JPAStrategyUnitTestCase.java
Log:
Added the getACLs() method to the JPAPersistenceStrategy, updated the tests and created a simple test resource factory.



Modified: projects/security/security-jboss-sx/trunk/acl/pom.xml
===================================================================
--- projects/security/security-jboss-sx/trunk/acl/pom.xml	2008-03-10 20:42:41 UTC (rev 70697)
+++ projects/security/security-jboss-sx/trunk/acl/pom.xml	2008-03-10 20:44:51 UTC (rev 70698)
@@ -81,7 +81,7 @@
       <dependency>
          <groupId>org.jboss.security</groupId>
          <artifactId>acl-spi</artifactId>
-         <version>2.0.2.Beta5</version>
+         <version>2.0.2-SNAPSHOT</version>
       </dependency>
       <dependency>
          <groupId>org.jboss.security</groupId>

Modified: projects/security/security-jboss-sx/trunk/acl/src/main/java/org/jboss/security/acl/ACLImpl.java
===================================================================
--- projects/security/security-jboss-sx/trunk/acl/src/main/java/org/jboss/security/acl/ACLImpl.java	2008-03-10 20:42:41 UTC (rev 70697)
+++ projects/security/security-jboss-sx/trunk/acl/src/main/java/org/jboss/security/acl/ACLImpl.java	2008-03-10 20:44:51 UTC (rev 70698)
@@ -196,6 +196,18 @@
       return false;
    }
 
+   /**
+    * <p>
+    * Obtains the stringfied representation of the resource associated with this {@code ACL}.
+    * </p>
+    * 
+    * @return   a {@code String} representation of the resource.
+    */
+   public String getResourceAsString()
+   {
+      return this.resourceAsString;
+   }
+
    /*
     * (non-Javadoc)
     * @see org.jboss.security.acl.ACL#getResource()

Added: projects/security/security-jboss-sx/trunk/acl/src/main/java/org/jboss/security/acl/ACLResourceFactory.java
===================================================================
--- projects/security/security-jboss-sx/trunk/acl/src/main/java/org/jboss/security/acl/ACLResourceFactory.java	                        (rev 0)
+++ projects/security/security-jboss-sx/trunk/acl/src/main/java/org/jboss/security/acl/ACLResourceFactory.java	2008-03-10 20:44:51 UTC (rev 70698)
@@ -0,0 +1,25 @@
+package org.jboss.security.acl;
+
+import org.jboss.security.authorization.Resource;
+
+/**
+ * <p>
+ * This interface provides a factory for {@code Resource}s.
+ * </p>
+ *  
+ * @author <a href="mailto:sguilhen at redhat.com">Stefan Guilhen</a>
+ */
+public interface ACLResourceFactory
+{
+
+   /**
+    * <p>
+    * Creates an instance of the {@code Resource} with the specified class name and id.
+    * </p>
+    * 
+    * @param resourceClassName  the fully-qualified class name of the {@code Resource}.
+    * @param id the unique identifier of the {@code Resource}.
+    * @return a reference to the instantiated {@code Resource}.
+    */
+   public Resource instantiateResource(String resourceClassName, Object id);
+}

Modified: projects/security/security-jboss-sx/trunk/acl/src/main/java/org/jboss/security/acl/JPAPersistenceStrategy.java
===================================================================
--- projects/security/security-jboss-sx/trunk/acl/src/main/java/org/jboss/security/acl/JPAPersistenceStrategy.java	2008-03-10 20:42:41 UTC (rev 70697)
+++ projects/security/security-jboss-sx/trunk/acl/src/main/java/org/jboss/security/acl/JPAPersistenceStrategy.java	2008-03-10 20:44:51 UTC (rev 70698)
@@ -27,12 +27,20 @@
    // in memory cache of the created ACLs.
    private final Map<Resource, ACL> aclMap;
 
-   private final EntityManagerFactory factory;
+   private final EntityManagerFactory managerFactory;
 
+   private final ACLResourceFactory resourceFactory;
+
    public JPAPersistenceStrategy()
    {
+      this(null);
+   }
+
+   public JPAPersistenceStrategy(ACLResourceFactory resourceFactory)
+   {
       this.aclMap = new HashMap<Resource, ACL>();
-      this.factory = Persistence.createEntityManagerFactory("ACL");
+      this.managerFactory = Persistence.createEntityManagerFactory("ACL");
+      this.resourceFactory = resourceFactory;
    }
 
    /*
@@ -57,7 +65,7 @@
       ACL acl = this.aclMap.get(resource);
       if (acl == null)
       {
-         EntityManager entityManager = this.factory.createEntityManager();
+         EntityManager entityManager = this.managerFactory.createEntityManager();
          EntityTransaction transaction = entityManager.getTransaction();
          transaction.begin();
          try
@@ -99,7 +107,7 @@
    {
       boolean result = false;
 
-      EntityManager entityManager = this.factory.createEntityManager();
+      EntityManager entityManager = this.managerFactory.createEntityManager();
       EntityTransaction transaction = entityManager.getTransaction();
       transaction.begin();
       try
@@ -136,7 +144,7 @@
       ACL acl = this.aclMap.get(resource);
       if (acl == null)
       {
-         EntityManager entityManager = this.factory.createEntityManager();
+         EntityManager entityManager = this.managerFactory.createEntityManager();
          try
          {
             acl = this.findACLByResource(resource, entityManager);
@@ -153,6 +161,35 @@
 
    /*
     * (non-Javadoc)
+    * @see org.jboss.security.acl.ACLPersistenceStrategy#getACLs()
+    */
+   @SuppressWarnings("unchecked")
+   public Collection<ACL> getACLs()
+   {
+      Collection<ACL> acls = null;
+      EntityManager entityManager = this.managerFactory.createEntityManager();
+      try
+      {
+         acls = entityManager.createQuery("SELECT a FROM ACLImpl a").getResultList();
+         if (acls != null && this.resourceFactory != null)
+         {
+            for (ACL acl : acls)
+            {
+               ACLImpl impl = (ACLImpl) acl;
+               String[] resourceName = impl.getResourceAsString().split(":");
+               impl.setResource(this.resourceFactory.instantiateResource(resourceName[0], resourceName[1]));
+            }
+         }
+      }
+      finally
+      {
+         entityManager.close();
+      }
+      return acls;
+   }
+
+   /*
+    * (non-Javadoc)
     * @see org.jboss.security.acl.ACLProvider#updateACL(org.jboss.security.acl.ACL)
     */
    public boolean updateACL(ACL acl)
@@ -160,7 +197,7 @@
       if (((ACLImpl) acl).getACLId() == 0)
          return false;
 
-      EntityManager entityManager = this.factory.createEntityManager();
+      EntityManager entityManager = this.managerFactory.createEntityManager();
       EntityTransaction transaction = entityManager.getTransaction();
       transaction.begin();
       try

Modified: projects/security/security-jboss-sx/trunk/acl/src/tests/java/org/jboss/test/security/acl/JPAStrategyUnitTestCase.java
===================================================================
--- projects/security/security-jboss-sx/trunk/acl/src/tests/java/org/jboss/test/security/acl/JPAStrategyUnitTestCase.java	2008-03-10 20:42:41 UTC (rev 70697)
+++ projects/security/security-jboss-sx/trunk/acl/src/tests/java/org/jboss/test/security/acl/JPAStrategyUnitTestCase.java	2008-03-10 20:44:51 UTC (rev 70698)
@@ -59,7 +59,7 @@
       for (int index = 0; index < this.resources.length; index++)
          this.resources[index] = new TestResource(index + 1);
       this.createdACLs = new ArrayList<ACL>();
-      this.strategy = new JPAPersistenceStrategy();
+      this.strategy = new JPAPersistenceStrategy(new TestResourceFactory());
    }
 
    @Override
@@ -184,4 +184,30 @@
       for (; index < this.resources.length; index++)
          assertNotNull(this.strategy.getACL(this.resources[index]));
    }
+
+   /**
+    * <p>
+    * Tests the search functionality of the {@code JPAPersistenceStrategy} when a {@code ResourceFactory}
+    * has been set.
+    * </p>
+    * 
+    * @throws Exception if an error occurs when running the test.
+    */
+   public void testStrategyWithResourceFactory() throws Exception
+   {
+      ACL[] acls = new ACL[this.resources.length];
+      for (int index = 0; index < this.resources.length; index++)
+      {
+         acls[index] = this.strategy.createACL(this.resources[index]);
+         this.createdACLs.add(acls[index]);
+      }
+
+      // retrieves all persisted ACLs.
+      Collection<ACL> retrievedACLs = this.strategy.getACLs();
+      assertNotNull(retrievedACLs);
+
+      // assert all retrieved ACLs had their resource correctly set by the factory.
+      for (ACL acl : retrievedACLs)
+         assertNotNull(acl.getResource());
+   }
 }

Added: projects/security/security-jboss-sx/trunk/acl/src/tests/java/org/jboss/test/security/acl/TestResourceFactory.java
===================================================================
--- projects/security/security-jboss-sx/trunk/acl/src/tests/java/org/jboss/test/security/acl/TestResourceFactory.java	                        (rev 0)
+++ projects/security/security-jboss-sx/trunk/acl/src/tests/java/org/jboss/test/security/acl/TestResourceFactory.java	2008-03-10 20:44:51 UTC (rev 70698)
@@ -0,0 +1,20 @@
+package org.jboss.test.security.acl;
+
+import org.jboss.security.acl.ACLResourceFactory;
+import org.jboss.security.authorization.Resource;
+
+public class TestResourceFactory implements ACLResourceFactory
+{
+
+   public Resource instantiateResource(String resourceClassName, Object id)
+   {
+      if (resourceClassName != null && resourceClassName.equals("org.jboss.test.security.acl.TestResource"))
+      {
+         int resourceId = Integer.parseInt((String) id);
+         TestResource resource = new TestResource(resourceId);
+         return resource;
+      }
+
+      return null;
+   }
+}




More information about the jboss-cvs-commits mailing list