1 package org.apache.beehive.controls.api.bean; 2 19 20 import org.apache.beehive.controls.api.properties.PropertyMap; 21 import org.apache.beehive.controls.api.bean.ControlBean; 22 import org.apache.beehive.controls.api.context.ControlBeanContext; 23 import org.apache.beehive.controls.api.ControlException; 24 25 import org.apache.beehive.controls.spi.bean.ControlFactory; 26 import org.apache.beehive.controls.spi.bean.JavaControlFactory; 27 28 import org.apache.commons.discovery.tools.DiscoverClass; 29 30 import java.lang.reflect.Constructor ; 31 import java.lang.reflect.Method ; 32 import java.lang.reflect.InvocationTargetException ; 33 34 38 public class Controls 39 { 40 final private static String DEFAULT_FACTORY_CLASS = JavaControlFactory.class.getName(); 41 42 52 public static ControlBean instantiate( ClassLoader cl, 53 String beanName, 54 PropertyMap props ) 55 throws ClassNotFoundException 56 { 57 return instantiate( cl, beanName, props, null, null ); 58 } 59 60 72 public static ControlBean instantiate( ClassLoader cl, 73 String beanName, 74 PropertyMap props, 75 ControlBeanContext cbc, 76 String id ) 77 throws ClassNotFoundException 78 { 79 Class beanClass = ( cl == null ) ? Class.forName( beanName ) : cl.loadClass( beanName ); 80 return instantiate(beanClass, props, cbc, id); 81 } 82 83 94 public static <T extends ControlBean> T instantiate( Class <T> beanClass, 95 PropertyMap props, 96 ControlBeanContext context, 97 String id ) 98 { 99 try 100 { 101 DiscoverClass discoverer = new DiscoverClass(); 102 Class factoryClass = discoverer.find( ControlFactory.class, DEFAULT_FACTORY_CLASS ); 103 ControlFactory factory = (ControlFactory)factoryClass.newInstance(); 104 return factory.instantiate( beanClass, props, context, id ); 105 } 106 catch ( Exception e ) 107 { 108 throw new ControlException( "Exception creating ControlBean", e ); 109 } 110 } 111 112 125 public static void initializeClient( ClassLoader cl, Object client, ControlBeanContext cbc ) 126 throws ClassNotFoundException 127 { 128 Class clientClass = client.getClass(); 129 String clientName = clientClass.getName(); 130 131 if ( cl == null ) 132 cl = clientClass.getClassLoader(); 133 134 String initName = clientName + "ClientInitializer"; 135 Class initClass = cl.loadClass( initName ); 136 137 try 138 { 139 Method m = initClass.getMethod( "initialize", ControlBeanContext.class, clientClass ); 140 m.invoke(null, cbc, client ); 141 } 142 catch ( Throwable e ) 143 { 144 if ( e instanceof InvocationTargetException ) 145 { 146 if ( e.getCause() != null ) 147 { 148 e = e.getCause(); 149 } 150 } 151 152 throw new ControlException( "Exception trying to run client initializer: " + e.getClass().getName() + ", " + 153 e.getMessage(), e ); 154 } 155 } 156 } 157 | Popular Tags |