1 55 package org.apache.avalon.framework.component; 56 57 import java.util.HashMap ; 58 import java.util.Iterator ; 59 import java.util.Map ; 60 61 75 public class DefaultComponentManager 76 implements ComponentManager 77 { 78 private final HashMap m_components = new HashMap (); 79 private final ComponentManager m_parent; 80 private boolean m_readOnly; 81 82 86 public DefaultComponentManager() 87 { 88 this( null ); 89 } 90 91 96 public DefaultComponentManager( final ComponentManager parent ) 97 { 98 m_parent = parent; 99 } 100 101 108 public Component lookup( final String key ) 109 throws ComponentException 110 { 111 final Component component = (Component)m_components.get( key ); 112 113 if( null != component ) 114 { 115 return component; 116 } 117 else if( null != m_parent ) 118 { 119 return m_parent.lookup( key ); 120 } 121 else 122 { 123 throw new ComponentException( key, "Unable to provide implementation." ); 124 } 125 } 126 127 134 public boolean hasComponent( final String key ) 135 { 136 boolean componentExists = false; 137 138 try 139 { 140 this.release( this.lookup( key ) ); 141 componentExists = true; 142 } 143 catch( Throwable t ) 144 { 145 } 147 148 return componentExists; 149 } 150 151 157 public void put( final String key, final Component component ) 158 { 159 checkWriteable(); 160 m_components.put( key, component ); 161 } 162 163 168 public void release( final Component component ) 169 { 170 } 173 174 179 public String toString() 180 { 181 final StringBuffer buffer = new StringBuffer (); 182 final Iterator components = m_components.keySet().iterator(); 183 buffer.append( "Components:" ); 184 185 while( components.hasNext() ) 186 { 187 buffer.append( "[" ); 188 buffer.append( components.next() ); 189 buffer.append( "]" ); 190 } 191 192 return buffer.toString(); 193 } 194 195 200 protected final ComponentManager getParent() 201 { 202 return m_parent; 203 } 204 205 210 protected final Map getComponentMap() 211 { 212 return m_components; 213 } 214 215 218 public void makeReadOnly() 219 { 220 m_readOnly = true; 221 } 222 223 228 protected final void checkWriteable() 229 throws IllegalStateException 230 { 231 if( m_readOnly ) 232 { 233 final String message = 234 "ComponentManager is read only and can not be modified"; 235 throw new IllegalStateException ( message ); 236 } 237 } 238 } 239 | Popular Tags |