Skip to content

Commit 44a3996

Browse files
authored
Merge pull request #718 from rhusar/MODCLUSTER-790-v2
MODCLUSTER-790 mod_cluster listener throws NPE when deploying a context in Tomcat - refine ContainerEventHandler#add contract and implementation
2 parents 24894ae + b56a618 commit 44a3996

File tree

11 files changed

+28
-31
lines changed

11 files changed

+28
-31
lines changed

container/spi/src/main/java/org/jboss/modcluster/container/ContainerEventHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ public interface ContainerEventHandler {
4242
void shutdown();
4343

4444
/**
45-
* Indicates the deployment of a new web application. This event triggers a ENABLE-APP command for this context, if it is
46-
* already started.
45+
* Indicates the deployment of a new web application which is not intending to start immediately. This will issue a STOP-APP
46+
* without session draining command on the proxies. In case the application will start
47+
* use {@link ContainerEventHandler#start(Context)}.
4748
*
4849
* @param context the added context
4950
*/

container/tomcat-10.0/src/main/java/org/jboss/modcluster/container/tomcat/TomcatEventHandlerAdapter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,7 @@ public void containerEvent(ContainerEvent event) {
135135
((Lifecycle) child).addLifecycleListener(this);
136136
((Container) child).addPropertyChangeListener(this);
137137

138-
if (this.start.get()) {
139-
this.eventHandler.add(new TomcatContext(registry, (Context) child));
140-
}
138+
// n.b. MODCLUSTER-790 do not ContainerEventHandler#add(..) the context here - wait for actual start(..)
141139
} else if (container instanceof Engine) {
142140
// Deploying a host
143141
container.addContainerListener(this);

container/tomcat-10.0/src/test/java/org/jboss/modcluster/container/tomcat/ContainerEventHandlerAdapterTestCase.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,10 @@ public void deployWebApp() throws Exception {
169169

170170
verify(context).addLifecycleListener(handler);
171171
verify(context).addPropertyChangeListener(handler);
172-
verify(this.eventHandler).add(eq(catalinaContext));
172+
173+
LifecycleEvent lifecycleEvent = new LifecycleEvent(context, Lifecycle.AFTER_START_EVENT, context);
174+
handler.lifecycleEvent(lifecycleEvent);
175+
verify(this.eventHandler).start(eq(catalinaContext));
173176
}
174177

175178
@Test

container/tomcat-10.1/src/main/java/org/jboss/modcluster/container/tomcat/TomcatEventHandlerAdapter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,7 @@ public void containerEvent(ContainerEvent event) {
135135
((Lifecycle) child).addLifecycleListener(this);
136136
((Container) child).addPropertyChangeListener(this);
137137

138-
if (this.start.get()) {
139-
this.eventHandler.add(new TomcatContext(registry, (Context) child));
140-
}
138+
// n.b. MODCLUSTER-790 do not ContainerEventHandler#add(..) the context here - wait for actual start(..)
141139
} else if (container instanceof Engine) {
142140
// Deploying a host
143141
container.addContainerListener(this);

container/tomcat-10.1/src/test/java/org/jboss/modcluster/container/tomcat/ContainerEventHandlerAdapterTestCase.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,10 @@ public void deployWebApp() throws Exception {
169169

170170
verify(context).addLifecycleListener(handler);
171171
verify(context).addPropertyChangeListener(handler);
172-
verify(this.eventHandler).add(eq(catalinaContext));
172+
173+
LifecycleEvent lifecycleEvent = new LifecycleEvent(context, Lifecycle.AFTER_START_EVENT, context);
174+
handler.lifecycleEvent(lifecycleEvent);
175+
verify(this.eventHandler).start(eq(catalinaContext));
173176
}
174177

175178
@Test

container/tomcat-8.5/src/main/java/org/jboss/modcluster/container/tomcat/TomcatEventHandlerAdapter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,7 @@ public void containerEvent(ContainerEvent event) {
135135
((Lifecycle) child).addLifecycleListener(this);
136136
((Container) child).addPropertyChangeListener(this);
137137

138-
if (this.start.get()) {
139-
this.eventHandler.add(new TomcatContext(registry, (Context) child));
140-
}
138+
// n.b. MODCLUSTER-790 do not ContainerEventHandler#add(..) the context here - wait for actual start(..)
141139
} else if (container instanceof Engine) {
142140
// Deploying a host
143141
container.addContainerListener(this);

container/tomcat-8.5/src/test/java/org/jboss/modcluster/container/tomcat/ContainerEventHandlerAdapterTestCase.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,10 @@ public void deployWebApp() throws Exception {
169169

170170
verify(context).addLifecycleListener(handler);
171171
verify(context).addPropertyChangeListener(handler);
172-
verify(this.eventHandler).add(eq(catalinaContext));
172+
173+
LifecycleEvent lifecycleEvent = new LifecycleEvent(context, Lifecycle.AFTER_START_EVENT, context);
174+
handler.lifecycleEvent(lifecycleEvent);
175+
verify(this.eventHandler).start(eq(catalinaContext));
173176
}
174177

175178
@Test

container/tomcat-9.0/src/main/java/org/jboss/modcluster/container/tomcat/TomcatEventHandlerAdapter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,7 @@ public void containerEvent(ContainerEvent event) {
135135
((Lifecycle) child).addLifecycleListener(this);
136136
((Container) child).addPropertyChangeListener(this);
137137

138-
if (this.start.get()) {
139-
this.eventHandler.add(new TomcatContext(registry, (Context) child));
140-
}
138+
// n.b. MODCLUSTER-790 do not ContainerEventHandler#add(..) the context here - wait for actual start(..)
141139
} else if (container instanceof Engine) {
142140
// Deploying a host
143141
container.addContainerListener(this);

container/tomcat-9.0/src/test/java/org/jboss/modcluster/container/tomcat/ContainerEventHandlerAdapterTestCase.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,10 @@ public void deployWebApp() throws Exception {
169169

170170
verify(context).addLifecycleListener(handler);
171171
verify(context).addPropertyChangeListener(handler);
172-
verify(this.eventHandler).add(eq(catalinaContext));
172+
173+
LifecycleEvent lifecycleEvent = new LifecycleEvent(context, Lifecycle.AFTER_START_EVENT, context);
174+
handler.lifecycleEvent(lifecycleEvent);
175+
verify(this.eventHandler).start(eq(catalinaContext));
173176
}
174177

175178
@Test

core/src/main/java/org/jboss/modcluster/ModClusterService.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,7 @@ public void start(Server server) {
211211
for (Engine engine : server.getEngines()) {
212212
this.config(engine);
213213

214-
for (Host host : engine.getHosts()) {
215-
for (Context context : host.getContexts()) {
216-
this.add(context);
217-
}
218-
}
214+
// n.b. MODCLUSTER-790 do not ContainerEventHandler#add(..) the context here - wait for actual start(..)
219215
}
220216
}
221217
}
@@ -300,12 +296,8 @@ public void add(Context context) {
300296
ModClusterLogger.LOGGER.addContext(context.getHost(), context);
301297

302298
if (this.include(context) && this.established) {
303-
// Send ENABLE-APP if state is started
304-
if (context.isStarted()) {
305-
this.enable(context);
306-
} else {
307-
this.stop(context);
308-
}
299+
// Send a STOP-APP directly, without session draining.
300+
this.mcmpHandler.sendRequest(this.requestFactory.createStopRequest(context));
309301
}
310302
}
311303

0 commit comments

Comments
 (0)