Author: niclas
Date: Sat Sep 18 19:18:31 2004
New Revision: 46311

Added:
   
avalon/trunk/runtime/composition/impl/src/test/org/apache/avalon/composition/model/test/EventTestCase.java
   (contents, props changed)
Modified:
   
avalon/trunk/runtime/composition/impl/src/java/org/apache/avalon/composition/model/impl/DefaultContainmentModel.java
Log:
Improvement of the Event model. Events are propagated to the parent, so one can listen 
for events high up in the hierarchy, instead of many places. Also the removeModel() 
was fixed so that it traverses its child models and remove the explicitly one by one.

Modified: 
avalon/trunk/runtime/composition/impl/src/java/org/apache/avalon/composition/model/impl/DefaultContainmentModel.java
==============================================================================
--- 
avalon/trunk/runtime/composition/impl/src/java/org/apache/avalon/composition/model/impl/DefaultContainmentModel.java
        (original)
+++ 
avalon/trunk/runtime/composition/impl/src/java/org/apache/avalon/composition/model/impl/DefaultContainmentModel.java
        Sat Sep 18 19:18:31 2004
@@ -209,7 +209,8 @@
         assemble();
         synchronized( m_commissioned )
         {
-            if( m_commissioned.isEnabled() ) return;
+            if( m_commissioned.isEnabled() ) 
+                return;
 
             //
             // get the startup sequence and from this
@@ -685,12 +686,14 @@
     * @param name the name of the subsidiary model to be removed
     * @exception IllegalArgumentException if the supplied name is unknown
     */
-    public void removeModel( String name ) throws IllegalArgumentException
+    public void removeModel( String name ) 
+        throws IllegalArgumentException
     {
         ModelRepository repository = m_context.getModelRepository();
+        DeploymentModel model = null;
         synchronized( repository )
         {
-            DeploymentModel model = (DeploymentModel) repository.getModel( name );
+            model = (DeploymentModel) repository.getModel( name );
             if( null == model )
             {
                 final String error = 
@@ -699,14 +702,23 @@
                   + this + "].";
                 throw new IllegalArgumentException( error ); 
             }
-            else
+            if( model instanceof ContainmentModel )
             {
-                m_context.getDependencyGraph().remove( model );
-                repository.removeModel( model );
-                CompositionEvent event = new CompositionEvent( this, model );
-                fireModelRemovedEvent( event );
-            }
-        }
+                ContainmentModel container = (ContainmentModel) model;
+                removeChildModels( container );
+            }            
+            m_context.getDependencyGraph().remove( model );
+            repository.removeModel( model );
+        }
+        CompositionEvent event = new CompositionEvent( this, model );
+        fireModelRemovedEvent( event );
+    }
+    
+    private void removeChildModels( ContainmentModel container )
+    {
+        DeploymentModel[] children = container.getModels();
+        for( int i = 0 ; i < children.length ; i++ )
+            container.removeModel( children[i].getName() );
     }
 
    /**
@@ -848,10 +860,10 @@
         {
             repository.addModel( name, model );
             m_context.getDependencyGraph().add( model );
-            CompositionEvent event = new CompositionEvent( this, model );
-            fireModelAddedEvent( event );
-            return model;
         }
+        CompositionEvent event = new CompositionEvent( this, model );
+        fireModelAddedEvent( event );
+        return model;
     }
 
     private void fireModelAddedEvent( CompositionEvent event )
@@ -872,6 +884,9 @@
                 getLogger().warn( error, e );
             }
         }
+        ContainmentModel parent = m_context.getParentContainmentModel();
+        if( parent instanceof DefaultContainmentModel )
+            ((DefaultContainmentModel) parent).fireModelAddedEvent( event );
     }
 
     private void fireModelRemovedEvent( CompositionEvent event )
@@ -892,6 +907,9 @@
                 getLogger().warn( error, e );
             }
         }
+        ContainmentModel parent = m_context.getParentContainmentModel();
+        if( parent instanceof DefaultContainmentModel )
+            ((DefaultContainmentModel) parent).fireModelRemovedEvent( event );
     }
 
    /**

Added: 
avalon/trunk/runtime/composition/impl/src/test/org/apache/avalon/composition/model/test/EventTestCase.java
==============================================================================
--- (empty file)
+++ 
avalon/trunk/runtime/composition/impl/src/test/org/apache/avalon/composition/model/test/EventTestCase.java
  Sat Sep 18 19:18:31 2004
@@ -0,0 +1,98 @@
+/* 
+ * Copyright 2004 Apache Software Foundation
+ * Licensed  under the  Apache License,  Version 2.0  (the "License");
+ * you may not use  this file  except in  compliance with the License.
+ * You may obtain a copy of the License at 
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed  under the  License is distributed on an "AS IS" BASIS,
+ * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
+ * implied.
+ * 
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.avalon.composition.model.test;
+
+import org.apache.avalon.composition.data.ComponentProfile;
+import org.apache.avalon.composition.data.ContainmentProfile;
+import org.apache.avalon.composition.data.DeploymentProfile;
+
+import org.apache.avalon.composition.event.CompositionEvent;
+import org.apache.avalon.composition.event.CompositionListener;
+
+import org.apache.avalon.composition.model.ModelException;
+import org.apache.avalon.composition.model.DeploymentModel;
+import org.apache.avalon.composition.model.ClassLoaderModel;
+import org.apache.avalon.composition.model.TypeRepository;
+import org.apache.avalon.composition.model.ComponentModel;
+import org.apache.avalon.composition.model.ContainmentModel;
+import org.apache.avalon.composition.model.DependencyModel;
+
+import org.apache.avalon.meta.info.Type;
+import org.apache.avalon.meta.info.ReferenceDescriptor;
+
+import org.apache.avalon.util.exception.ExceptionHelper;
+
+
+public class EventTestCase extends AbstractTestCase
+    implements CompositionListener
+{      
+    private static final String WIDGET = 
+      "org.apache.avalon.test.dynamics.Widget";
+
+    private static final String WIDGET_CLASS = 
+      "org.apache.avalon.test.dynamics.DefaultWidget";
+
+    private static final String GIZMO = 
+      "org.apache.avalon.test.dynamics.Gizmo";
+
+    private static final ContainmentProfile PROFILE = 
+      new ContainmentProfile();
+
+    private ContainmentModel m_container;
+
+    private int m_Counter;
+    
+   //-------------------------------------------------------
+   // tests
+   //-------------------------------------------------------
+
+   /**
+    * Validate resolution of a widget.
+    */
+    public void testEvents() throws Exception
+    {
+        m_Counter = 0;
+        m_model = super.setUp( "dynamics.xml" );
+        m_model.addCompositionListener( this );
+        m_model.assemble();
+        ContainmentModel c1 = (ContainmentModel) m_model.addModel( PROFILE );
+        c1.assemble();
+        ContainmentModel c2 = (ContainmentModel) c1.addModel( PROFILE );
+        c2.assemble();
+        ContainmentModel c3 = (ContainmentModel) c2.addModel( PROFILE );
+        c3.assemble();
+        ContainmentModel c4 = (ContainmentModel) c3.addModel( PROFILE );
+        c4.assemble();
+        assertEquals( "ModelAdded events are missing.", 4, m_Counter );
+        m_model.removeModel( c1.getName() );
+        assertEquals( "ModelRemoved events are missing.", 0, m_Counter );
+    }
+       
+    public void modelAdded( CompositionEvent event )
+    {
+        System.out.println( "Model Added Event: " + event );
+        m_Counter++;
+    }
+    
+    public void modelRemoved( CompositionEvent event )
+    {
+        System.out.println( "Model Removed Event: " + event );
+        m_Counter--;
+    }
+    
+}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to