From 68a092e2c13982fa5de6ca4c63638d6162edb675 Mon Sep 17 00:00:00 2001 From: Henri Sara Date: Mon, 30 Nov 2009 10:07:26 +0000 Subject: [PATCH] Refactoring in Portlet 2.0 support svn changeset:10103/svn branch:6.2 --- src/com/vaadin/Application.java | 102 +--------- .../vaadin/service/ApplicationContext.java | 80 ++++++-- .../server/AbstractApplicationPortlet.java | 93 ++------- .../server/AbstractApplicationServlet.java | 6 +- .../server/AbstractWebApplicationContext.java | 191 ++++++++++++++++++ .../server/PortletApplicationContext2.java | 154 ++++---------- .../gwt/server/WebApplicationContext.java | 171 +--------------- 7 files changed, 326 insertions(+), 471 deletions(-) create mode 100644 src/com/vaadin/terminal/gwt/server/AbstractWebApplicationContext.java diff --git a/src/com/vaadin/Application.java b/src/com/vaadin/Application.java index a97ce0d682..2b132fee64 100644 --- a/src/com/vaadin/Application.java +++ b/src/com/vaadin/Application.java @@ -121,11 +121,6 @@ public abstract class Application implements URIHandler, */ private URL applicationUrl; - /** - * The ID of the portlet window that this application runs in. - */ - private String portletWindowId; - /** * Name of the theme currently used by the application. */ @@ -188,92 +183,6 @@ public abstract class Application implements URIHandler, */ private Terminal.ErrorListener errorHandler = this; - // TODO Document me! - public String getPortletWindowId() { - return portletWindowId; - } - - // TODO Document me! - public void setPortletWindowId(String portletWindowId) { - this.portletWindowId = portletWindowId; - } - - // TODO Document me! - @Deprecated - public static interface ResourceURLGenerator extends Serializable { - - public String generateResourceURL(ApplicationResource resource, - String mapKey); - - public boolean isResourceURL(URL context, String relativeUri); - - public String getMapKey(URL context, String relativeUri); - - } - - /* - * Default resource URL generator for servlets - */ - @Deprecated - private static ResourceURLGenerator defaultResourceURLGenerator = new ResourceURLGenerator() { - public String generateResourceURL(ApplicationResource resource, - String mapKey) { - - final String filename = resource.getFilename(); - if (filename == null) { - return "APP/" + mapKey + "/"; - } else { - return "APP/" + mapKey + "/" + filename; - } - - } - - public boolean isResourceURL(URL context, String relativeUri) { - // If the relative uri is null, we are ready - if (relativeUri == null) { - return false; - } - - // Resolves the prefix - String prefix = relativeUri; - final int index = relativeUri.indexOf('/'); - if (index >= 0) { - prefix = relativeUri.substring(0, index); - } - - // Handles the resource requests - return (prefix.equals("APP")); - } - - public String getMapKey(URL context, String relativeUri) { - final int index = relativeUri.indexOf('/'); - final int next = relativeUri.indexOf('/', index + 1); - if (next < 0) { - return null; - } - return relativeUri.substring(index + 1, next); - }; - - }; - - @Deprecated - private ResourceURLGenerator resourceURLGenerator = defaultResourceURLGenerator; - - @Deprecated - public ResourceURLGenerator getResourceURLGenerator() { - return resourceURLGenerator; - } - - @Deprecated - public void setResourceURLGenerator( - ResourceURLGenerator resourceURLGenerator) { - if (resourceURLGenerator == null) { - this.resourceURLGenerator = defaultResourceURLGenerator; - } else { - this.resourceURLGenerator = resourceURLGenerator; - } - } - /** *

* Gets a window by name. Returns null if the application is @@ -821,7 +730,7 @@ public abstract class Application implements URIHandler, @Deprecated public String getRelativeLocation(ApplicationResource resource) { - // FIXME Move to ApplicationContext + // FIXME Move to ApplicationContext? // Gets the key final String key = resourceKeyMap.get(resource); @@ -831,7 +740,7 @@ public abstract class Application implements URIHandler, return null; } - return resourceURLGenerator.generateResourceURL(resource, key); + return context.generateApplicationResourceURL(resource, key); } /** @@ -854,12 +763,13 @@ public abstract class Application implements URIHandler, @Deprecated public DownloadStream handleURI(URL context, String relativeUri) { - // FIXME Move to ApplicationContext + // FIXME Move to ApplicationContext? - public API, need to leave hook + // here - if (resourceURLGenerator.isResourceURL(context, relativeUri)) { + if (this.context.isApplicationResourceURL(context, relativeUri)) { // Handles the resource request - final String key = resourceURLGenerator.getMapKey(context, + final String key = this.context.getURLKey(context, relativeUri); final ApplicationResource resource = keyResourceMap .get(key); diff --git a/src/com/vaadin/service/ApplicationContext.java b/src/com/vaadin/service/ApplicationContext.java index 756a8e50c8..ab201a4bd4 100644 --- a/src/com/vaadin/service/ApplicationContext.java +++ b/src/com/vaadin/service/ApplicationContext.java @@ -1,4 +1,4 @@ -/* +/* @ITMillApache2LicenseForJavaFiles@ */ @@ -6,6 +6,7 @@ package com.vaadin.service; import java.io.File; import java.io.Serializable; +import java.net.URL; import java.util.Collection; import com.vaadin.Application; @@ -16,7 +17,7 @@ import com.vaadin.terminal.ApplicationResource; * context of the application. Each context is shared by all applications that * are open for one user. In a web-environment this corresponds to a * HttpSession. - * + * * @author IT Mill Ltd. * @version * @VERSION@ @@ -26,13 +27,13 @@ public interface ApplicationContext extends Serializable { /** * Returns application context base directory. - * + * * Typically an application is deployed in a such way that is has an * application directory. For web applications this directory is the root * directory of the web applications. In some cases applications might not * have an application directory (for example web applications running * inside a war). - * + * * @return The application base directory or null if the application has no * base directory. */ @@ -40,46 +41,89 @@ public interface ApplicationContext extends Serializable { /** * Returns a collection of all the applications in this context. - * + * * Each application context contains all active applications for one user. - * + * * @return A collection containing all the applications in this context. */ public Collection getApplications(); - + /** - * Adds a transaction listener to this context. - * - * @param listener - * the listener to be added. - * @see TransactionListener + * Adds a transaction listener to this context. The transaction listener is + * called before and after each each request related to this session except + * when serving static resources. + * + * + * @see com.vaadin.service.ApplicationContext#addTransactionListener(com.vaadin.service.ApplicationContext.TransactionListener) */ public void addTransactionListener(TransactionListener listener); /** * Removes a transaction listener from this context. - * + * * @param listener * the listener to be removed. * @see TransactionListener */ public void removeTransactionListener(TransactionListener listener); + /** + * Generate a URL that can be used as the relative location of e.g. an + * {@link ApplicationResource}. + * + * @deprecated this is subject to change/removal from the interface + * + * @param resource + * @param urlKey + * a key for the resource that can later be extracted from a URL + * with {@link #getURLKey(URL, String)} + */ + @Deprecated + public String generateApplicationResourceURL(ApplicationResource resource, + String urlKey); + + /** + * Tests if a URL is for an application resource (APP/...). + * + * @deprecated this is subject to change/removal from the interface + * + * @param context + * @param relativeUri + * @return + */ + @Deprecated + public boolean isApplicationResourceURL(URL context, String relativeUri); + + /** + * Gets the identifier (key) from an application resource URL. This key is + * the one that was given to + * {@link #generateApplicationResourceURL(ApplicationResource, String)} when + * creating the URL. + * + * @deprecated this is subject to change/removal from the interface + * + * @param context + * @param relativeUri + * @return + */ + @Deprecated + public String getURLKey(URL context, String relativeUri); + /** * Interface for listening to transaction events. Implement this interface * to listen to all transactions between the client and the application. - * + * */ public interface TransactionListener extends Serializable { /** * Invoked at the beginning of every transaction. - * + * * The transaction is linked to the context, not the application so if * you have multiple applications running in the same context you need * to check that the request is associated with the application you are * interested in. This can be done looking at the application parameter. - * + * * @param application * the Application object. * @param transactionData @@ -90,12 +134,12 @@ public interface ApplicationContext extends Serializable { /** * Invoked at the end of every transaction. - * + * * The transaction is linked to the context, not the application so if * you have multiple applications running in the same context you need * to check that the request is associated with the application you are * interested in. This can be done looking at the application parameter. - * + * * @param applcation * the Application object. * @param transactionData diff --git a/src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java b/src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java index 0cfb04bb91..b465bf2258 100644 --- a/src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java +++ b/src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java @@ -10,9 +10,7 @@ import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.MalformedURLException; -import java.net.URL; import java.security.GeneralSecurityException; -import java.util.Collection; import java.util.Date; import java.util.Enumeration; import java.util.Iterator; @@ -45,7 +43,6 @@ import com.liferay.portal.kernel.util.PropsUtil; import com.vaadin.Application; import com.vaadin.Application.SystemMessages; import com.vaadin.external.org.apache.commons.fileupload.portlet.PortletFileUpload; -import com.vaadin.terminal.ApplicationResource; import com.vaadin.terminal.DownloadStream; import com.vaadin.terminal.Terminal; import com.vaadin.ui.Window; @@ -60,11 +57,7 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet private static final String PORTAL_PARAMETER_VAADIN_THEME = "vaadin.theme"; - /* - * TODO Big parts of this class are directly copy-pasted from - * AbstractApplicationServlet. On the long term, it would probably be wise - * to try to integrate the common parts into a shared super class. - */ + // TODO some parts could be shared with AbstractApplicationServlet // TODO Can we close the application when the portlet is removed? Do we know // when the portlet is removed? @@ -76,54 +69,6 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet private boolean productionMode = false; - private static class PortletResourceURLGenerator implements - Application.ResourceURLGenerator { - - private final MimeResponse response; - - public PortletResourceURLGenerator(MimeResponse response) { - this.response = response; - } - - public boolean isResourceURL(URL context, String relativeUri) { - // If the relative uri is null, we are ready - if (relativeUri == null) { - return false; - } - - // Resolves the prefix - String prefix = relativeUri; - final int index = relativeUri.indexOf('/'); - if (index >= 0) { - prefix = relativeUri.substring(0, index); - } - - // Handles the resource requests - return (prefix.equals("APP")); - } - - public String getMapKey(URL context, String relativeUri) { - final int index = relativeUri.indexOf('/'); - final int next = relativeUri.indexOf('/', index + 1); - if (next < 0) { - return null; - } - return relativeUri.substring(index + 1, next); - } - - public String generateResourceURL(ApplicationResource resource, - String mapKey) { - ResourceURL resourceURL = response.createResourceURL(); - final String filename = resource.getFilename(); - if (filename == null) { - resourceURL.setResourceID("APP/" + mapKey + "/"); - } else { - resourceURL.setResourceID("APP/" + mapKey + "/" + filename); - } - return resourceURL.toString(); - } - }; - @SuppressWarnings("unchecked") @Override public void init(PortletConfig config) throws PortletException { @@ -376,11 +321,6 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet if (application == null) { return; } - if (response instanceof MimeResponse) { - application - .setResourceURLGenerator(new PortletResourceURLGenerator( - (MimeResponse) response)); - } /* * Get or create an application context and an application @@ -388,6 +328,10 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet */ PortletApplicationContext2 applicationContext = PortletApplicationContext2 .getApplicationContext(request.getPortletSession()); + if (response instanceof MimeResponse) { + applicationContext.setMimeResponse((MimeResponse) response); + } + PortletCommunicationManager applicationManager = applicationContext .getApplicationManager(application); @@ -756,10 +700,9 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet private Application createApplication(PortletRequest request) throws PortletException, MalformedURLException { Application newApplication = getNewApplication(request); - newApplication.setPortletWindowId(request.getWindowID()); final PortletApplicationContext2 context = PortletApplicationContext2 .getApplicationContext(request.getPortletSession()); - context.addApplication(newApplication); + context.addApplication(newApplication, request.getWindowID()); return newApplication; } @@ -776,19 +719,17 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet PortletApplicationContext2 context = PortletApplicationContext2 .getApplicationContext(session); - - final Collection applications = context.getApplications(); - for (Application sessionApplication : applications) { - if (request.getWindowID().equals( - sessionApplication.getPortletWindowId())) { - if (sessionApplication.isRunning()) { - return sessionApplication; - } - PortletApplicationContext2.getApplicationContext(session) - .removeApplication(sessionApplication); - break; - } + Application application = context.getApplicationForWindowId(request + .getWindowID()); + if (application == null) { + return null; + } + if (application.isRunning()) { + return application; } + // application found but not running + PortletApplicationContext2.getApplicationContext(session) + .removeApplication(application); return null; } @@ -846,7 +787,7 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet systemMessages = getSystemMessages(); } catch (SystemMessageException e) { // failing to get the system messages is always a problem - throw new PortletException("CommunicationError!", e); + throw new PortletException("Failed to obtain system messages!", e); } page.write("