1 17 18 package org.apache.avalon.fortress.impl.role; 19 20 import org.apache.avalon.fortress.RoleEntry; 21 import org.apache.avalon.fortress.RoleManager; 22 import org.apache.avalon.fortress.impl.handler.PerThreadComponentHandler; 23 import org.apache.avalon.framework.logger.AbstractLogEnabled; 24 25 import java.util.HashMap ; 26 import java.util.Map ; 27 28 36 public abstract class AbstractRoleManager 37 extends AbstractLogEnabled 38 implements RoleManager 39 { 40 43 private final ClassLoader m_loader; 44 45 48 private final Map m_shorthands = new HashMap (); 49 50 53 private final Map m_classnames = new HashMap (); 54 55 58 private final RoleManager m_parent; 59 60 63 public AbstractRoleManager() 64 { 65 this( null ); 66 } 67 68 74 public AbstractRoleManager( final RoleManager parent ) 75 { 76 this( parent, Thread.currentThread().getContextClassLoader() ); 77 } 78 79 86 public AbstractRoleManager( final RoleManager parent, 87 final ClassLoader loader ) 88 { 89 ClassLoader thisLoader = loader; 90 if ( null == thisLoader ) 91 { 92 thisLoader = Thread.currentThread().getContextClassLoader(); 93 } 94 95 m_loader = thisLoader; 96 m_parent = parent; 97 } 98 99 106 protected final boolean addRole( final String shortName, 107 final String role, 108 final String className, 109 final String handlerClassName ) 110 { 111 final Class clazz; 112 final Class handlerKlass; 113 114 try 115 { 116 clazz = m_loader.loadClass( className ); 117 } 118 catch ( final Exception e ) 119 { 120 final String message = 121 "Unable to load class " + className + ". Skipping."; 122 getLogger().warn( message ); 123 return false; 124 } 125 126 if ( null != handlerClassName ) 127 { 128 try 129 { 130 handlerKlass = m_loader.loadClass( handlerClassName ); 131 } 132 catch ( final Exception e ) 133 { 134 final String message = "Unable to load handler " + 135 handlerClassName + " for class " + className + ". Skipping."; 136 getLogger().warn( message ); 137 return false; 138 } 139 } 140 else 141 { 142 handlerKlass = getDefaultHandler(); 143 } 144 145 if ( getLogger().isDebugEnabled() ) 146 { 147 getLogger().debug( "addRole role: name='" + shortName + "', role='" + role + "', " 148 + "class='" + className + "', handler='" + handlerClassName + "'" ); 149 } 150 151 final RoleEntry entry = new RoleEntry( role, shortName, clazz, handlerKlass ); 152 m_shorthands.put( shortName, entry ); 153 m_classnames.put( className, entry ); 154 155 return true; 156 } 157 158 163 protected final Class getDefaultHandler() 164 { 165 return PerThreadComponentHandler.class; 166 } 167 168 public final RoleEntry getRoleForClassname( final String classname ) 169 { 170 final RoleEntry roleEntry = (RoleEntry) m_classnames.get( classname ); 171 if ( null != roleEntry ) 172 { 173 return roleEntry; 174 } 175 else if ( null != m_parent ) 176 { 177 return m_parent.getRoleForClassname( classname ); 178 } 179 else 180 { 181 return null; 182 } 183 } 184 185 191 public final RoleEntry getRoleForShortName( final String shortname ) 192 { 193 final RoleEntry roleEntry = (RoleEntry) m_shorthands.get( shortname ); 194 if ( null != roleEntry ) 195 { 196 return roleEntry; 197 } 198 else if ( null != m_parent ) 199 { 200 return m_parent.getRoleForShortName( shortname ); 201 } 202 else 203 { 204 return null; 205 } 206 } 207 208 214 protected final ClassLoader getLoader() 215 { 216 return m_loader; 217 } 218 } 219 220 | Popular Tags |