Browse Source

Add ServiceDestroyListerner used by PushRequestHandler (#12251, #11878)

Change-Id: Id6147bbfe8da7cd3e3f3744acf3ef92b8c63b37b
tags/7.2.0.beta1
Leif Åstrand 11 years ago
parent
commit
5a0a9917f1

+ 50
- 0
server/src/com/vaadin/server/ServiceDestroyEvent.java View File

@@ -0,0 +1,50 @@
/*
* Copyright 2000-2013 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.vaadin.server;

import java.util.EventObject;

/**
* Event fired to {@link ServiceDestroyListener} when a {@link VaadinService} is
* being destroyed.
*
* @since 7.2
* @author Vaadin Ltd
*/
public class ServiceDestroyEvent extends EventObject {

/**
* Creates a new event for the given service.
*
* @param service
* the service being destroyed
*/
public ServiceDestroyEvent(VaadinService service) {
super(service);
}

/*
* (non-Javadoc)
*
* @see java.util.EventObject#getSource()
*/
@Override
public VaadinService getSource() {
return (VaadinService) super.getSource();
}

}

+ 39
- 0
server/src/com/vaadin/server/ServiceDestroyListener.java View File

@@ -0,0 +1,39 @@
/*
* Copyright 2000-2013 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.vaadin.server;

import java.io.Serializable;

/**
* Listener that gets notified when the {@link VaadinService} to which it has
* been registered is destroyed.
*
* @see VaadinService#addServiceDestroyListener(ServiceDestroyListener)
* @see VaadinService#removeServiceDestroyListener(ServiceDestroyListener)
*
* @since 7.2
* @author Vaadin Ltd
*/
public interface ServiceDestroyListener extends Serializable {
/**
* Invoked when a service is destroyed
*
* @param event
* the event
*/
public void serviceDestroy(ServiceDestroyEvent event);
}

+ 6
- 0
server/src/com/vaadin/server/VaadinPortlet.java View File

@@ -482,6 +482,12 @@ public class VaadinPortlet extends GenericPortlet implements Constants,
handleRequest(request, response);
}

@Override
public void destroy() {
super.destroy();
getService().destroy();
}

private static final Logger getLogger() {
return Logger.getLogger(VaadinPortlet.class.getName());
}

+ 51
- 0
server/src/com/vaadin/server/VaadinService.java View File

@@ -41,7 +41,9 @@ import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.portlet.Portlet;
import javax.portlet.PortletContext;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

@@ -81,6 +83,10 @@ public abstract class VaadinService implements Serializable {
.findMethod(SessionDestroyListener.class, "sessionDestroy",
SessionDestroyEvent.class);

private static final Method SERVICE_DESTROY_METHOD = ReflectTools
.findMethod(ServiceDestroyListener.class, "serviceDestroy",
ServiceDestroyEvent.class);

/**
* @deprecated As of 7.0. Only supported for {@link LegacyApplication}.
*/
@@ -1698,4 +1704,49 @@ public abstract class VaadinService implements Serializable {
}
}

/**
* Adds a service destroy listener that gets notified when this service is
* destroyed.
*
* @since 7.2
* @param listener
* the service destroy listener to add
*
* @see #destroy()
* @see #removeServiceDestroyListener(ServiceDestroyListener)
* @see ServiceDestroyListener
*/
public void addServiceDestroyListener(ServiceDestroyListener listener) {
eventRouter.addListener(ServiceDestroyEvent.class, listener,
SERVICE_DESTROY_METHOD);
}

/**
* Removes a service destroy listener that was previously added with
* {@link #addServiceDestroyListener(ServiceDestroyListener)}.
*
* @since 7.2
* @param listener
* the service destroy listener to remove
*/
public void removeServiceDestroyListener(ServiceDestroyListener listener) {
eventRouter.removeListener(ServiceDestroyEvent.class, listener,
SERVICE_DESTROY_METHOD);
}

/**
* Called when the servlet, portlet or similar for this service is being
* destroyed. After this method has been called, no more requests will be
* handled by this service.
*
* @see #addServiceDestroyListener(ServiceDestroyListener)
* @see Servlet#destroy()
* @see Portlet#destroy()
*
* @since 7.2
*/
public void destroy() {
eventRouter.fireEvent(new ServiceDestroyEvent(this));
}

}

+ 11
- 0
server/src/com/vaadin/server/VaadinServlet.java View File

@@ -1091,6 +1091,17 @@ public class VaadinServlet extends HttpServlet implements Constants {
return u;
}

/*
* (non-Javadoc)
*
* @see javax.servlet.GenericServlet#destroy()
*/
@Override
public void destroy() {
super.destroy();
getService().destroy();
}

/**
* Escapes characters to html entities. An exception is made for some
* "safe characters" to keep the text somewhat readable.

+ 9
- 0
server/src/com/vaadin/server/communication/PushRequestHandler.java View File

@@ -28,6 +28,8 @@ import org.atmosphere.cpr.AtmosphereRequest;
import org.atmosphere.cpr.AtmosphereResponse;

import com.vaadin.server.RequestHandler;
import com.vaadin.server.ServiceDestroyEvent;
import com.vaadin.server.ServiceDestroyListener;
import com.vaadin.server.ServiceException;
import com.vaadin.server.ServletPortletHelper;
import com.vaadin.server.SessionExpiredHandler;
@@ -63,6 +65,13 @@ public class PushRequestHandler implements RequestHandler,
}
};

service.addServiceDestroyListener(new ServiceDestroyListener() {
@Override
public void serviceDestroy(ServiceDestroyEvent event) {
destroy();
}
});

pushHandler = new PushHandler(service);
atmosphere.addAtmosphereHandler("/*", pushHandler);
atmosphere.addInitParameter(ApplicationConfig.PROPERTY_SESSION_SUPPORT,

Loading…
Cancel
Save