package com.vaadin.server;
import java.io.Serializable;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;
public abstract class ApplicationContext implements HttpSessionBindingListener,
Serializable {
- protected final HashSet<Application> applications = new HashSet<Application>();
+ private Application application;
protected WebBrowser browser = new WebBrowser();
- protected HashMap<Application, AbstractCommunicationManager> applicationToAjaxAppMgrMap = new HashMap<Application, AbstractCommunicationManager>();
+ private AbstractCommunicationManager communicationManager;
private long totalSessionTime = 0;
public void valueUnbound(HttpSessionBindingEvent event) {
// If we are going to be unbound from the session, the session must be
// closing
- try {
- while (!applications.isEmpty()) {
- final Application app = applications.iterator().next();
- app.close();
- removeApplication(app);
- }
- } catch (Exception e) {
- // This should never happen but is possible with rare
- // configurations (e.g. robustness tests). If you have one
- // thread doing HTTP socket write and another thread trying to
- // remove same application here. Possible if you got e.g. session
- // lifetime 1 min but socket write may take longer than 1 min.
- // FIXME: Handle exception
- getLogger().log(Level.SEVERE,
- "Could not remove application, leaking memory.", e);
- }
+ removeApplication();
}
/**
}
/**
- * Returns a collection of all the applications in this context.
+ * Returns the applications in this context.
*
- * Each application context contains all active applications for one user.
+ * Each application context contains the application for one user.
*
- * @return A collection containing all the applications in this context.
+ * @return The application of this context, or <code>null</code> if there is
+ * no application
*/
- public Collection<Application> getApplications() {
- return Collections.unmodifiableCollection(applications);
+ public Application getApplication() {
+ return application;
}
- protected void removeApplication(Application application) {
- applications.remove(application);
- applicationToAjaxAppMgrMap.remove(application);
+ public void removeApplication() {
+ if (application == null) {
+ return;
+ }
+ try {
+ application.close();
+ } catch (Exception e) {
+ // This should never happen but is possible with rare
+ // configurations (e.g. robustness tests). If you have one
+ // thread doing HTTP socket write and another thread trying to
+ // remove same application here. Possible if you got e.g. session
+ // lifetime 1 min but socket write may take longer than 1 min.
+ // FIXME: Handle exception
+ getLogger().log(Level.SEVERE,
+ "Could not close application, leaking memory.", e);
+ } finally {
+ application = null;
+ communicationManager = null;
+ }
}
/**
this.session = session;
}
+ public AbstractCommunicationManager getApplicationManager() {
+ return communicationManager;
+ }
+
+ public void setApplication(Application application,
+ AbstractCommunicationManager communicationManager) {
+ if (this.application != null) {
+ removeApplication();
+ }
+ this.application = application;
+ this.communicationManager = communicationManager;
+ }
+
}
\ No newline at end of file
protected Map<Application, Set<PortletListener>> portletListeners = new HashMap<Application, Set<PortletListener>>();
- protected HashMap<String, Application> portletWindowIdToApplicationMap = new HashMap<String, Application>();
-
private final Map<String, QName> eventActionDestinationMap = new HashMap<String, QName>();
private final Map<String, Serializable> eventActionValueMap = new HashMap<String, Serializable>();
private final Map<String, String> sharedParameterActionNameMap = new HashMap<String, String>();
private final Map<String, String> sharedParameterActionValueMap = new HashMap<String, String>();
- protected PortletCommunicationManager getApplicationManager(
- Application application) {
- PortletCommunicationManager mgr = (PortletCommunicationManager) applicationToAjaxAppMgrMap
- .get(application);
-
- if (mgr == null) {
- // Creates a new manager
- mgr = createPortletCommunicationManager(application);
- applicationToAjaxAppMgrMap.put(application, mgr);
- }
- return mgr;
- }
-
- protected PortletCommunicationManager createPortletCommunicationManager(
- Application application) {
- return new PortletCommunicationManager(application);
- }
-
public static PortletApplicationContext2 getApplicationContext(
PortletSession session) {
Object cxattr = session.getAttribute(PortletApplicationContext2.class
return cx;
}
- @Override
- protected void removeApplication(Application application) {
- super.removeApplication(application);
- // values() is backed by map, removes the key-value pair from the map
- portletWindowIdToApplicationMap.values().remove(application);
- }
-
- protected void addApplication(Application application,
- String portletWindowId) {
- applications.add(application);
- portletWindowIdToApplicationMap.put(portletWindowId, application);
- }
-
- public Application getApplicationForWindowId(String portletWindowId) {
- return portletWindowIdToApplicationMap.get(portletWindowId);
- }
-
public PortletSession getPortletSession() {
WrappedSession wrappedSession = getSession();
PortletSession session = ((WrappedPortletSession) wrappedSession)
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
-import com.vaadin.Application;
import com.vaadin.util.CurrentInstance;
/**
cx.setSession(new WrappedHttpSession(session));
return cx;
}
-
- protected void addApplication(Application application) {
- applications.add(application);
- }
-
- /**
- * Gets communication manager for an application.
- *
- * If this application has not been running before, a new manager is
- * created.
- *
- * @param application
- * @return CommunicationManager
- */
- public CommunicationManager getApplicationManager(Application application,
- VaadinServlet servlet) {
- CommunicationManager mgr = (CommunicationManager) applicationToAjaxAppMgrMap
- .get(application);
-
- if (mgr == null) {
- // Creates new manager
- mgr = servlet.createCommunicationManager(application);
- applicationToAjaxAppMgrMap.put(application, mgr);
- }
- return mgr;
- }
}
PortletApplicationContext2 applicationContext = getApplicationContext(request
.getPortletSession());
- PortletCommunicationManager applicationManager = applicationContext
- .getApplicationManager(application);
+ PortletCommunicationManager applicationManager = (PortletCommunicationManager) applicationContext
+ .getApplicationManager();
if (requestType == RequestType.CONNECTOR_RESOURCE) {
applicationManager.serveConnectorResource(wrappedRequest,
throws IOException {
final PortletSession session = request.getPortletSession();
if (session != null) {
- getApplicationContext(session).removeApplication(application);
+ getApplicationContext(session).removeApplication();
}
// Do not send any redirects when running inside a portlet.
}
application.close();
if (session != null) {
PortletApplicationContext2 context = getApplicationContext(session);
- context.removeApplication(application);
+ context.removeApplication();
}
}
private Application createApplication(PortletRequest request)
- throws PortletException, MalformedURLException {
+ throws PortletException {
Application newApplication = getNewApplication(request);
final PortletApplicationContext2 context = getApplicationContext(request
.getPortletSession());
- context.addApplication(newApplication, request.getWindowID());
+ context.setApplication(newApplication, new PortletCommunicationManager(
+ newApplication));
return newApplication;
}
}
PortletApplicationContext2 context = getApplicationContext(session);
- Application application = context.getApplicationForWindowId(request
- .getWindowID());
+ Application application = context.getApplication();
if (application == null) {
return null;
}
return application;
}
// application found but not running
- context.removeApplication(application);
+ context.removeApplication();
return null;
}
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
-import java.util.Iterator;
import java.util.Locale;
import java.util.Properties;
import java.util.logging.Level;
*/
ServletApplicationContext webApplicationContext = getApplicationContext(request
.getSession());
- CommunicationManager applicationManager = webApplicationContext
- .getApplicationManager(application, this);
+ CommunicationManager applicationManager = (CommunicationManager) webApplicationContext
+ .getApplicationManager();
if (requestType == RequestType.CONNECTOR_RESOURCE) {
applicationManager.serveConnectorResource(request, response);
final ServletApplicationContext context = getApplicationContext(request
.getSession());
- context.addApplication(newApplication);
+ context.setApplication(newApplication,
+ createCommunicationManager(newApplication));
return newApplication;
}
ServletApplicationContext context = getApplicationContext(session);
- // Gets application list for the session.
- final Collection<Application> applications = context.getApplications();
-
- // Search for the application (using the application URI) from the list
- for (final Iterator<Application> i = applications.iterator(); i
- .hasNext();) {
- final Application sessionApplication = i.next();
- final String sessionApplicationPath = sessionApplication.getURL()
- .getPath();
- String requestApplicationPath = getApplicationUrl(request)
- .getPath();
-
- if (requestApplicationPath.equals(sessionApplicationPath)) {
- // Found a running application
- if (sessionApplication.isRunning()) {
- return sessionApplication;
- }
- // Application has stopped, so remove it before creating a new
- // application
- getApplicationContext(session).removeApplication(
- sessionApplication);
- break;
- }
+ Application sessionApplication = context.getApplication();
+
+ if (sessionApplication == null) {
+ return null;
+ }
+
+ if (sessionApplication.isRunning()) {
+ // Found a running application
+ return sessionApplication;
}
+ // Application has stopped, so remove it before creating a new
+ // application
+ getApplicationContext(session).removeApplication();
// Existing application not found
return null;
final HttpSession session = request.getSession();
if (session != null) {
- getApplicationContext(session).removeApplication(application);
+ getApplicationContext(session).removeApplication();
}
response.sendRedirect(response.encodeRedirectURL(logoutUrl));
application.close();
if (session != null) {
ServletApplicationContext context = getApplicationContext(session);
- context.removeApplication(application);
+ context.removeApplication();
}
}
package com.vaadin.tests.application;
-import com.vaadin.Application;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.tests.components.TestBase;
import com.vaadin.ui.Button;
protected void setup() {
Label applications = new Label("Applications in session: <br/>",
ContentMode.XHTML);
- for (Application a : getContext().getApplications()) {
- applications.setValue(applications.getValue() + "App: " + a
- + "<br/>");
+ if (getContext().getApplication() != null) {
+ applications.setValue(applications.getValue() + "App: "
+ + getContext().getApplication() + "<br/>");
}
applications.setValue(applications.getValue() + "<br/><br/>");