Skip to content

Commit a86a0c2

Browse files
Add static utility methods on container to add and remove beans.
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
1 parent 57779c6 commit a86a0c2

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

jetty-util/src/main/java/org/eclipse/jetty/util/component/Container.java

+40
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,46 @@ default List<EventListener> getEventListeners()
152152
return Collections.unmodifiableList(new ArrayList<>(getBeans(EventListener.class)));
153153
}
154154

155+
/**
156+
* A utility method to add a bean to a container.
157+
* @param parent the parent container.
158+
* @param child the child bean.
159+
* @return true if the child was added as a bean, false if parent was not instance of {@link Container} or bean was already present.
160+
*/
161+
static boolean addBean(Object parent, Object child)
162+
{
163+
if (parent instanceof Container)
164+
return ((Container)parent).addBean(child);
165+
return false;
166+
}
167+
168+
/**
169+
* A utility method to add a bean to a container.
170+
* @param parent the parent container.
171+
* @param child the child bean.
172+
* @param managed whether to managed the lifecycle of the bean.
173+
* @return true if the child was added as a bean, false if parent was not instance of {@link Container} or bean was already present.
174+
*/
175+
static boolean addBean(Object parent, Object child, boolean managed)
176+
{
177+
if (parent instanceof Container)
178+
return ((Container)parent).addBean(child, managed);
179+
return false;
180+
}
181+
182+
/**
183+
* A utility method to remove a bean from a container.
184+
* @param parent the parent container.
185+
* @param child the child bean.
186+
* @return true if parent was an instance of {@link Container} and the bean was removed.
187+
*/
188+
static boolean removeBean(Object parent, Object child)
189+
{
190+
if (parent instanceof Container)
191+
return ((Container)parent).removeBean(child);
192+
return false;
193+
}
194+
155195
/**
156196
* A listener for Container events.
157197
* If an added bean implements this interface it will receive the events

jetty-util/src/main/java/org/eclipse/jetty/util/component/ContainerLifeCycle.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,9 @@ public boolean addEventListener(EventListener listener)
474474
if (listener instanceof InheritedListener && b.isManaged() && b._bean instanceof Container)
475475
{
476476
if (b._bean instanceof ContainerLifeCycle)
477-
((ContainerLifeCycle)b._bean).addBean(listener, false);
477+
Container.addBean(b._bean, listener, false);
478478
else
479-
((Container)b._bean).addBean(listener);
479+
Container.addBean(b._bean, listener);
480480
}
481481
}
482482
}
@@ -499,8 +499,8 @@ public boolean removeEventListener(EventListener listener)
499499
{
500500
cl.beanRemoved(this, b._bean);
501501

502-
if (listener instanceof InheritedListener && b.isManaged() && b._bean instanceof Container)
503-
((Container)b._bean).removeBean(listener);
502+
if (listener instanceof InheritedListener && b.isManaged())
503+
Container.removeBean(b._bean, listener);
504504
}
505505
}
506506
return true;
@@ -541,9 +541,9 @@ private void manage(Bean bean)
541541
if (l instanceof InheritedListener)
542542
{
543543
if (bean._bean instanceof ContainerLifeCycle)
544-
((ContainerLifeCycle)bean._bean).addBean(l, false);
544+
Container.addBean(bean._bean, l, false);
545545
else
546-
((Container)bean._bean).addBean(l);
546+
Container.addBean(bean._bean, l);
547547
}
548548
}
549549
}
@@ -579,7 +579,7 @@ private void unmanage(Bean bean)
579579
for (Container.Listener l : _listeners)
580580
{
581581
if (l instanceof InheritedListener)
582-
((Container)bean._bean).removeBean(l);
582+
Container.removeBean(bean._bean, l);
583583
}
584584
}
585585
bean._managed = Managed.UNMANAGED;

0 commit comments

Comments
 (0)