Author: woonsan
Date: Tue Mar 2 19:42:33 2010
New Revision: 918159
URL: http://svn.apache.org/viewvc?rev=918159&view=rev
Log:
JS2-1094: Adding reloadable property resource bundle with test case.
Added:
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ReloadablePropertyResourceBundle.java
(with props)
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/java/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle.java
(with props)
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/resources/
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/resources/org/
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/resources/org/apache/
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/resources/org/apache/jetspeed/
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/resources/org/apache/jetspeed/util/
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/resources/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle_en_US.properties
(with props)
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/resources/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle_modified_en_US.properties
(with props)
Added:
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ReloadablePropertyResourceBundle.java
URL:
http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ReloadablePropertyResourceBundle.java?rev=918159&view=auto
==============================================================================
---
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ReloadablePropertyResourceBundle.java
(added)
+++
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ReloadablePropertyResourceBundle.java
Tue Mar 2 19:42:33 2010
@@ -0,0 +1,168 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.util;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Enumeration;
+import java.util.Locale;
+import java.util.Properties;
+import java.util.PropertyResourceBundle;
+import java.util.ResourceBundle;
+
+/**
+ * ReloadablePropertyResourceBundle
+ *
+ * @version $Id$
+ */
+public class ReloadablePropertyResourceBundle extends ResourceBundle
+{
+ protected PropertyResourceBundle internalBundle;
+ protected String baseName;
+ protected Properties overridingProps;
+
+ public ReloadablePropertyResourceBundle(PropertyResourceBundle
internalBundle, String baseName)
+ {
+ this.internalBundle = internalBundle;
+ setParent(internalBundle);
+ this.baseName = baseName;
+ }
+
+ /**
+ * Reloads resource bundle from the properties file which can be decided
by the baseName.
+ * @param loader
+ * @throws IOException
+ */
+ public void reload(ClassLoader loader) throws IOException
+ {
+ String bundleName = baseName;
+ Locale locale = getLocale();
+
+ if (locale != null)
+ {
+ String localeSuffix = locale.toString();
+
+ if (localeSuffix.length() > 0)
+ {
+ bundleName += "_" + localeSuffix;
+ }
+ else if (locale.getVariant().length() > 0)
+ {
+ bundleName += "___" + locale.getVariant();
+ }
+ }
+
+ String resPath = bundleName.replace('.', '/') + ".properties";
+
+ Properties props = new Properties();
+ InputStream is = null;
+ BufferedInputStream bis = null;
+
+ try
+ {
+ is = loader.getResourceAsStream(resPath);
+ bis = new BufferedInputStream(is);
+ props.load(bis);
+
+ if (!props.isEmpty())
+ {
+ overridingProps = props;
+ }
+ }
+ finally
+ {
+ if (bis != null)
+ {
+ try
+ {
+ bis.close();
+ }
+ catch (Exception ignore)
+ {
+ }
+ }
+
+ if (is != null)
+ {
+ try
+ {
+ is.close();
+ }
+ catch (Exception ignore)
+ {
+ }
+ }
+ }
+ }
+
+ /**
+ * Resets overriding properties.
+ */
+ public void reset()
+ {
+ overridingProps = null;
+ }
+
+ @Override
+ public Locale getLocale()
+ {
+ return internalBundle.getLocale();
+ }
+
+ @Override
+ public Enumeration<String> getKeys()
+ {
+ if (overridingProps != null)
+ {
+ return new TypedEnumeration<String>(overridingProps.keys());
+ }
+
+ return internalBundle.getKeys();
+ }
+
+ @Override
+ protected Object handleGetObject(String key)
+ {
+ if (overridingProps != null)
+ {
+ return overridingProps.getProperty(key);
+ }
+
+ return null;
+ }
+
+ private class TypedEnumeration<T> implements Enumeration<T>
+ {
+ private Enumeration<Object> internalEnum;
+
+ private TypedEnumeration(Enumeration<Object> internalEnum)
+ {
+ this.internalEnum = internalEnum;
+ }
+
+ public boolean hasMoreElements()
+ {
+ return internalEnum.hasMoreElements();
+ }
+
+ public T nextElement()
+ {
+ return (T) internalEnum.nextElement();
+ }
+ }
+}
Propchange:
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ReloadablePropertyResourceBundle.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ReloadablePropertyResourceBundle.java
------------------------------------------------------------------------------
svn:keywords = Id
Propchange:
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/main/java/org/apache/jetspeed/util/ReloadablePropertyResourceBundle.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/java/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle.java
URL:
http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/java/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle.java?rev=918159&view=auto
==============================================================================
---
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/java/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle.java
(added)
+++
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/java/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle.java
Tue Mar 2 19:42:33 2010
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.util;
+
+import java.io.IOException;
+import java.util.Locale;
+import java.util.PropertyResourceBundle;
+import java.util.ResourceBundle;
+
+import junit.framework.TestCase;
+
+/**
+ * TestReloadablePropertyResourceBundle
+ *
+ * @version $Id$
+ */
+public class TestReloadablePropertyResourceBundle extends TestCase
+{
+ private ReloadablePropertyResourceBundle bundle;
+
+ @Override
+ public void setUp()
+ {
+ String baseName = TestReloadablePropertyResourceBundle.class.getName();
+ ResourceBundle internalBundle = ResourceBundle.getBundle(baseName, new
Locale("en_US"));
+ assertTrue(internalBundle instanceof PropertyResourceBundle);
+ bundle = new ReloadablePropertyResourceBundle((PropertyResourceBundle)
internalBundle, baseName);
+ }
+
+ public void testNormalResourceBundle()
+ {
+ assertEquals("Hello, World!", bundle.getString("greeting.message"));
+ }
+
+ public void testResourceBundle() throws IOException
+ {
+ bundle.baseName = TestReloadablePropertyResourceBundle.class.getName()
+ "_modified";
+ bundle.reload(getClass().getClassLoader());
+ assertEquals("Hello, World! (2)",
bundle.getString("greeting.message"));
+ bundle.reset();
+ assertEquals("Hello, World!", bundle.getString("greeting.message"));
+ bundle.reload(getClass().getClassLoader());
+ assertEquals("Hello, World! (2)",
bundle.getString("greeting.message"));
+ }
+}
Propchange:
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/java/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/java/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle.java
------------------------------------------------------------------------------
svn:keywords = Id
Propchange:
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/java/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/resources/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle_en_US.properties
URL:
http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/resources/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle_en_US.properties?rev=918159&view=auto
==============================================================================
---
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/resources/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle_en_US.properties
(added)
+++
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/resources/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle_en_US.properties
Tue Mar 2 19:42:33 2010
@@ -0,0 +1,16 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+greeting.message = Hello, World!
Propchange:
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/resources/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle_en_US.properties
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/resources/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle_en_US.properties
------------------------------------------------------------------------------
svn:keywords = Id
Propchange:
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/resources/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle_en_US.properties
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/resources/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle_modified_en_US.properties
URL:
http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/resources/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle_modified_en_US.properties?rev=918159&view=auto
==============================================================================
---
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/resources/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle_modified_en_US.properties
(added)
+++
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/resources/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle_modified_en_US.properties
Tue Mar 2 19:42:33 2010
@@ -0,0 +1,16 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+greeting.message = Hello, World! (2)
Propchange:
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/resources/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle_modified_en_US.properties
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/resources/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle_modified_en_US.properties
------------------------------------------------------------------------------
svn:keywords = Id
Propchange:
portals/jetspeed-2/portal/trunk/jetspeed-commons/src/test/resources/org/apache/jetspeed/util/TestReloadablePropertyResourceBundle_modified_en_US.properties
------------------------------------------------------------------------------
svn:mime-type = text/plain
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]