Search in sources :

Example 6 with ApplicationListener

use of org.apache.catalina.deploy.ApplicationListener in project tomcat70 by apache.

the class StandardContext method addApplicationListener.

@Override
public void addApplicationListener(ApplicationListener listener) {
    synchronized (applicationListenersLock) {
        ApplicationListener[] results = new ApplicationListener[applicationListeners.length + 1];
        for (int i = 0; i < applicationListeners.length; i++) {
            if (listener.equals(applicationListeners[i])) {
                log.info(sm.getString("standardContext.duplicateListener", listener.getClassName()));
                return;
            }
            results[i] = applicationListeners[i];
        }
        results[applicationListeners.length] = listener;
        applicationListeners = results;
    }
    fireContainerEvent("addApplicationListener", listener);
// FIXME - add instance if already started?
}
Also used : ApplicationListener(org.apache.catalina.deploy.ApplicationListener) SecurityConstraint(org.apache.catalina.deploy.SecurityConstraint)

Example 7 with ApplicationListener

use of org.apache.catalina.deploy.ApplicationListener in project tomcat70 by apache.

the class StandardContext method removeApplicationListener.

/**
 * Remove the specified application listener class from the set of
 * listeners for this application.
 *
 * @param listener Java class name of the listener to be removed
 */
@Override
public void removeApplicationListener(String listener) {
    synchronized (applicationListenersLock) {
        // Make sure this listener is currently present
        int n = -1;
        for (int i = 0; i < applicationListeners.length; i++) {
            if (applicationListeners[i].getClassName().equals(listener)) {
                n = i;
                break;
            }
        }
        if (n < 0)
            return;
        // Remove the specified listener
        int j = 0;
        ApplicationListener[] results = new ApplicationListener[applicationListeners.length - 1];
        for (int i = 0; i < applicationListeners.length; i++) {
            if (i != n)
                results[j++] = applicationListeners[i];
        }
        applicationListeners = results;
    }
    // Inform interested listeners
    fireContainerEvent("removeApplicationListener", listener);
// FIXME - behavior if already started?
}
Also used : ApplicationListener(org.apache.catalina.deploy.ApplicationListener) SecurityConstraint(org.apache.catalina.deploy.SecurityConstraint)

Example 8 with ApplicationListener

use of org.apache.catalina.deploy.ApplicationListener in project tomcat70 by apache.

the class StandardContext method listenerStart.

/**
 * Configure the set of instantiated application event listeners
 * for this Context.  Return <code>true</code> if all listeners wre
 * initialized successfully, or <code>false</code> otherwise.
 */
public boolean listenerStart() {
    if (log.isDebugEnabled())
        log.debug("Configuring application event listeners");
    // Instantiate the required listeners
    ApplicationListener[] listeners = applicationListeners;
    Object[] results = new Object[listeners.length];
    boolean ok = true;
    for (int i = 0; i < results.length; i++) {
        if (getLogger().isDebugEnabled())
            getLogger().debug(" Configuring event listener class '" + listeners[i] + "'");
        try {
            ApplicationListener listener = listeners[i];
            results[i] = getInstanceManager().newInstance(listener.getClassName());
            if (listener.isPluggabilityBlocked()) {
                noPluggabilityListeners.add(results[i]);
            }
        } catch (Throwable t) {
            t = ExceptionUtils.unwrapInvocationTargetException(t);
            ExceptionUtils.handleThrowable(t);
            getLogger().error(sm.getString("standardContext.applicationListener", listeners[i].getClassName()), t);
            ok = false;
        }
    }
    if (!ok) {
        getLogger().error(sm.getString("standardContext.applicationSkipped"));
        return (false);
    }
    // Sort listeners in two arrays
    ArrayList<Object> eventListeners = new ArrayList<Object>();
    ArrayList<Object> lifecycleListeners = new ArrayList<Object>();
    for (int i = 0; i < results.length; i++) {
        if ((results[i] instanceof ServletContextAttributeListener) || (results[i] instanceof ServletRequestAttributeListener) || (results[i] instanceof ServletRequestListener) || (results[i] instanceof HttpSessionAttributeListener)) {
            eventListeners.add(results[i]);
        }
        if ((results[i] instanceof ServletContextListener) || (results[i] instanceof HttpSessionListener)) {
            lifecycleListeners.add(results[i]);
        }
    }
    // list.
    for (Object eventListener : getApplicationEventListeners()) {
        eventListeners.add(eventListener);
    }
    setApplicationEventListeners(eventListeners.toArray());
    for (Object lifecycleListener : getApplicationLifecycleListeners()) {
        lifecycleListeners.add(lifecycleListener);
        if (lifecycleListener instanceof ServletContextListener) {
            noPluggabilityListeners.add(lifecycleListener);
        }
    }
    setApplicationLifecycleListeners(lifecycleListeners.toArray());
    if (getLogger().isDebugEnabled())
        getLogger().debug("Sending application start events");
    // Ensure context is not null
    getServletContext();
    context.setNewServletContextListenerAllowed(false);
    Object[] instances = getApplicationLifecycleListeners();
    if (instances == null || instances.length == 0) {
        return ok;
    }
    ServletContextEvent event = new ServletContextEvent(getServletContext());
    ServletContextEvent tldEvent = null;
    if (noPluggabilityListeners.size() > 0) {
        noPluggabilityServletContext = new NoPluggabilityServletContext(getServletContext());
        tldEvent = new ServletContextEvent(noPluggabilityServletContext);
    }
    for (int i = 0; i < instances.length; i++) {
        if (instances[i] == null)
            continue;
        if (!(instances[i] instanceof ServletContextListener))
            continue;
        ServletContextListener listener = (ServletContextListener) instances[i];
        try {
            fireContainerEvent("beforeContextInitialized", listener);
            if (noPluggabilityListeners.contains(listener)) {
                listener.contextInitialized(tldEvent);
            } else {
                listener.contextInitialized(event);
            }
            fireContainerEvent("afterContextInitialized", listener);
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            fireContainerEvent("afterContextInitialized", listener);
            getLogger().error(sm.getString("standardContext.listenerStart", instances[i].getClass().getName()), t);
            ok = false;
        }
    }
    return (ok);
}
Also used : ServletContextAttributeListener(javax.servlet.ServletContextAttributeListener) ServletRequestAttributeListener(javax.servlet.ServletRequestAttributeListener) HttpSessionListener(javax.servlet.http.HttpSessionListener) ServletContextListener(javax.servlet.ServletContextListener) ServletRequestListener(javax.servlet.ServletRequestListener) ArrayList(java.util.ArrayList) SecurityConstraint(org.apache.catalina.deploy.SecurityConstraint) ApplicationListener(org.apache.catalina.deploy.ApplicationListener) HttpSessionAttributeListener(javax.servlet.http.HttpSessionAttributeListener) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 9 with ApplicationListener

use of org.apache.catalina.deploy.ApplicationListener in project tomcat70 by apache.

the class TestWebSocket method testNoConnection.

@Test
public void testNoConnection() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(new ApplicationListener(TesterEchoServer.Config.class.getName(), false));
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");
    tomcat.start();
    WebSocketClient client = new WebSocketClient(getPort());
    // Send the WebSocket handshake
    client.writer.write("GET " + TesterEchoServer.Config.PATH_BASIC + " HTTP/1.1" + CRLF);
    client.writer.write("Host: foo" + CRLF);
    client.writer.write("Upgrade: websocket" + CRLF);
    client.writer.write("Sec-WebSocket-Version: 13" + CRLF);
    client.writer.write("Sec-WebSocket-Key: TODO" + CRLF);
    client.writer.write(CRLF);
    client.writer.flush();
    // Make sure we got an error response
    String responseLine = client.reader.readLine();
    Assert.assertTrue(responseLine.startsWith("HTTP/1.1 400"));
    // Finished with the socket
    client.close();
}
Also used : InitialContext(javax.naming.InitialContext) Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) TesterEchoServer(org.apache.tomcat.websocket.TesterEchoServer) ApplicationListener(org.apache.catalina.deploy.ApplicationListener) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

ApplicationListener (org.apache.catalina.deploy.ApplicationListener)9 InitialContext (javax.naming.InitialContext)5 Context (org.apache.catalina.Context)5 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)5 Tomcat (org.apache.catalina.startup.Tomcat)5 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)5 TesterEchoServer (org.apache.tomcat.websocket.TesterEchoServer)5 Test (org.junit.Test)5 SecurityConstraint (org.apache.catalina.deploy.SecurityConstraint)3 ArrayList (java.util.ArrayList)2 MessageDigest (java.security.MessageDigest)1 ServletContextAttributeListener (javax.servlet.ServletContextAttributeListener)1 ServletContextEvent (javax.servlet.ServletContextEvent)1 ServletContextListener (javax.servlet.ServletContextListener)1 ServletRequestAttributeListener (javax.servlet.ServletRequestAttributeListener)1 ServletRequestListener (javax.servlet.ServletRequestListener)1 HttpSessionAttributeListener (javax.servlet.http.HttpSessionAttributeListener)1 HttpSessionListener (javax.servlet.http.HttpSessionListener)1 StandardContext (org.apache.catalina.core.StandardContext)1 JarScanner (org.apache.tomcat.JarScanner)1