[jboss-cvs] jboss-portal/core/src/main/org/jboss/portal/core/impl/model/instance ...
Julien Viet
julien at jboss.com
Sun Jul 30 08:36:21 EDT 2006
User: julien
Date: 06/07/30 08:36:21
Modified: core/src/main/org/jboss/portal/core/impl/model/instance
InstanceContextImpl.java InstanceImpl.java
PersistentInstanceContainer.java
Log:
JBPORTAL-973 : Portlet instance container integration testing
JBPORTAL-972 : Portlet stateful invoker testing
Revision Changes Path
1.5 +3 -3 jboss-portal/core/src/main/org/jboss/portal/core/impl/model/instance/InstanceContextImpl.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: InstanceContextImpl.java
===================================================================
RCS file: /cvsroot/jboss/jboss-portal/core/src/main/org/jboss/portal/core/impl/model/instance/InstanceContextImpl.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- InstanceContextImpl.java 15 May 2006 01:45:31 -0000 1.4
+++ InstanceContextImpl.java 30 Jul 2006 12:36:21 -0000 1.5
@@ -27,7 +27,7 @@
/**
* @author <a href="mailto:julien at jboss.org">Julien Viet</a>
- * @version $Revision: 1.4 $
+ * @version $Revision: 1.5 $
*/
public class InstanceContextImpl implements InstanceContext
{
@@ -51,12 +51,12 @@
return instance.getId();
}
- public void portletCloned(String portletId, String cloneId)
+ public void portletCloned(String cloneId)
{
this.cloneId = cloneId;
}
- public void portletCloned(String portletId, byte[] state)
+ public void portletCloned(byte[] state)
{
throw new UnsupportedOperationException("NYI");
}
1.30 +56 -27 jboss-portal/core/src/main/org/jboss/portal/core/impl/model/instance/InstanceImpl.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: InstanceImpl.java
===================================================================
RCS file: /cvsroot/jboss/jboss-portal/core/src/main/org/jboss/portal/core/impl/model/instance/InstanceImpl.java,v
retrieving revision 1.29
retrieving revision 1.30
diff -u -b -r1.29 -r1.30
--- InstanceImpl.java 19 Jul 2006 12:08:12 -0000 1.29
+++ InstanceImpl.java 30 Jul 2006 12:36:21 -0000 1.30
@@ -23,6 +23,8 @@
import org.hibernate.Query;
import org.hibernate.Session;
+import org.hibernate.HibernateException;
+import org.hibernate.Transaction;
import org.jboss.portal.common.value.ValueMap;
import org.jboss.portal.core.model.instance.Instance;
import org.jboss.portal.core.model.instance.InstanceContainer;
@@ -31,9 +33,11 @@
import org.jboss.portal.portlet.PortletInvokerException;
import org.jboss.portal.portlet.invocation.PortletInvocation;
import org.jboss.portal.portlet.spi.UserContext;
+import org.jboss.portal.portlet.spi.InstanceContext;
import org.jboss.portal.portlet.state.AccessMode;
import org.jboss.portal.portlet.Mode;
import org.jboss.portal.jems.hibernate.ContextObject;
+import org.apache.log4j.Logger;
import java.util.HashMap;
import java.util.HashSet;
@@ -42,11 +46,14 @@
/**
* @author <a href="mailto:julien at jboss.org">Julien Viet</a>
- * @version $Revision: 1.29 $
+ * @version $Revision: 1.30 $
*/
public class InstanceImpl implements Instance, ContextObject
{
+ /** The logger. */
+ private static final Logger log = Logger.getLogger(InstanceImpl.class);
+
// Persistent fields
protected Long key;
@@ -85,12 +92,12 @@
// Hibernate ********************************************************************************************************
- protected Long getKey()
+ public Long getKey()
{
return key;
}
- protected void setKey(Long key)
+ public void setKey(Long key)
{
this.key = key;
}
@@ -183,19 +190,25 @@
public void setPreferences(ValueMap prefs) throws PortletInvokerException
{
- if (!modifiable)
- {
// Get the invoker
PersistentInstanceContainer container = (PersistentInstanceContainer)context.getContainer();
PortletInvoker portletInvoker = container.getPortletInvoker();
+ //
+ if (!modifiable)
+ {
// Clone the portlet
- portletRef = portletInvoker.createClone(portletRef);
+ String clonedPortletId = portletInvoker.createClone(portletRef);
+
+ // Update the state
+ portletRef = clonedPortletId;
+ modifiable = true;
+ Session session = container.getCurrentSession();
+ session.update(this);
}
- PersistentInstanceContainer container = (PersistentInstanceContainer)context.getContainer();
- PortletInvoker invoker = container.getPortletInvoker();
- invoker.setProperties(portletRef, prefs);
+ //
+ portletInvoker.setProperties(portletRef, prefs);
}
public ValueMap getPreferences() throws PortletInvokerException
@@ -207,23 +220,24 @@
public void invoke(PortletInvocation invocation) throws PortletInvokerException
{
- //
- String portletId = portletRef;
- AccessMode accessMode = AccessMode.READ_ONLY;
-
- //
+ boolean debug = log.isDebugEnabled();
PersistentInstanceContainer container = (PersistentInstanceContainer)this.context.getContainer();
- // Try to find if there is a clone existing for this user
+ //
Session session = container.getCurrentSession();
UserContext ctx = invocation.getUserContext();
String userId = ctx.getId();
+
+ //
+ String portletId = portletRef;
+ AccessMode accessMode = AccessMode.READ_ONLY;
if (userId != null)
{
if (Mode.EDIT_DEFAULTS.equals(invocation.getContext().getMode()))
{
// Implement it by using the shared portlet id, but a security check should be made on using this
// mode first
+ // Another idea is to shove that in the security interceptor
throw new UnsupportedOperationException("Implement me");
}
else
@@ -248,33 +262,48 @@
}
}
- // Set the instance context
+ // The instance context for the invocation
InstanceContextImpl instanceContext = new InstanceContextImpl(this, accessMode);
- invocation.setInstanceContext(instanceContext);
try
{
invocation.setAttribute(PortletInvocation.INSTANCE_ID_ATTRIBUTE, instanceId);
invocation.setAttribute(PortletInvocation.PORTLET_ID_ATTRIBUTE, portletId);
+ invocation.setInstanceContext(instanceContext);
// Perform invocation
container.getPortletInvoker().invoke(invocation);
}
finally
{
+ // Reset state before invocation
invocation.removeAttribute(PortletInvocation.INSTANCE_ID_ATTRIBUTE);
invocation.removeAttribute(PortletInvocation.PORTLET_ID_ATTRIBUTE);
+ invocation.setInstanceContext(null);
- //
- if (accessMode == AccessMode.CLONE_BEFORE_WRITE && instanceContext.cloneId != null)
+ // Create user instance if a clone operation occured
+ if (instanceContext.accessMode == AccessMode.CLONE_BEFORE_WRITE && instanceContext.cloneId != null)
{
+ try
+ {
+ if (debug)
+ {
+ log.debug("About to reference clone of (" + instanceId + "," + portletId +
+ ") having id " + instanceContext.cloneId + " for user " + userId);
+ }
UserInstance userInstance = new UserInstance(userId, instanceContext.cloneId);
session.persist(userInstance);
-
userInstance.setInstance(this);
userInstances.add(userInstance);
session.update(this);
}
+ catch (HibernateException e)
+ {
+ //
+ log.error("Cannot reference portlet clone", e);
+ throw e;
+ }
+ }
}
}
}
1.48 +43 -10 jboss-portal/core/src/main/org/jboss/portal/core/impl/model/instance/PersistentInstanceContainer.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: PersistentInstanceContainer.java
===================================================================
RCS file: /cvsroot/jboss/jboss-portal/core/src/main/org/jboss/portal/core/impl/model/instance/PersistentInstanceContainer.java,v
retrieving revision 1.47
retrieving revision 1.48
diff -u -b -r1.47 -r1.48
--- PersistentInstanceContainer.java 19 Jul 2006 12:08:12 -0000 1.47
+++ PersistentInstanceContainer.java 30 Jul 2006 12:36:21 -0000 1.48
@@ -61,7 +61,7 @@
*
* @author <a href="mailto:julien at jboss.org">Julien Viet</a>
* @author Anil.Saldhana at jboss.org
- * @version $Revision: 1.47 $
+ * @version $Revision: 1.48 $
*/
public class PersistentInstanceContainer extends AbstractJBossService
implements InstanceContainer, AuthorizationDomain, DomainConfigurator,
@@ -89,6 +89,9 @@
/** . */
protected ObjectContextualizer contextualizer;
+ /** If true clone the portlet on an instance creation. */
+ protected boolean cloneOnCreate;
+
public PersistentInstanceContainer()
{
ctx = new ContainerContext()
@@ -103,6 +106,16 @@
contextualizer = new ObjectContextualizer(ctx);
}
+ public boolean isCloneOnCreate()
+ {
+ return cloneOnCreate;
+ }
+
+ public void setCloneOnCreate(boolean cloneOnCreate)
+ {
+ this.cloneOnCreate = cloneOnCreate;
+ }
+
public PortletInvoker getPortletInvoker()
{
return portletInvoker;
@@ -169,27 +182,47 @@
return lookup(session, id);
}
- public Instance createInstance(String id, String portletId) throws DuplicateInstanceException, NoSuchPortletException
+ public Instance createInstance(String id, String portletId) throws DuplicateInstanceException, PortletInvokerException
{
if (id == null)
{
throw new IllegalArgumentException("id cannot be null");
}
- try
+
+ //
+ log.debug("Creating instance " + id + " of portlet " + portletId);
+
+ // Check the portlet exists
+ if (portletInvoker.getPortlet(portletId) == null)
{
- // Create the persistent instance
- InstanceImpl instance = new InstanceImpl(ctx, id, portletId);
- Session session = getCurrentSession();
- session.save(instance);
- session.flush();
+ throw new NoSuchPortletException(portletId);
+ }
//
- return instance;
+ InstanceImpl instance = null;
+ try
+ {
+ // Create the persistent instance which may raise a constraint violation exception if it already exist
+ instance = new InstanceImpl(ctx, id, portletId);
+ Session session = getCurrentSession();
+ session.persist(instance);
}
catch (ConstraintViolationException e)
{
throw new DuplicateInstanceException("An instance with id " + id + " already exist");
}
+
+ // Clone the portlet if required
+ if (cloneOnCreate)
+ {
+ // Clone the portlet state now and update the instance
+ String clonedPortletId = portletInvoker.createClone(portletId);
+ instance.setPortletRef(clonedPortletId);
+ instance.setModifiable(true);
+ }
+
+ //
+ return instance;
}
public void destroyInstance(String id) throws PortletInvokerException, NoSuchInstanceException
@@ -210,7 +243,7 @@
}
// Destroy the user instances first
- for (Iterator i = instance.userInstances.iterator(); i.hasNext();)
+ for (Iterator i = instance.getUserInstances().iterator(); i.hasNext();)
{
UserInstance userInstance = (UserInstance)i.next();
More information about the jboss-cvs-commits
mailing list