Author: chris.laprun(a)jboss.com
Date: 2010-10-14 15:51:38 -0400 (Thu, 14 Oct 2010)
New Revision: 4670
Added:
components/pc/trunk/api/src/test/java/org/gatein/pc/api/cache/
components/pc/trunk/api/src/test/java/org/gatein/pc/api/cache/CacheLevelTestCase.java
Modified:
components/pc/trunk/api/src/main/java/org/gatein/pc/api/cache/CacheLevel.java
components/pc/trunk/controller/src/main/java/org/gatein/pc/controller/impl/ControllerRequestFactory.java
components/pc/trunk/portlet/src/main/java/org/gatein/pc/portlet/impl/jsr168/api/ResourceURLImpl.java
Log:
- GTNPC-35: Changed CacheLevel into a class instead of an enumeration so more values than
standard ones can be used.
Modified: components/pc/trunk/api/src/main/java/org/gatein/pc/api/cache/CacheLevel.java
===================================================================
---
components/pc/trunk/api/src/main/java/org/gatein/pc/api/cache/CacheLevel.java 2010-10-14
12:39:34 UTC (rev 4669)
+++
components/pc/trunk/api/src/main/java/org/gatein/pc/api/cache/CacheLevel.java 2010-10-14
19:51:38 UTC (rev 4670)
@@ -22,13 +22,101 @@
******************************************************************************/
package org.gatein.pc.api.cache;
+import org.gatein.common.util.ParameterValidation;
+
+import java.io.Serializable;
+
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 630 $
*/
-public enum CacheLevel
+public final class CacheLevel implements Serializable
{
+ public static final CacheLevel FULL = new CacheLevel("FULL");
+ public static final CacheLevel PORTLET = new CacheLevel("PORTLET");
+ public static final CacheLevel PAGE = new CacheLevel("PAGE");
- FULL, PORTLET, PAGE
+ private static final long serialVersionUID = -7020875805659724988L;
+ private final String name;
+
+ private CacheLevel(String name)
+ {
+ ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(name, "CacheLevel
name", null);
+ this.name = name;
+ }
+
+ public final String name()
+ {
+ return name;
+ }
+
+ private Object readResolve()
+ {
+ CacheLevel standardCacheLevel = isStandardCacheLevel(name);
+ if (standardCacheLevel != null)
+ {
+ return standardCacheLevel;
+ }
+ else
+ {
+ return this;
+ }
+ }
+
+ @Override
+ public boolean equals(Object o)
+ {
+ if (this == o)
+ {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass())
+ {
+ return false;
+ }
+
+ CacheLevel that = (CacheLevel)o;
+
+ return !(name == null ? that.name != null : !name.equals(that.name));
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return name != null ? name.hashCode() : 0;
+ }
+
+ public static CacheLevel create(String name)
+ {
+ CacheLevel standardCacheLevel = isStandardCacheLevel(name);
+ if (standardCacheLevel != null)
+ {
+ return standardCacheLevel;
+ }
+ else
+ {
+ return new CacheLevel(name);
+ }
+ }
+
+ private static CacheLevel isStandardCacheLevel(String name)
+ {
+ if (FULL.name.equals(name))
+ {
+ return FULL;
+ }
+ else if (PORTLET.name.equals(name))
+ {
+ return PORTLET;
+ }
+ else if (PAGE.name.equals(name))
+ {
+ return PAGE;
+ }
+ else
+ {
+ return null;
+ }
+ }
}
Added:
components/pc/trunk/api/src/test/java/org/gatein/pc/api/cache/CacheLevelTestCase.java
===================================================================
--- components/pc/trunk/api/src/test/java/org/gatein/pc/api/cache/CacheLevelTestCase.java
(rev 0)
+++
components/pc/trunk/api/src/test/java/org/gatein/pc/api/cache/CacheLevelTestCase.java 2010-10-14
19:51:38 UTC (rev 4670)
@@ -0,0 +1,102 @@
+/*
+* JBoss, a division of Red Hat
+* Copyright 2008, Red Hat Middleware, LLC, and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.gatein.pc.api.cache;
+
+import junit.framework.TestCase;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public class CacheLevelTestCase extends TestCase
+{
+ public void testCacheLevelFactory()
+ {
+ CacheLevel cacheLevel = CacheLevel.create("FULL");
+ assertTrue(CacheLevel.FULL == cacheLevel);
+ assertEquals(CacheLevel.FULL, cacheLevel);
+ assertEquals(CacheLevel.FULL.name(), cacheLevel.name());
+
+ cacheLevel = CacheLevel.create("PORTLET");
+ assertTrue(CacheLevel.PORTLET == cacheLevel);
+ assertEquals(CacheLevel.PORTLET, cacheLevel);
+ assertEquals(CacheLevel.PORTLET.name(), cacheLevel.name());
+
+ cacheLevel = CacheLevel.create("PAGE");
+ assertTrue(CacheLevel.PAGE == cacheLevel);
+ assertEquals(CacheLevel.PAGE, cacheLevel);
+ assertEquals(CacheLevel.PAGE.name(), cacheLevel.name());
+
+ try
+ {
+ CacheLevel.create(null);
+ fail("Shouldn't allow creating a CacheLevel with null name!");
+ }
+ catch (Exception e)
+ {
+ // expected
+ }
+
+ cacheLevel = CacheLevel.create("foo");
+ assertNotNull(cacheLevel);
+ assertEquals("foo", cacheLevel.name());
+ assertEquals(cacheLevel, CacheLevel.create("foo"));
+ }
+
+ public void testSerialization() throws IOException, ClassNotFoundException
+ {
+ Object read = serializeCacheLevel(CacheLevel.FULL);
+
+ assertTrue(CacheLevel.FULL == read);
+
+ CacheLevel foo = CacheLevel.create("foo");
+ read = serializeCacheLevel(foo);
+
+ assertEquals(foo, read);
+ }
+
+ private Object serializeCacheLevel(final CacheLevel cacheLevel)
+ throws IOException, ClassNotFoundException
+ {
+ File tempFile = File.createTempFile("foo", "tmp");
+ tempFile.deleteOnExit();
+
+ FileOutputStream fos = new FileOutputStream(tempFile);
+ ObjectOutputStream oos = new ObjectOutputStream(fos);
+ oos.writeObject(cacheLevel);
+ oos.close();
+
+ FileInputStream fis = new FileInputStream(tempFile);
+ ObjectInputStream ois = new ObjectInputStream(fis);
+ Object read = ois.readObject();
+ ois.close();
+ return read;
+ }
+}
Modified:
components/pc/trunk/controller/src/main/java/org/gatein/pc/controller/impl/ControllerRequestFactory.java
===================================================================
---
components/pc/trunk/controller/src/main/java/org/gatein/pc/controller/impl/ControllerRequestFactory.java 2010-10-14
12:39:34 UTC (rev 4669)
+++
components/pc/trunk/controller/src/main/java/org/gatein/pc/controller/impl/ControllerRequestFactory.java 2010-10-14
19:51:38 UTC (rev 4670)
@@ -149,24 +149,26 @@
String resourceId =
queryParameters.get(ControllerRequestParameterNames.RESOURCE_ID);
//
- CacheLevel resourceCacheLevel =
CacheLevel.valueOf(queryParameters.get(ControllerRequestParameterNames.RESOURCE_CACHEABILITY));
+ CacheLevel resourceCacheLevel =
CacheLevel.create(queryParameters.get(ControllerRequestParameterNames.RESOURCE_CACHEABILITY));
//
PortletResourceRequest.Scope scope;
- switch (resourceCacheLevel)
+ if (CacheLevel.FULL.equals(resourceCacheLevel))
{
- case FULL:
- scope = new PortletResourceRequest.FullScope();
- break;
- case PORTLET:
- scope = new PortletResourceRequest.PortletScope(windowNavigationalState);
- break;
- case PAGE:
- scope = new PortletResourceRequest.PageScope(windowNavigationalState,
pageNavigationalState);
- break;
- default:
- throw new AssertionError();
+ scope = new PortletResourceRequest.FullScope();
}
+ else if (CacheLevel.PORTLET.equals(resourceCacheLevel))
+ {
+ scope = new PortletResourceRequest.PortletScope(windowNavigationalState);
+ }
+ else if (CacheLevel.PAGE.equals(resourceCacheLevel))
+ {
+ scope = new PortletResourceRequest.PageScope(windowNavigationalState,
pageNavigationalState);
+ }
+ else
+ {
+ throw new AssertionError();
+ }
//
return new PortletResourceRequest(
Modified:
components/pc/trunk/portlet/src/main/java/org/gatein/pc/portlet/impl/jsr168/api/ResourceURLImpl.java
===================================================================
---
components/pc/trunk/portlet/src/main/java/org/gatein/pc/portlet/impl/jsr168/api/ResourceURLImpl.java 2010-10-14
12:39:34 UTC (rev 4669)
+++
components/pc/trunk/portlet/src/main/java/org/gatein/pc/portlet/impl/jsr168/api/ResourceURLImpl.java 2010-10-14
19:51:38 UTC (rev 4670)
@@ -124,21 +124,16 @@
}
else
{
- switch (url.parentCacheLevel)
+ if (CacheLevel.FULL == url.parentCacheLevel && cacheLevel !=
CacheLevel.FULL)
{
- case FULL:
- if (cacheLevel != CacheLevel.FULL)
- {
- throw new IllegalStateException();
- }
- break;
- case PORTLET:
- if (cacheLevel == CacheLevel.PAGE)
- {
- throw new IllegalStateException();
- }
- break;
+ throw new IllegalStateException();
}
+
+ if (CacheLevel.PORTLET == url.parentCacheLevel && cacheLevel ==
CacheLevel.PAGE)
+ {
+ throw new IllegalStateException();
+ }
+
url.cacheLevel = cacheLevel;
}
}