1 8 package org.apache.avalon.phoenix.components.manager; 9 10 import java.util.HashMap ; 11 import org.apache.avalon.excalibur.i18n.ResourceManager; 12 import org.apache.avalon.excalibur.i18n.Resources; 13 import org.apache.avalon.framework.activity.Disposable; 14 import org.apache.avalon.framework.activity.Initializable; 15 import org.apache.avalon.framework.activity.Startable; 16 import org.apache.avalon.framework.logger.AbstractLogEnabled; 17 import org.apache.avalon.phoenix.interfaces.ManagerException; 18 import org.apache.avalon.phoenix.interfaces.SystemManager; 19 20 25 public abstract class AbstractSystemManager 26 extends AbstractLogEnabled 27 implements SystemManager, Initializable, Startable, Disposable 28 { 29 private static final Resources REZ = 30 ResourceManager.getPackageResources( AbstractSystemManager.class ); 31 32 private final HashMap m_entries = new HashMap (); 33 34 private SubContext m_subContext; 35 36 public void initialize() 37 throws Exception 38 { 39 m_subContext = new SubContext( this, null, null ); 40 } 41 42 45 public synchronized void register( final String name, 46 final Object object, 47 final Class [] interfaces ) 48 throws ManagerException, IllegalArgumentException 49 { 50 if( null == interfaces ) 51 { 52 final String message = 53 REZ.getString( "manager.error.interfaces.null", name ); 54 throw new IllegalArgumentException ( message ); 55 } 56 verifyInterfaces( object, interfaces ); 57 58 doRegister( name, object, interfaces ); 59 } 60 61 64 public synchronized void register( final String name, 65 final Object object ) 66 throws ManagerException, IllegalArgumentException 67 { 68 doRegister( name, object, null ); 69 } 70 71 74 public synchronized void unregister( final String name ) 75 throws ManagerException 76 { 77 final ManagedEntry entry = (ManagedEntry)m_entries.remove( name ); 78 if( null == entry ) 79 { 80 final String message = 81 REZ.getString( "manager.error.unregister.noentry", name ); 82 throw new ManagerException( message ); 83 } 84 85 unexport( name, entry.getExportedObject() ); 86 } 87 88 95 public SystemManager getSubContext( String parent, String type ) 96 throws ManagerException 97 { 98 return m_subContext.getSubContext( parent, type ); 99 } 100 101 112 protected abstract Object export( String name, Object object, Class [] interfaces ) 113 throws ManagerException; 114 115 122 protected abstract void unexport( String name, Object exportedObject ) 123 throws ManagerException; 124 125 131 protected void verifyName( final String name, 132 final Object object ) 133 throws ManagerException 134 { 135 } 136 137 143 protected abstract void verifyInterface( Class clazz ) 144 throws ManagerException; 145 146 154 private void verifyInterfaces( final Object object, final Class [] interfaces ) 155 throws ManagerException 156 { 157 for( int i = 0; i < interfaces.length; i++ ) 158 { 159 final Class clazz = interfaces[ i ]; 160 161 if( !clazz.isInterface() ) 162 { 163 final String message = 164 REZ.getString( "manager.error.verify.notinterface", clazz.getName() ); 165 throw new ManagerException( message ); 166 } 167 168 if( !clazz.isInstance( object ) ) 169 { 170 final String message = 171 REZ.getString( "manager.error.verify.notinstance", clazz.getName() ); 172 throw new ManagerException( message ); 173 } 174 175 verifyInterface( clazz ); 176 } 177 } 178 179 188 private void checkRegister( final String name, final Object object ) 189 throws ManagerException, IllegalArgumentException 190 { 191 if( null == object ) 192 { 193 throw new NullPointerException ( "object" ); 194 } 195 196 if( null == name ) 197 { 198 throw new NullPointerException ( "name" ); 199 } 200 201 verifyName( name, object ); 202 203 if( null != m_entries.get( name ) ) 204 { 205 final String message = REZ.getString( "manager.error.register.exists", name ); 206 throw new ManagerException( message ); 207 } 208 } 209 210 218 private void doRegister( final String name, 219 final Object object, 220 final Class [] interfaces ) 221 throws ManagerException 222 { 223 checkRegister( name, object ); 224 225 final Object exportedObject = export( name, object, interfaces ); 226 final ManagedEntry entry = 227 new ManagedEntry( object, interfaces, exportedObject ); 228 m_entries.put( name, entry ); 229 } 230 } 231 | Popular Tags |