Re: [picocontainer-dev] While looking at the streamlining of NanoWar ...
Paul Hammant <[email protected]>
| Newsgroups | gmane.comp.java.picocontainer.devel |
|---|---|
| Message-ID | <[email protected]> |
This is better I think -
The impl of LifecycleState for session and request container use,
would either be a Null-impl or do the same ThreadLocal stuff as the
'Storing' behavior
Thoughts?
I'm not going to commit it of course. Or rather only with a working
NanoWar in the new style.
- Paul
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
pluggable-state.patch
(application/octet-stream, 13.8 KB)
Index: container/src/test/org/picocontainer/lifecycle/DefaultLifecycleStateTestCase.java
===================================================================
--- container/src/test/org/picocontainer/lifecycle/DefaultLifecycleStateTestCase.java (revision 0)
+++ container/src/test/org/picocontainer/lifecycle/DefaultLifecycleStateTestCase.java (revision 0)
@@ -0,0 +1,45 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved. *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file. *
+ *****************************************************************************/
+package org.picocontainer.lifecycle;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.picocontainer.lifecycle.DefaultLifecycleState.State.CONSTRUCTED;
+import static org.picocontainer.lifecycle.DefaultLifecycleState.State.DISPOSED;
+import static org.picocontainer.lifecycle.DefaultLifecycleState.State.STARTED;
+import static org.picocontainer.lifecycle.DefaultLifecycleState.State.STOPPED;
+
+import org.junit.Test;
+
+/**
+ * @author Michael Rimov
+ */
+public class DefaultLifecycleStateTestCase {
+
+ @Test public void testIsStartAllowedOptions() {
+ assertTrue(CONSTRUCTED.isStartAllowed());
+ assertFalse(STARTED.isStartAllowed());
+ assertTrue(STOPPED.isStartAllowed());
+ assertFalse(DISPOSED.isStartAllowed());
+ }
+
+ @Test public void testIsStopAllowedOptions() {
+ assertFalse(CONSTRUCTED.isStopAllowed());
+ assertTrue(STARTED.isStopAllowed());
+ assertFalse(STOPPED.isStopAllowed());
+ assertFalse(DISPOSED.isStopAllowed());
+ }
+
+ @Test public void testIsDisposeAllowedOptions() {
+ assertTrue(CONSTRUCTED.isDisposedAllowed());
+ assertFalse(STARTED.isDisposedAllowed());
+ assertTrue(STOPPED.isDisposedAllowed());
+ assertFalse(DISPOSED.isDisposedAllowed());
+ }
+
+}
Index: container/src/test/org/picocontainer/lifecycle/LifecycleStateTestCase.java
===================================================================
--- container/src/test/org/picocontainer/lifecycle/LifecycleStateTestCase.java (revision 4422)
+++ container/src/test/org/picocontainer/lifecycle/LifecycleStateTestCase.java (working copy)
@@ -1,45 +0,0 @@
-/*****************************************************************************
- * Copyright (C) PicoContainer Organization. All rights reserved. *
- * ------------------------------------------------------------------------- *
- * The software in this package is published under the terms of the BSD *
- * style license a copy of which has been included with this distribution in *
- * the LICENSE.txt file. *
- *****************************************************************************/
-package org.picocontainer.lifecycle;
-
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-import static org.picocontainer.lifecycle.LifecycleState.CONSTRUCTED;
-import static org.picocontainer.lifecycle.LifecycleState.DISPOSED;
-import static org.picocontainer.lifecycle.LifecycleState.STARTED;
-import static org.picocontainer.lifecycle.LifecycleState.STOPPED;
-
-import org.junit.Test;
-
-/**
- * @author Michael Rimov
- */
-public class LifecycleStateTestCase {
-
- @Test public void testIsStartAllowedOptions() {
- assertTrue(CONSTRUCTED.isStartAllowed());
- assertFalse(STARTED.isStartAllowed());
- assertTrue(STOPPED.isStartAllowed());
- assertFalse(DISPOSED.isStartAllowed());
- }
-
- @Test public void testIsStopAllowedOptions() {
- assertFalse(CONSTRUCTED.isStopAllowed());
- assertTrue(STARTED.isStopAllowed());
- assertFalse(STOPPED.isStopAllowed());
- assertFalse(DISPOSED.isStopAllowed());
- }
-
- @Test public void testIsDisposeAllowedOptions() {
- assertTrue(CONSTRUCTED.isDisposedAllowed());
- assertFalse(STARTED.isDisposedAllowed());
- assertTrue(STOPPED.isDisposedAllowed());
- assertFalse(DISPOSED.isDisposedAllowed());
- }
-
-}
Index: container/src/java/org/picocontainer/lifecycle/LifecycleState.java
===================================================================
--- container/src/java/org/picocontainer/lifecycle/LifecycleState.java (revision 4422)
+++ container/src/java/org/picocontainer/lifecycle/LifecycleState.java (working copy)
@@ -12,72 +12,19 @@
* Current lifecycle state of the container.
* @author Michael Rimov
*/
-public enum LifecycleState {
+public interface LifecycleState {
- /**
- * Default state of a container once it has been built.
- */
- CONSTRUCTED,
-
- /**
- * 'Start' Lifecycle has been called.
- */
- STARTED,
-
- /**
- * 'Stop' lifecycle has been called.
- */
- STOPPED,
-
- /**
- * 'Dispose' lifecycle has been called.
- */
- DISPOSED;
-
-
- /**
- * Start is normally allowed if the object is constructed or
- * already stopped. It is not allowed if the system is already
- * started or disposed.
- * @return true if start lifecycle methods should be allowed.
- */
- public boolean isStartAllowed() {
- if (this.equals(CONSTRUCTED) || this.equals(STOPPED)) {
- return true;
- }
-
- return false;
- }
-
- /**
- * Returns true if stop is normally allowed in the container
- * lifecycle. Stop is normally only allowed while the current
- * container state is STARTED.
- * @return true if stop is allowed.
- */
- public boolean isStopAllowed() {
- if (this.equals(STARTED)) {
- return true;
- }
-
- return false;
- }
-
- public boolean isStarted() {
- return this.equals(STARTED);
- }
-
- /**
- * Returns true if the dispose lifecycle method is normally called.
- * Dispose is normally only allowed if the object has not been already
- * disposed, and it is not started.
- * @return
- */
- public boolean isDisposedAllowed() {
- if (this.equals(STOPPED) || this.equals(CONSTRUCTED)) {
- return true;
- }
-
- return false;
- }
+ void removingComponent();
+
+ void starting();
+
+ void stopping();
+
+ void stopped();
+
+ boolean isStarted();
+
+ void disposing();
+
+ void disposed();
}
Index: container/src/java/org/picocontainer/lifecycle/DefaultLifecycleState.java
===================================================================
--- container/src/java/org/picocontainer/lifecycle/DefaultLifecycleState.java (revision 0)
+++ container/src/java/org/picocontainer/lifecycle/DefaultLifecycleState.java (revision 0)
@@ -0,0 +1,125 @@
+package org.picocontainer.lifecycle;
+
+import org.picocontainer.PicoCompositionException;
+
+import java.io.Serializable;
+
+public class DefaultLifecycleState implements LifecycleState, Serializable {
+
+ private State state = State.CONSTRUCTED;
+
+ public void removingComponent() {
+ if (state == State.STARTED) {
+ throw new PicoCompositionException("Cannot remove components after the container has started");
+ }
+
+ if (state == State.DISPOSED) {
+ throw new PicoCompositionException("Cannot remove components after the container has been disposed");
+ }
+ }
+
+ public void starting() {
+ if (!state.isStartAllowed()) {
+ throw new IllegalStateException("Cannot start. Current container state was: " + state);
+ }
+
+ state = State.STARTED;
+
+ }
+
+ public void stopping() {
+ if (!state.isStopAllowed()) {
+ throw new IllegalStateException("Cannot stop. Current container state was: " + state);
+ }
+ }
+
+ public void stopped() {
+ state = State.STOPPED;
+ }
+
+ public boolean isStarted() {
+ return state == State.STARTED;
+ }
+
+ public void disposing() {
+ if (!state.isDisposedAllowed()) {
+ throw new IllegalStateException("Cannot dispose. Current lifecycle state is: " + state);
+ }
+
+ }
+
+ public void disposed() {
+ state = State.DISPOSED;
+ }
+
+ public enum State {
+
+ /**
+ * Default state of a container once it has been built.
+ */
+ CONSTRUCTED,
+
+ /**
+ * 'Start' Lifecycle has been called.
+ */
+ STARTED,
+
+ /**
+ * 'Stop' lifecycle has been called.
+ */
+ STOPPED,
+
+ /**
+ * 'Dispose' lifecycle has been called.
+ */
+ DISPOSED;
+
+
+ /**
+ * Start is normally allowed if the object is constructed or
+ * already stopped. It is not allowed if the system is already
+ * started or disposed.
+ * @return true if start lifecycle methods should be allowed.
+ */
+ public boolean isStartAllowed() {
+ if (this.equals(CONSTRUCTED) || this.equals(STOPPED)) {
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Returns true if stop is normally allowed in the container
+ * lifecycle. Stop is normally only allowed while the current
+ * container state is STARTED.
+ * @return true if stop is allowed.
+ */
+ public boolean isStopAllowed() {
+ if (this.equals(STARTED)) {
+ return true;
+ }
+
+ return false;
+ }
+
+ public boolean isStarted() {
+ return this.equals(STARTED);
+ }
+
+ /**
+ * Returns true if the dispose lifecycle method is normally called.
+ * Dispose is normally only allowed if the object has not been already
+ * disposed, and it is not started.
+ * @return
+ */
+ public boolean isDisposedAllowed() {
+ if (this.equals(STOPPED) || this.equals(CONSTRUCTED)) {
+ return true;
+ }
+
+ return false;
+ }
+}
+
+}
Index: container/src/java/org/picocontainer/DefaultPicoContainer.java
===================================================================
--- container/src/java/org/picocontainer/DefaultPicoContainer.java (revision 4422)
+++ container/src/java/org/picocontainer/DefaultPicoContainer.java (working copy)
@@ -20,8 +20,8 @@
import org.picocontainer.injectors.AbstractInjector;
import org.picocontainer.injectors.AdaptingInjection;
import org.picocontainer.lifecycle.LifecycleState;
-import static org.picocontainer.lifecycle.LifecycleState.*;
import org.picocontainer.lifecycle.StartableLifecycleStrategy;
+import org.picocontainer.lifecycle.DefaultLifecycleState;
import org.picocontainer.monitors.NullComponentMonitor;
import java.io.Serializable;
@@ -96,9 +96,9 @@
/**
* Current state of the container.
*/
- private LifecycleState lifecycleState = CONSTRUCTED;
-
- /**
+ private LifecycleState lifecycleState = new DefaultLifecycleState();
+
+ /**
* Keeps track of child containers started status.
*/
private final Set<WeakReference<PicoContainer>> childrenStarted = new HashSet<WeakReference<PicoContainer>>();
@@ -384,14 +384,8 @@
/** {@inheritDoc} **/
public <T> ComponentAdapter<T> removeComponent(final Object componentKey) {
- if (lifecycleState == STARTED) {
- throw new PicoCompositionException("Cannot remove components after the container has started");
- }
-
- if (lifecycleState == DISPOSED) {
- throw new PicoCompositionException("Cannot remove components after the container has been disposed");
- }
-
+ lifecycleState.removingComponent();
+
ComponentAdapter<T> adapter = (ComponentAdapter<T>) getComponentKeyToAdapterCache().remove(componentKey);
getModifiableComponentAdapterList().remove(adapter);
getOrderedComponentAdapters().remove(adapter);
@@ -632,12 +626,8 @@
* @see #removeChildContainer(PicoContainer)
*/
public void start() {
-
- if (!lifecycleState.isStartAllowed()) {
- throw new IllegalStateException("Cannot start. Current container state was: " + lifecycleState);
- }
- lifecycleState = STARTED;
+ lifecycleState.starting();
startAdapters();
childrenStarted.clear();
@@ -665,11 +655,10 @@
* @see #removeChildContainer(PicoContainer)
*/
public void stop() {
- if (!lifecycleState.isStopAllowed()) {
- throw new IllegalStateException("Cannot stop. Current container state was: " + lifecycleState);
- }
- for (PicoContainer child : children) {
+ lifecycleState.stopping();
+
+ for (PicoContainer child : children) {
if (childStarted(child)) {
if (child instanceof Startable) {
((Startable)child).stop();
@@ -677,7 +666,7 @@
}
}
stopAdapters();
- lifecycleState = STOPPED;
+ lifecycleState.stopped();
}
/**
@@ -719,20 +708,23 @@
if (lifecycleState.isStarted()) {
stop();
}
-
- if (!lifecycleState.isDisposedAllowed()) {
- throw new IllegalStateException("Cannot dispose. Current lifecycle state is: " + lifecycleState);
- }
+ lifecycleState.disposing();
+
for (PicoContainer child : children) {
if (child instanceof MutablePicoContainer) {
((Disposable)child).dispose();
}
}
disposeAdapters();
- lifecycleState = DISPOSED;
+
+ lifecycleState.disposed();
}
+ public void setLifecycleState(LifecycleState lifecycleState) {
+ this.lifecycleState = lifecycleState;
+ }
+
public MutablePicoContainer makeChildContainer() {
DefaultPicoContainer pc = new DefaultPicoContainer(componentFactory, lifecycleStrategy, this);
addChildContainer(pc);
@@ -775,7 +767,7 @@
checkCircularChildDependencies(child);
if (children.add(child)) {
// @todo Should only be added if child container has also be started
- if (lifecycleState == STARTED) {
+ if (lifecycleState.isStarted()) {
childrenStarted.add(new WeakReference<PicoContainer>(child));
}
}