1 22 package org.jboss.beans.metadata.plugins.factory; 23 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import org.jboss.beans.info.spi.BeanInfo; 29 import org.jboss.beans.metadata.spi.ClassLoaderMetaData; 30 import org.jboss.beans.metadata.spi.ConstructorMetaData; 31 import org.jboss.beans.metadata.spi.LifecycleMetaData; 32 import org.jboss.beans.metadata.spi.ParameterMetaData; 33 import org.jboss.beans.metadata.spi.ValueMetaData; 34 import org.jboss.joinpoint.spi.Joinpoint; 35 import org.jboss.joinpoint.spi.JoinpointException; 36 import org.jboss.joinpoint.spi.MethodJoinpoint; 37 import org.jboss.joinpoint.spi.TargettedJoinpoint; 38 import org.jboss.kernel.plugins.config.Configurator; 39 import org.jboss.kernel.spi.config.KernelConfigurator; 40 41 47 public class GenericBeanFactory 48 { 49 50 protected KernelConfigurator configurator; 51 52 53 protected String bean; 54 55 56 protected ClassLoaderMetaData classLoader; 57 58 59 protected ConstructorMetaData constructor; 60 61 62 protected Map properties; 63 64 65 protected LifecycleMetaData create; 66 67 68 protected LifecycleMetaData start; 69 70 75 public GenericBeanFactory(KernelConfigurator configurator) 76 { 77 this.configurator = configurator; 78 } 79 80 86 public Object createBean() throws Throwable 87 { 88 ClassLoader cl = Configurator.getClassLoader(classLoader); 89 BeanInfo info = configurator.getBeanInfo(bean, cl); 90 Joinpoint joinpoint = configurator.getConstructorJoinPoint(info, constructor, null); 91 Object result = joinpoint.dispatch(); 92 if (properties != null && properties.size() > 0) 93 { 94 for (Iterator i = properties.entrySet().iterator(); i.hasNext();) 95 { 96 Map.Entry entry = (Map.Entry ) i.next(); 97 String property = (String ) entry.getKey(); 98 ValueMetaData vmd = (ValueMetaData) entry.getValue(); 99 TargettedJoinpoint jp = configurator.getPropertySetterJoinPoint(info, property, cl, vmd); 100 jp.setTarget(result); 101 jp.dispatch(); 102 } 103 } 104 invokeLifecycle("create", create, info, cl, result); 105 invokeLifecycle("start", start, info, cl, result); 106 return result; 107 } 108 109 114 public String getBean() 115 { 116 return bean; 117 } 118 119 124 public void setBean(String bean) 125 { 126 this.bean = bean; 127 } 128 129 134 public ClassLoaderMetaData getClassLoader() 135 { 136 return classLoader; 137 } 138 139 144 public void setClassLoader(ClassLoaderMetaData classLoader) 145 { 146 this.classLoader = classLoader; 147 } 148 149 154 public ConstructorMetaData getConstructor() 155 { 156 return constructor; 157 } 158 159 164 public void setConstructor(ConstructorMetaData constructor) 165 { 166 this.constructor = constructor; 167 } 168 169 174 public Map getProperties() 175 { 176 return properties; 177 } 178 179 184 public void setProperties(Map properties) 185 { 186 this.properties = properties; 187 } 188 189 194 public LifecycleMetaData getCreate() 195 { 196 return create; 197 } 198 199 204 public void setCreate(LifecycleMetaData create) 205 { 206 this.create = create; 207 } 208 209 214 public LifecycleMetaData getStart() 215 { 216 return start; 217 } 218 219 224 public void setStart(LifecycleMetaData start) 225 { 226 this.start = start; 227 } 228 229 239 protected void invokeLifecycle(String methodName, LifecycleMetaData lifecycle, BeanInfo info, ClassLoader cl, Object target) throws Throwable 240 { 241 String method = methodName; 242 if (lifecycle != null && lifecycle.getMethodName() != null) 243 method = lifecycle.getMethodName(); 244 List <ParameterMetaData> parameters = null; 245 if (lifecycle != null) 246 parameters = lifecycle.getParameters(); 247 MethodJoinpoint joinpoint; 248 try 249 { 250 joinpoint = configurator.getMethodJoinPoint(info, cl, method, parameters, false, true); 251 } 252 catch (JoinpointException ignored) 253 { 254 return; 255 } 256 joinpoint.setTarget(target); 257 joinpoint.dispatch(); 258 } 259 } | Popular Tags |