1 17 18 package org.apache.avalon.fortress; 19 20 import java.util.*; 21 22 31 public final class MetaInfoEntry 32 { 33 public static final String THREADSAFE_HANDLER = "org.apache.avalon.fortress.impl.handler.ThreadSafeComponentHandler"; 34 public static final String POOLABLE_HANDLER = "org.apache.avalon.fortress.impl.handler.PoolableComponentHandler"; 35 public static final String FACTORY_HANDLER = "org.apache.avalon.fortress.impl.handler.FactoryComponentHandler"; 36 public static final String PER_THREAD_HANDLER = "org.apache.avalon.fortress.impl.handler.PerThreadComponentHandler"; 37 38 private final Class m_klass; 39 private final String m_configName; 40 private final Class m_handler; 41 private final Set m_roles; 42 private volatile boolean m_readOnly = false; 43 44 45 private static final Map m_lifecycleMap; 46 private final List m_dependencies; 47 private static final String TYPE_SINGLETON = "singleton"; 48 private static final String TYPE_THREAD = "thread"; 49 private static final String TYPE_POOLED = "pooled"; 50 private static final String TYPE_TRANSIENT = "transient"; 51 52 static 54 { 55 Map lifecycleMap = new HashMap(); 56 lifecycleMap.put( TYPE_SINGLETON, THREADSAFE_HANDLER ); 57 lifecycleMap.put( TYPE_THREAD, PER_THREAD_HANDLER ); 58 lifecycleMap.put( TYPE_POOLED, POOLABLE_HANDLER ); 59 lifecycleMap.put( TYPE_TRANSIENT, FACTORY_HANDLER ); 60 61 m_lifecycleMap = Collections.unmodifiableMap( lifecycleMap ); 62 } 63 64 73 public MetaInfoEntry( final Class componentClass, final Properties properties, final List deps ) throws ClassNotFoundException 74 { 75 if ( null == componentClass ) throw new NullPointerException ( "\"componentClass\" cannot be null." ); 76 if ( null == properties ) throw new NullPointerException ( "\"properties\" cannot be null." ); 77 if ( null == deps ) throw new NullPointerException ( "\"deps\" cannot be null." ); 78 79 m_klass = componentClass; 80 m_configName = properties.getProperty( "x-avalon.name", createShortName( componentClass.getName() ) ); 81 m_handler = Thread.currentThread().getContextClassLoader().loadClass( getHandler( properties ) ); 82 m_roles = new HashSet(); 83 m_dependencies = deps; 84 } 85 86 91 public MetaInfoEntry( final RoleEntry roleEntry ) 92 { 93 if ( null == roleEntry ) throw new NullPointerException ( "\"roleEntry\" cannot be null." ); 94 95 m_klass = roleEntry.getComponentClass(); 96 m_configName = roleEntry.getShortname(); 97 m_handler = roleEntry.getHandlerClass(); 98 m_roles = new HashSet(); 99 m_roles.add( roleEntry.getRole() ); 100 m_dependencies = new ArrayList(); 101 makeReadOnly(); 102 } 103 104 107 public void makeReadOnly() 108 { 109 m_readOnly = true; 110 } 111 112 117 public Class getComponentClass() 118 { 119 return m_klass; 120 } 121 122 128 public Class getHandlerClass() 129 { 130 return m_handler; 131 } 132 133 139 public String getConfigurationName() 140 { 141 return m_configName; 142 } 143 144 151 public void addRole( final String role ) 152 { 153 if ( null == role ) throw new NullPointerException ( "\"role\" cannot be null" ); 154 if ( m_readOnly ) throw new SecurityException ( "This MetaInfoEntry is read-only." ); 155 156 m_roles.add( role ); 157 } 158 159 165 public boolean containsRole( final String role ) 166 { 167 if ( null == role ) throw new NullPointerException ( "\"role\" cannot be null" ); 168 return m_roles.contains( role ); 169 } 170 171 176 public Iterator getRoles() 177 { 178 return m_roles.iterator(); 179 } 180 181 186 public List getDependencies() 187 { 188 return m_dependencies; 189 } 190 191 197 private String getHandler( final Properties meta ) 198 { 199 final String lifecycle = meta.getProperty( "x-avalon.lifestyle", null ); 200 String handler; 201 202 if ( null != lifecycle ) 203 { 204 handler = (String ) m_lifecycleMap.get( lifecycle ); 205 } 206 else 207 { 208 handler = meta.getProperty( "fortress.handler" ); 209 } 210 211 if ( null == handler ) 212 { 213 handler = PER_THREAD_HANDLER; 214 } 215 216 return handler; 217 } 218 219 227 public static final String createShortName( final String className ) 228 { 229 final StringBuffer shortName = new StringBuffer (); 230 231 final char[] name = className.substring( 232 className.lastIndexOf( '.' ) + 1 ).toCharArray(); 233 char last = '\0'; 234 235 for (int i = 0; i < name.length; i++) 236 { 237 if (Character.isUpperCase(name[i])) 238 { 239 if ( Character.isLowerCase( last ) ) 240 { 241 shortName.append('-'); 242 } 243 244 shortName.append(Character.toLowerCase(name[i])); 245 } 246 else 247 { 248 shortName.append(name[i]); 249 } 250 251 last = name[i]; 252 } 253 254 return shortName.toString().toLowerCase(); 255 } 256 257 } 258 | Popular Tags |