ServletsModuleBuilder.java revision b4b7f7209570bd75352eb322825ae79392f03978
1/**
2 * Copyright (C) 2008 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.google.inject.servlet;
17
18import com.google.inject.AbstractModule;
19import com.google.inject.Key;
20import com.google.inject.internal.util.Lists;
21import com.google.inject.internal.util.Sets;
22import com.google.inject.internal.UniqueAnnotations;
23import java.util.HashMap;
24import java.util.List;
25import java.util.Map;
26import java.util.Set;
27import javax.servlet.http.HttpServlet;
28
29/**
30 * Builds the guice module that binds configured servlets, with their
31 * wrapper ServletDefinitions. Is part of the binding EDSL. Very similar to
32 * {@link com.google.inject.servlet.FiltersModuleBuilder}.
33 *
34 * @author Dhanji R. Prasanna (dhanji@gmail.com)
35 */
36class ServletsModuleBuilder extends AbstractModule {
37  private final List<ServletDefinition> servletDefinitions = Lists.newArrayList();
38  private final List<ServletInstanceBindingEntry> servletInstanceEntries = Lists.newArrayList();
39
40  //invoked on injector config
41  @Override
42  protected void configure() {
43    // Create bindings for servlet instances
44    for (ServletInstanceBindingEntry entry : servletInstanceEntries) {
45      bind(entry.key).toInstance(entry.servlet);
46    }
47
48    // Ensure that servlets are not bound twice to the same pattern.
49    Set<String> servletUris = Sets.newHashSet();
50    for (ServletDefinition servletDefinition : servletDefinitions) {
51      if (servletUris.contains(servletDefinition.getPattern())) {
52        addError("More than one servlet was mapped to the same URI pattern: "
53            + servletDefinition.getPattern());
54      }
55      else {
56        bind(Key.get(ServletDefinition.class, UniqueAnnotations.create())).toProvider(servletDefinition);
57        servletUris.add(servletDefinition.getPattern());
58      }
59    }
60  }
61
62  //the first level of the EDSL--
63  public ServletModule.ServletKeyBindingBuilder serve(List<String> urlPatterns) {
64    return new ServletKeyBindingBuilderImpl(urlPatterns, UriPatternType.SERVLET);
65  }
66
67  public ServletModule.ServletKeyBindingBuilder serveRegex(List<String> regexes) {
68    return new ServletKeyBindingBuilderImpl(regexes, UriPatternType.REGEX);
69  }
70
71  private static class ServletInstanceBindingEntry {
72    final Key<HttpServlet> key;
73    final HttpServlet servlet;
74
75    ServletInstanceBindingEntry(Key<HttpServlet> key, HttpServlet servlet) {
76      this.key = key;
77      this.servlet = servlet;
78    }
79  }
80
81  //non-static inner class so it can access state of enclosing module class
82  class ServletKeyBindingBuilderImpl implements ServletModule.ServletKeyBindingBuilder {
83    private final List<String> uriPatterns;
84    private final UriPatternType uriPatternType;
85
86    private ServletKeyBindingBuilderImpl(List<String> uriPatterns, UriPatternType uriPatternType) {
87      this.uriPatterns = uriPatterns;
88      this.uriPatternType = uriPatternType;
89    }
90
91    public void with(Class<? extends HttpServlet> servletKey) {
92      with(Key.get(servletKey));
93    }
94
95    public void with(Key<? extends HttpServlet> servletKey) {
96      with(servletKey, new HashMap<String, String>());
97    }
98
99    public void with(HttpServlet servlet) {
100      with(servlet, new HashMap<String, String>());
101    }
102
103    public void with(Class<? extends HttpServlet> servletKey,
104        Map<String, String> contextParams) {
105      with(Key.get(servletKey), contextParams);
106    }
107
108    public void with(Key<? extends HttpServlet> servletKey,
109        Map<String, String> contextParams) {
110      with(servletKey, contextParams, null);
111    }
112
113    private void with(Key<? extends HttpServlet> servletKey,
114        Map<String, String> contextParams,
115        HttpServlet servletInstance) {
116      for (String pattern : uriPatterns) {
117        servletDefinitions.add(
118            new ServletDefinition(pattern, servletKey, UriPatternType.get(uriPatternType, pattern),
119                contextParams, servletInstance));
120      }
121    }
122
123    public void with(HttpServlet servlet,
124        Map<String, String> contextParams) {
125      Key<HttpServlet> servletKey = Key.get(HttpServlet.class, UniqueAnnotations.create());
126      servletInstanceEntries.add(new ServletInstanceBindingEntry(servletKey, servlet));
127      with(servletKey, contextParams, servlet);
128    }
129  }
130}
131