1 package org.apache.beehive.controls.spi.bean; 2 19 20 import java.lang.reflect.Constructor ; 21 import java.lang.reflect.InvocationTargetException ; 22 import java.util.concurrent.ConcurrentHashMap ; 23 import java.util.Properties ; 24 import java.io.InputStream ; 25 import java.io.IOException ; 26 27 import org.apache.beehive.controls.api.properties.PropertyMap; 28 import org.apache.beehive.controls.api.properties.BeanPropertyMap; 29 import org.apache.beehive.controls.api.properties.PropertyKey; 30 import org.apache.beehive.controls.api.bean.ControlBean; 31 import org.apache.beehive.controls.api.context.ControlBeanContext; 32 import org.apache.beehive.controls.api.ControlException; 33 34 42 public class JavaControlFactory implements ControlFactory 43 { 44 private static ConcurrentHashMap <Class , Constructor > _constructors = 45 new ConcurrentHashMap <Class , Constructor >(); 46 47 private static final Properties _extImplBindings = new Properties (); 48 49 private static final String EXT_IMPL_BINDING_CONFIG = "controlbindings.properties"; 50 51 static 52 { 53 InputStream is = JavaControlFactory.class.getClassLoader(). 54 getResourceAsStream( EXT_IMPL_BINDING_CONFIG ); 55 56 if ( is != null ) 57 { 58 try 59 { 60 _extImplBindings.load( is ); 61 } 62 catch ( IOException ie ) { } 63 } 64 } 65 66 80 public <T extends ControlBean> T instantiate(Class <T> beanClass, 81 PropertyMap props, 82 ControlBeanContext context, 83 String id) 84 { 85 String beanClassName = beanClass.getName(); 86 87 String extImplBinding = _extImplBindings.getProperty( beanClassName + "_" + id ); 88 if ( extImplBinding == null ) 89 extImplBinding = _extImplBindings.getProperty( beanClassName ); 90 91 if ( extImplBinding != null ) 92 { 93 BeanPropertyMap bpm = props == null ? new BeanPropertyMap( beanClass ) : new BeanPropertyMap( props ); 94 PropertyKey propKey = new PropertyKey( org.apache.beehive.controls.api.properties.BaseProperties.class, 95 "controlImplementation" ); 96 97 bpm.setProperty( propKey, extImplBinding ); 98 props = bpm; 99 } 100 101 T ret = null; 102 try 103 { 104 Constructor <T> ctor = _constructors.get(beanClass); 105 if (ctor == null) 106 { 107 ctor = beanClass.getConstructor(ControlBeanContext.class, String .class, 108 PropertyMap.class); 109 _constructors.put(beanClass, ctor); 110 } 111 ret = ctor.newInstance(context, id, props); 112 } 113 catch (InvocationTargetException ite) 114 { 115 Throwable t = ite.getCause(); 116 throw new ControlException("ControlBean constructor exception", t); 117 } 118 catch (Exception e) 119 { 120 throw new ControlException("Exception creating ControlBean", e); 121 } 122 123 return ret; 124 } 125 } 126 | Popular Tags |