1 55 package org.apache.avalon.framework.service; 56 57 import java.util.HashMap ; 58 import java.util.Iterator ; 59 import java.util.Map ; 60 61 69 public class DefaultServiceManager 70 implements ServiceManager 71 { 72 private final HashMap m_objects = new HashMap (); 73 private final ServiceManager m_parent; 74 private boolean m_readOnly; 75 76 80 public DefaultServiceManager() 81 { 82 this( null ); 83 } 84 85 90 public DefaultServiceManager( final ServiceManager parent ) 91 { 92 m_parent = parent; 93 } 94 95 102 public Object lookup( final String key ) 103 throws ServiceException 104 { 105 final Object object = m_objects.get( key ); 106 if( null != object ) 107 { 108 return object; 109 } 110 else if( null != m_parent ) 111 { 112 return m_parent.lookup( key ); 113 } 114 else 115 { 116 final String message = "Unable to provide implementation for " + key; 117 throw new ServiceException( key, message, null ); 118 } 119 } 120 121 127 public boolean hasService( final String key ) 128 { 129 try 130 { 131 lookup( key ); 132 return true; 133 } 134 catch( final Throwable t ) 135 { 136 return false; 137 } 138 } 139 140 146 public void put( final String key, final Object object ) 147 { 148 checkWriteable(); 149 m_objects.put( key, object ); 150 } 151 152 158 public String toString() 159 { 160 final StringBuffer buffer = new StringBuffer (); 161 final Iterator objects = m_objects.keySet().iterator(); 162 buffer.append( "Services:" ); 163 164 while( objects.hasNext() ) 165 { 166 buffer.append( "[" ); 167 buffer.append( objects.next() ); 168 buffer.append( "]" ); 169 } 170 171 return buffer.toString(); 172 } 173 174 179 protected final ServiceManager getParent() 180 { 181 return m_parent; 182 } 183 184 189 protected final Map getObjectMap() 190 { 191 return m_objects; 192 } 193 194 198 public void makeReadOnly() 199 { 200 m_readOnly = true; 201 } 202 203 209 protected final void checkWriteable() 210 throws IllegalStateException 211 { 212 if( m_readOnly ) 213 { 214 final String message = 215 "ServiceManager is read only and can not be modified"; 216 throw new IllegalStateException ( message ); 217 } 218 } 219 220 224 public void release( Object object ) 225 { 226 } 227 } 228 | Popular Tags |