Created
February 11, 2010 14:47
-
-
Save brindy/301569 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vaadin.osgi; | |
import java.util.HashSet; | |
import java.util.Set; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpSession; | |
import javax.servlet.http.HttpSessionEvent; | |
import javax.servlet.http.HttpSessionListener; | |
import org.osgi.service.component.ComponentFactory; | |
import org.osgi.service.component.ComponentInstance; | |
import com.vaadin.Application; | |
import com.vaadin.terminal.gwt.server.AbstractApplicationServlet; | |
import com.vaadin.terminal.gwt.server.WebApplicationContext; | |
/** | |
* Used to create instances of applications that have been registered with the | |
* container via a component factory. | |
* | |
* @author brindy | |
*/ | |
@SuppressWarnings("serial") | |
public class VaadinOSGiServlet extends AbstractApplicationServlet { | |
private final ComponentFactory factory; | |
private Set<VaadinSession> sessions = new HashSet<VaadinSession>(); | |
public VaadinOSGiServlet(ComponentFactory factory) { | |
this.factory = factory; | |
} | |
@Override | |
protected Class<? extends Application> getApplicationClass() | |
throws ClassNotFoundException { | |
// not used as the component factory creates instances for us | |
// but has to return something or getSystemMessages causes a NPE | |
return Application.class; | |
} | |
@Override | |
protected Application getNewApplication(HttpServletRequest request) | |
throws ServletException { | |
final VaadinSession info = new VaadinSession(factory.newInstance(null), | |
request.getSession()); | |
info.session.setAttribute(VaadinOSGiServlet.class.getName(), | |
new HttpSessionListener() { | |
@Override | |
public void sessionDestroyed(HttpSessionEvent arg0) { | |
info.dispose(); | |
} | |
@Override | |
public void sessionCreated(HttpSessionEvent arg0) { | |
} | |
}); | |
System.out.println("Ready: " + info); | |
return (Application) info.instance.getInstance(); | |
} | |
@Override | |
public void destroy() { | |
super.destroy(); | |
HashSet<VaadinSession> sessions = new HashSet<VaadinSession>(); | |
sessions.addAll(this.sessions); | |
this.sessions.clear(); | |
for (VaadinSession info : sessions) { | |
info.dispose(); | |
} | |
} | |
/** | |
* Track the component instance and session. If this is disposed the entire | |
* associated http session is also disposed. | |
*/ | |
class VaadinSession { | |
final ComponentInstance instance; | |
final HttpSession session; | |
public VaadinSession(ComponentInstance instance, HttpSession session) { | |
this.instance = instance; | |
this.session = session; | |
sessions.add(this); | |
} | |
public void dispose() { | |
System.out.println("Disposing: " + this); | |
Application app = (Application) instance.getInstance(); | |
if (app != null) { | |
app.close(); | |
} | |
instance.dispose(); | |
session.removeAttribute(VaadinOSGiServlet.class.getName()); | |
session.removeAttribute(WebApplicationContext.class.getName()); | |
sessions.remove(this); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment