1 22 package org.jboss.beans.metadata.plugins; 23 24 import java.util.*; 25 26 import org.jboss.beans.metadata.spi.*; 27 import org.jboss.dependency.plugins.AbstractDependencyItem; 28 import org.jboss.dependency.spi.ControllerContext; 29 import org.jboss.dependency.spi.ControllerMode; 30 import org.jboss.dependency.spi.ControllerState; 31 import org.jboss.dependency.spi.DependencyItem; 32 import org.jboss.kernel.spi.dependency.KernelController; 33 import org.jboss.kernel.spi.dependency.KernelControllerContext; 34 import org.jboss.reflect.spi.TypeInfo; 35 import org.jboss.util.JBossObject; 36 import org.jboss.util.JBossStringBuilder; 37 38 44 public class AbstractBeanMetaData extends AbstractFeatureMetaData implements BeanMetaData, BeanMetaDataFactory 45 { 46 47 protected String bean; 48 49 50 protected String name; 51 52 53 protected ControllerMode mode; 54 55 56 private Set<PropertyMetaData> properties; 57 58 59 protected ClassLoaderMetaData classLoader; 60 61 62 protected ConstructorMetaData constructor; 63 64 65 protected LifecycleMetaData create; 66 67 68 protected LifecycleMetaData start; 69 70 71 protected LifecycleMetaData stop; 72 73 74 protected LifecycleMetaData destroy; 75 76 77 protected Set<DemandMetaData> demands; 78 79 80 protected Set<SupplyMetaData> supplies; 81 82 83 protected Set<DependencyMetaData> depends; 84 85 86 protected List<InstallMetaData> installs; 87 88 89 protected List<InstallMetaData> uninstalls; 90 91 92 protected KernelController controller; 93 94 97 public AbstractBeanMetaData() 98 { 99 super(); 100 } 101 102 107 public AbstractBeanMetaData(String bean) 108 { 109 this.bean = bean; 110 } 111 117 public AbstractBeanMetaData(String name, String bean) 118 { 119 this.name = name; 120 this.bean = bean; 121 } 122 123 public List<BeanMetaData> getBeans() 124 { 125 List<BeanMetaData> nestedBeans = findNestedBeans(); 126 if (nestedBeans.isEmpty()) 127 { 128 return Collections.singletonList((BeanMetaData)this); 129 } 130 else 131 { 132 nestedBeans.add(this); 133 return nestedBeans; 134 } 135 } 136 137 protected List<BeanMetaData> findNestedBeans() 138 { 139 List<BeanMetaData> allBeans = new ArrayList<BeanMetaData>(); 140 addBeans(this, allBeans); 141 return allBeans; 142 } 143 144 protected void addBeans(MetaDataVisitorNode current, List<BeanMetaData> list) 145 { 146 for(Iterator<? extends MetaDataVisitorNode> children = current.getChildren(); children != null && children.hasNext();) 147 { 148 MetaDataVisitorNode next = children.next(); 149 if (next instanceof BeanMetaDataFactory) 150 { 151 list.addAll(((BeanMetaDataFactory) next).getBeans()); 152 } 153 else 154 { 155 addBeans(next, list); 156 if (next instanceof BeanMetaData) 157 { 158 list.add((BeanMetaData) current); 159 } 160 } 161 } 162 } 163 164 168 public String getBean() 169 { 170 return bean; 171 } 172 173 178 public void setBean(String bean) 179 { 180 this.bean = bean; 181 flushJBossObjectCache(); 182 } 183 184 190 public PropertyMetaData getProperty(String name) 191 { 192 if (name == null) 193 throw new IllegalArgumentException ("Null name"); 194 if (properties != null && properties.size() > 0) 195 { 196 for (Iterator i = properties.iterator(); i.hasNext();) 197 { 198 AbstractPropertyMetaData prop = (AbstractPropertyMetaData) i.next(); 199 if (name.equals(prop.getName())) 200 return prop; 201 } 202 } 203 return null; 204 } 205 206 211 public void addProperty(PropertyMetaData property) 212 { 213 if (property == null) 214 throw new IllegalArgumentException ("Null property"); 215 if (properties == null) 216 properties = new HashSet<PropertyMetaData>(); 217 properties.add(property); 218 flushJBossObjectCache(); 219 } 220 221 226 public void setProperties(Set<PropertyMetaData> properties) 227 { 228 this.properties = properties; 229 flushJBossObjectCache(); 230 } 231 232 public ClassLoaderMetaData getClassLoader() 233 { 234 return classLoader; 235 } 236 237 public void setClassLoader(ClassLoaderMetaData classLoader) 238 { 239 this.classLoader = classLoader; 240 } 241 242 247 public void setConstructor(ConstructorMetaData constructor) 248 { 249 this.constructor = constructor; 250 } 251 252 257 public void setDemands(Set<DemandMetaData> demands) 258 { 259 this.demands = demands; 260 flushJBossObjectCache(); 261 } 262 263 268 public void setSupplies(Set<SupplyMetaData> supplies) 269 { 270 this.supplies = supplies; 271 flushJBossObjectCache(); 272 } 273 274 279 public void setDepends(Set<DependencyMetaData> depends) 280 { 281 this.depends = depends; 282 flushJBossObjectCache(); 283 } 284 285 public String getName() 286 { 287 return name; 288 } 289 290 295 public void setName(String name) 296 { 297 this.name = name; 298 flushJBossObjectCache(); 299 } 300 301 public ControllerMode getMode() 302 { 303 return mode; 304 } 305 306 public void setMode(ControllerMode mode) 307 { 308 this.mode = mode; 309 flushJBossObjectCache(); 310 } 311 312 public Set<PropertyMetaData> getProperties() 313 { 314 return properties; 315 } 316 317 public ConstructorMetaData getConstructor() 318 { 319 return constructor; 320 } 321 322 public LifecycleMetaData getCreate() 323 { 324 return create; 325 } 326 327 332 public void setCreate(LifecycleMetaData lifecycle) 333 { 334 lifecycle.setState(ControllerState.CREATE); 335 this.create = lifecycle; 336 } 337 338 public LifecycleMetaData getStart() 339 { 340 return start; 341 } 342 343 348 public void setStart(LifecycleMetaData lifecycle) 349 { 350 lifecycle.setState(ControllerState.START); 351 this.start = lifecycle; 352 } 353 354 public LifecycleMetaData getStop() 355 { 356 return stop; 357 } 358 359 364 public void setStop(LifecycleMetaData lifecycle) 365 { 366 lifecycle.setState(ControllerState.START); 367 this.stop = lifecycle; 368 } 369 370 public LifecycleMetaData getDestroy() 371 { 372 return destroy; 373 } 374 375 380 public void setDestroy(LifecycleMetaData lifecycle) 381 { 382 lifecycle.setState(ControllerState.CREATE); 383 this.destroy = lifecycle; 384 } 385 386 public Set<DemandMetaData> getDemands() 387 { 388 return demands; 389 } 390 391 public Set<SupplyMetaData> getSupplies() 392 { 393 return supplies; 394 } 395 396 public Set<DependencyMetaData> getDepends() 397 { 398 return depends; 399 } 400 401 public List<InstallMetaData> getInstalls() 402 { 403 return installs; 404 } 405 406 411 public void setInstalls(List<InstallMetaData> installs) 412 { 413 this.installs = installs; 414 flushJBossObjectCache(); 415 } 416 417 public List<InstallMetaData> getUninstalls() 418 { 419 return uninstalls; 420 } 421 422 427 public void setUninstalls(List<InstallMetaData> uninstalls) 428 { 429 this.uninstalls = uninstalls; 430 flushJBossObjectCache(); 431 } 432 433 public void initialVisit(MetaDataVisitor visitor) 434 { 435 if (visitor.visitorNodeStack().isEmpty() == false || (classLoader != null && classLoader.getClassLoader() != this)) 436 { 437 KernelControllerContext controllerContext = visitor.getControllerContext(); 438 controller = (KernelController) controllerContext.getController(); 439 Object name = controllerContext.getName(); 440 Object iDependOn = getUnderlyingValue(); 441 if (name.equals(iDependOn) == false) 442 { 443 ControllerState whenRequired = visitor.getContextState(); 444 DependencyItem di = new AbstractDependencyItem(name, iDependOn, whenRequired, ControllerState.INSTALLED); 445 visitor.addDependency(di); 446 } 447 } 448 super.initialVisit(visitor); 449 } 450 451 public void setController(KernelController controller) 452 { 453 this.controller = controller; 454 } 455 456 protected void addChildren(Set<MetaDataVisitorNode> children) 457 { 458 super.addChildren(children); 459 if (classLoader != null && classLoader.getClassLoader() != this) 460 children.add(classLoader); 461 if (constructor != null) 462 children.add(constructor); 463 if (properties != null) 464 children.addAll(properties); 465 if (create != null) 466 children.add(create); 467 if (start != null) 468 children.add(start); 469 if (stop != null) 470 children.add(stop); 471 if (destroy != null) 472 children.add(destroy); 473 if (demands != null) 474 children.addAll(demands); 475 if (supplies != null) 476 children.addAll(supplies); 477 if (depends != null) 478 children.addAll(depends); 479 if (installs != null) 480 children.addAll(installs); 481 if (uninstalls != null) 482 children.addAll(uninstalls); 483 } 484 485 public Class getType(MetaDataVisitor visitor, MetaDataVisitorNode previous) throws Throwable 486 { 487 throw new IllegalArgumentException ("Cannot determine inject class type: " + this); 488 } 489 490 public Object getUnderlyingValue() 491 { 492 return name; 493 } 494 495 @SuppressWarnings ("unchecked") 496 public Object getValue(TypeInfo info, ClassLoader cl) throws Throwable 497 { 498 ControllerContext context = controller.getInstalledContext(name); 499 if (context == null || context.getTarget() == null) 500 { 501 if (info == null && classLoader != null && classLoader.getClassLoader() == this) 503 { 504 return cl; 505 } 506 throw new IllegalArgumentException ("Bean not yet installed: " + name); 507 } 508 Object target = context.getTarget(); 509 if (info != null && info.getType().isAssignableFrom(target.getClass()) == false) 510 { 511 throw new ClassCastException (target + " is not a " + info); 512 } 513 return target; 514 } 515 516 public void toString(JBossStringBuilder buffer) 517 { 518 buffer.append("name=").append(name); 519 buffer.append(" bean=").append(bean); 520 buffer.append(" properties="); 521 JBossObject.list(buffer, properties); 522 if (classLoader != null && classLoader.getClassLoader() != this) 523 buffer.append(" classLoader=").append(classLoader); 524 buffer.append(" constructor=").append(constructor); 525 if (create != null) 526 buffer.append(" create=").append(create); 527 if (start != null) 528 buffer.append(" start=").append(start); 529 if (stop != null) 530 buffer.append(" stop=").append(stop); 531 if (destroy != null) 532 buffer.append(" destroy=").append(destroy); 533 if (demands != null) 534 { 535 buffer.append(" demands="); 536 JBossObject.list(buffer, demands); 537 } 538 super.toString(buffer); 539 if (supplies != null) 540 { 541 buffer.append(" supplies="); 542 JBossObject.list(buffer, supplies); 543 } 544 if (depends != null) 545 { 546 buffer.append(" depends="); 547 JBossObject.list(buffer, depends); 548 } 549 if (installs != null) 550 { 551 buffer.append(" installs="); 552 JBossObject.list(buffer, installs); 553 } 554 if (uninstalls != null) 555 { 556 buffer.append(" uninstalls="); 557 JBossObject.list(buffer, uninstalls); 558 } 559 } 560 561 public void toShortString(JBossStringBuilder buffer) 562 { 563 buffer.append(bean); 564 buffer.append('/'); 565 buffer.append(name); 566 } 567 } 568 | Popular Tags |