Revision: 1121
Author: dhanji
Date: Fri Oct 30 06:19:19 2009
Log: with(instance) api for servlet module. Thanks to isaac.shum
http://code.google.com/p/google-guice/source/detail?r=1121
Modified:
/trunk/servlet/src/com/google/inject/servlet/FiltersModuleBuilder.java
/trunk/servlet/src/com/google/inject/servlet/ServletModule.java
/trunk/servlet/src/com/google/inject/servlet/ServletsModuleBuilder.java
/trunk/servlet/test/com/google/inject/servlet/EdslTest.java
=======================================
--- /trunk/servlet/src/com/google/inject/servlet/FiltersModuleBuilder.java
Fri Apr 24 11:19:41 2009
+++ /trunk/servlet/src/com/google/inject/servlet/FiltersModuleBuilder.java
Fri Oct 30 06:19:19 2009
@@ -34,10 +34,16 @@
*/
class FiltersModuleBuilder extends AbstractModule {
private final List<FilterDefinition> filterDefinitions =
Lists.newArrayList();
+ private final List<FilterInstanceBindingEntry> filterInstanceEntries =
Lists.newArrayList();
//invoked on injector config
@Override
protected void configure() {
+ // Create bindings for filter instances
+ for (FilterInstanceBindingEntry entry : filterInstanceEntries) {
+ bind(entry.key).toInstance(entry.filter);
+ }
+
// Bind these filter definitions to a unique random key. Doesn't
matter what it is,
// coz it's never used.
bind(Key.get(new TypeLiteral<List<FilterDefinition>>() {},
UniqueAnnotations.create()))
@@ -51,6 +57,16 @@
public ServletModule.FilterKeyBindingBuilder filterRegex(List<String>
regexes) {
return new FilterKeyBindingBuilderImpl(regexes, UriPatternType.REGEX);
}
+
+ private static class FilterInstanceBindingEntry {
+ final Key<Filter> key;
+ final Filter filter;
+
+ FilterInstanceBindingEntry(Key<Filter> key, Filter filter) {
+ this.key = key;
+ this.filter = filter;
+ }
+ }
//non-static inner class so it can access state of enclosing module class
class FilterKeyBindingBuilderImpl implements
ServletModule.FilterKeyBindingBuilder {
@@ -69,6 +85,10 @@
public void through(Key<? extends Filter> filterKey) {
through(filterKey, new HashMap<String, String>());
}
+
+ public void through(Filter filter) {
+ through(filter, new HashMap<String, String>());
+ }
public void through(Class<? extends Filter> filterKey,
Map<String, String> contextParams) {
@@ -86,5 +106,12 @@
contextParams));
}
}
+
+ public void through(Filter filter,
+ Map<String, String> contextParams) {
+ Key<Filter> filterKey = Key.get(Filter.class,
UniqueAnnotations.create());
+ filterInstanceEntries.add(new FilterInstanceBindingEntry(filterKey,
filter));
+ through(filterKey, contextParams);
+ }
}
}
=======================================
--- /trunk/servlet/src/com/google/inject/servlet/ServletModule.java Fri Feb
20 16:25:27 2009
+++ /trunk/servlet/src/com/google/inject/servlet/ServletModule.java Fri Oct
30 06:19:19 2009
@@ -90,10 +90,12 @@
* protected void configureServlets() {
* filter("/*").through(MyFilter.class);
* filter("*.css").through(MyCssFilter.class);
+ * filter("*.jpg").through(new MyJpgFilter());
* // etc..
*
* serve("*.html").with(MyServlet.class);
* serve("/my/*").with(MyServlet.class);
+ * serve("*.jpg").with(new MyServlet());
* // etc..
* }
* }
@@ -259,8 +261,10 @@
public static interface FilterKeyBindingBuilder {
void through(Class<? extends Filter> filterKey);
void through(Key<? extends Filter> filterKey);
+ void through(Filter filter);
void through(Class<? extends Filter> dummyFilterClass, Map<String,
String> contextParams);
void through(Key<? extends Filter> dummyFilterClass, Map<String,
String> contextParams);
+ void through(Filter filter, Map<String, String> contextParams);
}
/**
@@ -271,7 +275,9 @@
public static interface ServletKeyBindingBuilder {
void with(Class<? extends HttpServlet> servletKey);
void with(Key<? extends HttpServlet> servletKey);
+ void with(HttpServlet servlet);
void with(Class<? extends HttpServlet> servletKey, Map<String, String>
contextParams);
void with(Key<? extends HttpServlet> servletKey, Map<String, String>
contextParams);
+ void with(HttpServlet servlet, Map<String, String> contextParams);
}
}
=======================================
--- /trunk/servlet/src/com/google/inject/servlet/ServletsModuleBuilder.java
Fri Apr 24 11:19:41 2009
+++ /trunk/servlet/src/com/google/inject/servlet/ServletsModuleBuilder.java
Fri Oct 30 06:19:19 2009
@@ -36,10 +36,15 @@
*/
class ServletsModuleBuilder extends AbstractModule {
private final List<ServletDefinition> servletDefinitions =
Lists.newArrayList();
+ private final List<ServletInstanceBindingEntry> servletInstanceEntries =
Lists.newArrayList();
//invoked on injector config
@Override
protected void configure() {
+ // Create bindings for servlet instances
+ for (ServletInstanceBindingEntry entry : servletInstanceEntries) {
+ bind(entry.key).toInstance(entry.servlet);
+ }
// Ensure that servlets are not bound twice to the same pattern.
Set<String> servletUris = Sets.newHashSet();
@@ -67,6 +72,16 @@
public ServletModule.ServletKeyBindingBuilder serveRegex(List<String>
regexes) {
return new ServletKeyBindingBuilderImpl(regexes, UriPatternType.REGEX);
}
+
+ private static class ServletInstanceBindingEntry {
+ final Key<HttpServlet> key;
+ final HttpServlet servlet;
+
+ ServletInstanceBindingEntry(Key<HttpServlet> key, HttpServlet servlet)
{
+ this.key = key;
+ this.servlet = servlet;
+ }
+ }
//non-static inner class so it can access state of enclosing module class
class ServletKeyBindingBuilderImpl implements
ServletModule.ServletKeyBindingBuilder {
@@ -85,6 +100,10 @@
public void with(Key<? extends HttpServlet> servletKey) {
with(servletKey, new HashMap<String, String>());
}
+
+ public void with(HttpServlet servlet) {
+ with(servlet, new HashMap<String, String>());
+ }
public void with(Class<? extends HttpServlet> servletKey,
Map<String, String> contextParams) {
@@ -100,5 +119,12 @@
contextParams));
}
}
+
+ public void with(HttpServlet servlet,
+ Map<String, String> contextParams) {
+ Key<HttpServlet> servletKey = Key.get(HttpServlet.class,
UniqueAnnotations.create());
+ servletInstanceEntries.add(new
ServletInstanceBindingEntry(servletKey, servlet));
+ with(servletKey, contextParams);
+ }
}
}
=======================================
--- /trunk/servlet/test/com/google/inject/servlet/EdslTest.java Tue Jan 6
22:49:07 2009
+++ /trunk/servlet/test/com/google/inject/servlet/EdslTest.java Fri Oct 30
06:19:19 2009
@@ -39,6 +39,7 @@
filter("/*").through(DummyFilterImpl.class);
filter("*.html").through(DummyFilterImpl.class);
filter("/*").through(Key.get(DummyFilterImpl.class));
+ filter("/*").through(new DummyFilterImpl());
filter("*.html").through(DummyFilterImpl.class,
new HashMap<String, String>());
@@ -51,10 +52,15 @@
filterRegex("/person/[0-9]*").through(Key.get(DummyFilterImpl.class),
new HashMap<String, String>());
+ filterRegex("/person/[0-9]*").through(new DummyFilterImpl());
+ filterRegex("/person/[0-9]*").through(new DummyFilterImpl(),
+ new HashMap<String, String>());
+
serve("/1/*").with(DummyServlet.class);
serve("/2/*").with(Key.get(DummyServlet.class));
- serve("/3/*").with(DummyServlet.class, new HashMap<String,
String>());
+ serve("/3/*").with(new DummyServlet());
+ serve("/4/*").with(DummyServlet.class, new HashMap<String,
String>());
serve("*.htm").with(Key.get(DummyServlet.class));
serve("*.html").with(Key.get(DummyServlet.class),
@@ -67,6 +73,10 @@
serveRegex("/person/[0-6]*").with(Key.get(DummyServlet.class));
serveRegex("/person/[0-9]/2/*").with(Key.get(DummyServlet.class),
new HashMap<String, String>());
+
+ serveRegex("/person/[0-5]*").with(new DummyServlet());
+ serveRegex("/person/[0-9]/3/*").with(new DummyServlet(),
+ new HashMap<String, String>());
}
};
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-guice-dev" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-guice-dev?hl=en
-~----------~----~----~----~------~----~------~--~---