1 50 package org.apache.avalon.excalibur.monitor; 51 52 import java.lang.reflect.Constructor ; 53 import java.util.ArrayList ; 54 55 import org.apache.avalon.framework.configuration.Configuration; 56 import org.apache.avalon.framework.logger.Logger; 57 58 65 class MonitorUtil 66 { 67 private static final Class [] c_constructorParams = 68 new Class []{String .class}; 69 70 public static Resource[] configureResources( final Configuration[] resources, 71 final Logger logger ) 72 { 73 final ArrayList results = new ArrayList (); 74 for( int i = 0; i < resources.length; i++ ) 75 { 76 final Configuration initialResource = resources[ i ]; 77 final String key = 78 initialResource.getAttribute( "key", "** Unspecified key **" ); 79 final String className = 80 initialResource.getAttribute( "class", "** Unspecified class **" ); 81 82 try 83 { 84 final Resource resource = createResource( className, key ); 85 results.add( resource ); 86 87 if( logger.isDebugEnabled() ) 88 { 89 final String message = 90 "Initial Resource: \"" + key + "\" Initialized."; 91 logger.debug( message ); 92 } 93 } 94 catch( final Exception e ) 95 { 96 if( logger.isWarnEnabled() ) 97 { 98 final String message = 99 "Initial Resource: \"" + key + 100 "\" Failed (" + className + ")."; 101 logger.warn( message, e ); 102 } 103 } 104 } 105 106 return (Resource[])results.toArray( new Resource[ results.size() ] ); 107 } 108 109 private static Resource createResource( final String className, 110 final String key ) 111 throws Exception 112 { 113 final ClassLoader loader = Thread.currentThread().getContextClassLoader(); 114 final Class clazz = loader.loadClass( className ); 115 final Constructor initializer = 116 clazz.getConstructor( c_constructorParams ); 117 return (Resource)initializer.newInstance( new Object []{key} ); 118 } 119 } 120 | Popular Tags |