You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2008/07/28 21:10:31 UTC
svn commit: r680445 - in /tapestry/tapestry5/trunk:
tapestry-core/src/main/java/org/apache/tapestry5/
tapestry-core/src/main/java/org/apache/tapestry5/internal/renderers/
tapestry-core/src/main/java/org/apache/tapestry5/internal/services/
tapestry-core...
Author: hlship
Date: Mon Jul 28 12:10:29 2008
New Revision: 680445
URL: http://svn.apache.org/viewvc?rev=680445&view=rev
Log:
TAPESTRY-2540: Tapestry should place the Registry into the ServletContext using a well-known attribute name, for use by non-Tapestry code
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/TapestryFilter.java
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/renderers/RequestRenderer.java
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ContextImpl.java
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/test/PageTesterContext.java
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/Context.java
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/RegistryImpl.java
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/TapestryFilter.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/TapestryFilter.java?rev=680445&r1=680444&r2=680445&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/TapestryFilter.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/TapestryFilter.java Mon Jul 28 12:10:29 2008
@@ -56,6 +56,13 @@
private HttpServletRequestHandler handler;
/**
+ * Key under which that Tapestry IoC {@link org.apache.tapestry5.ioc.Registry} is stored in the ServletContext. This
+ * allows other code, beyond Tapestry, to obtain the Registry and, from it, any Tapestry services. Such code should
+ * be careful about invoking {@link org.apache.tapestry5.ioc.Registry#cleanupThread()} appopriately.
+ */
+ public static final String REGISTRY_CONTEXT_NAME = "org.apache.tapestry5.application-registry";
+
+ /**
* Initializes the filter using the {@link TapestryAppInitializer}. The application name is the capitalization of
* the filter name (as specified in web.xml).
*/
@@ -75,6 +82,8 @@
registry = appInitializer.getRegistry();
+ context.setAttribute(REGISTRY_CONTEXT_NAME, registry);
+
long start = appInitializer.getStartTime();
long toRegistry = appInitializer.getRegistryCreatedTime();
@@ -176,7 +185,8 @@
}
/**
- * Shuts down and discards the registry.
+ * Shuts down and discards the registry. Invokes {@link #destroy(org.apache.tapestry5.ioc.Registry)} to allow
+ * subclasses to peform any shutdown logic, then shuts down the registry, and removes it from the ServletContext.
*/
public final void destroy()
{
@@ -184,6 +194,8 @@
registry.shutdown();
+ config.getServletContext().removeAttribute(REGISTRY_CONTEXT_NAME);
+
registry = null;
config = null;
handler = null;
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/renderers/RequestRenderer.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/renderers/RequestRenderer.java?rev=680445&r1=680444&r2=680445&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/renderers/RequestRenderer.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/renderers/RequestRenderer.java Mon Jul 28 12:10:29 2008
@@ -16,6 +16,8 @@
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.internal.InternalConstants;
+import org.apache.tapestry5.ioc.annotations.Primary;
+import org.apache.tapestry5.services.Context;
import org.apache.tapestry5.services.ObjectRenderer;
import org.apache.tapestry5.services.Request;
@@ -23,6 +25,16 @@
public class RequestRenderer implements ObjectRenderer<Request>
{
+ private final Context context;
+
+ private final ObjectRenderer masterObjectRenderer;
+
+ public RequestRenderer(@Primary ObjectRenderer masterObjectRenderer, Context context)
+ {
+ this.masterObjectRenderer = masterObjectRenderer;
+ this.context = context;
+ }
+
public void render(Request request, MarkupWriter writer)
{
writer.element("dl");
@@ -61,6 +73,31 @@
parameters(request, writer);
headers(request, writer);
+ context(request, writer);
+ }
+
+ private void context(Request request, MarkupWriter writer)
+ {
+ List<String> attributeNames = context.getAttributeNames();
+
+ if (attributeNames.isEmpty()) return;
+
+ section(writer, "Context Attributes");
+
+ writer.element("dl");
+
+ for (String name : attributeNames)
+ {
+ dt(writer, name);
+
+ writer.element("dd");
+
+ masterObjectRenderer.render(context.getAttribute(name), writer);
+
+ writer.end(); // dd
+ }
+
+ writer.end(); // dl
}
private void parameters(Request request, MarkupWriter writer)
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ContextImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ContextImpl.java?rev=680445&r1=680444&r2=680445&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ContextImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ContextImpl.java Mon Jul 28 12:10:29 2008
@@ -15,6 +15,7 @@
package org.apache.tapestry5.internal.services;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
+import org.apache.tapestry5.ioc.internal.util.InternalUtils;
import org.apache.tapestry5.ioc.util.Stack;
import org.apache.tapestry5.services.Context;
@@ -98,4 +99,9 @@
return servletContext.getAttribute(name);
}
+ public List<String> getAttributeNames()
+ {
+ return InternalUtils.toList(servletContext.getAttributeNames());
+ }
+
}
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/test/PageTesterContext.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/test/PageTesterContext.java?rev=680445&r1=680444&r2=680445&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/test/PageTesterContext.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/test/PageTesterContext.java Mon Jul 28 12:10:29 2008
@@ -20,6 +20,7 @@
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
+import java.util.Collections;
import java.util.List;
public class PageTesterContext implements Context
@@ -64,6 +65,11 @@
throw new UnsupportedOperationException("getAttribute() is not supported for ContextForPageTester.");
}
+ public List<String> getAttributeNames()
+ {
+ return Collections.emptyList();
+ }
+
/**
* Always returns null.
*/
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/Context.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/Context.java?rev=680445&r1=680444&r2=680445&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/Context.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/Context.java Mon Jul 28 12:10:29 2008
@@ -64,4 +64,9 @@
* @return the attribute, or null if not found
*/
Object getAttribute(String name);
+
+ /**
+ * Returns the names of all attributes of the context, sorted alphabetically.
+ */
+ List<String> getAttributeNames();
}
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java?rev=680445&r1=680444&r2=680445&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java Mon Jul 28 12:10:29 2008
@@ -1326,7 +1326,7 @@
}
});
- configuration.add(Request.class, new RequestRenderer());
+ configuration.add(Request.class, locator.autobuild(RequestRenderer.class));
configuration.add(Location.class, locationRenderer);
Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/RegistryImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/RegistryImpl.java?rev=680445&r1=680444&r2=680445&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/RegistryImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/RegistryImpl.java Mon Jul 28 12:10:29 2008
@@ -21,12 +21,7 @@
import org.apache.tapestry5.ioc.def.ServiceDef;
import org.apache.tapestry5.ioc.internal.services.PerthreadManagerImpl;
import org.apache.tapestry5.ioc.internal.services.RegistryShutdownHubImpl;
-import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
-import static org.apache.tapestry5.ioc.internal.util.CollectionFactory.*;
-import static org.apache.tapestry5.ioc.internal.util.Defense.notNull;
-import org.apache.tapestry5.ioc.internal.util.InternalUtils;
-import org.apache.tapestry5.ioc.internal.util.OneShotLock;
-import org.apache.tapestry5.ioc.internal.util.Orderer;
+import org.apache.tapestry5.ioc.internal.util.*;
import org.apache.tapestry5.ioc.services.*;
import org.slf4j.Logger;
@@ -66,9 +61,9 @@
private final OneShotLock eagerLoadLock = new OneShotLock();
- private final Map<String, Object> builtinServices = newCaseInsensitiveMap();
+ private final Map<String, Object> builtinServices = CollectionFactory.newCaseInsensitiveMap();
- private final Map<String, Class> builtinTypes = newCaseInsensitiveMap();
+ private final Map<String, Class> builtinTypes = CollectionFactory.newCaseInsensitiveMap();
private final RegistryShutdownHubImpl registryShutdownHub;
@@ -77,9 +72,9 @@
/**
* Map from service id to the Module that contains the service.
*/
- private final Map<String, Module> serviceIdToModule = newCaseInsensitiveMap();
+ private final Map<String, Module> serviceIdToModule = CollectionFactory.newCaseInsensitiveMap();
- private final Map<String, ServiceLifecycle> lifecycles = newCaseInsensitiveMap();
+ private final Map<String, ServiceLifecycle> lifecycles = CollectionFactory.newCaseInsensitiveMap();
private final PerthreadManager perthreadManager;
@@ -89,12 +84,12 @@
private SymbolSource symbolSource;
- private final List<Module> modules = newList();
+ private final List<Module> modules = CollectionFactory.newList();
/**
* From marker type to a list of marked service instances.
*/
- private final Map<Class, List<ServiceDef>> markerToServiceDef = newMap();
+ private final Map<Class, List<ServiceDef>> markerToServiceDef = CollectionFactory.newMap();
public static final class OrderedConfigurationToOrdererAdaptor<T> implements OrderedConfiguration<T>
@@ -337,7 +332,7 @@
{
lock.check();
- final Collection<T> result = newList();
+ final Collection<T> result = CollectionFactory.newList();
Configuration<T> configuration = new Configuration<T>()
{
@@ -421,12 +416,12 @@
{
if (keyType.equals(String.class))
{
- Map<String, K> result = newCaseInsensitiveMap();
+ Map<String, K> result = CollectionFactory.newCaseInsensitiveMap();
return (Map<K, V>) result;
}
- return newMap();
+ return CollectionFactory.newMap();
}
private <K, V> void addToMappedConfiguration(MappedConfiguration<K, V> configuration,
@@ -538,7 +533,7 @@
private List<String> findServiceIdsForInterface(Class serviceInterface)
{
- List<String> result = newList();
+ List<String> result = CollectionFactory.newList();
for (Module module : modules)
result.addAll(module.findServiceIdsForInterface(serviceInterface));
@@ -636,7 +631,7 @@
{
if (provider.getAnnotation(marker) == null) continue;
- List<ServiceDef> matches = newList();
+ List<ServiceDef> matches = CollectionFactory.newList();
for (ServiceDef def : markerToServiceDef.get(marker))
{
@@ -711,7 +706,7 @@
public <T> T autobuild(Class<T> clazz)
{
- notNull(clazz, "clazz");
+ Defense.notNull(clazz, "clazz");
Constructor constructor = InternalUtils.findAutobuildConstructor(clazz);
@@ -747,8 +742,8 @@
public <T> T proxy(Class<T> interfaceClass, final Class<? extends T> implementationClass)
{
- notNull(interfaceClass, "interfaceClass");
- notNull(implementationClass, "implementationClass");
+ Defense.notNull(interfaceClass, "interfaceClass");
+ Defense.notNull(implementationClass, "implementationClass");
// TODO: Check really an interface
// TODO: Check impl class extends interfaceClass and is concrete