Author: julien(a)jboss.com
Date: 2006-12-15 11:04:07 -0500 (Fri, 15 Dec 2006)
New Revision: 5872
Added:
trunk/portlet/src/main/org/jboss/portal/portlet/impl/state/producer/ProducerStateContextImpl.java
Modified:
trunk/core/src/main/org/jboss/portal/core/impl/portlet/state/PersistentState.java
trunk/core/src/main/org/jboss/portal/core/impl/portlet/state/PersistentStateStore.java
trunk/core/src/main/org/jboss/portal/test/core/state/ProducerTestCase.java
trunk/portlet/build.xml
trunk/portlet/src/main/org/jboss/portal/portlet/impl/state/producer/ProducerPersistenceManagerService.java
trunk/portlet/src/main/org/jboss/portal/portlet/state/producer/ProducerPortletInvoker.java
trunk/portlet/src/main/org/jboss/portal/portlet/state/producer/ProducerStateContext.java
Log:
improve the ProducerPersistenceManager interface definition : don't make the
ProducerStateContext a class that inherits ProducerState (a POJO), but rather have the
ProducerStateContext be an interface that own a ProducerState object.
Modified:
trunk/core/src/main/org/jboss/portal/core/impl/portlet/state/PersistentState.java
===================================================================
---
trunk/core/src/main/org/jboss/portal/core/impl/portlet/state/PersistentState.java 2006-12-15
15:15:26 UTC (rev 5871)
+++
trunk/core/src/main/org/jboss/portal/core/impl/portlet/state/PersistentState.java 2006-12-15
16:04:07 UTC (rev 5872)
@@ -26,6 +26,7 @@
import org.jboss.portal.portlet.state.AbstractPropertyMap;
import org.jboss.portal.portlet.state.PropertyMap;
import org.jboss.portal.portlet.state.producer.ProducerStateContext;
+import org.jboss.portal.portlet.state.producer.ProducerState;
import java.util.Calendar;
import java.util.Date;
@@ -39,7 +40,7 @@
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision$
*/
-public class PersistentState
+public class PersistentState implements ProducerStateContext
{
/** The primary key. */
@@ -67,7 +68,7 @@
protected PersistentState parent;
/** . */
- private ProducerStateContext ctx;
+ private ProducerState ctx;
public PersistentState()
{
@@ -188,7 +189,9 @@
this.parent = parent;
}
- public ProducerStateContext getContext()
+ //
+
+ public ProducerState getState()
{
if (ctx == null)
{
@@ -210,7 +213,7 @@
return entry.getValue();
}
};
- ctx = new ProducerStateContext(key.toString(), portletId, props);
+ ctx = new ProducerState(portletId, props);
}
return ctx;
}
Modified:
trunk/core/src/main/org/jboss/portal/core/impl/portlet/state/PersistentStateStore.java
===================================================================
---
trunk/core/src/main/org/jboss/portal/core/impl/portlet/state/PersistentStateStore.java 2006-12-15
15:15:26 UTC (rev 5871)
+++
trunk/core/src/main/org/jboss/portal/core/impl/portlet/state/PersistentStateStore.java 2006-12-15
16:04:07 UTC (rev 5872)
@@ -77,8 +77,7 @@
public ProducerStateContext loadState(String id) throws InvalidStateIdException,
NoSuchStateException
{
Session session = getCurrentSession();
- PersistentState persistentState = loadState(session, id);
- return persistentState.getContext();
+ return loadState(session, id);
}
public String createState(String portletId, PropertyMap propertyMap)
@@ -96,12 +95,12 @@
Session session = getCurrentSession();
// Create the persistent state
- PersistentState state = new PersistentState(portletId, propertyMap);
- session.save(state);
+ PersistentState context = new PersistentState(portletId, propertyMap);
+ session.save(context);
session.flush();
//
- return state.getId();
+ return context.getId();
}
public String cloneState(String stateId, PropertyMap propertyMap) throws
InvalidStateIdException, NoSuchStateException
@@ -117,22 +116,22 @@
//
Session session = getCurrentSession();
- PersistentState parentState = loadState(session, stateId);
+ PersistentState parentContext = loadState(session, stateId);
// Create the persistent state
- PersistentState state = new PersistentState(parentState.getPortletId(),
propertyMap);
- session.persist(state);
+ PersistentState context = new PersistentState(parentContext.getPortletId(),
propertyMap);
+ session.persist(context);
// Make the association
- state.setParent(parentState);
- parentState.getChildren().add(state);
- session.update(parentState);
+ context.setParent(parentContext);
+ parentContext.getChildren().add(context);
+ session.update(parentContext);
//
session.flush();
//
- return state.getId();
+ return context.getId();
}
public String cloneState(String stateId) throws IllegalArgumentException,
NoSuchStateException, InvalidStateIdException
@@ -144,41 +143,41 @@
//
Session session = getCurrentSession();
- PersistentState parentState = loadState(session, stateId);
+ PersistentState parentContext = loadState(session, stateId);
// Create the persistent state
- PersistentState state = new PersistentState(parentState.getPortletId(), new
SimplePropertyMap(parentState.getContext().getProperties()));
- session.persist(state);
+ PersistentState context = new PersistentState(parentContext.getPortletId(), new
SimplePropertyMap(parentContext.getState().getProperties()));
+ session.persist(context);
// Make the association
- state.setParent(parentState);
- parentState.getChildren().add(state);
- session.update(parentState);
+ context.setParent(parentContext);
+ parentContext.getChildren().add(context);
+ session.update(parentContext);
//
session.flush();
//
- return state.getId();
+ return context.getId();
}
public void updateState(String stateId, PropertyMap propertyMap) throws
InvalidStateIdException, NoSuchStateException
{
Session session = getCurrentSession();
- PersistentState state = loadState(session, stateId);
+ PersistentState context = loadState(session, stateId);
//
- state.entries.clear();
+ context.entries.clear();
for (Iterator i = propertyMap.keySet().iterator(); i.hasNext();)
{
String key = (String)i.next();
Value value = propertyMap.getProperty(key);
PersistentStateEntry entry = new PersistentStateEntry(key, value);
- state.entries.put(key, entry);
+ context.entries.put(key, entry);
}
//
- session.update(state);
+ session.update(context);
}
public void destroyState(String stateId) throws InvalidStateIdException,
NoSuchStateException
@@ -190,15 +189,15 @@
//
Session session = getCurrentSession();
- PersistentState state = loadState(session, stateId);
+ PersistentState context = loadState(session, stateId);
// Efficiently set the children parent to null
String update = "update PersistentState p set p.parent=NULL where
p.parent=:parent";
- Query query = session.createQuery(update).setLong("parent",
state.getKey().longValue());
+ Query query = session.createQuery(update).setLong("parent",
context.getKey().longValue());
query.executeUpdate();
// Delete the state
- session.delete(state);
+ session.delete(context);
session.flush();
}
@@ -230,16 +229,16 @@
try
{
Long key = new Long(stateId);
- PersistentState state = (PersistentState)session.get(PersistentState.class,
key);
+ PersistentState context = (PersistentState)session.get(PersistentState.class,
key);
//
- if (state == null)
+ if (context == null)
{
throw new NoSuchStateException(stateId);
}
//
- return state;
+ return context;
}
catch (NumberFormatException e)
{
Modified: trunk/core/src/main/org/jboss/portal/test/core/state/ProducerTestCase.java
===================================================================
--- trunk/core/src/main/org/jboss/portal/test/core/state/ProducerTestCase.java 2006-12-15
15:15:26 UTC (rev 5871)
+++ trunk/core/src/main/org/jboss/portal/test/core/state/ProducerTestCase.java 2006-12-15
16:04:07 UTC (rev 5872)
@@ -336,8 +336,8 @@
ProducerStateContext cloneState =
persistenceManager.loadState(cloneCtx.getId().substring(1));
assertNotNull(cloneState);
assertEquals(cloneCtx.getId(), "_" + cloneState.getId());
- assertEquals("SimplePortlet", cloneState.getPortletId());
- PropertyMap cloneValues = cloneState.getProperties();
+ assertEquals("SimplePortlet", cloneState.getState().getPortletId());
+ PropertyMap cloneValues = cloneState.getState().getProperties();
assertNotNull(cloneValues);
assertNotNull(cloneValues.keySet());
assertEquals(1, cloneValues.keySet().size());
@@ -362,8 +362,8 @@
ProducerStateContext cloneCloneState =
persistenceManager.loadState(cloneCloneCtx.getId().substring(1));
assertNotNull(cloneCloneState);
assertEquals(cloneCloneCtx.getId(), "_" + cloneCloneState.getId());
- assertEquals("SimplePortlet", cloneCloneState.getPortletId());
- PropertyMap cloneCloneValues = cloneCloneState.getProperties();
+ assertEquals("SimplePortlet",
cloneCloneState.getState().getPortletId());
+ PropertyMap cloneCloneValues = cloneCloneState.getState().getProperties();
assertNotNull(cloneCloneValues);
assertNotNull(cloneCloneValues.keySet());
assertEquals(1, cloneCloneValues.keySet().size());
@@ -466,11 +466,11 @@
ProducerStateContext state =
persistenceManager.loadState(cloneId.getId().substring(1));
assertNotNull(state);
assertEquals(cloneId.getId().substring(1), state.getId());
- assertEquals("CloningPortlet", state.getPortletId());
+ assertEquals("CloningPortlet", state.getState().getPortletId());
SimplePropertyMap expectedValue = new SimplePropertyMap();
expectedValue.setProperty("abc", new StringValue("def"));
expectedValue.setProperty("_abc", new StringValue("_def"));
- ValueMapAssert.assertEquals(expectedValue, state.getProperties());
+ ValueMapAssert.assertEquals(expectedValue, state.getState().getProperties());
TransactionAssert.commitTransaction();
}
@@ -518,7 +518,7 @@
// Modify the state of the CCP
TransactionAssert.beginTransaction();
- ProducerState cloningPortletState =
persistenceManager.loadState(cloningPortletId.getId().substring(1));
+ ProducerState cloningPortletState =
persistenceManager.loadState(cloningPortletId.getId().substring(1)).getState();
SimplePropertyMap newCloningPortletStateValue = new
SimplePropertyMap(cloningPortletState.getProperties());
newCloningPortletStateValue.setProperty("abc", new
StringValue("deff"));
persistenceManager.updateState(cloningPortletId.getId().substring(1),
newCloningPortletStateValue);
@@ -541,11 +541,11 @@
ProducerStateContext state =
persistenceManager.loadState(cloneId.getId().substring(1));
assertNotNull(state);
assertEquals(cloneId.getId().substring(1), state.getId());
- assertEquals("CloningPortlet", state.getPortletId());
+ assertEquals("CloningPortlet", state.getState().getPortletId());
SimplePropertyMap expectedValue = new SimplePropertyMap();
expectedValue.setProperty("abc", new StringValue("deff"));
expectedValue.setProperty("_abc", new StringValue("_def"));
- ValueMapAssert.assertEquals(expectedValue, state.getProperties());
+ ValueMapAssert.assertEquals(expectedValue, state.getState().getProperties());
TransactionAssert.commitTransaction();
}
@@ -570,12 +570,12 @@
assertNull(instanceContext.getClonedContext());
ProducerStateContext state =
persistenceManager.loadState(cloningPortletId.getId().substring(1));
assertNotNull(state);
- assertEquals("CloningPortlet", state.getPortletId());
+ assertEquals("CloningPortlet", state.getState().getPortletId());
assertEquals(cloningPortletId.getId().substring(1), state.getId());
SimplePropertyMap expectedValue = new SimplePropertyMap();
expectedValue.setProperty("abc", new StringValue("def"));
expectedValue.setProperty("_abc", new StringValue("_def"));
- ValueMapAssert.assertEquals(expectedValue, state.getProperties());
+ ValueMapAssert.assertEquals(expectedValue, state.getState().getProperties());
TransactionAssert.commitTransaction();
}
Modified: trunk/portlet/build.xml
===================================================================
--- trunk/portlet/build.xml 2006-12-15 15:15:26 UTC (rev 5871)
+++ trunk/portlet/build.xml 2006-12-15 16:04:07 UTC (rev 5872)
@@ -555,6 +555,10 @@
<x-sysproperty>
<sysproperty key="test.root"
value="${build.lib}"/>
<sysproperty key="test.uri"
value="/test/redirect/"/>
+<!--
+ <jvmarg value="-Xdebug"/>
+ <jvmarg
value="-Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y"/>
+-->
</x-sysproperty>
<x-classpath>
<path refid="oswego.concurrent.classpath"/>
Modified:
trunk/portlet/src/main/org/jboss/portal/portlet/impl/state/producer/ProducerPersistenceManagerService.java
===================================================================
---
trunk/portlet/src/main/org/jboss/portal/portlet/impl/state/producer/ProducerPersistenceManagerService.java 2006-12-15
15:15:26 UTC (rev 5871)
+++
trunk/portlet/src/main/org/jboss/portal/portlet/impl/state/producer/ProducerPersistenceManagerService.java 2006-12-15
16:04:07 UTC (rev 5872)
@@ -29,6 +29,7 @@
import org.jboss.portal.portlet.state.SimplePropertyMap;
import org.jboss.portal.portlet.state.producer.ProducerPersistenceManager;
import org.jboss.portal.portlet.state.producer.ProducerStateContext;
+import org.jboss.portal.portlet.state.producer.ProducerState;
import java.util.HashMap;
import java.util.Map;
@@ -46,7 +47,8 @@
/** . */
private int counter = 0;
- public synchronized ProducerStateContext loadState(String stateId) throws
NoSuchStateException, InvalidStateIdException
+
+ public synchronized ProducerStateContext loadState(String stateId) throws
IllegalArgumentException, NoSuchStateException, InvalidStateIdException
{
if (stateId == null)
{
@@ -60,14 +62,20 @@
{
throw new InvalidStateIdException(e, stateId);
}
- ProducerStateContext state = (ProducerStateContext)store.get(stateId);
- if (state == null)
+ ProducerStateContext context = (ProducerStateContext)store.get(stateId);
+ if (context == null)
{
throw new NoSuchStateException(stateId);
}
- return state;
+ return context;
}
+ private synchronized ProducerState getState(String stateId) throws
NoSuchStateException, InvalidStateIdException
+ {
+ ProducerStateContext context = loadState(stateId);
+ return context.getState();
+ }
+
public synchronized String createState(String portletId, PropertyMap propertyMap)
{
if (portletId == null)
@@ -79,7 +87,7 @@
throw new IllegalArgumentException("No null value map accepted");
}
String id = Integer.toString(counter++);
- ProducerStateContext state = new ProducerStateContext(id, portletId, new
SimplePropertyMap(propertyMap));
+ ProducerStateContext state = new ProducerStateContextImpl(id, portletId, new
SimplePropertyMap(propertyMap));
store.put(id, state);
return id;
}
@@ -90,13 +98,13 @@
{
throw new IllegalArgumentException();
}
- ProducerStateContext state = loadState(stateId);
- return createState(state.getPortletId(), propertyMap);
+ ProducerState stateContext = getState(stateId);
+ return createState(stateContext.getPortletId(), propertyMap);
}
public String cloneState(String stateId) throws IllegalArgumentException,
NoSuchStateException, InvalidStateIdException
{
- ProducerStateContext state = loadState(stateId);
+ ProducerState state = getState(stateId);
return createState(state.getPortletId(), new
SimplePropertyMap(state.getProperties()));
}
@@ -106,7 +114,7 @@
{
throw new IllegalArgumentException("No null value map");
}
- ProducerStateContext ctx = loadState(stateId);
+ ProducerState ctx = getState(stateId);
ctx.getProperties().clear();
ctx.getProperties().putAll(propertyMap);
}
Added:
trunk/portlet/src/main/org/jboss/portal/portlet/impl/state/producer/ProducerStateContextImpl.java
===================================================================
---
trunk/portlet/src/main/org/jboss/portal/portlet/impl/state/producer/ProducerStateContextImpl.java 2006-12-15
15:15:26 UTC (rev 5871)
+++
trunk/portlet/src/main/org/jboss/portal/portlet/impl/state/producer/ProducerStateContextImpl.java 2006-12-15
16:04:07 UTC (rev 5872)
@@ -0,0 +1,57 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, 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.jboss.portal.portlet.impl.state.producer;
+
+import org.jboss.portal.portlet.state.producer.ProducerStateContext;
+import org.jboss.portal.portlet.state.producer.ProducerState;
+import org.jboss.portal.portlet.state.PropertyMap;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class ProducerStateContextImpl implements ProducerStateContext
+{
+
+ /** . */
+ private final String id;
+
+ /** . */
+ private ProducerState state;
+
+ public ProducerStateContextImpl(String id, String portletId, PropertyMap propertyMap)
+ {
+ this.id = id;
+ this.state = new ProducerState(portletId, propertyMap);
+ }
+
+ public String getId()
+ {
+ return id;
+ }
+
+ public ProducerState getState()
+ {
+ return state;
+ }
+}
Modified:
trunk/portlet/src/main/org/jboss/portal/portlet/state/producer/ProducerPortletInvoker.java
===================================================================
---
trunk/portlet/src/main/org/jboss/portal/portlet/state/producer/ProducerPortletInvoker.java 2006-12-15
15:15:26 UTC (rev 5871)
+++
trunk/portlet/src/main/org/jboss/portal/portlet/state/producer/ProducerPortletInvoker.java 2006-12-15
16:04:07 UTC (rev 5872)
@@ -158,7 +158,9 @@
{
try
{
- ProducerState state =
persistenceManager.loadState(portletId.substring(PRODUCER_CLONE_ID_PREFIX.length()));
+ String stateId = portletId.substring(PRODUCER_CLONE_ID_PREFIX.length());
+ ProducerStateContext stateContext = persistenceManager.loadState(stateId);
+ ProducerState state = stateContext.getState();
return
portletInvoker.getPortlet(PortletContext.createPortletContext(state.getPortletId()));
}
catch (NoSuchStateException e)
@@ -189,47 +191,32 @@
InstanceContext instanceCtx = invocation.getInstanceContext();
AccessMode access = instanceCtx.getAccessMode();
- // Value map of the state loaded
- PropertyMap propertyMap = null;
+ // Get a state contxt for the portlet context
+ InternalPortletContext context = getStateContext(portletContext);
- // The next portlet to invoke
- String targetPortletId = portletContext.getId();
-
- // Try to get state
- ProducerState state = getProducerState(portletContext);
- if (state != null)
+ // If it is a producer offered portlet we consider read-write as read-only
+ if (context.isStateful() == false && access == AccessMode.READ_WRITE)
{
- propertyMap = state.getProperties();
- targetPortletId = state.getPortletId();
+ access = AccessMode.READ_ONLY;
}
- else
- {
- // If it is a producer offered portlet we consider read-write as read-only
- if (access == AccessMode.READ_WRITE)
- {
- access = AccessMode.READ_ONLY;
- }
- }
// Get the portlet container and set it on invocation
- Portlet portlet =
portletInvoker.getPortlet(PortletContext.createPortletContext(targetPortletId));
+ Portlet portlet =
portletInvoker.getPortlet(PortletContext.createPortletContext(context.getPortletId()));
if (portlet == null)
{
- throw new NoSuchPortletException("Portlet " + targetPortletId + "
not found", targetPortletId);
+ throw new NoSuchPortletException("Portlet " + context.getPortletId() +
" not found", context.getPortletId());
}
+
+ // Create prefs
PortletInfo info = portlet.getInfo();
-
- // Create prefs and set it on invocation
PreferencesInfo prefsInfo = info.getPreferences();
+ AbstractPropertyContext prefs = new AbstractPropertyContext(access,
context.isStateful() ? ((StatefulContext)context).getProperties() : null, prefsInfo);
//
- AbstractPropertyContext prefs = new AbstractPropertyContext(access, propertyMap,
prefsInfo);
-
- //
PortletInvocationResponse response;
try
{
- invocation.setAttribute(PortletInvocation.REQUEST_SCOPE,
PortletInvocation.PORTLET_CONTEXT_ATTRIBUTE,
PortletContext.createPortletContext(targetPortletId));
+ invocation.setAttribute(PortletInvocation.REQUEST_SCOPE,
PortletInvocation.PORTLET_CONTEXT_ATTRIBUTE,
PortletContext.createPortletContext(context.getPortletId()));
invocation.setAttribute(PortletInvocation.REQUEST_SCOPE,
PortletInvocation.PREFERENCES_ATTRIBUTE, prefs);
invocation.setInfo(info);
@@ -256,15 +243,17 @@
if (access == AccessMode.CLONE_BEFORE_WRITE)
{
// Create the state
- if (state != null)
+ if (context.isStateful())
{
+ StatefulContext statefulContext = (StatefulContext)context;
boolean persist = stateManagementPolicy.persistLocally();
if (persist)
{
try
{
// The state id should be ok as it was used before to load the
state
- String portletStateId = ((ProducerStateContext)state).getId();
+ LocalContext localContext = (LocalContext)statefulContext;
+ String portletStateId = localContext.getStateId();
String cloneStateId = persistenceManager.cloneState(portletStateId,
newPrefs);
// Return the clone context
@@ -284,7 +273,7 @@
}
else
{
- PortletContext clonedCtx = marshall(state.getPortletId(), newPrefs);
+ PortletContext clonedCtx = marshall(context.getPortletId(), newPrefs);
StateEvent event = new StateEvent(clonedCtx,
StateEvent.PORTLET_CLONED_EVENT);
instanceCtx.onStateEvent(event);
}
@@ -306,7 +295,7 @@
}
// Create the new state
- String cloneStateId = persistenceManager.createState(targetPortletId,
newPrefs);
+ String cloneStateId =
persistenceManager.createState(context.getPortletId(), newPrefs);
// Return the clone context
String cloneId = PRODUCER_CLONE_ID_PREFIX + cloneStateId;
@@ -317,13 +306,15 @@
}
else if (access == AccessMode.READ_WRITE)
{
- if (!(portletContext instanceof StatefulPortletContext))
+ StatefulContext statefulContext = (StatefulContext)context;
+ if (statefulContext.isLocal())
{
// Update the state
try
{
- String portletStateId = ((ProducerStateContext)state).getId();
- persistenceManager.updateState(portletStateId, newPrefs);
+ LocalContext localContext = (LocalContext)statefulContext;
+ String stateId = localContext.getStateId();
+ persistenceManager.updateState(stateId, newPrefs);
}
catch (NoSuchStateException e)
{
@@ -336,7 +327,7 @@
}
else
{
- PortletContext modifiedCtx = marshall(targetPortletId, newPrefs);
+ PortletContext modifiedCtx = marshall(context.getPortletId(), newPrefs);
StateEvent event = new StateEvent(modifiedCtx,
StateEvent.PORTLET_MODIFIED_EVENT);
instanceCtx.onStateEvent(event);
}
@@ -344,7 +335,7 @@
else if (access == AccessMode.READ_ONLY)
{
throw new PortletStateChangeRequiredException("Modification was
requested for portlet with id '"
- + targetPortletId + "' but access mode was READ ONLY.");
+ + context.getPortletId() + "' but access mode was READ
ONLY.");
}
}
@@ -361,12 +352,13 @@
//
String portletId = portletContext.getId();
- ProducerState state = getProducerState(portletContext);
+ InternalPortletContext context = getStateContext(portletContext);
boolean useStore = stateManagementPolicy.persistLocally();
//
- if (state != null)
+ if (context.isStateful())
{
+ StatefulContext statefulContext = (StatefulContext)context;
if (useStore)
{
try
@@ -386,7 +378,7 @@
}
else
{
- return marshall(state.getPortletId(), state.getProperties());
+ return marshall(statefulContext.getPortletId(),
statefulContext.getProperties());
}
}
else
@@ -478,17 +470,18 @@
//
String portletId = portletContext.getId();
- ProducerState state = getProducerState(portletContext);
+ InternalPortletContext context = getStateContext(portletContext);
//
- if (state != null)
+ if (context.isStateful())
{
- //
+ StatefulContext statefulContext = (StatefulContext)context;
+
// Get the content
- PropertyMap props = new SimplePropertyMap(state.getProperties());
+ PropertyMap props = new SimplePropertyMap(statefulContext.getProperties());
// Dereference the portlet
- String referencedPortletId = state.getPortletId();
+ String referencedPortletId = context.getPortletId();
// Get the container
Portlet referencedPortlet =
portletInvoker.getPortlet(PortletContext.createPortletContext(referencedPortletId));
@@ -521,7 +514,7 @@
else
{
// Get the container
- return getPropertiesFromMetaData(portletId);
+ return getPropertiesFromMetaData(context.getPortletId());
}
}
@@ -546,16 +539,17 @@
//
String portletId = portletContext.getId();
- ProducerState state = getProducerState(portletContext);
+ InternalPortletContext context = getStateContext(portletContext);
//
- if (state == null)
+ if (context.isStateful() == false)
{
throw new InvalidPortletIdException("Cannot configure producer offered
portlets", portletId);
}
+ StatefulContext statefulContext = (StatefulContext)context;
// Dereference the portlet
- String referencedPortletId = state.getPortletId();
+ String referencedPortletId = context.getPortletId();
// Get the container
Portlet referencedPortlet =
portletInvoker.getPortlet(PortletContext.createPortletContext(referencedPortletId));
@@ -573,7 +567,7 @@
PreferencesInfo prefs = referencedPortletInfo.getPreferences();
// Clone the current state
- PropertyMap properties = new SimplePropertyMap(state.getProperties());
+ PropertyMap properties = new SimplePropertyMap(statefulContext.getProperties());
// Clone argument
for (int i = 0; i < changes.length; i++)
@@ -604,9 +598,10 @@
}
//
- if (!(portletContext instanceof StatefulPortletContext))
+ if (statefulContext.isLocal())
{
- String stateId = ((ProducerStateContext)state).getId();
+ LocalContext localContext = (LocalContext)statefulContext;
+ String stateId = localContext.getStateId();
try
{
persistenceManager.updateState(stateId, properties);
@@ -670,18 +665,17 @@
}
/**
- * Return the producer state from the specified portlet context or null if that
portlet context reference a producer
- * offered portlet.
+ * Return an internal portlet context from the specified portlet context.
*
* @param portletContext the portlet context
* @return the state that the portlet context carries
* @throws NoSuchPortletException if the underlying state does not exist
* @throws InvalidPortletIdException if the state id is not valid
*/
- private ProducerState getProducerState(PortletContext portletContext) throws
NoSuchPortletException, InvalidPortletIdException
+ private InternalPortletContext getStateContext(final PortletContext portletContext)
throws NoSuchPortletException, InvalidPortletIdException
{
- byte[] state = portletContext.getState();
- if (state == null)
+ byte[] bytes = portletContext.getState();
+ if (bytes == null)
{
String portletId = portletContext.getId();
if (portletContext.getId().startsWith(PRODUCER_CLONE_ID_PREFIX))
@@ -689,7 +683,8 @@
String stateId = portletId.substring(PRODUCER_CLONE_ID_PREFIX.length());
try
{
- return persistenceManager.loadState(stateId);
+ ProducerStateContext stateContext =
persistenceManager.loadState(stateId);
+ return new LocalContext(stateContext.getState().getPortletId(),
stateContext.getState().getProperties(), stateContext.getId());
}
catch (NoSuchStateException e)
{
@@ -702,14 +697,15 @@
}
else
{
- return null;
+ return new StatelessContext(portletContext.getId());
}
}
else
{
try
{
- return stateConverter.unmarshall(state);
+ final ProducerState state = stateConverter.unmarshall(bytes);
+ return new RemoteContext(state.getPortletId(), state.getProperties());
}
catch (StateConversionException e)
{
@@ -717,4 +713,132 @@
}
}
}
+
+ /**
+ * An internal portlet context that describe how the portlet was obtained.
+ */
+ private abstract static class InternalPortletContext
+ {
+
+ /** . */
+ private final String portletId;
+
+ /**
+ * @return true if the context represent a cloned portlet
+ */
+ public abstract boolean isStateful();
+
+ public InternalPortletContext(String portletId)
+ {
+ if (portletId == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ this.portletId = portletId;
+ }
+
+ /**
+ * @return the portlet id in the context of the delegate
+ */
+ public String getPortletId()
+ {
+ return portletId;
+ }
+ }
+
+ /**
+ * Describe delegate portlets.
+ */
+ private static class StatelessContext extends InternalPortletContext
+ {
+ public StatelessContext(String portletId)
+ {
+ super(portletId);
+ }
+
+ public boolean isStateful()
+ {
+ return false;
+ }
+ }
+
+ /**
+ * A cloned portlet that points to a delegate portlet.
+ */
+ private abstract static class StatefulContext extends InternalPortletContext
+ {
+
+ /** . */
+ private final PropertyMap properties;
+
+ protected StatefulContext(String portletId, PropertyMap properties)
+ {
+ super(portletId);
+
+ //
+ if (properties == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ this.properties = properties;
+ }
+
+ /**
+ * @return true if the portlet is local
+ */
+ public abstract boolean isLocal();
+
+ public PropertyMap getProperties()
+ {
+ return properties;
+ }
+
+ public boolean isStateful()
+ {
+ return true;
+ }
+ }
+
+ private static class LocalContext extends StatefulContext
+ {
+
+ /** . */
+ private String stateId;
+
+ public LocalContext(String portletId, PropertyMap state, String stateId)
+ {
+ super(portletId, state);
+
+ //
+ if (stateId == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ this.stateId = stateId;
+ }
+
+ public String getStateId()
+ {
+ return stateId;
+ }
+
+ public boolean isLocal()
+ {
+ return true;
+ }
+ }
+
+ private static class RemoteContext extends StatefulContext
+ {
+
+ public RemoteContext(String portletId, PropertyMap state)
+ {
+ super(portletId, state);
+ }
+
+ public boolean isLocal()
+ {
+ return false;
+ }
+ }
}
Modified:
trunk/portlet/src/main/org/jboss/portal/portlet/state/producer/ProducerStateContext.java
===================================================================
---
trunk/portlet/src/main/org/jboss/portal/portlet/state/producer/ProducerStateContext.java 2006-12-15
15:15:26 UTC (rev 5871)
+++
trunk/portlet/src/main/org/jboss/portal/portlet/state/producer/ProducerStateContext.java 2006-12-15
16:04:07 UTC (rev 5872)
@@ -22,46 +22,23 @@
******************************************************************************/
package org.jboss.portal.portlet.state.producer;
-import org.jboss.portal.portlet.state.PropertyMap;
-
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
- * @version $Revision: 1.1 $
+ * @version $Revision$
*/
-public class ProducerStateContext extends ProducerState
+public interface ProducerStateContext
{
- /** . */
- private final String id;
+ /**
+ *
+ * @return
+ */
+ String getId();
- public ProducerStateContext(String id, String portletId)
- {
- super(portletId);
+ /**
+ *
+ * @return
+ */
+ ProducerState getState();
- //
- if (id == null)
- {
- throw new IllegalArgumentException();
- }
-
- //
- this.id = id;
- }
-
- public ProducerStateContext(String id, String portletId, PropertyMap properties)
- {
- super(portletId, properties);
-
- //
- if (id == null)
- {
- throw new IllegalArgumentException();
- }
- this.id = id;
- }
-
- public String getId()
- {
- return id;
- }
}