Author: julien(a)jboss.com
Date: 2007-12-27 16:22:27 -0500 (Thu, 27 Dec 2007)
New Revision: 9394
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/AssociationContext.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/ManagedObject.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/ObjectContext.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/ModelTestCase.java
Log:
implement StateException handling during parent/children loading
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/AssociationContext.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/AssociationContext.java 2007-12-27
20:16:20 UTC (rev 9393)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/AssociationContext.java 2007-12-27
21:22:27 UTC (rev 9394)
@@ -24,6 +24,7 @@
import org.jboss.portal.presentation.impl.model.container.spi.UIContainerObject;
import org.jboss.portal.presentation.model.state.structural.StructuralObject;
+import org.jboss.portal.presentation.model.state.StateException;
import org.jboss.portal.presentation.model.UIObject;
import java.util.List;
@@ -121,51 +122,64 @@
private void load()
{
- StructuralObject relatedSO = doLoad();
-
- // If null it is the root so nothing is done
- if (relatedSO != null)
+ try
{
- UIContainerObject related = owner.container.get(relatedSO);
+ StructuralObject relatedSO = doLoad();
- //
- if (related != null)
+ // If null it is the root so nothing is done
+ if (relatedSO != null)
{
- this.related = related;
+ UIContainerObject related = owner.container.get(relatedSO);
//
- ObjectContext parentContext = (ObjectContext)related.getContext();
+ if (related != null)
+ {
+ this.related = related;
- //
- if (!relatedSO.compareTo(parentContext.structuralObject))
+ //
+ ObjectContext parentContext = (ObjectContext)related.getContext();
+
+ //
+ if (!relatedSO.compareTo(parentContext.structuralObject))
+ {
+ parentContext.status = UIObject.Status.STALE;
+ }
+ }
+ else
{
- parentContext.status = UIObject.Status.STALE;
+ related = owner.container.create(relatedSO);
+
+ //
+ owner.container.put(related);
+
+ //
+ this.related = related;
}
}
- else
- {
- related = owner.container.create(relatedSO);
- //
- owner.container.put(related);
-
- //
- this.related = related;
- }
+ //
+ this.loaded = true;
}
-
- //
- this.loaded = true;
+ catch (StateException e)
+ {
+ owner.updateStatus(e);
+ }
}
UIContainerObject getRelated()
{
+ owner.checkAccess();
+
+ //
if (!loaded)
{
load();
}
//
+ owner.checkAccess();
+
+ //
return related;
}
}
@@ -255,49 +269,53 @@
private void load()
{
- ArrayList<UIContainerObject> relateds = new
ArrayList<UIContainerObject>();
-
- //
- for (StructuralObject relatedSO : doLoad())
+ try
{
- UIContainerObject related = owner.container.get(relatedSO);
+ ArrayList<UIContainerObject> relateds = new
ArrayList<UIContainerObject>();
//
- if (related != null)
+ for (StructuralObject relatedSO : doLoad())
{
- ObjectContext relatedContext = (ObjectContext)related.getContext();
+ UIContainerObject related = owner.container.get(relatedSO);
//
- if (!relatedContext.structuralObject.compareTo(relatedSO))
+ if (related != null)
{
- relatedContext.status = UIObject.Status.STALE;
+ ObjectContext relatedContext = (ObjectContext)related.getContext();
+
+ //
+ if (!relatedContext.structuralObject.compareTo(relatedSO))
+ {
+ relatedContext.status = UIObject.Status.STALE;
+ }
}
- }
- else
- {
- related = owner.container.create(relatedSO);
+ else
+ {
+ related = owner.container.create(relatedSO);
+ //
+ owner.container.put(related);
+ }
+
//
- owner.container.put(related);
+ relateds.add(related);
}
//
- relateds.add(related);
+ this.relateds = relateds;
+ this.loaded = true;
}
-
- //
- this.relateds = relateds;
- this.loaded = true;
+ catch (StateException e)
+ {
+ owner.updateStatus(e);
+ }
}
private class ProxyList extends AbstractList<UIContainerObject>
{
public UIContainerObject get(int i)
{
- if (!owner.isValid())
- {
- throw new IllegalStateException("Relationship not valid");
- }
+ owner.checkAccess();
// Load the entire relationship
if (!isLoaded())
@@ -306,19 +324,26 @@
}
//
+ owner.checkAccess();
+
+ //
return relateds.get(i);
}
public int size()
{
- if (!owner.isValid())
- {
- throw new IllegalStateException("Relationship cannot be used because
the owner " + owner + " is not valid");
- }
+ owner.checkAccess();
+
+ //
if (!isLoaded())
{
load();
}
+
+ //
+ owner.checkAccess();
+
+ //
return relateds.size();
}
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/ManagedObject.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/ManagedObject.java 2007-12-27
20:16:20 UTC (rev 9393)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/ManagedObject.java 2007-12-27
21:22:27 UTC (rev 9394)
@@ -132,10 +132,9 @@
public <T> T getProperty(StateType stateType, String propertyName,
Class<T> propertyType)
{
- if (!context.isValid())
- {
- throw new IllegalStateException();
- }
+ context.checkAccess();
+
+ //
if (stateType == null)
{
throw new IllegalArgumentException();
@@ -170,10 +169,9 @@
public final <T> void setProperty(StateType stateType, String propertyName, T
propertyValue) throws StateChangeVetoException
{
- if (!context.isValid())
- {
- throw new IllegalStateException();
- }
+ context.checkAccess();
+
+ //
if (stateType == null)
{
throw new IllegalArgumentException();
@@ -248,10 +246,9 @@
public final UIContainerObject getChild(String name)
{
- if (!context.isValid())
- {
- throw new IllegalStateException();
- }
+ context.checkAccess();
+
+ //
if (name == null)
{
throw new IllegalArgumentException("No null name accepted");
@@ -272,10 +269,7 @@
public final String getName()
{
- if (!context.isValid())
- {
- throw new IllegalStateException();
- }
+ context.checkAccess();
//
return context.structuralObject.getState().getName();
@@ -283,10 +277,7 @@
public final UIObject getParent()
{
- if (!context.isValid())
- {
- throw new IllegalStateException();
- }
+ context.checkAccess();
//
return context.associationContext.parent.getRelated();
@@ -294,10 +285,7 @@
public final List<UIContainerObject> getChildren()
{
- if (!context.isValid())
- {
- throw new IllegalStateException();
- }
+ context.checkAccess();
//
return context.associationContext.children.getRelateds();
@@ -305,10 +293,9 @@
public final <T extends UIObject> T createChild(String name, Class<T>
type) throws IllegalArgumentException
{
- if (!context.isValid())
- {
- throw new IllegalStateException();
- }
+ context.checkAccess();
+
+ //
if (!isModifiable())
{
throw new IllegalStateException("Cannot change state");
@@ -346,10 +333,9 @@
public final void destroyChild(String name) throws IllegalArgumentException,
StateException
{
- if (!context.isValid())
- {
- throw new IllegalStateException();
- }
+ context.checkAccess();
+
+ //
if (name == null)
{
throw new IllegalArgumentException();
@@ -377,6 +363,9 @@
public final void move(UIObject destination) throws IllegalArgumentException,
StateException
{
+ context.checkAccess();
+
+ //
if (destination == null)
{
throw new IllegalArgumentException("No null object accepted");
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/ObjectContext.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/ObjectContext.java 2007-12-27
20:16:20 UTC (rev 9393)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/impl/model/container/ObjectContext.java 2007-12-27
21:22:27 UTC (rev 9394)
@@ -93,6 +93,19 @@
//
+ void checkAccess() throws NoSuchStateException, StaleStateException
+ {
+ switch (status)
+ {
+ case VALID:
+ break;
+ case INVALID:
+ throw new NoSuchStateException();
+ case STALE:
+ throw new StaleStateException();
+ }
+ }
+
StructuralState getState()
{
return structuralObject.getState();
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/ModelTestCase.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/ModelTestCase.java 2007-12-27
20:16:20 UTC (rev 9393)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/ModelTestCase.java 2007-12-27
21:22:27 UTC (rev 9394)
@@ -46,6 +46,7 @@
import java.util.List;
import java.util.HashSet;
import java.util.Map;
+import java.util.ArrayList;
/**
* @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
@@ -172,7 +173,117 @@
assertSame(context, someOtherPortal.getParent());
assertEquals(Collections.EMPTY_LIST, defaultPortal.getChildren());
}
-
+
+ public void testLoadChildrenFromStale() throws Exception
+ {
+ MockObject mockRoot = model.getRoot();
+ MockObject mockFoo = mockRoot.addChild("foo", MockObject.Type.PORTAL);
+ MockObject mockBar = mockFoo.addChild("bar", MockObject.Type.PORTAL);
+
+ // Load the context object
+ UIObject foo = createContext().getObject(mockFoo.getId());
+
+ // Make a concurrent modification of the children
+ model.destroy(mockBar);
+
+ // Access children
+ try
+ {
+ List<? extends UIObject> children = foo.getChildren();
+ for (UIObject child : children)
+ {
+ }
+ fail();
+ }
+ catch (StaleStateException expected)
+ {
+ }
+
+ // Now state should be stale
+ assertEquals(UIObject.Status.STALE, foo.getStatus());
+ }
+
+ public void testLoadParentFromStale() throws Exception
+ {
+ MockObject mockRoot = model.getRoot();
+ MockObject mockFoo = mockRoot.addChild("foo", MockObject.Type.PORTAL);
+ MockObject mockBar = mockFoo.addChild("bar", MockObject.Type.PORTAL);
+
+ // Load the context object
+ UIObject foo = createContext().getObject(mockFoo.getId());
+
+ // Make a concurrent modification of the children
+ model.destroy(mockBar);
+
+ // Access children
+ try
+ {
+ foo.getParent();
+ fail();
+ }
+ catch (StaleStateException expected)
+ {
+ }
+
+ // Now state should be stale
+ assertEquals(UIObject.Status.STALE, foo.getStatus());
+ }
+
+ public void loadChildrenFromDestroyed() throws Exception
+ {
+ MockObject mockRoot = model.getRoot();
+ MockObject mockFoo = mockRoot.addChild("foo", MockObject.Type.PORTAL);
+ MockObject mockBar = mockFoo.addChild("bar", MockObject.Type.PORTAL);
+
+ // Load the context object
+ UIObject foo = createContext().getObject(mockFoo.getId());
+
+ // Make a concurrent modification of the children
+ model.destroy(mockFoo);
+
+ // Access children
+ try
+ {
+ List<? extends UIObject> children = foo.getChildren();
+ for (UIObject child : children)
+ {
+ }
+ fail();
+ }
+ catch (NoSuchStateException expected)
+ {
+ }
+
+ // Now state should be invalid
+ assertEquals(UIObject.Status.INVALID, foo.getStatus());
+ }
+
+ public void loadParentFromDestroyed() throws Exception
+ {
+ MockObject mockRoot = model.getRoot();
+ MockObject mockFoo = mockRoot.addChild("foo", MockObject.Type.PORTAL);
+ MockObject mockBar = mockFoo.addChild("bar", MockObject.Type.PORTAL);
+
+ // Load the context object
+ UIObject foo = createContext().getObject(mockFoo.getId());
+
+ // Make a concurrent modification of the children
+ model.destroy(mockFoo);
+
+ // Access children
+ try
+ {
+ foo.getParent();
+ fail();
+ }
+ catch (NoSuchStateException expected)
+ {
+ }
+
+ // Now state should be invalid
+ assertEquals(UIObject.Status.INVALID, foo.getStatus());
+ }
+
public void testLoadParent() throws Exception
{
MockObject mockDefaultPortal = model.getRoot().addChild("defaultPortal",
MockObject.Type.PORTAL);
@@ -483,7 +594,7 @@
daaChildren.size();
fail();
}
- catch (IllegalStateException ignore)
+ catch (NoSuchStateException ignore)
{
}
}
@@ -520,7 +631,7 @@
juuChildren.size();
fail();
}
- catch (IllegalStateException ignore)
+ catch (NoSuchStateException ignore)
{
}
try
@@ -528,7 +639,7 @@
daaChildren.size();
fail();
}
- catch (IllegalStateException ignore)
+ catch (NoSuchStateException ignore)
{
}
assertEquals(0, fooChildren.size());