[jboss-cvs] JBossAS SVN: r104388 - in projects/ejb-book/trunk/ch09-secureschool/src: main/java/org/jboss/ejb3/examples/ch09/secureschool/impl and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sat May 1 16:23:57 EDT 2010


Author: ALRubinger
Date: 2010-05-01 16:23:56 -0400 (Sat, 01 May 2010)
New Revision: 104388

Added:
   projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/api/FireDepartmentLocalBusiness.java
   projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/impl/FireDepartmentBean.java
   projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/impl/Roles.java
Removed:
   projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/api/Roles.java
Modified:
   projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/api/SchoolClosedException.java
   projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/api/SecureSchoolLocalBusiness.java
   projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/impl/SecureSchoolBean.java
   projects/ejb-book/trunk/ch09-secureschool/src/test/java/org/jboss/ejb3/examples/ch09/secureschool/SecureSchoolIntegrationTest.java
Log:
[EJBBOOK-22] Add to security example @RunAs in use

Added: projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/api/FireDepartmentLocalBusiness.java
===================================================================
--- projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/api/FireDepartmentLocalBusiness.java	                        (rev 0)
+++ projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/api/FireDepartmentLocalBusiness.java	2010-05-01 20:23:56 UTC (rev 104388)
@@ -0,0 +1,43 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, 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.ejb3.examples.ch09.secureschool.api;
+
+/**
+ * Represents a fire department capable of declaring 
+ * a state of emergency.  Anyone may invoke this support, 
+ * and when an alert is raised we'll close the local school.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public interface FireDepartmentLocalBusiness
+{
+   // ---------------------------------------------------------------------------||
+   // Contracts -----------------------------------------------------------------||
+   // ---------------------------------------------------------------------------||
+
+   /**
+    * Declares a state of emergency, so we must close the local school
+    */
+   void declareEmergency();
+
+}

Deleted: projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/api/Roles.java
===================================================================
--- projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/api/Roles.java	2010-05-01 11:24:40 UTC (rev 104387)
+++ projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/api/Roles.java	2010-05-01 20:23:56 UTC (rev 104388)
@@ -1,57 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2010, 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.ejb3.examples.ch09.secureschool.api;
-
-/**
- * Holds the list of roles with which users of the school 
- * may be affiliated.  EJB Security is role-based, so this
- * is how we'll determine access.
- *
- * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
- * @version $Revision: $
- */
-public interface Roles
-{
-   // ---------------------------------------------------------------------------||
-   // Constants -----------------------------------------------------------------||
-   // ---------------------------------------------------------------------------||
-
-   /*
-    * Roles of callers to the system
-    */
-
-   /**
-    * Role denoting the user is a school administrator
-    */
-   String ADMIN = "Administrator";
-
-   /**
-    * Role denoting the user is a student
-    */
-   String STUDENT = "Student";
-
-   /**
-    * Role denoting the user is a janitor
-    */
-   String JANITOR = "Janitor";
-
-}

Modified: projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/api/SchoolClosedException.java
===================================================================
--- projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/api/SchoolClosedException.java	2010-05-01 11:24:40 UTC (rev 104387)
+++ projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/api/SchoolClosedException.java	2010-05-01 20:23:56 UTC (rev 104388)
@@ -24,6 +24,8 @@
 import javax.ejb.ApplicationException;
 import javax.ejb.EJBAccessException;
 
+import org.jboss.ejb3.examples.ch09.secureschool.impl.Roles;
+
 /**
  * Thrown when a user in role other than {@link Roles#ADMIN}
  * attempts to open the front door to school while it's closed

Modified: projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/api/SecureSchoolLocalBusiness.java
===================================================================
--- projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/api/SecureSchoolLocalBusiness.java	2010-05-01 11:24:40 UTC (rev 104387)
+++ projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/api/SecureSchoolLocalBusiness.java	2010-05-01 20:23:56 UTC (rev 104388)
@@ -21,6 +21,8 @@
  */
 package org.jboss.ejb3.examples.ch09.secureschool.api;
 
+import org.jboss.ejb3.examples.ch09.secureschool.impl.Roles;
+
 /**
  * Represents a school holding doors which may be 
  * opened by various users.  Using the EJB Security model,
@@ -56,8 +58,13 @@
     * Opens the front door.  While school is open, 
     * any authenticated user may open the door, else 
     * only the {@link Roles#ADMIN} may open.
+    * 
+    * @throws SchoolClosedException If the current user
+    * is not in {@link Roles#ADMIN} and is attempting to open 
+    * the door while {@link SecureSchoolLocalBusiness#isOpen()}
+    * is false.
     */
-   void openFrontDoor();
+   void openFrontDoor() throws SchoolClosedException;
 
    /**
     * Opens the service door. Users in {@link Roles#STUDENT}

Added: projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/impl/FireDepartmentBean.java
===================================================================
--- projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/impl/FireDepartmentBean.java	                        (rev 0)
+++ projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/impl/FireDepartmentBean.java	2010-05-01 20:23:56 UTC (rev 104388)
@@ -0,0 +1,82 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, 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.ejb3.examples.ch09.secureschool.impl;
+
+import java.util.logging.Logger;
+
+import javax.annotation.security.PermitAll;
+import javax.annotation.security.RunAs;
+import javax.ejb.EJB;
+import javax.ejb.Singleton;
+
+import org.jboss.ejb3.examples.ch09.secureschool.api.FireDepartmentLocalBusiness;
+import org.jboss.ejb3.examples.ch09.secureschool.api.SecureSchoolLocalBusiness;
+
+/**
+ * Bean implementation class of the fire department.
+ * Closes the local school in case of emergency.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at Singleton
+ at RunAs(Roles.ADMIN)
+ at PermitAll
+// Implicit, but included here to show access policy
+public class FireDepartmentBean implements FireDepartmentLocalBusiness
+{
+
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Logger
+    */
+   private static final Logger log = Logger.getLogger(FireDepartmentBean.class.getName());
+
+   //-------------------------------------------------------------------------------------||
+   // Instance Members -------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * School to close in case of emergency
+    */
+   @EJB
+   private SecureSchoolLocalBusiness school;
+
+   //-------------------------------------------------------------------------------------||
+   // Required Implementations -----------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * {@inheritDoc}
+    * @see org.jboss.ejb3.examples.ch09.secureschool.api.FireDepartmentLocalBusiness#declareEmergency()
+    */
+   @Override
+   public void declareEmergency()
+   {
+      log.info("Dispatching emergency support from the Fire Department, closing local school");
+      school.close();
+   }
+
+}

Copied: projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/impl/Roles.java (from rev 104384, projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/api/Roles.java)
===================================================================
--- projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/impl/Roles.java	                        (rev 0)
+++ projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/impl/Roles.java	2010-05-01 20:23:56 UTC (rev 104388)
@@ -0,0 +1,57 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, 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.ejb3.examples.ch09.secureschool.impl;
+
+/**
+ * Holds the list of roles with which users of the school 
+ * may be affiliated.  EJB Security is role-based, so this
+ * is how we'll determine access.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public interface Roles
+{
+   // ---------------------------------------------------------------------------||
+   // Constants -----------------------------------------------------------------||
+   // ---------------------------------------------------------------------------||
+
+   /*
+    * Roles of callers to the system
+    */
+
+   /**
+    * Role denoting the user is a school administrator
+    */
+   String ADMIN = "Administrator";
+
+   /**
+    * Role denoting the user is a student
+    */
+   String STUDENT = "Student";
+
+   /**
+    * Role denoting the user is a janitor
+    */
+   String JANITOR = "Janitor";
+
+}

Modified: projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/impl/SecureSchoolBean.java
===================================================================
--- projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/impl/SecureSchoolBean.java	2010-05-01 11:24:40 UTC (rev 104387)
+++ projects/ejb-book/trunk/ch09-secureschool/src/main/java/org/jboss/ejb3/examples/ch09/secureschool/impl/SecureSchoolBean.java	2010-05-01 20:23:56 UTC (rev 104388)
@@ -33,7 +33,6 @@
 import javax.ejb.Singleton;
 import javax.ejb.Startup;
 
-import org.jboss.ejb3.examples.ch09.secureschool.api.Roles;
 import org.jboss.ejb3.examples.ch09.secureschool.api.SchoolClosedException;
 import org.jboss.ejb3.examples.ch09.secureschool.api.SecureSchoolLocalBusiness;
 
@@ -142,7 +141,6 @@
    public void close()
    {
       this.open = false;
-
    }
 
    /**

Modified: projects/ejb-book/trunk/ch09-secureschool/src/test/java/org/jboss/ejb3/examples/ch09/secureschool/SecureSchoolIntegrationTest.java
===================================================================
--- projects/ejb-book/trunk/ch09-secureschool/src/test/java/org/jboss/ejb3/examples/ch09/secureschool/SecureSchoolIntegrationTest.java	2010-05-01 11:24:40 UTC (rev 104387)
+++ projects/ejb-book/trunk/ch09-secureschool/src/test/java/org/jboss/ejb3/examples/ch09/secureschool/SecureSchoolIntegrationTest.java	2010-05-01 20:23:56 UTC (rev 104388)
@@ -40,6 +40,7 @@
 import org.jboss.arquillian.api.Deployment;
 import org.jboss.arquillian.junit.Arquillian;
 import org.jboss.arquillian.prototyping.context.api.ArquillianContext;
+import org.jboss.ejb3.examples.ch09.secureschool.api.FireDepartmentLocalBusiness;
 import org.jboss.ejb3.examples.ch09.secureschool.api.SchoolClosedException;
 import org.jboss.ejb3.examples.ch09.secureschool.api.SecureSchoolLocalBusiness;
 import org.jboss.ejb3.examples.ch09.secureschool.impl.SecureSchoolBean;
@@ -135,8 +136,16 @@
     * login properties to inject the proxy into this target.
     */
    @EJB
-   private SecureSchoolLocalBusiness unauthenticatedEjbReference;
+   private SecureSchoolLocalBusiness unauthenticatedSchool;
 
+   /**
+    * Reference to the fire department from an unauthenticated user.
+    * If we use this EJB to declare an emergency, anyone may close
+    * the school.
+    */
+   @EJB
+   private FireDepartmentLocalBusiness fireDepartment;
+
    //-------------------------------------------------------------------------------------||
    // Tests ------------------------------------------------------------------------------||
    //-------------------------------------------------------------------------------------||
@@ -149,7 +158,7 @@
    {
 
       // Try to open the front door before we've authenticated; should fail
-      unauthenticatedEjbReference.openFrontDoor();
+      unauthenticatedSchool.openFrontDoor();
    }
 
    /**
@@ -314,7 +323,7 @@
    {
 
       // See if school is open
-      Assert.assertTrue("Unauthenticated user should see that school is open", unauthenticatedEjbReference.isOpen());
+      Assert.assertTrue("Unauthenticated user should see that school is open", unauthenticatedSchool.isOpen());
    }
 
    /**
@@ -407,6 +416,57 @@
       }
    }
 
+   /**
+    * Ensures that any unauthenticated user can declare an emergency, hence closing the school
+    */
+   @Test
+   public void anyoneCanDeclareEmergencyAndCloseSchool() throws NamingException
+   {
+
+      // First check that school's open
+      Assert.assertTrue("School should be open to start the test", unauthenticatedSchool.isOpen());
+
+      // Ensure we can't close the school directly (we don't have access)
+      boolean gotAccessException = false;
+      try
+      {
+         unauthenticatedSchool.close();
+      }
+      catch (final EJBAccessException e)
+      {
+         // Expected
+         log.info("We can't close the school on our own, make an emergency");
+         gotAccessException = true;
+      }
+      Assert.assertTrue("We shouldn't be able to close school directly", gotAccessException);
+
+      // Now declare an emergency via the fire department
+      fireDepartment.declareEmergency();
+
+      // The school should now be closed, even though the don't have rights to do that directly on our own.
+      Assert.assertFalse("School should be closed after emergency was declared", unauthenticatedSchool.isOpen());
+
+      // Reset the school to open
+      // Cleanup and open the school for other tests
+      final Context context = this.login(USER_NAME_ADMIN, PASSWORD_ADMIN);
+      try
+      {
+         final SecureSchoolLocalBusiness school = this.getEjb(context);
+
+         // Reset the school to open for subsequent tests
+         school.open();
+
+         // Test
+         Assert.assertTrue("School should now be open", school.isOpen());
+      }
+      finally
+      {
+         // Clean up, closing the context to log out
+         context.close();
+      }
+
+   }
+
    //-------------------------------------------------------------------------------------||
    // Internal Helper Methods ------------------------------------------------------------||
    //-------------------------------------------------------------------------------------||




More information about the jboss-cvs-commits mailing list