From 4019f7d03a1d8437a24ccadc562c30f99da5efe0 Mon Sep 17 00:00:00 2001 From: Leif Åstrand Date: Wed, 5 Sep 2012 19:32:27 +0300 Subject: Rename LegacyApplication -> Application (#9402) --- server/src/com/vaadin/Application.java | 302 +++++++++++++++++++++ server/src/com/vaadin/LegacyApplication.java | 302 --------------------- .../src/com/vaadin/server/LegacyVaadinPortlet.java | 10 +- .../src/com/vaadin/server/LegacyVaadinServlet.java | 10 +- .../com/vaadin/server/ServletPortletHelper.java | 6 +- server/src/com/vaadin/ui/UI.java | 2 +- .../component/window/AddRemoveSubWindow.java | 4 +- 7 files changed, 318 insertions(+), 318 deletions(-) create mode 100644 server/src/com/vaadin/Application.java delete mode 100644 server/src/com/vaadin/LegacyApplication.java (limited to 'server') diff --git a/server/src/com/vaadin/Application.java b/server/src/com/vaadin/Application.java new file mode 100644 index 0000000000..13ce23d1e4 --- /dev/null +++ b/server/src/com/vaadin/Application.java @@ -0,0 +1,302 @@ +/* + * 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; + +import java.net.URL; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import com.vaadin.server.AbstractUIProvider; +import com.vaadin.server.VaadinSession; +import com.vaadin.server.Terminal.ErrorEvent; +import com.vaadin.server.Terminal.ErrorListener; +import com.vaadin.server.WrappedRequest; +import com.vaadin.ui.UI; + +/** + * A special application designed to help migrating applications from Vaadin 6 + * to Vaadin 7. The legacy application supports setting a main window, adding + * additional browser level windows and defining the theme for the entire + * application. + * + * @deprecated This class is only intended to ease migration and should not be + * used for new projects. + * + * @since 7.0 + */ +@Deprecated +public abstract class Application extends AbstractUIProvider implements + ErrorListener { + /** + * Ignore initial / and then get everything up to the next / + */ + private static final Pattern WINDOW_NAME_PATTERN = Pattern + .compile("^/?([^/]+).*"); + + private UI.LegacyWindow mainWindow; + private String theme; + + private Map legacyUINames = new HashMap(); + + /** + * Sets the main window of this application. Setting window as a main window + * of this application also adds the window to this application. + * + * @param mainWindow + * the UI to set as the default window + */ + public void setMainWindow(UI.LegacyWindow mainWindow) { + if (this.mainWindow != null) { + throw new IllegalStateException("mainWindow has already been set"); + } + if (mainWindow.getSession() == null) { + mainWindow.setSession(VaadinSession.getCurrent()); + } else if (mainWindow.getSession() != VaadinSession.getCurrent()) { + throw new IllegalStateException( + "mainWindow is attached to another application"); + } + if (UI.getCurrent() == null) { + // Assume setting a main window from Application.init if there's + // no current UI -> set the main window as the current UI + UI.setCurrent(mainWindow); + } + this.mainWindow = mainWindow; + } + + public void doInit() { + VaadinSession.getCurrent().setErrorHandler(this); + init(); + } + + protected abstract void init(); + + @Override + public Class getUIClass(VaadinSession application, + WrappedRequest request) { + UI uiInstance = getUIInstance(request); + if (uiInstance != null) { + return uiInstance.getClass(); + } + return null; + } + + @Override + public UI createInstance(VaadinSession application, Class type, + WrappedRequest request) { + return getUIInstance(request); + } + + @Override + public String getThemeForUI(WrappedRequest request, + Class uiClass) { + return theme; + } + + @Override + public String getPageTitleForUI(WrappedRequest request, + Class uiClass) { + UI uiInstance = getUIInstance(request); + if (uiInstance != null) { + return uiInstance.getCaption(); + } else { + return super.getPageTitleForUI(request, uiClass); + } + } + + /** + * Gets the mainWindow of the application. + * + *

+ * The main window is the window attached to the application URL ( + * {@link #getURL()}) and thus which is show by default to the user. + *

+ *

+ * Note that each application must have at least one main window. + *

+ * + * @return the UI used as the default window + */ + public UI.LegacyWindow getMainWindow() { + return mainWindow; + } + + private UI getUIInstance(WrappedRequest request) { + String pathInfo = request.getRequestPathInfo(); + String name = null; + if (pathInfo != null && pathInfo.length() > 0) { + Matcher matcher = WINDOW_NAME_PATTERN.matcher(pathInfo); + if (matcher.matches()) { + // Skip the initial slash + name = matcher.group(1); + } + } + UI.LegacyWindow window = getWindow(name); + if (window != null) { + return window; + } + return mainWindow; + } + + /** + * This implementation simulates the way of finding a window for a request + * by extracting a window name from the requested path and passes that name + * to {@link #getWindow(String)}. + *

+ * {@inheritDoc} + */ + @Override + public UI getExistingUI(WrappedRequest request) { + UI uiInstance = getUIInstance(request); + if (uiInstance.getUIId() == -1) { + // Not initialized -> Let go through createUIInstance to make it + // initialized + return null; + } else { + UI.setCurrent(uiInstance); + return uiInstance; + } + } + + /** + * Sets the application's theme. + *

+ * Note that this theme can be overridden for a specific UI with + * {@link VaadinSession#getThemeForUI(UI)}. Setting theme to be + * null selects the default theme. For the available theme + * names, see the contents of the VAADIN/themes directory. + *

+ * + * @param theme + * the new theme for this application. + */ + public void setTheme(String theme) { + this.theme = theme; + } + + /** + * Gets the application's theme. The application's theme is the default + * theme used by all the uIs for which a theme is not explicitly defined. If + * the application theme is not explicitly set, null is + * returned. + * + * @return the name of the application's theme. + */ + public String getTheme() { + return theme; + } + + /** + *

+ * Gets a UI by name. Returns null if the application is not + * running or it does not contain a window corresponding to the name. + *

+ * + * @param name + * the name of the requested window + * @return a UI corresponding to the name, or null to use the + * default window + */ + public UI.LegacyWindow getWindow(String name) { + return legacyUINames.get(name); + } + + /** + * Counter to get unique names for windows with no explicit name + */ + private int namelessUIIndex = 0; + + /** + * Adds a new browser level window to this application. Please note that UI + * doesn't have a name that is used in the URL - to add a named window you + * should instead use {@link #addWindow(UI, String)} + * + * @param uI + * the UI window to add to the application + * @return returns the name that has been assigned to the window + * + * @see #addWindow(UI, String) + */ + public void addWindow(UI.LegacyWindow uI) { + if (uI.getName() == null) { + String name = Integer.toString(namelessUIIndex++); + uI.setName(name); + } + + legacyUINames.put(uI.getName(), uI); + uI.setSession(VaadinSession.getCurrent()); + } + + /** + * Removes the specified window from the application. This also removes all + * name mappings for the window (see {@link #addWindow(UI, String) and + * #getWindowName(UI)}. + * + *

+ * Note that removing window from the application does not close the browser + * window - the window is only removed from the server-side. + *

+ * + * @param uI + * the UI to remove + */ + public void removeWindow(UI.LegacyWindow uI) { + for (Entry entry : legacyUINames.entrySet()) { + if (entry.getValue() == uI) { + legacyUINames.remove(entry.getKey()); + } + } + } + + /** + * Gets the set of windows contained by the application. + * + *

+ * Note that the returned set of windows can not be modified. + *

+ * + * @return the unmodifiable collection of windows. + */ + public Collection getWindows() { + return Collections.unmodifiableCollection(legacyUINames.values()); + } + + @Override + public void terminalError(ErrorEvent event) { + VaadinSession.getCurrent().terminalError(event); + } + + public VaadinSession getContext() { + return VaadinSession.getCurrent(); + } + + protected void close() { + VaadinSession.getCurrent().close(); + } + + public boolean isRunning() { + return VaadinSession.getCurrent().isRunning(); + } + + public URL getURL() { + return VaadinSession.getCurrent().getURL(); + } +} \ No newline at end of file diff --git a/server/src/com/vaadin/LegacyApplication.java b/server/src/com/vaadin/LegacyApplication.java deleted file mode 100644 index 66b2ca3973..0000000000 --- a/server/src/com/vaadin/LegacyApplication.java +++ /dev/null @@ -1,302 +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; - -import java.net.URL; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import com.vaadin.server.AbstractUIProvider; -import com.vaadin.server.VaadinSession; -import com.vaadin.server.Terminal.ErrorEvent; -import com.vaadin.server.Terminal.ErrorListener; -import com.vaadin.server.WrappedRequest; -import com.vaadin.ui.UI; - -/** - * A special application designed to help migrating applications from Vaadin 6 - * to Vaadin 7. The legacy application supports setting a main window, adding - * additional browser level windows and defining the theme for the entire - * application. - * - * @deprecated This class is only intended to ease migration and should not be - * used for new projects. - * - * @since 7.0 - */ -@Deprecated -public abstract class LegacyApplication extends AbstractUIProvider implements - ErrorListener { - /** - * Ignore initial / and then get everything up to the next / - */ - private static final Pattern WINDOW_NAME_PATTERN = Pattern - .compile("^/?([^/]+).*"); - - private UI.LegacyWindow mainWindow; - private String theme; - - private Map legacyUINames = new HashMap(); - - /** - * Sets the main window of this application. Setting window as a main window - * of this application also adds the window to this application. - * - * @param mainWindow - * the UI to set as the default window - */ - public void setMainWindow(UI.LegacyWindow mainWindow) { - if (this.mainWindow != null) { - throw new IllegalStateException("mainWindow has already been set"); - } - if (mainWindow.getSession() == null) { - mainWindow.setSession(VaadinSession.getCurrent()); - } else if (mainWindow.getSession() != VaadinSession.getCurrent()) { - throw new IllegalStateException( - "mainWindow is attached to another application"); - } - if (UI.getCurrent() == null) { - // Assume setting a main window from Application.init if there's - // no current UI -> set the main window as the current UI - UI.setCurrent(mainWindow); - } - this.mainWindow = mainWindow; - } - - public void doInit() { - VaadinSession.getCurrent().setErrorHandler(this); - init(); - } - - protected abstract void init(); - - @Override - public Class getUIClass(VaadinSession application, - WrappedRequest request) { - UI uiInstance = getUIInstance(request); - if (uiInstance != null) { - return uiInstance.getClass(); - } - return null; - } - - @Override - public UI createInstance(VaadinSession application, Class type, - WrappedRequest request) { - return getUIInstance(request); - } - - @Override - public String getThemeForUI(WrappedRequest request, - Class uiClass) { - return theme; - } - - @Override - public String getPageTitleForUI(WrappedRequest request, - Class uiClass) { - UI uiInstance = getUIInstance(request); - if (uiInstance != null) { - return uiInstance.getCaption(); - } else { - return super.getPageTitleForUI(request, uiClass); - } - } - - /** - * Gets the mainWindow of the application. - * - *

- * The main window is the window attached to the application URL ( - * {@link #getURL()}) and thus which is show by default to the user. - *

- *

- * Note that each application must have at least one main window. - *

- * - * @return the UI used as the default window - */ - public UI.LegacyWindow getMainWindow() { - return mainWindow; - } - - private UI getUIInstance(WrappedRequest request) { - String pathInfo = request.getRequestPathInfo(); - String name = null; - if (pathInfo != null && pathInfo.length() > 0) { - Matcher matcher = WINDOW_NAME_PATTERN.matcher(pathInfo); - if (matcher.matches()) { - // Skip the initial slash - name = matcher.group(1); - } - } - UI.LegacyWindow window = getWindow(name); - if (window != null) { - return window; - } - return mainWindow; - } - - /** - * This implementation simulates the way of finding a window for a request - * by extracting a window name from the requested path and passes that name - * to {@link #getWindow(String)}. - *

- * {@inheritDoc} - */ - @Override - public UI getExistingUI(WrappedRequest request) { - UI uiInstance = getUIInstance(request); - if (uiInstance.getUIId() == -1) { - // Not initialized -> Let go through createUIInstance to make it - // initialized - return null; - } else { - UI.setCurrent(uiInstance); - return uiInstance; - } - } - - /** - * Sets the application's theme. - *

- * Note that this theme can be overridden for a specific UI with - * {@link VaadinSession#getThemeForUI(UI)}. Setting theme to be - * null selects the default theme. For the available theme - * names, see the contents of the VAADIN/themes directory. - *

- * - * @param theme - * the new theme for this application. - */ - public void setTheme(String theme) { - this.theme = theme; - } - - /** - * Gets the application's theme. The application's theme is the default - * theme used by all the uIs for which a theme is not explicitly defined. If - * the application theme is not explicitly set, null is - * returned. - * - * @return the name of the application's theme. - */ - public String getTheme() { - return theme; - } - - /** - *

- * Gets a UI by name. Returns null if the application is not - * running or it does not contain a window corresponding to the name. - *

- * - * @param name - * the name of the requested window - * @return a UI corresponding to the name, or null to use the - * default window - */ - public UI.LegacyWindow getWindow(String name) { - return legacyUINames.get(name); - } - - /** - * Counter to get unique names for windows with no explicit name - */ - private int namelessUIIndex = 0; - - /** - * Adds a new browser level window to this application. Please note that UI - * doesn't have a name that is used in the URL - to add a named window you - * should instead use {@link #addWindow(UI, String)} - * - * @param uI - * the UI window to add to the application - * @return returns the name that has been assigned to the window - * - * @see #addWindow(UI, String) - */ - public void addWindow(UI.LegacyWindow uI) { - if (uI.getName() == null) { - String name = Integer.toString(namelessUIIndex++); - uI.setName(name); - } - - legacyUINames.put(uI.getName(), uI); - uI.setSession(VaadinSession.getCurrent()); - } - - /** - * Removes the specified window from the application. This also removes all - * name mappings for the window (see {@link #addWindow(UI, String) and - * #getWindowName(UI)}. - * - *

- * Note that removing window from the application does not close the browser - * window - the window is only removed from the server-side. - *

- * - * @param uI - * the UI to remove - */ - public void removeWindow(UI.LegacyWindow uI) { - for (Entry entry : legacyUINames.entrySet()) { - if (entry.getValue() == uI) { - legacyUINames.remove(entry.getKey()); - } - } - } - - /** - * Gets the set of windows contained by the application. - * - *

- * Note that the returned set of windows can not be modified. - *

- * - * @return the unmodifiable collection of windows. - */ - public Collection getWindows() { - return Collections.unmodifiableCollection(legacyUINames.values()); - } - - @Override - public void terminalError(ErrorEvent event) { - VaadinSession.getCurrent().terminalError(event); - } - - public VaadinSession getContext() { - return VaadinSession.getCurrent(); - } - - protected void close() { - VaadinSession.getCurrent().close(); - } - - public boolean isRunning() { - return VaadinSession.getCurrent().isRunning(); - } - - public URL getURL() { - return VaadinSession.getCurrent().getURL(); - } -} \ No newline at end of file diff --git a/server/src/com/vaadin/server/LegacyVaadinPortlet.java b/server/src/com/vaadin/server/LegacyVaadinPortlet.java index 6efd9b29b3..f436cbb624 100644 --- a/server/src/com/vaadin/server/LegacyVaadinPortlet.java +++ b/server/src/com/vaadin/server/LegacyVaadinPortlet.java @@ -19,12 +19,12 @@ package com.vaadin.server; import javax.portlet.PortletException; import javax.portlet.PortletRequest; -import com.vaadin.LegacyApplication; +import com.vaadin.Application; import com.vaadin.server.ServletPortletHelper.ApplicationClassException; public class LegacyVaadinPortlet extends VaadinPortlet { - protected Class getApplicationClass() + protected Class getApplicationClass() throws ClassNotFoundException { try { return ServletPortletHelper @@ -34,10 +34,10 @@ public class LegacyVaadinPortlet extends VaadinPortlet { } } - protected LegacyApplication getNewApplication(PortletRequest request) + protected Application getNewApplication(PortletRequest request) throws PortletException { try { - Class applicationClass = getApplicationClass(); + Class applicationClass = getApplicationClass(); return applicationClass.newInstance(); } catch (Exception e) { throw new PortletException(e); @@ -53,7 +53,7 @@ public class LegacyVaadinPortlet extends VaadinPortlet { // Must set current before running init() VaadinSession.setCurrent(application); - LegacyApplication legacyApplication = getNewApplication(request); + Application legacyApplication = getNewApplication(request); legacyApplication.doInit(); application.addUIProvider(legacyApplication); diff --git a/server/src/com/vaadin/server/LegacyVaadinServlet.java b/server/src/com/vaadin/server/LegacyVaadinServlet.java index d853e55099..93a9410509 100644 --- a/server/src/com/vaadin/server/LegacyVaadinServlet.java +++ b/server/src/com/vaadin/server/LegacyVaadinServlet.java @@ -19,12 +19,12 @@ package com.vaadin.server; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; -import com.vaadin.LegacyApplication; +import com.vaadin.Application; import com.vaadin.server.ServletPortletHelper.ApplicationClassException; public class LegacyVaadinServlet extends VaadinServlet { - protected Class getApplicationClass() + protected Class getApplicationClass() throws ClassNotFoundException { try { return ServletPortletHelper @@ -34,10 +34,10 @@ public class LegacyVaadinServlet extends VaadinServlet { } } - protected LegacyApplication getNewApplication(HttpServletRequest request) + protected Application getNewApplication(HttpServletRequest request) throws ServletException { try { - Class applicationClass = getApplicationClass(); + Class applicationClass = getApplicationClass(); return applicationClass.newInstance(); } catch (Exception e) { throw new ServletException(e); @@ -53,7 +53,7 @@ public class LegacyVaadinServlet extends VaadinServlet { // Must set current before running init() VaadinSession.setCurrent(application); - LegacyApplication legacyApplication = getNewApplication(request); + Application legacyApplication = getNewApplication(request); legacyApplication.doInit(); application.addUIProvider(legacyApplication); diff --git a/server/src/com/vaadin/server/ServletPortletHelper.java b/server/src/com/vaadin/server/ServletPortletHelper.java index 068a9f9192..609168ee96 100644 --- a/server/src/com/vaadin/server/ServletPortletHelper.java +++ b/server/src/com/vaadin/server/ServletPortletHelper.java @@ -3,7 +3,7 @@ package com.vaadin.server; import java.io.Serializable; import java.util.Properties; -import com.vaadin.LegacyApplication; +import com.vaadin.Application; import com.vaadin.shared.ApplicationConstants; import com.vaadin.ui.UI; @@ -41,7 +41,7 @@ class ServletPortletHelper implements Serializable { } } - static Class getLegacyApplicationClass( + static Class getLegacyApplicationClass( DeploymentConfiguration deploymentConfiguration) throws ApplicationClassException { Properties initParameters = deploymentConfiguration @@ -56,7 +56,7 @@ class ServletPortletHelper implements Serializable { try { return classLoader.loadClass(applicationParameter).asSubclass( - LegacyApplication.class); + Application.class); } catch (final ClassNotFoundException e) { throw new ApplicationClassException( "Failed to load application class: " + applicationParameter, diff --git a/server/src/com/vaadin/ui/UI.java b/server/src/com/vaadin/ui/UI.java index 0b376f54c1..1c1fcf5492 100644 --- a/server/src/com/vaadin/ui/UI.java +++ b/server/src/com/vaadin/ui/UI.java @@ -89,7 +89,7 @@ public abstract class UI extends AbstractComponentContainer implements /** * Helper class to emulate the main window from Vaadin 6 using UIs. This * class should be used in the same way as Window used as a browser level - * window in Vaadin 6 with {@link com.vaadin.LegacyApplication} + * window in Vaadin 6 with {@link com.vaadin.Application} */ @Deprecated public static class LegacyWindow extends UI { diff --git a/server/tests/src/com/vaadin/tests/server/component/window/AddRemoveSubWindow.java b/server/tests/src/com/vaadin/tests/server/component/window/AddRemoveSubWindow.java index 9ee4ffe6e7..2e59f9ee41 100644 --- a/server/tests/src/com/vaadin/tests/server/component/window/AddRemoveSubWindow.java +++ b/server/tests/src/com/vaadin/tests/server/component/window/AddRemoveSubWindow.java @@ -6,14 +6,14 @@ import static org.junit.Assert.assertTrue; import org.junit.Test; -import com.vaadin.LegacyApplication; +import com.vaadin.Application; import com.vaadin.ui.UI; import com.vaadin.ui.UI.LegacyWindow; import com.vaadin.ui.Window; public class AddRemoveSubWindow { - public class TestApp extends LegacyApplication { + public class TestApp extends Application { @Override public void init() { -- cgit v1.2.3