From: Leif Åstrand Date: Fri, 31 Aug 2012 16:40:01 +0000 (+0300) Subject: Rename WebApplicationContext -> ServletApplicationContext (#9402) X-Git-Tag: 7.0.0.beta1~206^2~4 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7c3febaf37b8bdfad31c7d407e9150d04e598bb2;p=vaadin-framework.git Rename WebApplicationContext -> ServletApplicationContext (#9402) --- diff --git a/server/src/com/vaadin/Application.java b/server/src/com/vaadin/Application.java index 2ea7f01eea..90f354e180 100644 --- a/server/src/com/vaadin/Application.java +++ b/server/src/com/vaadin/Application.java @@ -64,7 +64,7 @@ import com.vaadin.server.RequestHandler; import com.vaadin.server.Terminal; import com.vaadin.server.UIProvider; import com.vaadin.server.VariableOwner; -import com.vaadin.server.WebApplicationContext; +import com.vaadin.server.ServletApplicationContext; import com.vaadin.server.WrappedRequest; import com.vaadin.server.WrappedRequest.BrowserDetails; import com.vaadin.server.WrappedResponse; @@ -867,7 +867,7 @@ public class Application implements Terminal.ErrorListener, Serializable { *

*

* By default, when you are deploying your application to a servlet - * container, the implementation class is {@link WebApplicationContext} - + * container, the implementation class is {@link ServletApplicationContext} - * you can safely cast to this class and use the methods from there. When * you are deploying your application as a portlet, context implementation * is {@link PortletApplicationContext}. diff --git a/server/src/com/vaadin/server/AbstractApplicationServlet.java b/server/src/com/vaadin/server/AbstractApplicationServlet.java index 2f0dad7079..87d36da255 100644 --- a/server/src/com/vaadin/server/AbstractApplicationServlet.java +++ b/server/src/com/vaadin/server/AbstractApplicationServlet.java @@ -275,7 +275,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements * Get or create a WebApplicationContext and an ApplicationManager * for the session */ - WebApplicationContext webApplicationContext = getApplicationContext(request + ServletApplicationContext webApplicationContext = getApplicationContext(request .getSession()); CommunicationManager applicationManager = webApplicationContext .getApplicationManager(application, this); @@ -363,7 +363,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements // Notifies transaction end try { if (transactionStarted) { - ((WebApplicationContext) application.getContext()) + ((ServletApplicationContext) application.getContext()) .endTransaction(application, request); } @@ -694,7 +694,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements throws ServletException, MalformedURLException { Application newApplication = getNewApplication(request); - final WebApplicationContext context = getApplicationContext(request + final ServletApplicationContext context = getApplicationContext(request .getSession()); context.addApplication(newApplication); @@ -856,7 +856,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements * @throws MalformedURLException */ private void startApplication(HttpServletRequest request, - Application application, WebApplicationContext webApplicationContext) + Application application, ServletApplicationContext webApplicationContext) throws ServletException, MalformedURLException { if (!application.isRunning()) { @@ -1385,7 +1385,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements throw new SessionExpiredException(); } - WebApplicationContext context = getApplicationContext(session); + ServletApplicationContext context = getApplicationContext(session); // Gets application list for the session. final Collection applications = context.getApplications(); @@ -1491,7 +1491,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements application.close(); if (session != null) { - WebApplicationContext context = getApplicationContext(session); + ServletApplicationContext context = getApplicationContext(session); context.removeApplication(application); } } @@ -1506,13 +1506,13 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements * the HTTP session. * @return the application context for HttpSession. */ - protected WebApplicationContext getApplicationContext(HttpSession session) { + protected ServletApplicationContext getApplicationContext(HttpSession session) { /* * TODO the ApplicationContext.getApplicationContext() should be removed * and logic moved here. Now overriding context type is possible, but * the whole creation logic should be here. MT 1101 */ - return WebApplicationContext.getApplicationContext(session); + return ServletApplicationContext.getApplicationContext(session); } public class RequestError implements Terminal.ErrorEvent, Serializable { @@ -1535,11 +1535,11 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements * mananger implementation. * * @deprecated Instead of overriding this method, override - * {@link WebApplicationContext} implementation via + * {@link ServletApplicationContext} implementation via * {@link AbstractApplicationServlet#getApplicationContext(HttpSession)} * method and in that customized implementation return your * CommunicationManager in - * {@link WebApplicationContext#getApplicationManager(Application, AbstractApplicationServlet)} + * {@link ServletApplicationContext#getApplicationManager(Application, AbstractApplicationServlet)} * method. * * @param application diff --git a/server/src/com/vaadin/server/CombinedRequest.java b/server/src/com/vaadin/server/CombinedRequest.java index c186fcc87c..0577c0098a 100644 --- a/server/src/com/vaadin/server/CombinedRequest.java +++ b/server/src/com/vaadin/server/CombinedRequest.java @@ -153,8 +153,8 @@ public class CombinedRequest implements WrappedRequest { @Override public WebBrowser getWebBrowser() { - WebApplicationContext context = (WebApplicationContext) Application - .getCurrent().getContext(); + ApplicationContext context = Application.getCurrent() + .getContext(); return context.getBrowser(); } }; diff --git a/server/src/com/vaadin/server/CommunicationManager.java b/server/src/com/vaadin/server/CommunicationManager.java index 3c594eaf02..af28438f57 100644 --- a/server/src/com/vaadin/server/CommunicationManager.java +++ b/server/src/com/vaadin/server/CommunicationManager.java @@ -112,7 +112,7 @@ public class CommunicationManager extends AbstractCommunicationManager { @Override protected InputStream getThemeResourceAsStream(UI uI, String themeName, String resource) { - WebApplicationContext context = (WebApplicationContext) uI + ServletApplicationContext context = (ServletApplicationContext) uI .getApplication().getContext(); ServletContext servletContext = context.getHttpSession() .getServletContext(); diff --git a/server/src/com/vaadin/server/GAEApplicationServlet.java b/server/src/com/vaadin/server/GAEApplicationServlet.java index 7e0b52c382..240984c760 100644 --- a/server/src/com/vaadin/server/GAEApplicationServlet.java +++ b/server/src/com/vaadin/server/GAEApplicationServlet.java @@ -322,7 +322,7 @@ public class GAEApplicationServlet extends ApplicationServlet { ois = new ObjectInputStream(bais); ApplicationContext applicationContext = (ApplicationContext) ois .readObject(); - session.setAttribute(WebApplicationContext.class.getName(), + session.setAttribute(ServletApplicationContext.class.getName(), applicationContext); } catch (IOException e) { getLogger().log( @@ -360,7 +360,7 @@ public class GAEApplicationServlet extends ApplicationServlet { private void cleanSession(HttpServletRequest request) { HttpSession session = request.getSession(false); if (session != null) { - session.removeAttribute(WebApplicationContext.class.getName()); + session.removeAttribute(ServletApplicationContext.class.getName()); } } diff --git a/server/src/com/vaadin/server/Page.java b/server/src/com/vaadin/server/Page.java index b8fdae6cfb..da172ed837 100644 --- a/server/src/com/vaadin/server/Page.java +++ b/server/src/com/vaadin/server/Page.java @@ -391,8 +391,7 @@ public class Page implements Serializable { } public WebBrowser getWebBrowser() { - return ((WebApplicationContext) uI.getApplication().getContext()) - .getBrowser(); + return uI.getApplication().getContext().getBrowser(); } public void setBrowserWindowSize(int width, int height) { diff --git a/server/src/com/vaadin/server/ServletApplicationContext.java b/server/src/com/vaadin/server/ServletApplicationContext.java new file mode 100644 index 0000000000..639ff117d1 --- /dev/null +++ b/server/src/com/vaadin/server/ServletApplicationContext.java @@ -0,0 +1,194 @@ +/* + * Copyright 2011 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.File; +import java.util.Enumeration; +import java.util.HashMap; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; +import javax.servlet.http.HttpSessionBindingEvent; +import javax.servlet.http.HttpSessionBindingListener; + +import com.vaadin.Application; + +/** + * Web application context for Vaadin applications. + * + * This is automatically added as a {@link HttpSessionBindingListener} when + * added to a {@link HttpSession}. + * + * @author Vaadin Ltd. + * @since 3.1 + */ +@SuppressWarnings("serial") +public class ServletApplicationContext extends ApplicationContext { + + protected transient HttpSession session; + private transient boolean reinitializingSession = false; + + /** + * Stores a reference to the currentRequest. Null it not inside a request. + */ + private transient Object currentRequest = null; + + /** + * Creates a new Web Application Context. + * + */ + protected ServletApplicationContext() { + + } + + @Override + protected void startTransaction(Application application, Object request) { + currentRequest = request; + super.startTransaction(application, request); + } + + @Override + protected void endTransaction(Application application, Object request) { + super.endTransaction(application, request); + currentRequest = null; + } + + @Override + public void valueUnbound(HttpSessionBindingEvent event) { + if (!reinitializingSession) { + // Avoid closing the application if we are only reinitializing the + // session. Closing the application would cause the state to be lost + // and a new application to be created, which is not what we want. + super.valueUnbound(event); + } + } + + /** + * Discards the current session and creates a new session with the same + * contents. The purpose of this is to introduce a new session key in order + * to avoid session fixation attacks. + */ + @SuppressWarnings("unchecked") + public void reinitializeSession() { + + HttpSession oldSession = getHttpSession(); + + // Stores all attributes (security key, reference to this context + // instance) so they can be added to the new session + HashMap attrs = new HashMap(); + for (Enumeration e = oldSession.getAttributeNames(); e + .hasMoreElements();) { + String name = e.nextElement(); + attrs.put(name, oldSession.getAttribute(name)); + } + + // Invalidate the current session, set flag to avoid call to + // valueUnbound + reinitializingSession = true; + oldSession.invalidate(); + reinitializingSession = false; + + // Create a new session + HttpSession newSession = ((HttpServletRequest) currentRequest) + .getSession(); + + // Restores all attributes (security key, reference to this context + // instance) + for (String name : attrs.keySet()) { + newSession.setAttribute(name, attrs.get(name)); + } + + // Update the "current session" variable + session = newSession; + } + + /** + * Gets the application context base directory. + * + * @see com.vaadin.server.ApplicationContext#getBaseDirectory() + */ + @Override + public File getBaseDirectory() { + final String realPath = ApplicationServlet.getResourcePath( + session.getServletContext(), "/"); + if (realPath == null) { + return null; + } + return new File(realPath); + } + + /** + * Gets the http-session application is running in. + * + * @return HttpSession this application context resides in. + */ + public HttpSession getHttpSession() { + return session; + } + + /** + * Gets the application context for an HttpSession. + * + * @param session + * the HTTP session. + * @return the application context for HttpSession. + */ + static public ServletApplicationContext getApplicationContext( + HttpSession session) { + ServletApplicationContext cx = (ServletApplicationContext) session + .getAttribute(ServletApplicationContext.class.getName()); + if (cx == null) { + cx = new ServletApplicationContext(); + session.setAttribute(ServletApplicationContext.class.getName(), cx); + } + if (cx.session == null) { + cx.session = 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, + AbstractApplicationServlet servlet) { + CommunicationManager mgr = (CommunicationManager) applicationToAjaxAppMgrMap + .get(application); + + if (mgr == null) { + // Creates new manager + mgr = servlet.createCommunicationManager(application); + applicationToAjaxAppMgrMap.put(application, mgr); + } + return mgr; + } + + @Override + public int getMaxInactiveInterval() { + return getHttpSession().getMaxInactiveInterval(); + } +} diff --git a/server/src/com/vaadin/server/WebApplicationContext.java b/server/src/com/vaadin/server/WebApplicationContext.java deleted file mode 100644 index 02e902f79e..0000000000 --- a/server/src/com/vaadin/server/WebApplicationContext.java +++ /dev/null @@ -1,194 +0,0 @@ -/* - * Copyright 2011 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.File; -import java.util.Enumeration; -import java.util.HashMap; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; -import javax.servlet.http.HttpSessionBindingEvent; -import javax.servlet.http.HttpSessionBindingListener; - -import com.vaadin.Application; - -/** - * Web application context for Vaadin applications. - * - * This is automatically added as a {@link HttpSessionBindingListener} when - * added to a {@link HttpSession}. - * - * @author Vaadin Ltd. - * @since 3.1 - */ -@SuppressWarnings("serial") -public class WebApplicationContext extends ApplicationContext { - - protected transient HttpSession session; - private transient boolean reinitializingSession = false; - - /** - * Stores a reference to the currentRequest. Null it not inside a request. - */ - private transient Object currentRequest = null; - - /** - * Creates a new Web Application Context. - * - */ - protected WebApplicationContext() { - - } - - @Override - protected void startTransaction(Application application, Object request) { - currentRequest = request; - super.startTransaction(application, request); - } - - @Override - protected void endTransaction(Application application, Object request) { - super.endTransaction(application, request); - currentRequest = null; - } - - @Override - public void valueUnbound(HttpSessionBindingEvent event) { - if (!reinitializingSession) { - // Avoid closing the application if we are only reinitializing the - // session. Closing the application would cause the state to be lost - // and a new application to be created, which is not what we want. - super.valueUnbound(event); - } - } - - /** - * Discards the current session and creates a new session with the same - * contents. The purpose of this is to introduce a new session key in order - * to avoid session fixation attacks. - */ - @SuppressWarnings("unchecked") - public void reinitializeSession() { - - HttpSession oldSession = getHttpSession(); - - // Stores all attributes (security key, reference to this context - // instance) so they can be added to the new session - HashMap attrs = new HashMap(); - for (Enumeration e = oldSession.getAttributeNames(); e - .hasMoreElements();) { - String name = e.nextElement(); - attrs.put(name, oldSession.getAttribute(name)); - } - - // Invalidate the current session, set flag to avoid call to - // valueUnbound - reinitializingSession = true; - oldSession.invalidate(); - reinitializingSession = false; - - // Create a new session - HttpSession newSession = ((HttpServletRequest) currentRequest) - .getSession(); - - // Restores all attributes (security key, reference to this context - // instance) - for (String name : attrs.keySet()) { - newSession.setAttribute(name, attrs.get(name)); - } - - // Update the "current session" variable - session = newSession; - } - - /** - * Gets the application context base directory. - * - * @see com.vaadin.server.ApplicationContext#getBaseDirectory() - */ - @Override - public File getBaseDirectory() { - final String realPath = ApplicationServlet.getResourcePath( - session.getServletContext(), "/"); - if (realPath == null) { - return null; - } - return new File(realPath); - } - - /** - * Gets the http-session application is running in. - * - * @return HttpSession this application context resides in. - */ - public HttpSession getHttpSession() { - return session; - } - - /** - * Gets the application context for an HttpSession. - * - * @param session - * the HTTP session. - * @return the application context for HttpSession. - */ - static public WebApplicationContext getApplicationContext( - HttpSession session) { - WebApplicationContext cx = (WebApplicationContext) session - .getAttribute(WebApplicationContext.class.getName()); - if (cx == null) { - cx = new WebApplicationContext(); - session.setAttribute(WebApplicationContext.class.getName(), cx); - } - if (cx.session == null) { - cx.session = 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, - AbstractApplicationServlet servlet) { - CommunicationManager mgr = (CommunicationManager) applicationToAjaxAppMgrMap - .get(application); - - if (mgr == null) { - // Creates new manager - mgr = servlet.createCommunicationManager(application); - applicationToAjaxAppMgrMap.put(application, mgr); - } - return mgr; - } - - @Override - public int getMaxInactiveInterval() { - return getHttpSession().getMaxInactiveInterval(); - } -} diff --git a/server/src/com/vaadin/server/WrappedHttpServletRequest.java b/server/src/com/vaadin/server/WrappedHttpServletRequest.java index cb8a9e8c5f..b069235843 100644 --- a/server/src/com/vaadin/server/WrappedHttpServletRequest.java +++ b/server/src/com/vaadin/server/WrappedHttpServletRequest.java @@ -99,8 +99,8 @@ public class WrappedHttpServletRequest extends HttpServletRequestWrapper @Override public WebBrowser getWebBrowser() { - WebApplicationContext context = (WebApplicationContext) Application - .getCurrent().getContext(); + ApplicationContext context = Application.getCurrent() + .getContext(); return context.getBrowser(); } }; diff --git a/server/tests/src/com/vaadin/tests/server/TransactionListenersConcurrency.java b/server/tests/src/com/vaadin/tests/server/TransactionListenersConcurrency.java index 302b534d8a..05ffcd1e36 100644 --- a/server/tests/src/com/vaadin/tests/server/TransactionListenersConcurrency.java +++ b/server/tests/src/com/vaadin/tests/server/TransactionListenersConcurrency.java @@ -23,7 +23,7 @@ import com.vaadin.Application; import com.vaadin.Application.ApplicationStartEvent; import com.vaadin.server.ApplicationContext; import com.vaadin.server.DeploymentConfiguration; -import com.vaadin.server.WebApplicationContext; +import com.vaadin.server.ServletApplicationContext; public class TransactionListenersConcurrency extends TestCase { @@ -39,7 +39,7 @@ public class TransactionListenersConcurrency extends TestCase { final List exceptions = new ArrayList(); HttpSession session = createSession(); - final WebApplicationContext context = WebApplicationContext + final ServletApplicationContext context = ServletApplicationContext .getApplicationContext(session); List threads = new ArrayList(); @@ -151,10 +151,10 @@ public class TransactionListenersConcurrency extends TestCase { private static HttpSession createSession() { HttpSession session = createMock(HttpSession.class); EasyMock.expect( - session.getAttribute(WebApplicationContext.class.getName())) + session.getAttribute(ServletApplicationContext.class.getName())) .andReturn(null).anyTimes(); session.setAttribute( - EasyMock.eq(WebApplicationContext.class.getName()), + EasyMock.eq(ServletApplicationContext.class.getName()), EasyMock.anyObject()); EasyMock.replay(session); diff --git a/uitest/src/com/vaadin/tests/VerifyBrowserVersion.java b/uitest/src/com/vaadin/tests/VerifyBrowserVersion.java index 8a07168ef7..f4cf236a24 100644 --- a/uitest/src/com/vaadin/tests/VerifyBrowserVersion.java +++ b/uitest/src/com/vaadin/tests/VerifyBrowserVersion.java @@ -1,6 +1,6 @@ package com.vaadin.tests; -import com.vaadin.server.WebApplicationContext; +import com.vaadin.server.ApplicationContext; import com.vaadin.server.WebBrowser; import com.vaadin.tests.components.TestBase; import com.vaadin.ui.Label; @@ -9,7 +9,7 @@ public class VerifyBrowserVersion extends TestBase { @Override protected void setup() { - WebApplicationContext context = (WebApplicationContext) getContext(); + ApplicationContext context = getContext(); WebBrowser browser = context.getBrowser(); addComponent(new Label(browser.getBrowserApplication())); addComponent(new Label("Touch device? " diff --git a/uitest/src/com/vaadin/tests/application/ApplicationCloseTest.java b/uitest/src/com/vaadin/tests/application/ApplicationCloseTest.java index 233da9722e..1f5f0dc691 100644 --- a/uitest/src/com/vaadin/tests/application/ApplicationCloseTest.java +++ b/uitest/src/com/vaadin/tests/application/ApplicationCloseTest.java @@ -1,7 +1,6 @@ package com.vaadin.tests.application; import com.vaadin.Application; -import com.vaadin.server.WebApplicationContext; import com.vaadin.shared.ui.label.ContentMode; import com.vaadin.tests.components.TestBase; import com.vaadin.ui.Button; @@ -16,8 +15,7 @@ public class ApplicationCloseTest extends TestBase { protected void setup() { Label applications = new Label("Applications in session:
", ContentMode.XHTML); - for (Application a : ((WebApplicationContext) getContext()) - .getApplications()) { + for (Application a : getContext().getApplications()) { applications.setValue(applications.getValue() + "App: " + a + "
"); } diff --git a/uitest/src/com/vaadin/tests/applicationcontext/ChangeSessionId.java b/uitest/src/com/vaadin/tests/applicationcontext/ChangeSessionId.java index 9146cf5dea..96ebe1345f 100644 --- a/uitest/src/com/vaadin/tests/applicationcontext/ChangeSessionId.java +++ b/uitest/src/com/vaadin/tests/applicationcontext/ChangeSessionId.java @@ -1,6 +1,6 @@ package com.vaadin.tests.applicationcontext; -import com.vaadin.server.WebApplicationContext; +import com.vaadin.server.ServletApplicationContext; import com.vaadin.tests.components.AbstractTestCase; import com.vaadin.tests.util.Log; import com.vaadin.ui.Button; @@ -32,7 +32,7 @@ public class ChangeSessionId extends AbstractTestCase { loginButton.addListener(new ClickListener() { @Override public void buttonClick(ClickEvent event) { - WebApplicationContext context = ((WebApplicationContext) getContext()); + ServletApplicationContext context = ((ServletApplicationContext) getContext()); String oldSessionId = context.getHttpSession().getId(); context.reinitializeSession(); @@ -55,7 +55,7 @@ public class ChangeSessionId extends AbstractTestCase { } protected String getSessionId() { - return ((WebApplicationContext) getContext()).getHttpSession().getId(); + return ((ServletApplicationContext) getContext()).getHttpSession().getId(); } @Override diff --git a/uitest/src/com/vaadin/tests/tickets/Ticket1975.java b/uitest/src/com/vaadin/tests/tickets/Ticket1975.java index 9c6dd8c272..e85fe294f2 100644 --- a/uitest/src/com/vaadin/tests/tickets/Ticket1975.java +++ b/uitest/src/com/vaadin/tests/tickets/Ticket1975.java @@ -5,7 +5,7 @@ import java.io.File; import java.io.FileInputStream; import com.vaadin.Application; -import com.vaadin.server.WebApplicationContext; +import com.vaadin.server.ServletApplicationContext; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.Button.ClickListener; @@ -33,7 +33,7 @@ public class Ticket1975 extends Application.LegacyApplication { try { cl1 = new CustomLayout(new ByteArrayInputStream(s.getBytes())); layout.addComponent(cl1); - WebApplicationContext wc = ((WebApplicationContext) getContext()); + ServletApplicationContext wc = ((ServletApplicationContext) getContext()); layout.addComponent(new Button("Disable/Enable", new ClickListener() {