1 8 package org.apache.avalon.excalibur.container; 9 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.Map ; 13 import org.apache.avalon.framework.component.Component; 14 import org.apache.avalon.framework.logger.AbstractLoggable; 15 16 22 public abstract class AbstractContainer 23 extends AbstractLoggable 24 implements Container 25 { 26 private final HashMap m_entrys = new HashMap (); 27 28 33 public final void add( final String 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 47 public final void remove( final String 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 68 public Entry getEntry( final String 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 88 public final String [] list() 89 { 90 return (String [])m_entrys.keySet().toArray( new String [ 0 ] ); 91 } 92 93 101 protected void preAdd( final String name, final Entry entry ) 102 throws ContainerException 103 { 104 } 105 106 113 protected void postAdd( final String name, final Entry entry ) 114 { 115 } 116 117 125 protected void preRemove( final String name, final Entry entry ) 126 throws ContainerException 127 { 128 } 129 130 137 protected void postRemove( final String name, final Entry entry ) 138 { 139 } 140 141 146 protected final Iterator listEntries() 147 { 148 final HashMap clone = new HashMap (); 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 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 |