Revision: 1405
Author: sberlin
Date: Sat Nov 20 20:05:26 2010
Log: Created wiki page through web user interface.
http://code.google.com/p/google-guice/source/detail?r=1405
Added:
/wiki/ServletExtensionSPI.wiki
=======================================
--- /dev/null
+++ /wiki/ServletExtensionSPI.wiki Sat Nov 20 20:05:26 2010
@@ -0,0 +1,54 @@
+#summary Using the servlet module SPI
+#labels Guice30
+
+=ServletModule Extension SPI=
+
+_New in Guice 3.0_
+
+Sometimes you want to examine your Modules and/or Bindings to inspect
which URLs are serving which servlets or filters. This is useful for tests
or to assert preconditions in your code. The !ServletModule extension SPI,
built on the [ExtensionSPI core extension SPI], lets you do this the same
way you would inspect a binding using the normal [InspectingModules
elements SPI].
+
+==Using ServletModuleTargetVisitor==
+
+[http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/servlet/ServletModuleTargetVisitor.html
ServletModuleTargetVisitor]
is an extension to the core
[http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/spi/BindingTargetVisitor.html
BindingTargetVisitor]. You can implement this interface and call
binding.acceptTargetVisitor(myVisitor) to learn details about servlet
bindings.
+
+{{{
+ boolean isBindingForUri(Binding<?> binding, String uri) {
+ return binding.acceptTargetVisitor(new Visitor(uri));
+ }
+
+ private static class Visitor
+ extends DefaultBindingTargetVisitor<Object, Boolean>
+ implements ServletModuleTargetVisitor<Object, Boolean> {
+ private final String uri;
+
+ Visitor(String uri) {
+ this.uri = uri;
+ }
+
+ @Override boolean visitOther(Binding<?> binding) {
+ return false;
+ }
+
+ @Override visit(InstanceFilterBinding binding) {
+ return matchesUri(binding);
+ }
+
+ @Override visit(InstanceServletBinding binding) {
+ return matchesUri(binding);
+ }
+
+ @Override visit(LinkedFilterBinding binding) {
+ return matchesUri(binding);
+ }
+
+ @Override visit(LinkedServletBinding binding) {
+ return matchesUri(binding);
+ }
+
+ private boolean matchesUri(ServletModuleBinding binding) {
+ return binding.matchesUri(uri);
+ }
+ }
+}}}
+
+These visitors will work both on bindings retrieved from an Injector and
bindings retrieved from Elements.
--
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.