1 8 package org.apache.avalon.excalibur.component; 9 10 import java.util.Collections ; 11 import java.util.HashMap ; 12 import java.util.Map ; 13 import org.apache.avalon.framework.configuration.Configurable; 14 import org.apache.avalon.framework.configuration.Configuration; 15 import org.apache.avalon.framework.configuration.ConfigurationException; 16 import org.apache.avalon.framework.logger.AbstractLoggable; 17 18 28 public class DefaultRoleManager 29 extends AbstractLoggable 30 implements RoleManager, Configurable 31 { 32 33 private Map m_shorthands; 34 35 36 private Map m_classNames; 37 38 39 private Map m_hintClassNames; 40 41 42 private final RoleManager m_parent; 43 44 47 public DefaultRoleManager() 48 { 49 m_parent = null; 50 } 51 52 58 public DefaultRoleManager(RoleManager parent) 59 { 60 m_parent = parent; 61 } 62 63 72 public final String getRoleForName( final String shorthandName ) 73 { 74 final String role = (String )m_shorthands.get( shorthandName ); 75 76 if( null == role && null != m_parent ) 77 { 78 return m_parent.getRoleForName( shorthandName ); 79 } 80 81 if (getLogger().isDebugEnabled()) 82 { 83 getLogger().debug( "looking up shorthand " + shorthandName + 84 ", returning " + role ); 85 } 86 87 return role; 88 } 89 90 100 public final String getDefaultClassNameForRole( final String role ) 101 { 102 final String className = (String )m_classNames.get( role ); 103 104 if( null == className && null != m_parent ) 105 { 106 return m_parent.getDefaultClassNameForRole( role ); 107 } 108 109 return className; 110 } 111 112 124 public final String getDefaultClassNameForHint( final String role, 125 final String shorthand ) 126 { 127 if (getLogger().isDebugEnabled()) 128 { 129 getLogger().debug( "looking up hintmap for role " + role ); 130 } 131 132 final Map hintMap = (Map )m_hintClassNames.get( role ); 133 134 if( null == hintMap ) 135 { 136 if( null != m_parent ) 137 { 138 return m_parent.getDefaultClassNameForHint( role, shorthand ); 139 } 140 else 141 { 142 return ""; 143 } 144 } 145 146 if (getLogger().isDebugEnabled()) 147 { 148 getLogger().debug( "looking up classname for hint " + shorthand ); 149 } 150 151 return (String )hintMap.get( shorthand ); 152 } 153 154 161 public final void configure( final Configuration configuration ) 162 throws ConfigurationException 163 { 164 final Map shorts = new HashMap (); 165 final Map classes = new HashMap (); 166 final Map hintclasses = new HashMap (); 167 168 final Configuration[] roles = configuration.getChildren( "role" ); 169 170 for( int i = 0; i < roles.length; i++ ) 171 { 172 final String name = roles[ i ].getAttribute( "name" ); 173 final String shorthand = roles[ i ].getAttribute( "shorthand" ); 174 final String defaultClassName = 175 roles[ i ].getAttribute( "default-class", null ); 176 177 shorts.put( shorthand, name ); 178 179 if( null != defaultClassName ) 180 { 181 classes.put( name, defaultClassName ); 182 } 183 184 final Configuration[] hints = roles[ i ].getChildren( "hint" ); 185 if( hints.length > 0 ) 186 { 187 HashMap hintMap = new HashMap (); 188 189 for( int j = 0; j < hints.length; j++ ) 190 { 191 final String shortHand = hints[ j ].getAttribute("shorthand").trim(); 192 final String className = hints[ j ].getAttribute("class").trim(); 193 194 hintMap.put( shortHand, className ); 195 if (getLogger().isDebugEnabled()) 196 { 197 getLogger().debug( "Adding hint type " + shortHand + 198 " associated with role " + name + 199 " and class " + className ); 200 } 201 } 202 203 hintclasses.put( name, Collections.unmodifiableMap( hintMap ) ); 204 } 205 206 if (getLogger().isDebugEnabled()) 207 { 208 getLogger().debug( "added Role " + name + " with shorthand " + 209 shorthand + " for " + defaultClassName ); 210 } 211 } 212 213 m_shorthands = Collections.unmodifiableMap( shorts ); 214 m_classNames = Collections.unmodifiableMap( classes ); 215 m_hintClassNames = Collections.unmodifiableMap( hintclasses ); 216 } 217 } 218 | Popular Tags |