001/* 002 * JGrapes Event Driven Framework 003 * Copyright (C) 2016-2026 Michael N. Lipp 004 * 005 * This program is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU Affero General Public License as published by 007 * the Free Software Foundation; either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, but 011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public 013 * License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with this program; if not, see <http://www.gnu.org/licenses/>. 017 */ 018 019package org.jgrapes.core.internal; 020 021import java.lang.annotation.Annotation; 022import java.lang.reflect.Method; 023import java.util.ArrayList; 024import java.util.Collection; 025import java.util.Collections; 026import java.util.ConcurrentModificationException; 027import java.util.Iterator; 028import java.util.List; 029import java.util.NoSuchElementException; 030import java.util.Stack; 031import java.util.concurrent.ExecutorService; 032import org.jgrapes.core.Channel; 033import org.jgrapes.core.ComponentType; 034import org.jgrapes.core.Components; 035import org.jgrapes.core.Event; 036import org.jgrapes.core.EventPipeline; 037import org.jgrapes.core.HandlerScope; 038import org.jgrapes.core.Manager; 039import org.jgrapes.core.annotation.HandlerDefinition; 040import org.jgrapes.core.annotation.HandlerDefinition.ChannelReplacements; 041import org.jgrapes.core.events.Attached; 042import org.jgrapes.core.events.Detached; 043import org.jgrapes.core.events.Start; 044 045/** 046 * ComponentVertex is the base class for all nodes in the component tree. 047 * ComponentVertex is extended by {@link org.jgrapes.core.Component} 048 * for the use as base class for component implementations. As an 049 * alternative for implementing components with an independent base class, 050 * the derived class {@link org.jgrapes.core.internal.ComponentProxy} can be 051 * used. 052 */ 053@SuppressWarnings({ "PMD.TooManyMethods", "PMD.GodClass", 054 "PMD.CouplingBetweenObjects", "PMD.AvoidSynchronizedStatement" }) 055public abstract class ComponentVertex implements Manager, Channel { 056 057 /** The component's (optional) name. */ 058 private String name; 059 /** Reference to the common properties of the tree nodes. */ 060 private ComponentTree tree; 061 /** Reference to the parent node. */ 062 private ComponentVertex parent; 063 /** All the node's children */ 064 private final List<ComponentVertex> children = new ArrayList<>(); 065 /** The handlers provided by this component. */ 066 private List<HandlerReference> handlers; 067 /** The channel replacements. */ 068 @SuppressWarnings({ "PMD.LooseCoupling", "PMD.AvoidDuplicateLiterals" }) 069 private final ChannelReplacements channelReplacements; 070 071 /** 072 * 073 * Initialize the ComponentVertex. By default it forms a stand-alone 074 * tree, i.e. the root is set to the component itself. 075 * 076 * @param channelReplacements the channel replacements 077 */ 078 @SuppressWarnings("PMD.LooseCoupling") 079 protected ComponentVertex(ChannelReplacements channelReplacements) { 080 this.channelReplacements = channelReplacements; 081 } 082 083 /** 084 * Return the channel replacements passed to the constructor. 085 * 086 * @return the channel replacements 087 */ 088 @SuppressWarnings("PMD.LooseCoupling") 089 public ChannelReplacements channelReplacements() { 090 return channelReplacements; 091 } 092 093 /** 094 * Initialize the handler list of this component. May only be called 095 * when {@link #component()} can be relied on to return the 096 * correct value. 097 */ 098 protected void initComponentsHandlers() { 099 handlers = new ArrayList<>(); 100 // Have a look at all methods. 101 for (Method m : component().getClass().getMethods()) { 102 maybeAddHandler(m, channelReplacements); 103 } 104 handlers = Collections.synchronizedList(handlers); 105 } 106 107 @SuppressWarnings("PMD.LooseCoupling") 108 private void maybeAddHandler( 109 Method method, ChannelReplacements channelReplacements) { 110 for (Annotation annotation : method.getDeclaredAnnotations()) { 111 Class<?> annoType = annotation.annotationType(); 112 HandlerDefinition hda = annoType.getAnnotation( 113 HandlerDefinition.class); 114 if (hda == null) { 115 continue; 116 } 117 HandlerDefinition.Evaluator evaluator 118 = CoreUtils.definitionEvaluator(hda); 119 HandlerScope scope = evaluator.scope(component(), method, 120 channelReplacements); 121 if (scope == null) { 122 continue; 123 } 124 handlers.add(HandlerReference.newRef( 125 component(), method, evaluator.priority(annotation), 126 scope)); 127 } 128 } 129 130 /* 131 * (non-Javadoc) 132 * 133 * @see org.jgrapes.core.Manager#setName(java.lang.String) 134 */ 135 @Override 136 public ComponentType setName(String name) { 137 this.name = name; 138 return component(); 139 } 140 141 /* 142 * (non-Javadoc) 143 * 144 * @see org.jgrapes.core.Manager#name() 145 */ 146 @Override 147 public String name() { 148 return name; 149 } 150 151 /* 152 * (non-Javadoc) 153 * 154 * @see org.jgrapes.core.Manager#path() 155 */ 156 @Override 157 public String componentPath() { 158 StringBuilder res = new StringBuilder(); 159 buildPath(res); 160 return res.toString(); 161 } 162 163 private void buildPath(StringBuilder builder) { 164 if (parent != null) { 165 parent.buildPath(builder); 166 } 167 builder.append('/') 168 .append(name == null ? getClass().getSimpleName() : name); 169 } 170 171 /** 172 * Return the component node for a given component. 173 * 174 * @param component the component 175 * @param componentChannel the component's channel 176 * @return the node representing the component in the tree 177 */ 178 public static ComponentVertex componentVertex( 179 ComponentType component, Channel componentChannel) { 180 if (component instanceof ComponentVertex) { 181 return (ComponentVertex) component; 182 } 183 return ComponentProxy.getComponentProxy(component, componentChannel); 184 } 185 186 /** 187 * Returns the component represented by this node in the tree. 188 * 189 * @return the component 190 */ 191 @Override 192 public abstract ComponentType component(); 193 194 /* 195 * (non-Javadoc) 196 * 197 * @see org.jgrapes.core.Manager#getChildren() 198 */ 199 @Override 200 public List<ComponentType> children() { 201 synchronized (this) { 202 List<ComponentType> children = new ArrayList<>(); 203 for (ComponentVertex child : this.children) { 204 children.add(child.component()); 205 } 206 return Collections.unmodifiableList(children); 207 } 208 } 209 210 /* 211 * (non-Javadoc) 212 * 213 * @see org.jgrapes.core.Manager#getParent() 214 */ 215 @Override 216 public ComponentType parent() { 217 synchronized (this) { 218 if (parent == null) { 219 return null; 220 } 221 return parent.component(); 222 } 223 } 224 225 /* 226 * (non-Javadoc) 227 * 228 * @see org.jgrapes.core.Manager#getRoot() 229 */ 230 @Override 231 public ComponentType root() { 232 return tree().root().component(); 233 } 234 235 /** 236 * Return the tree that this node belongs to. If the node does not 237 * belong to a tree yet, a tree is automatically created. 238 * 239 * @return the tree 240 */ 241 /* default */ ComponentTree tree() { 242 if (tree != null) { 243 return tree; 244 } 245 // Build complete tree before assigning it. 246 ComponentTree newTree = new ComponentTree(this); 247 newTree.setEventPipeline(new BufferingEventPipeline(newTree)); 248 tree = newTree; 249 fire(new Attached(component(), null), channel()); 250 return tree; 251 } 252 253 /** 254 * Set the reference to the common properties of this component 255 * and all its children to the given value. 256 * 257 * @param comp the new root 258 */ 259 private void setTree(ComponentTree tree) { 260 synchronized (this) { 261 this.tree = tree; 262 for (ComponentVertex child : children) { 263 child.setTree(tree); 264 } 265 } 266 } 267 268 /* 269 * (non-Javadoc) 270 * 271 * @see org.jgrapes.core.Manager#attach(Component) 272 */ 273 @Override 274 @SuppressWarnings({ "PMD.CyclomaticComplexity", "PMD.NcssCount", 275 "PMD.CognitiveComplexity" }) 276 public <T extends ComponentType> T attach(T child) { 277 synchronized (this) { 278 ComponentVertex childNode = componentVertex(child, null); 279 List<Channel> attachedAsChannels = new ArrayList<>(); 280 synchronized (childNode) { 281 if (tree != null && tree.isStarted()) { 282 for (TreeIterator itr = new TreeIterator(childNode); 283 itr.hasNext();) { 284 attachedAsChannels.add(itr.next()); 285 } 286 } 287 synchronized (tree()) { 288 if (childNode.tree == null) { 289 // Newly created, stand-alone child node 290 childNode.parent = this; 291 childNode.setTree(tree); 292 children.add(childNode); 293 } else { 294 // Attaching a tree... 295 synchronized (childNode.tree) { 296 if (childNode.parent != null) { 297 throw new IllegalStateException( 298 "Cannot attach a node with a parent."); 299 } 300 if (childNode.tree.isStarted()) { 301 throw new IllegalStateException( 302 "Cannot attach a started subtree."); 303 } 304 childNode.parent = this; 305 ComponentTree childTree = childNode.tree; 306 childNode.setTree(tree); 307 children.add(childNode); 308 tree.mergeEvents(childTree); 309 } 310 } 311 tree.clearHandlerCache(); 312 } 313 } 314 Channel parentChan = channel(); 315 if (parentChan == null) { 316 parentChan = BROADCAST; 317 } 318 Channel childChan = childNode.channel(); 319 if (childChan == null) { 320 parentChan = BROADCAST; 321 } 322 Attached evt = new Attached(childNode.component(), component()); 323 if (parentChan.equals(BROADCAST) || childChan.equals(BROADCAST)) { 324 fire(evt, BROADCAST); 325 } else if (parentChan.equals(childChan)) { 326 fire(evt, parentChan); 327 } else { 328 fire(evt, parentChan, childChan); 329 } 330 if (!attachedAsChannels.isEmpty()) { 331 fire(new Start(), attachedAsChannels.toArray(new Channel[0])); 332 } 333 return child; 334 } 335 } 336 337 /* 338 * (non-Javadoc) 339 * 340 * @see org.jgrapes.core.Manager#detach() 341 */ 342 @Override 343 public ComponentType detach() { 344 synchronized (this) { 345 if (parent != null) { 346 ComponentVertex oldParent = parent; 347 synchronized (tree) { 348 if (!tree.isStarted()) { 349 throw new IllegalStateException( 350 "Components may not be detached from a tree before" 351 + " a Start event has been fired on it."); 352 } 353 synchronized (oldParent) { 354 parent.children.remove(this); 355 parent.tree.clearHandlerCache(); 356 parent = null; 357 } 358 ComponentTree newTree = new ComponentTree(this); 359 newTree.setEventPipeline(new FeedBackPipelineFilter( 360 newTree, new EventProcessor(newTree))); 361 setTree(newTree); 362 } 363 Detached evt = new Detached(component(), oldParent.component()); 364 oldParent.fire(evt); 365 evt = new Detached(component(), oldParent.component()); 366 fire(evt); 367 } 368 return component(); 369 } 370 } 371 372 /* 373 * (non-Javadoc) 374 * 375 * @see java.lang.Iterable#iterator() 376 */ 377 @Override 378 public Iterator<ComponentType> iterator() { 379 return new ComponentIterator(new TreeIterator(this)); 380 } 381 382 /** 383 * A simple wrapper that converts a component vertex iterator 384 * to a component (type) iterator. 385 */ 386 @SuppressWarnings("PMD.PublicMemberInNonPublicType") 387 private static class ComponentIterator implements Iterator<ComponentType> { 388 389 private final TreeIterator baseIterator; 390 391 /** 392 * @param baseIterator 393 */ 394 public ComponentIterator(TreeIterator baseIterator) { 395 this.baseIterator = baseIterator; 396 } 397 398 @Override 399 public boolean hasNext() { 400 return baseIterator.hasNext(); 401 } 402 403 @Override 404 public ComponentType next() { 405 return baseIterator.next().component(); 406 } 407 408 } 409 410 /** 411 * An iterator for getting all nodes of the tree. 412 */ 413 @SuppressWarnings("PMD.PublicMemberInNonPublicType") 414 private static class TreeIterator implements Iterator<ComponentVertex> { 415 416 @SuppressWarnings({ "PMD.LooseCoupling" }) 417 private final Stack<CurPos> stack = new Stack<>(); 418 private final ComponentTree tree; 419 420 /** 421 * The current position. 422 */ 423 private class CurPos { 424 public final ComponentVertex current; 425 public final Iterator<ComponentVertex> childIter; 426 427 /** 428 * Instantiates a new current position 429 * 430 * @param vertex the cm 431 */ 432 public CurPos(ComponentVertex vertex) { 433 current = vertex; 434 childIter = current.children.iterator(); 435 } 436 } 437 438 /** 439 * Instantiates a new tree iterator. 440 * 441 * @param root the root 442 */ 443 public TreeIterator(ComponentVertex root) { 444 tree = root.tree(); 445 stack.push(new CurPos(root)); 446 } 447 448 /* 449 * (non-Javadoc) 450 * 451 * @see java.util.Iterator#hasNext() 452 */ 453 @Override 454 public boolean hasNext() { 455 return !stack.empty(); 456 } 457 458 /* 459 * (non-Javadoc) 460 * 461 * @see java.util.Iterator#next() 462 */ 463 @Override 464 public ComponentVertex next() { 465 if (stack.empty()) { 466 throw new NoSuchElementException(); 467 } 468 CurPos pos = stack.peek(); 469 ComponentVertex res = pos.current; 470 while (true) { 471 synchronized (pos.current) { 472 if (pos.current.tree != tree) { // NOPMD (comparison) 473 throw new ConcurrentModificationException(); 474 } 475 if (pos.childIter.hasNext()) { 476 stack.push(new CurPos(pos.childIter.next())); 477 break; 478 } 479 } 480 stack.pop(); 481 if (stack.empty()) { 482 break; 483 } 484 pos = stack.peek(); 485 } 486 return res; 487 } 488 489 /* 490 * (non-Javadoc) 491 * 492 * @see java.util.Iterator#remove() 493 */ 494 @Override 495 public void remove() { 496 throw new UnsupportedOperationException(); 497 } 498 } 499 500 @Override 501 public void addHandler(Method method, HandlerScope scope, int priority) { 502 handlers.add(HandlerReference.newRef(component(), 503 method, priority, scope)); 504 } 505 506 /* 507 * (non-Javadoc) 508 * 509 * @see org.jgrapes.core.core.Manager#fire 510 * (org.jgrapes.core.Event, org.jgrapes.core.Channel) 511 */ 512 @Override 513 public <T> Event<T> fire(Event<T> event, Channel... channels) { 514 if (channels.length == 0) { 515 channels = event.channels(); 516 if (channels.length == 0) { 517 channels = new Channel[] { channel() }; 518 } 519 } 520 event.setChannels(channels); 521 tree().fire(event, channels); 522 return event; 523 } 524 525 /** 526 * Collects all handlers. Iterates over the tree with this object 527 * as root and for all child components adds the matching handlers to 528 * the result set recursively. 529 * 530 * @param hdlrs the result set 531 * @param event the event to match 532 * @param channels the channels to match 533 */ 534 @SuppressWarnings("PMD.UseVarargs") 535 /* default */ void collectHandlers(Collection<HandlerReference> hdlrs, 536 EventBase<?> event, Channel[] channels) { 537 for (HandlerReference hdlr : handlers) { 538 if (hdlr.handles(event, channels)) { 539 hdlrs.add(hdlr); 540 } 541 } 542 for (ComponentVertex child : children) { 543 child.collectHandlers(hdlrs, event, channels); 544 } 545 } 546 547 /* 548 * (non-Javadoc) 549 * 550 * @see org.jgrapes.core.Manager#activeEventPipeline() 551 */ 552 @Override 553 public EventPipeline activeEventPipeline() { 554 return new CheckingPipelineFilter(tree(), 555 tree().eventPipeline(), channel()); 556 } 557 558 /* 559 * (non-Javadoc) 560 * 561 * @see org.jgrapes.core.Manager#newEventPipeline() 562 */ 563 @Override 564 public EventPipeline newEventPipeline() { 565 return new CheckingPipelineFilter(tree(), new EventProcessor(tree()), 566 channel()); 567 } 568 569 /* 570 * (non-Javadoc) 571 * 572 * @see org.jgrapes.core.Manager#newEventPipeline(java.util.concurrent. 573 * ExecutorService) 574 */ 575 @Override 576 public EventPipeline newEventPipeline(ExecutorService executorService) { 577 return new CheckingPipelineFilter(tree(), 578 new EventProcessor(tree(), executorService), channel()); 579 } 580 581 /** 582 * If a name has been set for this component 583 * (see {@link Manager#setName(String)}), return the name, 584 * else return the object name provided by 585 * {@link Components#objectName(Object)}, using 586 * {@link #component()} as argument. 587 */ 588 @Override 589 public String toString() { 590 if (name != null) { 591 return name; 592 } 593 return Components.objectName(component()); 594 } 595 596 /* 597 * (non-Javadoc) 598 * 599 * @see org.jgrapes.core.Manager#registerAsGenerator() 600 */ 601 @Override 602 public void registerAsGenerator() { 603 GeneratorRegistry.instance().add(component()); 604 } 605 606 /* 607 * (non-Javadoc) 608 * 609 * @see org.jgrapes.core.Manager#unregisterAsGenerator() 610 */ 611 @Override 612 public void unregisterAsGenerator() { 613 GeneratorRegistry.instance().remove(component()); 614 } 615 616}