Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2020, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.modcluster.container.tomcat;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.LifecycleState;
import org.apache.catalina.Server;
import org.apache.catalina.Service;
import org.apache.catalina.deploy.NamingResources;
import org.apache.catalina.startup.Catalina;

import javax.naming.Context;
import java.util.Objects;

/**
* A {@link Server} delegate which filters found services to return only one configured {@link Service}.
*
* @author Radoslav Husar
*/
public class ServiceFilteringDelegatingServer implements Server {

private final Server delegate;
private final String serviceName;

public ServiceFilteringDelegatingServer(Service service) {
super();
this.delegate = service.getServer();
this.serviceName = service.getName();
}

@Override
public Service[] findServices() {
for (Service service : delegate.findServices()) {
if (service.getName().equals(serviceName)) {
return new Service[] { service };
}
}

return new Service[] {};
}

// Delegating methods

@Override
public String getInfo() {
return delegate.getInfo();
}

@Override
public NamingResources getGlobalNamingResources() {
return delegate.getGlobalNamingResources();
}

@Override
public void setGlobalNamingResources(NamingResources globalNamingResources) {
delegate.setGlobalNamingResources(globalNamingResources);
}

@Override
public Context getGlobalNamingContext() {
return delegate.getGlobalNamingContext();
}

@Override
public int getPort() {
return delegate.getPort();
}

@Override
public void setPort(int port) {
delegate.setPort(port);
}

@Override
public String getAddress() {
return delegate.getAddress();
}

@Override
public void setAddress(String address) {
delegate.setAddress(address);
}

@Override
public String getShutdown() {
return delegate.getShutdown();
}

@Override
public void setShutdown(String shutdown) {
delegate.setShutdown(shutdown);
}

@Override
public ClassLoader getParentClassLoader() {
return delegate.getParentClassLoader();
}

@Override
public void setParentClassLoader(ClassLoader parent) {
delegate.setParentClassLoader(parent);
}

@Override
public Catalina getCatalina() {
return delegate.getCatalina();
}

@Override
public void setCatalina(Catalina catalina) {
delegate.setCatalina(catalina);
}

@Override
public void addService(Service service) {
delegate.addService(service);
}

@Override
public void await() {
delegate.await();
}

@Override
public Service findService(String name) {
return delegate.findService(name);
}

@Override
public void removeService(Service service) {
delegate.removeService(service);
}

@Override
public void addLifecycleListener(LifecycleListener listener) {
delegate.addLifecycleListener(listener);
}

@Override
public LifecycleListener[] findLifecycleListeners() {
return delegate.findLifecycleListeners();
}

@Override
public void removeLifecycleListener(LifecycleListener listener) {
delegate.removeLifecycleListener(listener);
}

@Override
public void init() throws LifecycleException {
delegate.init();
}

@Override
public void start() throws LifecycleException {
delegate.start();
}

@Override
public void stop() throws LifecycleException {
delegate.stop();
}

@Override
public void destroy() throws LifecycleException {
delegate.destroy();
}

@Override
public LifecycleState getState() {
return delegate.getState();
}

@Override
public String getStateName() {
return delegate.getStateName();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

ServiceFilteringDelegatingServer that = (ServiceFilteringDelegatingServer) o;

if (!Objects.equals(delegate, that.delegate)) return false;
return Objects.equals(serviceName, that.serviceName);
}

@Override
public int hashCode() {
int result = delegate != null ? delegate.hashCode() : 0;
result = 31 * result + (serviceName != null ? serviceName.hashCode() : 0);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@
*/
package org.jboss.modcluster.container.tomcat;

import java.beans.PropertyChangeEvent;
import java.lang.management.ManagementFactory;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

import org.apache.catalina.Container;
import org.apache.catalina.ContainerEvent;
import org.apache.catalina.Context;
Expand All @@ -40,8 +34,17 @@
import org.apache.catalina.connector.Connector;
import org.jboss.modcluster.container.ContainerEventHandler;

import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import java.beans.PropertyChangeEvent;
import java.lang.management.ManagementFactory;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* Adapts lifecycle and container listener events to the {@link ContainerEventHandler} interface.
*
* @author Paul Ferraro
* @author Radoslav Husar
*/
public class TomcatEventHandlerAdapter implements TomcatEventHandler {

Expand Down Expand Up @@ -112,7 +115,7 @@ public void stop() {
}

private boolean containsListener(Lifecycle lifecycle) {
for (LifecycleListener listener: lifecycle.findLifecycleListeners()) {
for (LifecycleListener listener : lifecycle.findLifecycleListeners()) {
if (listener.equals(this)) {
return true;
}
Expand Down Expand Up @@ -173,7 +176,8 @@ public void containerEvent(ContainerEvent event) {
}

/**
* Primary entry point for startup and shutdown events.
* Primary entry point for startup and shutdown events. Handles both situations where Listener is defined on either
* a top-level Server concept or Service in which case only the associated service is handled.
*/
@Override
public void lifecycleEvent(LifecycleEvent event) {
Expand All @@ -186,20 +190,35 @@ public void lifecycleEvent(LifecycleEvent event) {
Server server = (Server) source;
init(server);
}
} else if (source instanceof Service) {
if (this.init.compareAndSet(false, true)) {
Server server = new ServiceFilteringDelegatingServer((Service) source);
init(server);
}
}
} else if (type.equals(Lifecycle.START_EVENT)) {
if (source instanceof Server) {
if (this.init.compareAndSet(false, true)) {
Server server = (Server) source;
init(server);
}
} else if (source instanceof Service) {
if (this.init.compareAndSet(false, true)) {
Server server = new ServiceFilteringDelegatingServer((Service) source);
this.init(server);
}
}
} else if (type.equals(Lifecycle.AFTER_START_EVENT)) {
if (source instanceof Server) {
if (this.init.get() && this.start.compareAndSet(false, true)) {
this.eventHandler.start(this.factory.createServer((Server) source));
}
} else if(source instanceof Context) {
} else if (source instanceof Service) {
if (this.init.get() && this.start.compareAndSet(false, true)) {
Server server = new ServiceFilteringDelegatingServer((Service) source);
this.eventHandler.start(this.factory.createServer(server));
}
} else if (source instanceof Context) {
// Start a webapp
this.eventHandler.start(this.factory.createContext((Context) source));
}
Expand All @@ -213,12 +232,22 @@ public void lifecycleEvent(LifecycleEvent event) {
if (this.init.get() && this.start.compareAndSet(true, false)) {
this.eventHandler.stop(this.factory.createServer((Server) source));
}
} else if (source instanceof Service) {
if (this.init.get() && this.start.compareAndSet(true, false)) {
Server server = new ServiceFilteringDelegatingServer((Service) source);
this.eventHandler.stop(this.factory.createServer(server));
}
}
} else if (isBeforeDestroy(event)) {
if (source instanceof Server) {
if (this.init.compareAndSet(true, false)) {
this.destroy((Server) source);
}
} else if (source instanceof Service) {
if (this.init.compareAndSet(true, false)) {
Server server = new ServiceFilteringDelegatingServer((Service) source);
this.destroy(server);
}
}
} else if (type.equals(Lifecycle.PERIODIC_EVENT)) {
if (source instanceof Engine) {
Expand All @@ -241,10 +270,6 @@ protected boolean isBeforeDestroy(LifecycleEvent event) {
return event.getType().equals(Lifecycle.BEFORE_DESTROY_EVENT);
}

/**
* initialize server stuff: in jbossweb-2.1.x the server can't be destroyed so you could start (restart) one that needs
* initializations...
*/
protected void init(Server server) {
this.eventHandler.init(this.factory.createServer(server));

Expand Down
Loading