Home | History | Annotate | Download | only in servlet
      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  */
     16 package com.google.inject.servlet;
     17 
     18 import com.google.common.collect.Sets;
     19 import com.google.inject.Binder;
     20 import com.google.inject.Key;
     21 import com.google.inject.internal.UniqueAnnotations;
     22 
     23 import java.util.HashMap;
     24 import java.util.List;
     25 import java.util.Map;
     26 import java.util.Set;
     27 
     28 import javax.servlet.http.HttpServlet;
     29 
     30 /**
     31  * Builds the guice module that binds configured servlets, with their
     32  * wrapper ServletDefinitions. Is part of the binding EDSL. Very similar to
     33  * {@link com.google.inject.servlet.FiltersModuleBuilder}.
     34  *
     35  * @author Dhanji R. Prasanna (dhanji (at) gmail.com)
     36  */
     37 class ServletsModuleBuilder {
     38 
     39   private final Set<String> servletUris = Sets.newHashSet();
     40   private final Binder binder;
     41 
     42   public ServletsModuleBuilder(Binder binder) {
     43     this.binder = binder;
     44   }
     45 
     46   //the first level of the EDSL--
     47   public ServletModule.ServletKeyBindingBuilder serve(List<String> urlPatterns) {
     48     return new ServletKeyBindingBuilderImpl(urlPatterns, UriPatternType.SERVLET);
     49   }
     50 
     51   public ServletModule.ServletKeyBindingBuilder serveRegex(List<String> regexes) {
     52     return new ServletKeyBindingBuilderImpl(regexes, UriPatternType.REGEX);
     53   }
     54 
     55   //non-static inner class so it can access state of enclosing module class
     56   class ServletKeyBindingBuilderImpl implements ServletModule.ServletKeyBindingBuilder {
     57     private final List<String> uriPatterns;
     58     private final UriPatternType uriPatternType;
     59 
     60     private ServletKeyBindingBuilderImpl(List<String> uriPatterns, UriPatternType uriPatternType) {
     61       this.uriPatterns = uriPatterns;
     62       this.uriPatternType = uriPatternType;
     63     }
     64 
     65     public void with(Class<? extends HttpServlet> servletKey) {
     66       with(Key.get(servletKey));
     67     }
     68 
     69     public void with(Key<? extends HttpServlet> servletKey) {
     70       with(servletKey, new HashMap<String, String>());
     71     }
     72 
     73     public void with(HttpServlet servlet) {
     74       with(servlet, new HashMap<String, String>());
     75     }
     76 
     77     public void with(Class<? extends HttpServlet> servletKey,
     78         Map<String, String> initParams) {
     79       with(Key.get(servletKey), initParams);
     80     }
     81 
     82     public void with(Key<? extends HttpServlet> servletKey,
     83         Map<String, String> initParams) {
     84       with(servletKey, initParams, null);
     85     }
     86 
     87     private void with(Key<? extends HttpServlet> servletKey, Map<String, String> initParams,
     88         HttpServlet servletInstance) {
     89       for (String pattern : uriPatterns) {
     90         // Ensure two servlets aren't bound to the same pattern.
     91         if (!servletUris.add(pattern)) {
     92           binder.addError("More than one servlet was mapped to the same URI pattern: " + pattern);
     93         } else {
     94           binder.bind(Key.get(ServletDefinition.class, UniqueAnnotations.create())).toProvider(
     95               new ServletDefinition(pattern, servletKey, UriPatternType
     96                   .get(uriPatternType, pattern), initParams, servletInstance));
     97         }
     98       }
     99     }
    100 
    101     public void with(HttpServlet servlet,
    102         Map<String, String> initParams) {
    103       Key<HttpServlet> servletKey = Key.get(HttpServlet.class, UniqueAnnotations.create());
    104       binder.bind(servletKey).toInstance(servlet);
    105       with(servletKey, initParams, servlet);
    106     }
    107   }
    108 }
    109