[picocontainer-scm] [5672] java/2.x/trunk/pico/gems/src/java/org/picocontainer/gems/containers/ReusablePicoContainer.java: Update ReusablePicoContainer to handle flushing adapter registrations as well.

rimovm-yCVjj/[email protected]
Newsgroups gmane.comp.java.picocontainer.cvs
Message-ID <[email protected]>
Revision 5672
Author rimovm
Date 2010-03-30 17:44:00 -0500 (Tue, 30 Mar 2010)

Log Message

Update ReusablePicoContainer to handle flushing adapter registrations as well.

Modified Paths

- java/2.x/trunk/pico/gems/src/java/org/picocontainer/gems/containers/ReusablePicoContainer.java

Diff

Modified: java/2.x/trunk/pico/gems/src/java/org/picocontainer/gems/containers/ReusablePicoContainer.java (5671 => 5672)

--- java/2.x/trunk/pico/gems/src/java/org/picocontainer/gems/containers/ReusablePicoContainer.java 2010-03-30 22:42:59 UTC (rev 5671)
+++ java/2.x/trunk/pico/gems/src/java/org/picocontainer/gems/containers/ReusablePicoContainer.java 2010-03-30 22:44:00 UTC (rev 5672)
@@ -38,7 +38,7 @@
* behavior with a picocontainer)
* </p>
* <p>
- * (2) All instance registrations will be unregistered when stop is called. (For example,
+ * (2) All instance and adapter registrations will be unregistered when stop is called. (For example,
* HttpServletRequest would be removed), and all component adapter instance values
* are flushed.
* </p>
@@ -54,6 +54,8 @@

private final List<ComponentAdapter<?>> instanceRegistrations = new ArrayList<ComponentAdapter<?>>();

+ private final List<ComponentAdapter<?>> adapterRegistrations = new ArrayList<ComponentAdapter<?>>();
+
private final Map<ComponentAdapter<?>, Stored<?>> storedReferences = new HashMap<ComponentAdapter<?>, Stored<?>>();

public ReusablePicoContainer() {
@@ -159,10 +161,14 @@
//Remove all instance registrations.
for (ComponentAdapter<?> eachAdapter : this.instanceRegistrations) {
this.removeComponent(eachAdapter.getComponentKey());
+ }
+ instanceRegistrations.clear();
+
+ for (ComponentAdapter<?> eachAdapter : this.adapterRegistrations) {
+ this.removeComponent(eachAdapter.getComponentKey());
}
+ adapterRegistrations.clear();

- instanceRegistrations.clear();
-
//Flush all remaining objects.
for (Stored<?> eachStoredBehavior : this.storedReferences.values()) {
eachStoredBehavior.flush();
@@ -181,6 +187,19 @@
return this;
}

+ /**
+ * Use this instead of addAdapter in cases where you have custom adapters that the container
+ * cannot figure out whether it should be flushed after each request or not. Anything
+ * added through this method will be flushed after each stop() of the container.
+ * @param componentAdapter
+ * @return <em>this</em> to allow for method chaining.
+ */
+ public MutablePicoContainer addFlushableAdapter(final ComponentAdapter<?> componentAdapter) {
+ adapterRegistrations.add(componentAdapter);
+ addAdapter(componentAdapter);
+ return this;
+ }
+
@Override
public MutablePicoContainer addAdapter(final ComponentAdapter<?> componentAdapter) {
super.addAdapter(componentAdapter);
@@ -199,8 +218,15 @@

@Override
public <T> ComponentAdapter<T> removeComponent(final Object componentKey) {
- ComponentAdapter<T> result = super.removeComponent(componentKey);
- if (result != null) {
+ ComponentAdapter< T > result= null;
+ try {
+ result = super.removeComponent(componentKey);
+ } catch (PicoCompositionException e) {
+ //Help with debugging any lifecycle errors.
+ throw new PicoCompositionException("There was an error removing component by key: '" + componentKey + "'",e);
+ }
+
+ if (result != null) {
removeLocalReferences(result);
}

----------

To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.