1 17 package org.apache.avalon.fortress.impl; 18 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 import org.apache.avalon.fortress.MetaInfoEntry; 23 import org.apache.avalon.fortress.util.Service; 24 import org.apache.avalon.fortress.impl.handler.ComponentHandler; 25 import org.apache.avalon.fortress.impl.handler.LEAwareComponentHandler; 26 import org.apache.avalon.fortress.impl.lookup.FortressServiceSelector; 27 import org.apache.avalon.fortress.impl.role.ECMMetaInfoManager; 28 import org.apache.avalon.framework.configuration.Configuration; 29 import org.apache.avalon.framework.configuration.ConfigurationException; 30 import org.apache.avalon.framework.container.ContainerUtil; 31 import org.apache.avalon.framework.service.DefaultServiceManager; 32 import org.apache.avalon.framework.thread.SingleThreaded; 33 import org.apache.avalon.framework.thread.ThreadSafe; 34 import org.apache.excalibur.instrument.Instrumentable; 35 import org.d_haven.mpool.ObjectFactory; 36 37 43 public class DefaultECMContainer extends DefaultContainer { 44 45 51 private String getRole( final Configuration config ) 52 throws ConfigurationException { 53 final String className; 54 55 if ( "component".equals( config.getName() ) ) 56 { 57 className = config.getAttribute( "role" ); 58 } 59 else 60 { 61 final MetaInfoEntry roleEntry = m_metaManager.getMetaInfoForShortName( config.getName() ); 62 if ( null == roleEntry ) 63 { 64 65 final String message = "No class found matching configuration name " + 66 "[name: " + config.getName() + ", location: " + config.getLocation() + "]"; 67 throw new ConfigurationException( message ); 68 } 69 70 Iterator roleIterator = roleEntry.getRoles(); 71 if ( roleIterator.hasNext() ) 72 { 73 className = (String )roleIterator.next(); 74 } 75 else 76 { 77 className = roleEntry.getComponentClass().getName(); 78 } 79 } 80 81 return className; 82 } 83 84 90 private String getClassname( final Configuration config ) 91 throws ConfigurationException { 92 final String className; 93 94 if ( "component".equals( config.getName() ) ) 95 { 96 className = config.getAttribute( "class" ); 97 } 98 else 99 { 100 if ( config.getAttribute("class", null) != null ) 101 { 102 className = config.getAttribute("class"); 103 } 104 else 105 { 106 final MetaInfoEntry roleEntry = m_metaManager.getMetaInfoForShortName( config.getName() ); 107 if ( null == roleEntry ) 108 { 109 110 final String message = "No class found matching configuration name " + 111 "[name: " + config.getName() + ", location: " + config.getLocation() + "]"; 112 throw new ConfigurationException( message ); 113 } 114 115 className = roleEntry.getComponentClass().getName(); 116 } 117 } 118 119 return className; 120 } 121 122 128 public void configure( Configuration conf ) 129 throws ConfigurationException { 130 this.interpretProxy( conf.getAttribute("proxy-type", this.getDefaultProxyType()) ); 131 132 final Configuration[] elements = conf.getChildren(); 133 for ( int i = 0; i < elements.length; i++ ) 134 { 135 final Configuration element = elements[i]; 136 137 String role = getRole( element ); 139 if ( role.endsWith("Selector") ) 140 { 141 processSelector(role.substring(0, role.length()-8), element ); 142 } 143 else 144 { 145 146 final String className = getClassname( element ); 148 149 final int pos = role.indexOf('/'); 150 final String hint; 151 if ( pos != -1 ) 152 { 153 hint = role.substring(pos+1); 154 role = role.substring(0, pos); 155 } 156 else 157 { 158 hint = null; 159 } 160 161 final String shortName; 162 if ( "component".equals( element.getName() )) 163 { 164 shortName = null; 165 } 166 else 167 { 168 shortName = element.getName(); 169 } 170 171 this.addComponent(role, hint, shortName, className, element ); 172 } 173 174 if ( getLogger().isDebugEnabled() ) 175 { 176 getLogger().debug( "Configuration processed for: " + role ); 177 } 178 } 179 } 180 181 190 private ComponentHandler getComponentHandler( final String classname, 191 final Class handlerClass, 192 final ComponentHandlerMetaData metaData) 193 throws Exception 194 { 195 final Configuration configuration = metaData.getConfiguration(); 196 197 final ComponentHandler handler; 199 200 try 201 { 202 final ObjectFactory factory = 203 createObjectFactory( classname, configuration ); 204 205 final ComponentHandler targetHandler = 207 (ComponentHandler) handlerClass.newInstance(); 208 209 ContainerUtil.enableLogging( targetHandler, getLogger() ); 211 ContainerUtil.contextualize( targetHandler, m_context ); 212 final DefaultServiceManager serviceManager = 213 new DefaultServiceManager( getServiceManager() ); 214 serviceManager.put( ObjectFactory.class.getName(), factory ); 215 serviceManager.makeReadOnly(); 216 217 ContainerUtil.service( targetHandler, serviceManager ); 218 ContainerUtil.configure( targetHandler, configuration ); 219 ContainerUtil.initialize( targetHandler ); 220 221 if ( targetHandler instanceof Instrumentable ) 222 { 223 final Instrumentable instrumentable = (Instrumentable) targetHandler; 224 final String name = instrumentable.getInstrumentableName(); 225 m_instrumentManager.registerInstrumentable( instrumentable, name ); 226 } 227 228 231 handler = new LEAwareComponentHandler( targetHandler, m_extManager, m_context ); 232 } 233 catch ( final Exception e ) 234 { 235 if ( getLogger().isDebugEnabled() ) 239 { 240 final String message = 241 "Could not create the handler for the '" + 242 classname + "' component."; 243 getLogger().debug( message, e ); 244 } 245 throw e; 246 } 247 248 if ( getLogger().isDebugEnabled() ) 249 { 250 final String message = 251 "Component " + classname + 252 " uses handler " + handlerClass.getName(); 253 getLogger().debug( message ); 254 } 255 256 final ComponentHandlerEntry entry = 259 new ComponentHandlerEntry( handler, metaData ); 260 m_components.add( entry ); 261 262 return handler; 263 } 264 265 protected Class getComponentHandlerClass(final String defaultClassName, final String shortName ) 266 throws Exception 267 { 268 if ( shortName == null ) 269 { 270 String handlerClassName = null; 271 272 Class clazz; 273 try 274 { 275 clazz = m_classLoader.loadClass( defaultClassName ); 276 277 if ( ThreadSafe.class.isAssignableFrom( clazz ) ) 278 { 279 handlerClassName = MetaInfoEntry.THREADSAFE_HANDLER; 280 } 281 else if ( Service.isClassPoolable(clazz) ) 282 { 283 handlerClassName = MetaInfoEntry.POOLABLE_HANDLER; 284 } 285 else if ( SingleThreaded.class.isAssignableFrom( clazz) ) 286 { 287 handlerClassName = MetaInfoEntry.FACTORY_HANDLER; 288 } 289 } 290 catch ( final Exception e ) 291 { 292 final String message = 293 "Unable to load class " + defaultClassName + ". Using dfault component handler."; 294 getLogger().warn( message ); 295 } 296 297 if ( handlerClassName == null ) 299 { 300 handlerClassName = MetaInfoEntry.THREADSAFE_HANDLER; 301 } 302 return m_classLoader.loadClass( handlerClassName ) ; 303 } 304 else 305 { 306 final MetaInfoEntry roleEntry = m_metaManager.getMetaInfoForShortName( shortName ); 307 if ( null == roleEntry ) 308 { 309 310 final String message = "No class found matching configuration name " + 311 "[name: " + shortName + "]"; 312 throw new ConfigurationException( message ); 313 } 314 315 return roleEntry.getHandlerClass(); 316 } 317 } 318 319 protected void processSelector(String role, Configuration config) 320 throws ConfigurationException 321 { 322 final String selectorRole = role + "Selector"; 323 FortressServiceSelector fss = new FortressServiceSelector(this, selectorRole); 324 Map hintMap = createHintMap(); 325 hintMap.put( DEFAULT_ENTRY, fss ); 326 hintMap.put( SELECTOR_ENTRY, 327 new FortressServiceSelector( this, selectorRole ) ); 328 m_mapper.put( selectorRole, hintMap ); 329 330 final Configuration[] children = config.getChildren(); 331 if ( children != null ) 332 { 333 for(int i=0; i<children.length; i++) 334 { 335 final Configuration element = children[i]; 336 final String hint = element.getAttribute("name"); 337 final String className = element.getAttribute("class"); 338 339 if ( m_metaManager instanceof ECMMetaInfoManager ) 340 { 341 try 342 { 343 ((ECMMetaInfoManager)m_metaManager).addSelectorComponent(role, hint, className, getComponentHandlerClass(className, null).getName()); 344 } 345 catch (ConfigurationException ce ) 346 { 347 throw ce; 348 } 349 catch (Exception e) 350 { 351 throw new ConfigurationException("Unable to add selector component.", e); 352 } 353 } 354 addComponent(role, hint, null, className, element ); 355 } 356 } 357 } 358 359 protected void addComponent(final String role, 360 String hint, 361 String shortName, 362 final String className, 363 final Configuration element) 364 throws ConfigurationException 365 { 366 final int activation = ComponentHandlerMetaData.ACTIVATION_BACKGROUND; 367 368 final String metaDataHint = element.getAttribute( "id", element.getLocation() ); 370 371 if ( hint == null ) 372 { 373 hint = metaDataHint; 374 } 375 376 final ComponentHandlerMetaData metaData = 377 new ComponentHandlerMetaData( metaDataHint, className, element, activation ); 378 379 try 380 { 381 382 if ( DEFAULT_ENTRY.equals( metaData.getName() ) || 383 SELECTOR_ENTRY.equals( metaData.getName() ) ) 384 { 385 throw new IllegalArgumentException ( "Using a reserved id name" + metaData.getName() ); 386 } 387 388 final ComponentHandler handler = 390 getComponentHandler( className, 391 getComponentHandlerClass( className, shortName), 392 metaData ); 393 394 Map hintMap = (Map ) m_mapper.get( role ); 398 399 if ( null == hintMap ) 401 { 402 hintMap = createHintMap(); 403 hintMap.put( DEFAULT_ENTRY, handler ); 404 hintMap.put( SELECTOR_ENTRY, 405 new FortressServiceSelector( this, role ) ); 406 m_mapper.put( role, hintMap ); 407 } 408 409 hintMap.put( hint, handler ); 410 411 if ( element.getAttributeAsBoolean( "default", false ) ) 412 { 413 hintMap.put( DEFAULT_ENTRY, handler ); 414 } 415 } 416 catch ( ConfigurationException ce ) 417 { 418 throw ce; 419 } 420 catch ( Exception e ) 421 { 422 throw new ConfigurationException( "Could not add component", e ); 423 } 424 } 425 426 431 protected String getDefaultProxyType() { 432 return "none"; 433 } 434 } 435 | Popular Tags |