1 6 7 package org.jfox.ioc; 8 9 import java.util.ArrayList ; 10 import java.util.List ; 11 12 import org.jfox.ioc.depend.Dependency; 13 import org.jfox.ioc.depend.DependencyPack; 14 import org.jfox.ioc.depend.RefDependency; 15 import org.jfox.ioc.exception.ComponentException; 16 import org.jfox.ioc.ext.ManagableComponent; 17 import org.jfox.ioc.ext.SingletonComponent; 18 import org.jfox.ioc.factory.ComponentFactory; 19 import org.jfox.ioc.factory.ConstrComponentFactory; 20 import org.jfox.ioc.factory.InstanceComponentFactory; 21 import org.jfox.ioc.factory.PropertyComponentFactory; 22 import org.jfox.ioc.logger.Logger; 23 import org.jfox.ioc.management.ManagementInfo; 24 25 28 29 public class ComponentMeta implements Comparable { 30 31 public final static boolean DEFAULT_SINGLETON = true; 32 33 public final static boolean DEFAULT_TYPE_SINGLETON = false; 34 35 public final static int DEFAULT_PRIORITY = 0; 36 37 private static Logger logger = Logger.getLogger(ComponentMeta.class 38 .getName()); 39 40 43 protected String description = ""; 44 45 51 protected boolean singleton = DEFAULT_SINGLETON; 52 53 57 protected boolean typeSingleton = DEFAULT_TYPE_SINGLETON; 58 59 62 protected int priority = DEFAULT_PRIORITY; 63 64 67 protected ComponentFactory compFactory; 68 69 72 protected Component component = null; 73 74 77 protected List refComponentNames = new ArrayList (); 78 79 private String module = ""; 80 83 private String moduleDesc = "Default Module"; 84 85 88 private String moduleDir = null; 89 90 protected ComponentMeta(ComponentFactory compFactory) { 91 this.compFactory = compFactory; 92 } 93 94 100 public boolean isSingleton() { 101 return SingletonComponent.class 102 .isAssignableFrom(getComponentImplementation()) 103 || singleton; 104 } 105 106 public void setSingleton(boolean singleton) { 107 if (!(compFactory instanceof InstanceComponentFactory)) { 109 this.singleton = singleton; 110 } 111 } 112 113 public boolean isTypeSingleton() { 114 return typeSingleton; 115 } 116 117 public void setTypeSingleton(boolean typeSingleton) { 118 this.typeSingleton = typeSingleton; 119 } 120 121 public int getPriority() { 122 return priority; 123 } 124 125 public void setPriority(int priority) { 126 this.priority = priority; 127 } 128 129 138 public synchronized Component getComponentInstance( 139 DependencyPack[] paramPacks) throws ComponentException { 140 logger.debug("get component instance " 141 + getComponentFactory().getComponentName()); 142 143 if (isSingleton()) { 144 if (component == null) { 145 logger.debug("make new component instance, name: " 146 + getComponentFactory().getComponentName() 147 + ", component factory: " + getComponentFactory()); 148 ComponentFactory factory = getComponentFactory(); 149 if (paramPacks != null && paramPacks.length != 0) { 150 if (factory instanceof ConstrComponentFactory) { 151 factory = new PropertyComponentFactory( 152 (ConstrComponentFactory) factory, paramPacks); 153 } 154 else if (factory instanceof PropertyComponentFactory) { 155 ((PropertyComponentFactory) factory) 156 .addParameterPack(paramPacks); 157 } 158 } 159 160 ComponentContext context = initComponentContext(); 161 if(factory instanceof PropertyComponentFactory){ 162 component = ((PropertyComponentFactory)factory).makeComponent(context); 164 } 165 else { 166 component = factory.makeComponent(); 168 component.setComponentContext(context); 169 } 170 logger.debug("make new instance: " + component); 171 buildRefComponentNames(); 172 173 if (getRegistry().isStarted()) { 175 getRegistry().initComponent(this, component); 176 getRegistry().startComponent(this, component); 177 } 178 } 179 else { 180 if (paramPacks != null && paramPacks.length != 0) { 181 ComponentFactory factory = getComponentFactory(); 182 factory = new PropertyComponentFactory( 183 new InstanceComponentFactory(component), paramPacks); 184 component = factory.makeComponent(); 185 } 186 } 187 } 188 else { 189 logger.debug("make new component instance with component name " 190 + getComponentFactory().getComponentName() 191 + ",component factory " + getComponentFactory()); 192 ComponentFactory factory = getComponentFactory(); 193 if (paramPacks != null && paramPacks.length != 0) { 194 if (factory instanceof ConstrComponentFactory) { 195 factory = new PropertyComponentFactory( 196 (ConstrComponentFactory) factory, paramPacks); 197 } 198 else if (factory instanceof PropertyComponentFactory) { 199 ((PropertyComponentFactory) factory) 200 .addParameterPack(paramPacks); 201 } 202 } 203 204 ComponentContext context = initComponentContext(); 205 Component comp = null; 206 if(factory instanceof PropertyComponentFactory) { 207 comp = ((PropertyComponentFactory) factory).makeComponent(context); 209 } 210 else { 211 comp = factory.makeComponent(); 213 comp.setComponentContext(context); 214 } 215 216 if(component == null) { 218 buildRefComponentNames(); 219 } 220 component = comp; 221 222 if (getRegistry().isStarted()) { 224 getRegistry().initComponent(this, component); 225 getRegistry().startComponent(this, component); 226 } 227 228 } 229 return component; 230 } 231 232 private void buildRefComponentNames() { 234 if (compFactory instanceof PropertyComponentFactory) { 236 DependencyPack[] packs = ((PropertyComponentFactory) compFactory) 237 .getParameterPacks(); 238 for (int i = 0; packs != null && i < packs.length; i++) { 239 Dependency param = packs[i].getParameter(); 240 if (param instanceof RefDependency) { 241 refComponentNames.add(((RefDependency) param) 242 .getRefComponentName()); 243 } 244 } 245 } 246 247 Dependency[] params = getComponentFactory().getParameters(); 249 for (int i = 0; params != null && i < params.length; i++) { 250 Dependency param = params[i]; 251 if (param instanceof RefDependency) { 252 refComponentNames.add(((RefDependency) param) 253 .getRefComponentName()); 254 } 255 } 256 257 } 258 259 public ComponentName getComponentName() { 260 return getComponentFactory().getComponentName(); 261 } 262 263 public Class getComponentImplementation() { 264 return getComponentFactory().getImplementation(); 265 } 266 267 public ComponentFactory getComponentFactory() { 268 return compFactory; 269 } 270 271 public ComponentName[] getRefComponentNames() { 272 return (ComponentName[]) refComponentNames 273 .toArray(new ComponentName[refComponentNames.size()]); 274 } 275 276 281 public Registry getRegistry() { 282 return getComponentFactory().getRegistry(); 283 } 284 285 void setRegistry(Registry registry) { 286 getComponentFactory().setRegistry(registry); 287 } 288 289 public ManagementInfo getManagementInfo() { 290 if (!isSingleton()) { 291 return null; 292 } 293 else if (component == null) { 294 return null; 295 } 296 else { 297 if (component instanceof ManagableComponent) { 298 return ManagementInfo.getManagementInfoByAnnotation(component 299 .getClass()); 300 } 301 else { 302 return null; 303 } 304 } 305 } 306 307 public String getDescription() { 308 return description; 309 } 310 311 public void setDescription(String description) { 312 this.description = description; 313 } 314 315 318 public String getModule() { 319 return module; 320 } 321 322 323 326 public void setModule(String module) { 327 this.module = module; 328 } 329 330 331 public String getModuleDesc() { 332 return moduleDesc; 333 } 334 335 public void setModuleDescription(String module) { 336 this.moduleDesc = module; 337 } 338 339 public String getModuleDir() { 340 return moduleDir; 341 } 342 343 public void setModuleDir(String moduleDir) { 344 this.moduleDir = moduleDir; 345 } 346 347 private ComponentContext initComponentContext() { 348 ComponentContext ctx = new ComponentContext(); 349 if (moduleDir == null) { 350 moduleDir = getComponentImplementation().getProtectionDomain() 353 .getCodeSource().getLocation().getPath(); 354 } 355 ctx.setRegistry(getRegistry()); 356 ctx.setModule(module); 357 ctx.setModuleDesc(moduleDesc); 358 ctx.setModuleDir(moduleDir); 359 ctx.setSingleton(singleton); 360 ctx.setTypeSingleton(typeSingleton); 361 return ctx; 362 } 363 364 370 public int compareTo(Object o) { 371 if (!(o instanceof ComponentMeta)) 372 return 0; 373 ComponentMeta cm = (ComponentMeta) o; 374 if (priority > cm.priority) { 375 return 1; 376 } 377 else if (priority < cm.priority) { 378 return -1; 379 } 380 else { 381 return getComponentName().toString().compareTo( 382 cm.getComponentName().toString()); 383 } 384 } 385 386 public String toString() { 387 return "ComponentMeta{" + "componentType=" 388 + getComponentImplementation().getName() + ", compFactory=" 389 + compFactory.toString() + ", singleton=" + singleton 390 + ", typeSingleton=" + typeSingleton + "}"; 391 } 392 393 } | Popular Tags |