KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > container > AbstractContainer


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.container;
9
10 import java.util.HashMap JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Map JavaDoc;
13 import org.apache.avalon.framework.component.Component;
14 import org.apache.avalon.framework.logger.AbstractLoggable;
15
16 /**
17  * This contains it during execution and may provide certain
18  * facilities (like a thread per EJB etc).
19  *
20  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
21  */

22 public abstract class AbstractContainer
23     extends AbstractLoggable
24     implements Container
25 {
26     private final HashMap JavaDoc m_entrys = new HashMap JavaDoc();
27
28     /**
29      * Add a component instance to container.
30      *
31      * @param entry the component entry
32      */

33     public final void add( final String JavaDoc name, final Entry entry )
34         throws ContainerException
35     {
36         checkEntry( name, entry );
37         preAdd( name, entry );
38         m_entrys.put( name, entry );
39         postAdd( name, entry );
40     }
41
42     /**
43      * Remove a component instance from container.
44      *
45      * @param name the name of component
46      */

47     public final void remove( final String JavaDoc name )
48         throws ContainerException
49     {
50         final Entry entry = (Entry)m_entrys.get( name );
51
52         if( null == entry )
53         {
54             throw new ContainerException( "Component named " + name + " not contained" );
55         }
56
57         preRemove( name, entry );
58         m_entrys.remove( name );
59         postRemove( name, entry );
60     }
61
62     /**
63      * Retrieve Entry from container
64      *
65      * @param name the name of entry
66      * @return the entry
67      */

68     public Entry getEntry( final String JavaDoc name )
69         throws ContainerException
70     {
71         final Entry entry = (Entry)m_entrys.get( name );
72
73         if( null == entry )
74         {
75             throw new ContainerException( "Name " + name + " not contained" );
76         }
77         else
78         {
79             return entry;
80         }
81     }
82
83     /**
84      * List all names of entries in container.
85      *
86      * @return the list of all entries
87      */

88     public final String JavaDoc[] list()
89     {
90         return (String JavaDoc[])m_entrys.keySet().toArray( new String JavaDoc[ 0 ] );
91     }
92
93     /**
94      * This method is called before entry is added to give chance for
95      * sub-class to veto removal.
96      *
97      * @param name the name of entry
98      * @param entry the entry
99      * @exception ContainerException to stop removal of entry
100      */

101     protected void preAdd( final String JavaDoc name, final Entry entry )
102         throws ContainerException
103     {
104     }
105
106     /**
107      * This method is called after entry is added to give chance for
108      * sub-class to do some cleanup.
109      *
110      * @param name the name of entry
111      * @param entry the entry
112      */

113     protected void postAdd( final String JavaDoc name, final Entry entry )
114     {
115     }
116
117     /**
118      * This method is called before entry is removed to give chance for
119      * sub-class to veto removal.
120      *
121      * @param name the name of entry
122      * @param entry the entry
123      * @exception ContainerException to stop removal of entry
124      */

125     protected void preRemove( final String JavaDoc name, final Entry entry )
126         throws ContainerException
127     {
128     }
129
130     /**
131      * This method is called after entry is removed to give chance for
132      * sub-class to do some cleanup.
133      *
134      * @param name the name of entry
135      * @param entry the entry
136      */

137     protected void postRemove( final String JavaDoc name, final Entry entry )
138     {
139     }
140
141     /**
142      * List all entries in container.
143      *
144      * @return the list of all entries
145      */

146     protected final Iterator JavaDoc listEntries()
147     {
148         final HashMap JavaDoc clone = new HashMap JavaDoc();
149         clone.putAll( m_entrys );
150         return clone.values().iterator();
151     }
152
153     protected final int getEntryCount()
154     {
155         return m_entrys.size();
156     }
157
158     protected void checkEntry( final String JavaDoc name, final Entry entry )
159         throws ContainerException
160     {
161         if( null != m_entrys.get( name ) )
162         {
163             throw new ContainerException( "Can not add component to container because " +
164                                           "entry already exists with name " + name );
165         }
166     }
167 }
168
Popular Tags