Author: chris.laprun(a)jboss.com
Date: 2011-04-07 11:45:49 -0400 (Thu, 07 Apr 2011)
New Revision: 6175
Modified:
components/pc/trunk/api/src/main/java/org/gatein/pc/api/PortletContext.java
components/pc/trunk/api/src/test/java/org/gatein/pc/api/PortletContextTestCase.java
components/pc/trunk/portlet/src/main/java/org/gatein/pc/portlet/state/producer/ProducerPortletInvoker.java
Log:
- GTNPC-58: Added support for clones. Moved cloning constants to PortletContext so that
they can be used elsewhere.
Modified: components/pc/trunk/api/src/main/java/org/gatein/pc/api/PortletContext.java
===================================================================
--- components/pc/trunk/api/src/main/java/org/gatein/pc/api/PortletContext.java 2011-04-07
15:02:19 UTC (rev 6174)
+++ components/pc/trunk/api/src/main/java/org/gatein/pc/api/PortletContext.java 2011-04-07
15:45:49 UTC (rev 6175)
@@ -38,10 +38,10 @@
private static final String PREFIX = "/";
private static final char SEPARATOR = '.';
- /* TODO: remove from ProducerPortletInvoker so that we can use these constants in
GateIn
- public static final String CONSUMER_CLONE_ID = "_dumbvalue";
public static final String PRODUCER_CLONE_ID_PREFIX = "_";
- public final static PortletContext CONSUMER_CLONE =
PortletContext.createPortletContext(PortletInvoker.LOCAL_PORTLET_INVOKER_ID + SEPARATOR +
CONSUMER_CLONE_ID);*/
+ public static final String CONSUMER_CLONE_DUMMY_STATE_ID = "dumbvalue";
+ public static final String CONSUMER_CLONE_ID = PRODUCER_CLONE_ID_PREFIX +
CONSUMER_CLONE_DUMMY_STATE_ID;
+ public final static PortletContext LOCAL_CONSUMER_CLONE =
PortletContext.createPortletContext(PortletInvoker.LOCAL_PORTLET_INVOKER_ID + SEPARATOR +
CONSUMER_CLONE_ID);
protected final String id;
private final PortletContextComponents components;
@@ -59,6 +59,7 @@
boolean isSimpleAppPortlet = false;
boolean isOpaquePortlet = false;
boolean isCompoundAppPortlet = false;
+ boolean isCloned = false;
// components
if (interpret)
@@ -78,46 +79,71 @@
isSimpleAppPortlet = separator != -1 && appName.length() > 0
&& portletName.length() > 0;
if (isSimpleAppPortlet)
{
- components = new PortletContextComponents(null, appName, portletName);
+ components = new PortletContextComponents(null, appName, portletName,
false);
}
}
else
{
- int invoker = trimmedId.indexOf(SEPARATOR);
- int prefix = trimmedId.indexOf(PREFIX);
+ if (!trimmedId.startsWith(PRODUCER_CLONE_ID_PREFIX))
+ {
+ int invoker = trimmedId.indexOf(SEPARATOR);
+ int prefix = trimmedId.indexOf(PREFIX);
- if (prefix != -1)
- {
- // check if we have the case: invokerId./something
- if (invoker > 0 && invoker < prefix)
+ if (prefix != -1)
{
+ // check if we have the case: invokerId./something
+ if (invoker > 0 && invoker < prefix)
+ {
+ String invokerId = trimmedId.substring(0, invoker).trim();
+
+ int separator = trimmedId.indexOf(SEPARATOR, prefix);
+ // check the case: invokerId./application.portlet
+ if (separator != -1)
+ {
+ String portletName = trimmedId.substring(separator +
1).trim();
+ trimmedId = trimmedId.substring(invoker + 1).trim();
+ String applicationName = trimmedId.substring(1,
trimmedId.indexOf(SEPARATOR)).trim();
+ isCompoundAppPortlet = invokerId.length() > 0 &&
applicationName.length() > 0 && portletName.length() > 0;
+ if (isCompoundAppPortlet)
+ {
+ components = new PortletContextComponents(invokerId,
applicationName, portletName, false);
+ }
+ }
+ }
+ }
+
+ // check if we have the case: invokerId.something
+ if (!isCompoundAppPortlet && invoker > 0)
+ {
String invokerId = trimmedId.substring(0, invoker).trim();
- int separator = trimmedId.indexOf(SEPARATOR, prefix);
- // check the case: invokerId./application.portlet
- if (separator != -1)
+ if (invokerId.length() > 0)
{
- String portletName = trimmedId.substring(separator + 1).trim();
- trimmedId = trimmedId.substring(invoker + 1).trim();
- String applicationName = trimmedId.substring(1,
trimmedId.indexOf(SEPARATOR)).trim();
- isCompoundAppPortlet = invokerId.length() > 0 &&
applicationName.length() > 0 && portletName.length() > 0;
- if (isCompoundAppPortlet)
+ String portletNameOrStateId = trimmedId.substring(invoker +
1).trim();
+
+ if (portletNameOrStateId.length() > 0)
{
- components = new PortletContextComponents(invokerId,
applicationName, portletName);
+ if
(portletNameOrStateId.startsWith(PRODUCER_CLONE_ID_PREFIX))
+ {
+ isCloned = true;
+ components = new PortletContextComponents(invokerId, null,
portletNameOrStateId.substring(PRODUCER_CLONE_ID_PREFIX.length()).trim(), true);
+ }
+ else
+ {
+ isOpaquePortlet = true;
+ components = new PortletContextComponents(invokerId, null,
portletNameOrStateId, false);
+ }
}
}
}
}
-
- // check if we have the case: invokerId.portletId
- if (!isCompoundAppPortlet && invoker > 0)
+ else
{
- String invokerId = trimmedId.substring(0, invoker).trim();
- String portletName = trimmedId.substring(invoker + 1).trim();
- isOpaquePortlet = invokerId.length() > 0 &&
portletName.length() > 0;
- if (isOpaquePortlet)
+ String stateId =
trimmedId.substring(PRODUCER_CLONE_ID_PREFIX.length()).trim();
+ isCloned = stateId.length() > 0;
+ if (isCloned)
{
- components = new PortletContextComponents(invokerId, null,
portletName);
+ components = new PortletContextComponents(null, null, stateId,
true);
}
}
}
@@ -128,7 +154,7 @@
}
}
- if (interpret && !(isSimpleAppPortlet || isCompoundAppPortlet ||
isOpaquePortlet))
+ if (interpret && !(isSimpleAppPortlet || isCompoundAppPortlet ||
isOpaquePortlet || isCloned))
{
throw new IllegalArgumentException("Couldn't interpret id '" +
id + "'");
}
@@ -203,9 +229,14 @@
public static PortletContext createPortletContext(String portletId)
{
- return createPortletContext(portletId, null, true);
+ return createPortletContext(portletId, true);
}
+ public static PortletContext createPortletContext(String portletId, boolean
interpret)
+ {
+ return createPortletContext(portletId, null, interpret);
+ }
+
public static PortletContext createPortletContext(String portletId, byte[] state,
boolean interpret)
{
if (state != null && state.length > 0)
@@ -238,7 +269,7 @@
applicationName = applicationName.substring(1);
}
- return new PortletContext(new PortletContextComponents(null, applicationName,
portletName));
+ return new PortletContext(new PortletContextComponents(null, applicationName,
portletName, false));
}
public PortletContextComponents getComponents()
@@ -251,12 +282,19 @@
private final String applicationName;
private final String portletName;
private final String invokerName;
+ private final boolean cloned;
- public PortletContextComponents(String invokerName, String applicationName, String
portletName)
+ public PortletContextComponents(String invokerName, String applicationName, String
portletNameOrStateId, boolean cloned)
{
+ if (cloned && !ParameterValidation.isNullOrEmpty(applicationName))
+ {
+ throw new IllegalArgumentException("Cannot be a clone if applicationName
is provided");
+ }
+
this.applicationName = applicationName;
- this.portletName = portletName;
+ this.portletName = portletNameOrStateId;
this.invokerName = invokerName;
+ this.cloned = cloned;
}
public String getApplicationName()
@@ -266,7 +304,7 @@
public String getPortletName()
{
- return portletName;
+ return isCloned() ? null : portletName;
}
public String getInvokerName()
@@ -274,11 +312,21 @@
return invokerName;
}
+ public boolean isCloned()
+ {
+ return cloned;
+ }
+
+ public String getStateId()
+ {
+ return isCloned() ? portletName : null;
+ }
+
public String getId()
{
return (invokerName == null ? "" : invokerName + SEPARATOR)
+ (applicationName == null ? "" : PREFIX + applicationName +
SEPARATOR)
- + (portletName == null ? "" : portletName);
+ + (portletName == null ? "" : (cloned ? PRODUCER_CLONE_ID_PREFIX :
"") + portletName);
}
}
}
Modified:
components/pc/trunk/api/src/test/java/org/gatein/pc/api/PortletContextTestCase.java
===================================================================
---
components/pc/trunk/api/src/test/java/org/gatein/pc/api/PortletContextTestCase.java 2011-04-07
15:02:19 UTC (rev 6174)
+++
components/pc/trunk/api/src/test/java/org/gatein/pc/api/PortletContextTestCase.java 2011-04-07
15:45:49 UTC (rev 6175)
@@ -39,6 +39,8 @@
assertNull(components.getInvokerName());
assertEquals("applicationName", components.getApplicationName());
assertEquals("portletName", components.getPortletName());
+ assertFalse(components.isCloned());
+ assertNull(components.getStateId());
context = PortletContext.createPortletContext("\t\t\n
/applicationName.portletName \t");
assertEquals("/applicationName.portletName", context.getId());
@@ -47,6 +49,8 @@
assertNull(components.getInvokerName());
assertEquals("applicationName", components.getApplicationName());
assertEquals("portletName", components.getPortletName());
+ assertFalse(components.isCloned());
+ assertNull(components.getStateId());
try
{
@@ -65,6 +69,8 @@
assertEquals("applicationName", components.getInvokerName());
assertNull(components.getApplicationName());
assertEquals("portletName", components.getPortletName());
+ assertFalse(components.isCloned());
+ assertNull(components.getStateId());
context =
PortletContext.createPortletContext("/applicationName.portlet.Name");
assertEquals("/applicationName.portlet.Name", context.getId());
@@ -73,6 +79,8 @@
assertNull(components.getInvokerName());
assertEquals("applicationName", components.getApplicationName());
assertEquals("portlet.Name", components.getPortletName());
+ assertFalse(components.isCloned());
+ assertNull(components.getStateId());
try
{
@@ -91,6 +99,8 @@
assertNull(components.getInvokerName());
assertEquals("applicationName", components.getApplicationName());
assertEquals("portlet Name", components.getPortletName());
+ assertFalse(components.isCloned());
+ assertNull(components.getStateId());
}
public void testPortletContextWithInvokerId()
@@ -102,6 +112,8 @@
assertEquals("local", components.getInvokerName());
assertEquals("foo", components.getApplicationName());
assertEquals("bar", components.getPortletName());
+ assertFalse(components.isCloned());
+ assertNull(components.getStateId());
context = PortletContext.createPortletContext(" local\t . / foo \t. \t\n
bar");
assertEquals("local./foo.bar", context.getId());
@@ -110,6 +122,8 @@
assertEquals("local", components.getInvokerName());
assertEquals("foo", components.getApplicationName());
assertEquals("bar", components.getPortletName());
+ assertFalse(components.isCloned());
+ assertNull(components.getStateId());
context = PortletContext.createPortletContext("local.foo.bar");
assertEquals("local.foo.bar", context.getId());
@@ -118,6 +132,8 @@
assertEquals("local", components.getInvokerName());
assertNull(components.getApplicationName());
assertEquals("foo.bar", components.getPortletName());
+ assertFalse(components.isCloned());
+ assertNull(components.getStateId());
context = PortletContext.createPortletContext("local./foo");
assertEquals("local./foo", context.getId());
@@ -126,6 +142,8 @@
assertEquals("local", components.getInvokerName());
assertNull(components.getApplicationName());
assertEquals("/foo", components.getPortletName());
+ assertFalse(components.isCloned());
+ assertNull(components.getStateId());
}
public void testCreateFromComponents()
@@ -140,6 +158,8 @@
assertEquals("applicationName", components.getApplicationName());
assertEquals("portletName", components.getPortletName());
assertEquals(context, fromId);
+ assertFalse(components.isCloned());
+ assertNull(components.getStateId());
}
public void testShouldProperlyHandleApplicationNameStartingWithSlash()
@@ -153,9 +173,72 @@
assertNull(components.getInvokerName());
assertEquals("applicationName", components.getApplicationName());
assertEquals("portletName", components.getPortletName());
+ assertFalse(components.isCloned());
+ assertNull(components.getStateId());
assertEquals(context, fromId);
}
+ public void testShouldWorkWithoutInterpretation()
+ {
+ PortletContext context = PortletContext.createPortletContext("foo",
false);
+ assertNull(context.getComponents());
+ assertEquals("foo", context.getId());
+ }
+
+ public void testAcceptClones()
+ {
+ PortletContext context = PortletContext.createPortletContext("_clone");
+ assertEquals("_clone", context.getId());
+ PortletContext.PortletContextComponents components = context.getComponents();
+ assertNotNull(components);
+ assertNull(components.getInvokerName());
+ assertNull(components.getApplicationName());
+ assertNull(components.getPortletName());
+ assertTrue(components.isCloned());
+ assertEquals("clone", components.getStateId());
+
+ context = PortletContext.createPortletContext("foo._clone");
+ assertEquals("foo._clone", context.getId());
+ components = context.getComponents();
+ assertNotNull(components);
+ assertEquals("foo", components.getInvokerName());
+ assertNull(components.getApplicationName());
+ assertNull(components.getPortletName());
+ assertTrue(components.isCloned());
+ assertEquals("clone", components.getStateId());
+
+ context = PortletContext.createPortletContext("foo \t \n. _
\t\nclone");
+ assertEquals("foo._clone", context.getId());
+ components = context.getComponents();
+ assertNotNull(components);
+ assertEquals("foo", components.getInvokerName());
+ assertNull(components.getApplicationName());
+ assertNull(components.getPortletName());
+ assertTrue(components.isCloned());
+ assertEquals("clone", components.getStateId());
+
+ context = PortletContext.createPortletContext("foo." +
PortletContext.CONSUMER_CLONE_ID);
+ assertEquals("foo." + PortletContext.CONSUMER_CLONE_ID,
context.getId());
+ components = context.getComponents();
+ assertNotNull(components);
+ assertEquals("foo", components.getInvokerName());
+ assertNull(components.getApplicationName());
+ assertNull(components.getPortletName());
+ assertTrue(components.isCloned());
+ assertEquals(PortletContext.CONSUMER_CLONE_DUMMY_STATE_ID,
components.getStateId());
+ }
+
+ public void testAcceptConsumerClone()
+ {
+ PortletContext.PortletContextComponents components =
PortletContext.LOCAL_CONSUMER_CLONE.getComponents();
+ assertNotNull(components);
+ assertEquals(PortletInvoker.LOCAL_PORTLET_INVOKER_ID,
components.getInvokerName());
+ assertNull(components.getApplicationName());
+ assertNull(components.getPortletName());
+ assertTrue(components.isCloned());
+ assertEquals(PortletContext.CONSUMER_CLONE_DUMMY_STATE_ID,
components.getStateId());
+ }
+
public void testCreateFromNullOrEmpty()
{
try
Modified:
components/pc/trunk/portlet/src/main/java/org/gatein/pc/portlet/state/producer/ProducerPortletInvoker.java
===================================================================
---
components/pc/trunk/portlet/src/main/java/org/gatein/pc/portlet/state/producer/ProducerPortletInvoker.java 2011-04-07
15:02:19 UTC (rev 6174)
+++
components/pc/trunk/portlet/src/main/java/org/gatein/pc/portlet/state/producer/ProducerPortletInvoker.java 2011-04-07
15:45:49 UTC (rev 6175)
@@ -73,12 +73,6 @@
{
/** . */
- private static final String PRODUCER_CLONE_ID_PREFIX = "_";
-
- /** . */
- private static final String CONSUMER_CLONE_ID = "_dumbvalue";
-
- /** . */
private PortletStatePersistenceManager persistenceManager;
/** . */
@@ -160,7 +154,7 @@
String portletId = portletContext.getId();
//
- if (CONSUMER_CLONE_ID.equals(portletId))
+ if (PortletContext.CONSUMER_CLONE_ID.equals(portletId))
{
if (portletContext instanceof StatefulPortletContext)
{
@@ -184,11 +178,11 @@
throw new InvalidPortletIdException("", portletId);
}
}
- else if (portletId.startsWith(PRODUCER_CLONE_ID_PREFIX))
+ else if (portletId.startsWith(PortletContext.PRODUCER_CLONE_ID_PREFIX))
{
try
{
- String stateId = portletId.substring(PRODUCER_CLONE_ID_PREFIX.length());
+ String stateId =
portletId.substring(PortletContext.PRODUCER_CLONE_ID_PREFIX.length());
PortletStateContext stateContext = persistenceManager.loadState(stateId);
PortletState state = stateContext.getState();
Portlet delegate =
super.getPortlet(PortletContext.createPortletContext(state.getPortletId()));
@@ -300,7 +294,7 @@
String cloneStateId =
persistenceManager.cloneState(portletStateId, newPrefs);
// Return the clone context
- String cloneId = PRODUCER_CLONE_ID_PREFIX + cloneStateId;
+ String cloneId = PortletContext.PRODUCER_CLONE_ID_PREFIX +
cloneStateId;
PortletContext clonedCtx =
PortletContext.createPortletContext(cloneId);
StateEvent event = new StateEvent(clonedCtx,
StateEvent.Type.PORTLET_CLONED_EVENT);
instanceCtx.onStateEvent(event);
@@ -333,7 +327,7 @@
String cloneStateId =
persistenceManager.createState(context.getPortletId(), newPrefs);
// Return the clone context
- String cloneId = PRODUCER_CLONE_ID_PREFIX + cloneStateId;
+ String cloneId = PortletContext.PRODUCER_CLONE_ID_PREFIX +
cloneStateId;
PortletContext clonedCtx =
PortletContext.createPortletContext(cloneId);
StateEvent event = new StateEvent(clonedCtx,
StateEvent.Type.PORTLET_CLONED_EVENT);
instanceCtx.onStateEvent(event);
@@ -418,8 +412,8 @@
{
try
{
- String stateId = portletId.substring(PRODUCER_CLONE_ID_PREFIX.length());
- String cloneId = PRODUCER_CLONE_ID_PREFIX +
persistenceManager.cloneState(stateId);
+ String stateId =
portletId.substring(PortletContext.PRODUCER_CLONE_ID_PREFIX.length());
+ String cloneId = PortletContext.PRODUCER_CLONE_ID_PREFIX +
persistenceManager.cloneState(stateId);
return PortletContext.createPortletContext(cloneId);
}
catch (NoSuchStateException e)
@@ -443,7 +437,7 @@
if (persistLocally)
{
String cloneId = persistenceManager.createState(portletId, newState);
- return PortletContext.createPortletContext(PRODUCER_CLONE_ID_PREFIX +
cloneId);
+ return
PortletContext.createPortletContext(PortletContext.PRODUCER_CLONE_ID_PREFIX + cloneId);
}
else
{
@@ -467,7 +461,7 @@
if (!(portletContext instanceof StatefulPortletContext))
{
String portletId = portletContext.getId();
- if (!portletId.startsWith(PRODUCER_CLONE_ID_PREFIX))
+ if (!portletId.startsWith(PortletContext.PRODUCER_CLONE_ID_PREFIX))
{
log.debug("Attempt to destroy a producer offered portlet " +
portletId);
DestroyCloneFailure failure = new DestroyCloneFailure(portletId,
"Cannot destroy POP");
@@ -477,7 +471,7 @@
{
try
{
-
persistenceManager.destroyState(portletId.substring(PRODUCER_CLONE_ID_PREFIX.length()));
+
persistenceManager.destroyState(portletId.substring(PortletContext.PRODUCER_CLONE_ID_PREFIX.length()));
}
catch (NoSuchStateException e)
{
@@ -733,7 +727,7 @@
String cloneStateId =
persistenceManager.createState(statefulPortletContext.getId(),
portletState.getProperties());
// Return the clone context
- String cloneId = PRODUCER_CLONE_ID_PREFIX + cloneStateId;
+ String cloneId = PortletContext.PRODUCER_CLONE_ID_PREFIX + cloneStateId;
return PortletContext.createPortletContext(cloneId);
}
else
@@ -758,7 +752,7 @@
{
PortletState sstate = new PortletState(portletId, props);
S marshalledState = stateConverter.marshall(stateType, sstate);
- return StatefulPortletContext.create(CONSUMER_CLONE_ID, stateType,
marshalledState);
+ return StatefulPortletContext.create(PortletContext.CONSUMER_CLONE_ID,
stateType, marshalledState);
}
catch (StateConversionException e)
{
@@ -816,9 +810,9 @@
if (!(portletContext instanceof StatefulPortletContext))
{
String portletId = portletContext.getId();
- if (portletContext.getId().startsWith(PRODUCER_CLONE_ID_PREFIX))
+ if (portletContext.getId().startsWith(PortletContext.PRODUCER_CLONE_ID_PREFIX))
{
- String stateId = portletId.substring(PRODUCER_CLONE_ID_PREFIX.length());
+ String stateId =
portletId.substring(PortletContext.PRODUCER_CLONE_ID_PREFIX.length());
try
{
PortletStateContext stateContext = persistenceManager.loadState(stateId);