1 8 package org.apache.avalon.excalibur.system; 9 10 import org.apache.avalon.framework.component.Component; 11 12 import java.util.ArrayList ; 13 import java.util.Collections ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 import java.util.Map ; 17 18 26 public abstract class AbstractRoleManager 27 implements RoleManager 28 { 29 protected static final String EMPTY_STRING = ""; 30 31 protected final ClassLoader m_loader; 32 33 34 protected Map m_shorthands; 35 36 37 protected Map m_classNames; 38 39 40 protected Map m_handlerNames; 41 42 43 protected final RoleManager m_parent; 44 45 48 public AbstractRoleManager() 49 { 50 this( null ); 51 } 52 53 59 public AbstractRoleManager(RoleManager parent) 60 { 61 this( parent, Thread.currentThread().getContextClassLoader() ); 62 } 63 64 70 public AbstractRoleManager(RoleManager parent, ClassLoader loader) 71 { 72 ClassLoader thisLoader = loader; 73 74 if ( null == thisLoader ) 75 { 76 thisLoader = Thread.currentThread().getContextClassLoader(); 77 } 78 79 m_loader = thisLoader; 80 m_parent = parent; 81 } 82 83 protected void setup( Map shorts, Map classes, Map handlers, 84 String shortName, String role, String className, 85 String handlerClassName ) 86 { 87 final Class klass; 88 Class handlerKlass; 89 90 try 91 { 92 klass = m_loader.loadClass( className ); 93 94 if ( ! Component.class.isAssignableFrom( klass ) ) 95 { 96 return; 98 } 99 } 100 catch ( Exception e ) 101 { 102 return; 104 } 105 106 try 107 { 108 handlerKlass = m_loader.loadClass( handlerClassName ); 109 } 110 catch ( Exception e ) 111 { 112 handlerKlass = org.apache.avalon.excalibur.system.handler.PerThreadComponentHandler.class; 113 } 114 115 shorts.put( shortName, klass ); 116 shorts.put( klass, shortName ); 117 classes.put( klass, 118 role ); 119 120 List classList = (List ) classes.get( role ); 121 122 if ( null == classList ) 123 { 124 classList = new ArrayList (5); 125 } 126 127 classList.add( klass ); 128 classes.put( role, classList ); 129 130 handlers.put( klass, handlerKlass ); 131 } 132 133 140 public final Class getClassForName( final String shorthandName ) 141 { 142 final Class component = (Class )m_shorthands.get( shorthandName ); 143 144 if( null == component && null != m_parent ) 145 { 146 return m_parent.getClassForName( shorthandName ); 147 } 148 149 return component; 150 } 151 152 161 public final String getNameForClass( final Class klass ) 162 { 163 final String shorthandName = (String )m_shorthands.get( klass ); 164 165 if( null == shorthandName && null != m_parent ) 166 { 167 return m_parent.getNameForClass( klass ); 168 } 169 170 return shorthandName; 171 } 172 173 182 public final Class getHandlerClassForClass( final Class className ) 183 { 184 final Class handler = (Class )m_handlerNames.get( className ); 185 186 if( null == handler && null != m_parent ) 187 { 188 return m_parent.getHandlerClassForClass( className ); 189 } 190 191 return handler; 192 } 193 194 204 public final Class [] getClassesForRole( final String role ) 205 { 206 final List classes = (List )m_classNames.get( role ); 207 208 if( null == classes && null != m_parent ) 209 { 210 return m_parent.getClassesForRole( role ); 211 } 212 213 return (Class []) classes.toArray( new Class [] {} ); 214 } 215 216 228 public final String getRoleForClass( final Class klass ) 229 { 230 final String role = (String )m_classNames.get( klass ); 231 232 if( null == role ) 233 { 234 if( null != m_parent ) 235 { 236 return m_parent.getRoleForClass( klass ); 237 } 238 else 239 { 240 return EMPTY_STRING; 241 } 242 } 243 244 return role; 245 } 246 } 247 | Popular Tags |