diff options
author | Petter Holmström <petter.holmstrom@itmill.com> | 2009-11-04 11:57:10 +0000 |
---|---|---|
committer | Petter Holmström <petter.holmstrom@itmill.com> | 2009-11-04 11:57:10 +0000 |
commit | b81fdccc968d25bb4b9ba80a1f82bf3fa40e9891 (patch) | |
tree | d6e4bea9fe4fa459b09fef6f9e00fe768d49b326 /src/com/vaadin/Application.java | |
parent | c2c9ed7777d68a0babb79ffb93945632f04eb8e3 (diff) | |
download | vaadin-framework-b81fdccc968d25bb4b9ba80a1f82bf3fa40e9891.tar.gz vaadin-framework-b81fdccc968d25bb4b9ba80a1f82bf3fa40e9891.zip |
More refactorings, application resource downloads should now work on both servlets and portlets.
svn changeset:9617/svn branch:portlet_2.0
Diffstat (limited to 'src/com/vaadin/Application.java')
-rw-r--r-- | src/com/vaadin/Application.java | 137 |
1 files changed, 93 insertions, 44 deletions
diff --git a/src/com/vaadin/Application.java b/src/com/vaadin/Application.java index bea366307d..dfdf4e3230 100644 --- a/src/com/vaadin/Application.java +++ b/src/com/vaadin/Application.java @@ -107,7 +107,7 @@ public abstract class Application implements URIHandler, /** * Mapping from window name to window instance. */ - private final Hashtable windows = new Hashtable(); + private final Hashtable<String, Window> windows = new Hashtable<String, Window>(); /** * Main window of the application. @@ -123,7 +123,7 @@ public abstract class Application implements URIHandler, * The ID of the portlet window that this application runs in. */ private String portletWindowId; - + /** * Name of the theme currently used by the application. */ @@ -147,24 +147,24 @@ public abstract class Application implements URIHandler, /** * List of listeners listening user changes. */ - private LinkedList userChangeListeners = null; + private LinkedList<UserChangeListener> userChangeListeners = null; /** * Window attach listeners. */ - private LinkedList windowAttachListeners = null; + private LinkedList<WindowAttachListener> windowAttachListeners = null; /** * Window detach listeners. */ - private LinkedList windowDetachListeners = null; + private LinkedList<WindowDetachListener> windowDetachListeners = null; /** * Application resource mapping: key <-> resource. */ - private final Hashtable resourceKeyMap = new Hashtable(); + private final Hashtable<ApplicationResource, String> resourceKeyMap = new Hashtable<ApplicationResource, String>(); - private final Hashtable keyResourceMap = new Hashtable(); + private final Hashtable<String, ApplicationResource> keyResourceMap = new Hashtable<String, ApplicationResource>(); private long lastResourceKeyNumber = 0; @@ -190,12 +190,82 @@ public abstract class Application implements URIHandler, public String getPortletWindowId() { return portletWindowId; } - + // TODO Document me! public void setPortletWindowId(String portletWindowId) { this.portletWindowId = portletWindowId; } - + + // TODO Document me! + public static interface ResourceURLGenerator { + + 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 + */ + 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); + }; + + }; + + private ResourceURLGenerator resourceURLGenerator = defaultResourceURLGenerator; + + public ResourceURLGenerator getResourceURLGenerator() { + return resourceURLGenerator; + } + + public void setResourceURLGenerator( + ResourceURLGenerator resourceURLGenerator) { + if (resourceURLGenerator == null) + this.resourceURLGenerator = defaultResourceURLGenerator; + else + this.resourceURLGenerator = resourceURLGenerator; + } + /** * <p> * Gets a window by name. Returns <code>null</code> if the application is @@ -516,7 +586,7 @@ public abstract class Application implements URIHandler, * * @return the Unmodifiable collection of windows. */ - public Collection getWindows() { + public Collection<Window> getWindows() { return Collections.unmodifiableCollection(windows.values()); } @@ -554,10 +624,10 @@ public abstract class Application implements URIHandler, */ public void setTheme(String theme) { // Collect list of windows not having the current or future theme - final LinkedList toBeUpdated = new LinkedList(); + final LinkedList<Window> toBeUpdated = new LinkedList<Window>(); final String oldAppTheme = getTheme(); - for (final Iterator i = getWindows().iterator(); i.hasNext();) { - final Window w = (Window) i.next(); + for (final Iterator<Window> i = getWindows().iterator(); i.hasNext();) { + final Window w = i.next(); final String windowTheme = w.getTheme(); if ((windowTheme == null) || (!windowTheme.equals(theme) && windowTheme @@ -570,8 +640,8 @@ public abstract class Application implements URIHandler, this.theme = theme; // Ask windows to update themselves - for (final Iterator i = toBeUpdated.iterator(); i.hasNext();) { - ((Window) i.next()).requestRepaint(); + for (final Iterator<Window> i = toBeUpdated.iterator(); i.hasNext();) { + i.next().requestRepaint(); } } @@ -610,7 +680,7 @@ public abstract class Application implements URIHandler, * the keys in the default property list. * */ - public Enumeration getPropertyNames() { + public Enumeration<?> getPropertyNames() { return properties.propertyNames(); } @@ -682,12 +752,7 @@ public abstract class Application implements URIHandler, return null; } - final String filename = resource.getFilename(); - if (filename == null) { - return "APP/" + key + "/"; - } else { - return "APP/" + key + "/" + filename; - } + return resourceURLGenerator.generateResourceURL(resource, key); } /** @@ -701,27 +766,11 @@ public abstract class Application implements URIHandler, */ public DownloadStream handleURI(URL context, String relativeUri) { - // If the relative uri is null, we are ready - if (relativeUri == null) { - return null; - } - - // Resolves the prefix - String prefix = relativeUri; - final int index = relativeUri.indexOf('/'); - if (index >= 0) { - prefix = relativeUri.substring(0, index); - } - - // Handles the resource requests - if (prefix.equals("APP")) { + if (resourceURLGenerator.isResourceURL(context, relativeUri)) { // Handles the resource request - final int next = relativeUri.indexOf('/', index + 1); - if (next < 0) { - return null; - } - final String key = relativeUri.substring(index + 1, next); + final String key = resourceURLGenerator.getMapKey(context, + relativeUri); final ApplicationResource resource = (ApplicationResource) keyResourceMap .get(key); if (resource != null) { @@ -861,7 +910,7 @@ public abstract class Application implements URIHandler, */ public void addListener(UserChangeListener listener) { if (userChangeListeners == null) { - userChangeListeners = new LinkedList(); + userChangeListeners = new LinkedList<UserChangeListener>(); } userChangeListeners.add(listener); } @@ -992,7 +1041,7 @@ public abstract class Application implements URIHandler, */ public void addListener(WindowAttachListener listener) { if (windowAttachListeners == null) { - windowAttachListeners = new LinkedList(); + windowAttachListeners = new LinkedList<WindowAttachListener>(); } windowAttachListeners.add(listener); } @@ -1005,7 +1054,7 @@ public abstract class Application implements URIHandler, */ public void addListener(WindowDetachListener listener) { if (windowDetachListeners == null) { - windowDetachListeners = new LinkedList(); + windowDetachListeners = new LinkedList<WindowDetachListener>(); } windowDetachListeners.add(listener); } |