[exo-jcr-commits] exo-jcr SVN: r4346 - in kernel/trunk/exo.kernel.container/src: test/java/org/exoplatform/container and 1 other directory.

do-not-reply at jboss.org do-not-reply at jboss.org
Thu May 5 14:56:45 EDT 2011


Author: nfilotto
Date: 2011-05-05 14:56:45 -0400 (Thu, 05 May 2011)
New Revision: 4346

Modified:
   kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/container/ExoContainer.java
   kernel/trunk/exo.kernel.container/src/test/java/org/exoplatform/container/TestExoContainer.java
Log:
EXOJCR-1333: Provide a hasProfile method on ExoContainer

Modified: kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/container/ExoContainer.java
===================================================================
--- kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/container/ExoContainer.java	2011-05-05 12:14:47 UTC (rev 4345)
+++ kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/container/ExoContainer.java	2011-05-05 18:56:45 UTC (rev 4346)
@@ -55,7 +55,17 @@
    private static final long serialVersionUID = -8068506531004854036L;
 
    /**
-    * Returns an unmodifable set of profiles defined by the value returned by invoking
+    * The current list of profiles
+    */
+   private static volatile String PROFILES;
+   
+   /**
+    * The current set of profiles
+    */
+   private static Set<String> SET_PROFILES = Collections.unmodifiableSet(new HashSet<String>());
+   
+   /**
+    * Returns an unmodifiable set of profiles defined by the value returned by invoking
     * {@link PropertyManager#getProperty(String)} with the {@link org.exoplatform.commons.utils.PropertyManager#RUNTIME_PROFILES}
     * property.
     *
@@ -63,11 +73,40 @@
     */
    public static Set<String> getProfiles()
    {
-      //
+      String profiles = PropertyManager.getProperty(PropertyManager.RUNTIME_PROFILES);
+      if ((profiles == null && PROFILES != null) || (profiles != null && !profiles.equals(PROFILES)))
+      {
+         synchronized (ExoContainer.class)
+         {
+            if ((profiles == null && PROFILES != null) || (profiles != null && !profiles.equals(PROFILES)))
+            {
+               SET_PROFILES = getProfiles(profiles);
+               PROFILES = profiles;
+            }
+         }
+      }      
+ 
+      return SET_PROFILES;
+   }
+   
+   /**
+    * Indicates whether or not a given profile exists
+    * @param profileName the name of the profile to check
+    * @return <code>true</code> if the profile exists, <code>false</code> otherwise.
+    */
+   public static boolean hasProfile(String profileName)
+   {
+      return getProfiles().contains(profileName);
+   }
+   
+   /**
+    * Convert the list of profiles into a Set of String
+    */
+   private static Set<String> getProfiles(String profileList)
+   {
       Set<String> profiles = new HashSet<String>();
 
       // Obtain profile list by runtime properties
-      String profileList = PropertyManager.getProperty(PropertyManager.RUNTIME_PROFILES);
       if (profileList != null)
       {
          for (String profile : profileList.split(","))
@@ -77,9 +116,9 @@
       }
 
       //
-      return Collections.unmodifiableSet(profiles);
+      return Collections.unmodifiableSet(profiles);      
    }
-
+   
    static Log log = ExoLogger.getLogger("exo.kernel.container.ExoContainer");
 
    private Map<String, ComponentLifecyclePlugin> componentLifecylePlugin_ =

Modified: kernel/trunk/exo.kernel.container/src/test/java/org/exoplatform/container/TestExoContainer.java
===================================================================
--- kernel/trunk/exo.kernel.container/src/test/java/org/exoplatform/container/TestExoContainer.java	2011-05-05 12:14:47 UTC (rev 4345)
+++ kernel/trunk/exo.kernel.container/src/test/java/org/exoplatform/container/TestExoContainer.java	2011-05-05 18:56:45 UTC (rev 4346)
@@ -16,6 +16,7 @@
  */
 package org.exoplatform.container;
 
+import org.exoplatform.commons.utils.PropertyManager;
 import org.exoplatform.container.component.BaseComponentPlugin;
 import org.exoplatform.container.configuration.ConfigurationManager;
 import org.exoplatform.container.jmx.AbstractTestContainer;
@@ -58,6 +59,55 @@
       }
    }
 
+   public void testHasProfile()
+   {
+      String oldValue = PropertyManager.getProperty(PropertyManager.RUNTIME_PROFILES);
+      try
+      {
+         System.clearProperty(PropertyManager.RUNTIME_PROFILES);
+         PropertyManager.refresh();
+         assertFalse(ExoContainer.hasProfile(null));
+         assertFalse(ExoContainer.hasProfile("foo0"));
+         PropertyManager.setProperty(PropertyManager.RUNTIME_PROFILES, "foo1");
+         assertFalse(ExoContainer.hasProfile(null));
+         assertFalse(ExoContainer.hasProfile("foo0"));
+         assertTrue(ExoContainer.hasProfile("foo1"));
+         System.clearProperty(PropertyManager.RUNTIME_PROFILES);
+         PropertyManager.refresh();
+         assertFalse(ExoContainer.hasProfile("foo0"));
+         PropertyManager.setProperty(PropertyManager.RUNTIME_PROFILES, "foo1, foo2, foo3");
+         assertFalse(ExoContainer.hasProfile("foo0"));
+         assertTrue(ExoContainer.hasProfile("foo1"));
+         assertTrue(ExoContainer.hasProfile("foo2"));
+         assertTrue(ExoContainer.hasProfile("foo3"));
+         PropertyManager.setProperty(PropertyManager.RUNTIME_PROFILES, "  \tfoo   ");
+         assertFalse(ExoContainer.hasProfile("foo0"));
+         assertTrue(ExoContainer.hasProfile("foo"));
+         PropertyManager.setProperty(PropertyManager.RUNTIME_PROFILES, ",foo   ");
+         assertFalse(ExoContainer.hasProfile("foo0"));
+         assertTrue(ExoContainer.hasProfile("foo"));
+         PropertyManager.setProperty(PropertyManager.RUNTIME_PROFILES, "foo, bar, \t baz \t");
+         assertFalse(ExoContainer.hasProfile("foo0"));
+         assertTrue(ExoContainer.hasProfile("baz"));
+         PropertyManager.setProperty(PropertyManager.RUNTIME_PROFILES, "foo1, bar, \t baz1 \t");
+         assertFalse(ExoContainer.hasProfile("foo0"));
+         assertFalse(ExoContainer.hasProfile("baz"));
+         assertTrue(ExoContainer.hasProfile("bar"));
+      }
+      finally
+      {
+         if (oldValue == null)
+         {
+            System.clearProperty(PropertyManager.RUNTIME_PROFILES);
+            PropertyManager.refresh();
+         }
+         else
+         {
+            PropertyManager.setProperty(PropertyManager.RUNTIME_PROFILES, oldValue);
+         }         
+      }
+   }
+   
    public void testRemoveComponent() throws Exception
    {
       RootContainer container = RootContainer.getInstance();



More information about the exo-jcr-commits mailing list