[gatein-commits] gatein SVN: r1666 - in portal/trunk/component/resources/src: main/java/org/exoplatform/services/resources/impl and 1 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Fri Feb 12 20:41:02 EST 2010


Author: julien_viet
Date: 2010-02-12 20:41:02 -0500 (Fri, 12 Feb 2010)
New Revision: 1666

Modified:
   portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/ExoResourceBundle.java
   portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/ResourceBundleData.java
   portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/impl/SimpleResourceBundleService.java
   portal/trunk/component/resources/src/test/java/org/exoplatform/services/resources/TestExoResourceBundle.java
Log:
move the code from ExoResourceBundle to ResourceBundleData as the RBD is cached in order to avoid the same computation to happen again and again and create object and consume CPU cycle contributing to global warmup


Modified: portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/ExoResourceBundle.java
===================================================================
--- portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/ExoResourceBundle.java	2010-02-13 00:59:26 UTC (rev 1665)
+++ portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/ExoResourceBundle.java	2010-02-13 01:41:02 UTC (rev 1666)
@@ -40,36 +40,26 @@
 public class ExoResourceBundle extends ListResourceBundle implements Serializable
 {
 
-   private static Pattern LINE_SEPARATOR = Pattern.compile("[\\r]?\\n");
+   /** . */
+   private final ResourceBundleData data;
 
-   private static Pattern UNICODE_CHARACTER = Pattern.compile("\\\\u[\\p{XDigit}]{4}+");
-
-   private Object[][] contents;
-
    public ExoResourceBundle(String data)
    {
-      String[] tokens = LINE_SEPARATOR.split(data);
-      List<String[]> properties = new ArrayList<String[]>();
-      for (String token : tokens)
+      this.data = new ResourceBundleData(data);
+   }
+
+   public ExoResourceBundle(ResourceBundleData data)
+   {
+      if (data == null)
       {
-         int idx = token.indexOf('=');
-         if (idx < 0 || idx >= token.length() - 1)
-         {
-            continue;
-         }
-         String key = token.substring(0, idx);
-         if (key.trim().startsWith("#"))
-         {
-            continue;
-         }
-         String value = convert(token.substring(idx + 1, token.length()));
-         properties.add(new String[]{key, value});
+         throw new NullPointerException();
       }
-      String[][] aProperties = new String[properties.size()][2];
-      contents = (String[][])properties.toArray(aProperties);
+
+      //
+      this.data = data;
    }
 
-   public ExoResourceBundle(String data, ResourceBundle parent)
+   public ExoResourceBundle(ResourceBundleData data, ResourceBundle parent)
    {
       this(data);
       setParent(parent);
@@ -77,7 +67,7 @@
 
    public Object[][] getContents()
    {
-      return contents;
+      return data.contents;
    }
 
    public void putAll(Map<? super Object, ? super Object> map)
@@ -93,69 +83,4 @@
       }
    }
 
-   static String convert(String content)
-   {
-      Matcher matcher = UNICODE_CHARACTER.matcher(content);
-      StringBuilder buffer = new StringBuilder(content.length());
-      int start = 0;
-      while (matcher.find(start))
-      {
-         buffer.append(content.substring(start, matcher.start()));
-         buffer.append(unicode2Char(matcher.group()));
-         start = matcher.end();
-      }
-      if (start >= 0 && start < content.length())
-      {
-         buffer.append(content.substring(start));
-      }
-      return buffer.toString();
-   }
-
-   static char unicode2Char(String unicodeChar)
-   {
-      int value = 0;
-      char aChar;
-      for (int i = 0; i < 4; i++)
-      {
-         aChar = unicodeChar.charAt(i + 2);
-         switch (aChar)
-         {
-            case '0' :
-            case '1' :
-            case '2' :
-            case '3' :
-            case '4' :
-            case '5' :
-            case '6' :
-            case '7' :
-            case '8' :
-            case '9' : {
-               value = (value << 4) + aChar - '0';
-               break;
-            }
-            case 'a' :
-            case 'b' :
-            case 'c' :
-            case 'd' :
-            case 'e' :
-            case 'f' : {
-               value = (value << 4) + 10 + aChar - 'a';
-               break;
-            }
-            case 'A' :
-            case 'B' :
-            case 'C' :
-            case 'D' :
-            case 'E' :
-            case 'F' : {
-               value = (value << 4) + 10 + aChar - 'A';
-               break;
-            }
-            default : {
-               throw new IllegalArgumentException("Malformed \\uxxxx encoding.");
-            }
-         }
-      }
-      return (char)value;
-   }
 }

Modified: portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/ResourceBundleData.java
===================================================================
--- portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/ResourceBundleData.java	2010-02-13 00:59:26 UTC (rev 1665)
+++ portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/ResourceBundleData.java	2010-02-13 01:41:02 UTC (rev 1666)
@@ -20,6 +20,10 @@
 package org.exoplatform.services.resources;
 
 import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * Created by The eXo Platform SAS . Author : Tuan Nguyen
@@ -29,25 +33,131 @@
 public class ResourceBundleData extends ResourceBundleDescription implements Serializable
 {
 
-   private String data_;
+   /** . */
+   private static Pattern LINE_SEPARATOR = Pattern.compile("[\\r]?\\n");
 
-   public ResourceBundleData()
+   /** . */
+   private static Pattern UNICODE_CHARACTER = Pattern.compile("\\\\u[\\p{XDigit}]{4}+");
+
+   /** . */
+   private String data;
+
+   /** . */
+   Object[][] contents;
+
+   public ResourceBundleData(String data)
    {
+      setData(data);
       setResourceType("-");
       setLanguage(DEFAULT_LANGUAGE);
    }
 
-   /**
-    * @hibernate.property length="65535"
-    *                     type="org.exoplatform.services.database.impl.TextClobType"
-    **/
+   public ResourceBundleData()
+   {
+      this(null);
+   }
+
    public String getData()
    {
-      return data_;
+      return data;
    }
 
    public void setData(String data)
    {
-      data_ = data;
+      this.data = data;
+
+      if (data != null)
+      {
+         String[] tokens = LINE_SEPARATOR.split(data);
+         List<String[]> properties = new ArrayList<String[]>();
+         for (String token : tokens)
+         {
+            int idx = token.indexOf('=');
+            if (idx < 0 || idx >= token.length() - 1)
+            {
+               continue;
+            }
+            String key = token.substring(0, idx);
+            if (key.trim().startsWith("#"))
+            {
+               continue;
+            }
+            String value = convert(token.substring(idx + 1, token.length()));
+            properties.add(new String[]{key, value});
+         }
+         String[][] aProperties = new String[properties.size()][2];
+         contents = properties.toArray(aProperties);
+      }
+      else
+      {
+         contents = new Object[0][];
+      }
+
    }
+
+   static String convert(String content)
+   {
+      Matcher matcher = UNICODE_CHARACTER.matcher(content);
+      StringBuilder buffer = new StringBuilder(content.length());
+      int start = 0;
+      while (matcher.find(start))
+      {
+         buffer.append(content.substring(start, matcher.start()));
+         buffer.append(unicode2Char(matcher.group()));
+         start = matcher.end();
+      }
+      if (start >= 0 && start < content.length())
+      {
+         buffer.append(content.substring(start));
+      }
+      return buffer.toString();
+   }
+
+   static char unicode2Char(String unicodeChar)
+   {
+      int value = 0;
+      char aChar;
+      for (int i = 0; i < 4; i++)
+      {
+         aChar = unicodeChar.charAt(i + 2);
+         switch (aChar)
+         {
+            case '0' :
+            case '1' :
+            case '2' :
+            case '3' :
+            case '4' :
+            case '5' :
+            case '6' :
+            case '7' :
+            case '8' :
+            case '9' : {
+               value = (value << 4) + aChar - '0';
+               break;
+            }
+            case 'a' :
+            case 'b' :
+            case 'c' :
+            case 'd' :
+            case 'e' :
+            case 'f' : {
+               value = (value << 4) + 10 + aChar - 'a';
+               break;
+            }
+            case 'A' :
+            case 'B' :
+            case 'C' :
+            case 'D' :
+            case 'E' :
+            case 'F' : {
+               value = (value << 4) + 10 + aChar - 'A';
+               break;
+            }
+            default : {
+               throw new IllegalArgumentException("Malformed \\uxxxx encoding.");
+            }
+         }
+      }
+      return (char)value;
+   }
 }

Modified: portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/impl/SimpleResourceBundleService.java
===================================================================
--- portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/impl/SimpleResourceBundleService.java	2010-02-13 00:59:26 UTC (rev 1665)
+++ portal/trunk/component/resources/src/main/java/org/exoplatform/services/resources/impl/SimpleResourceBundleService.java	2010-02-13 01:41:02 UTC (rev 1666)
@@ -30,7 +30,6 @@
 import org.exoplatform.services.resources.LocaleConfigService;
 import org.exoplatform.services.resources.Query;
 import org.exoplatform.services.resources.ResourceBundleData;
-import org.exoplatform.services.resources.impl.BaseResourceBundleService;
 
 import java.util.ArrayList;
 import java.util.Collections;
@@ -141,6 +140,6 @@
       {
          return null;
       }
-      return new MapResourceBundle(new ExoResourceBundle(data.getData(), parent), locale);
+      return new MapResourceBundle(new ExoResourceBundle(data, parent), locale);
    }
 }
\ No newline at end of file

Modified: portal/trunk/component/resources/src/test/java/org/exoplatform/services/resources/TestExoResourceBundle.java
===================================================================
--- portal/trunk/component/resources/src/test/java/org/exoplatform/services/resources/TestExoResourceBundle.java	2010-02-13 00:59:26 UTC (rev 1665)
+++ portal/trunk/component/resources/src/test/java/org/exoplatform/services/resources/TestExoResourceBundle.java	2010-02-13 01:41:02 UTC (rev 1666)
@@ -65,20 +65,20 @@
          {
             value = "0" + value;
          }
-         assertEquals((char)i, ExoResourceBundle.unicode2Char("\\u" + value));
+         assertEquals((char)i, ResourceBundleData.unicode2Char("\\u" + value));
       }
    }
 
    public void testConvert()
    {
-      assertEquals("Normal Value", ExoResourceBundle.convert("Normal Value"));
-      assertEquals("\u00E9\u00E7\u00E0\u00F9\u0194\u0BF5", ExoResourceBundle
+      assertEquals("Normal Value", ResourceBundleData.convert("Normal Value"));
+      assertEquals("\u00E9\u00E7\u00E0\u00F9\u0194\u0BF5", ResourceBundleData
          .convert("\\u00E9\\u00E7\\u00E0\\u00F9\\u0194\\u0BF5"));
-      assertEquals("before \u00E9\u00E7\u00E0\u00F9\u0194\u0BF5", ExoResourceBundle
+      assertEquals("before \u00E9\u00E7\u00E0\u00F9\u0194\u0BF5", ResourceBundleData
          .convert("before \\u00E9\\u00E7\\u00E0\\u00F9\\u0194\\u0BF5"));
-      assertEquals("\u00E9\u00E7\u00E0\u00F9\u0194\u0BF5 after", ExoResourceBundle
+      assertEquals("\u00E9\u00E7\u00E0\u00F9\u0194\u0BF5 after", ResourceBundleData
          .convert("\\u00E9\\u00E7\\u00E0\\u00F9\\u0194\\u0BF5 after"));
-      assertEquals("before \u00E9\u00E7\u00E0 between \u00F9\u0194\u0BF5 after", ExoResourceBundle
+      assertEquals("before \u00E9\u00E7\u00E0 between \u00F9\u0194\u0BF5 after", ResourceBundleData
          .convert("before \\u00E9\\u00E7\\u00E0 between \\u00F9\\u0194\\u0BF5 after"));
    }
 }



More information about the gatein-commits mailing list