1 17 18 package org.apache.avalon.fortress.impl.role; 19 20 import org.apache.avalon.fortress.MetaInfoEntry; 21 import org.apache.avalon.fortress.MetaInfoManager; 22 import org.apache.avalon.fortress.RoleManager; 23 import org.apache.avalon.framework.logger.AbstractLogEnabled; 24 25 import java.util.HashMap ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.Properties ; 29 30 36 public abstract class AbstractMetaInfoManager extends AbstractLogEnabled implements MetaInfoManager 37 { 38 41 private final ClassLoader m_loader; 42 43 46 private final Map m_shorthands = new HashMap (); 47 48 51 private final Map m_classnames = new HashMap (); 52 53 56 private final MetaInfoManager m_parent; 57 58 61 public AbstractMetaInfoManager() 62 { 63 this( (MetaInfoManager) null ); 64 } 65 66 71 public AbstractMetaInfoManager( final RoleManager parent ) 72 { 73 this( new Role2MetaInfoManager( parent ) ); 74 } 75 76 81 public AbstractMetaInfoManager( final MetaInfoManager parent ) 82 { 83 this( parent, Thread.currentThread().getContextClassLoader() ); 84 } 85 86 93 public AbstractMetaInfoManager( final MetaInfoManager parent, 94 final ClassLoader loader ) 95 { 96 ClassLoader thisLoader = loader; 97 if ( null == thisLoader ) 98 { 99 thisLoader = Thread.currentThread().getContextClassLoader(); 100 } 101 102 m_loader = thisLoader; 103 m_parent = parent; 104 } 105 106 112 protected void addComponent( final String role, 113 final String className, 114 final Properties meta, 115 final List deps ) 116 { 117 final Class klass; 118 119 MetaInfoEntry entry = (MetaInfoEntry) m_classnames.get( className ); 120 121 if ( null != entry ) 122 { 123 entry.addRole( role ); 124 return; 125 } 126 127 if ( getLogger().isDebugEnabled() ) 128 { 129 getLogger().debug( "addComponent component: type='" + className + 130 "', meta='" + meta.toString() + "', role='" + role + "', with deps=" + deps ); 131 } 132 133 try 134 { 135 klass = m_loader.loadClass( className ); 136 } 137 catch ( final ClassNotFoundException e ) 138 { 139 final String message = 140 "Unable to load class " + className + ". Skipping."; 141 getLogger().warn( message ); 142 return; 144 } 145 146 try 147 { 148 entry = new MetaInfoEntry( klass, meta, deps ); 149 entry.addRole( role ); 150 151 m_shorthands.put( entry.getConfigurationName(), entry ); 152 m_classnames.put( className, entry ); 153 } 154 catch ( ClassNotFoundException cfne ) 155 { 156 final String message = 157 "Unable to load the handler class for " + className + ". Skipping."; 158 getLogger().warn( message ); 159 } 160 } 161 162 172 public MetaInfoEntry getMetaInfoForClassname( final String classname ) 173 { 174 final MetaInfoEntry metaEntry = (MetaInfoEntry) m_classnames.get( classname ); 175 if ( null != metaEntry ) 176 { 177 return metaEntry; 178 } 179 else if ( null != m_parent ) 180 { 181 return m_parent.getMetaInfoForClassname( classname ); 182 } 183 else 184 { 185 return null; 186 } 187 } 188 189 195 public MetaInfoEntry getMetaInfoForShortName( final String shortname ) 196 { 197 final MetaInfoEntry metaEntry = (MetaInfoEntry) m_shorthands.get( shortname ); 198 if ( null != metaEntry ) 199 { 200 return metaEntry; 201 } 202 else if ( null != m_parent ) 203 { 204 return m_parent.getMetaInfoForShortName( shortname ); 205 } 206 else 207 { 208 return null; 209 } 210 } 211 212 218 protected ClassLoader getLoader() 219 { 220 return m_loader; 221 } 222 223 230 protected boolean isAlreadyAdded( String className ) 231 { 232 return m_classnames.containsKey( className ); 233 } 234 } 235 | Popular Tags |