1 /*
2  * Copyright (C) 2010 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 
17 package com.google.inject.persist;
18 
19 import com.google.inject.Inject;
20 import com.google.inject.Singleton;
21 import java.io.IOException;
22 import javax.servlet.Filter;
23 import javax.servlet.FilterChain;
24 import javax.servlet.FilterConfig;
25 import javax.servlet.ServletException;
26 import javax.servlet.ServletRequest;
27 import javax.servlet.ServletResponse;
28 
29 /**
30  * Apply this filter to enable the HTTP Request unit of work and to have guice-persist manage the
31  * lifecycle of active units of work. The filter automatically starts and stops the relevant {@link
32  * PersistService} upon {@link javax.servlet.Filter#init(javax.servlet.FilterConfig)} and {@link
33  * javax.servlet.Filter#destroy()} respectively.
34  *
35  * <p>To be able to use the open session-in-view pattern (i.e. work per request), register this
36  * filter <b>once</b> in your Guice {@code ServletModule}. It is important that you register this
37  * filter before any other filter.
38  *
39  * <p>For multiple providers, you should register this filter once per provider, inside a private
40  * module for each persist module installed (this must be the same private module where the specific
41  * persist module is itself installed).
42  *
43  * <p>Example configuration:
44  *
45  * <pre>{@code
46  * public class MyModule extends ServletModule {
47  *   public void configureServlets() {
48  *     filter("/*").through(PersistFilter.class);
49  *
50  *     serve("/index.html").with(MyHtmlServlet.class);
51  *     // Etc.
52  *   }
53  * }
54  * }</pre>
55  *
56  * <p>This filter is thread safe and allows you to create injectors concurrently and deploy multiple
57  * guice-persist modules within the same injector, or even multiple injectors with persist modules
58  * withing the same JVM or web app.
59  *
60  * <p>This filter requires the Guice Servlet extension.
61  *
62  * @author Dhanji R. Prasanna (dhanji@gmail.com)
63  */
64 @Singleton
65 public final class PersistFilter implements Filter {
66   private final UnitOfWork unitOfWork;
67   private final PersistService persistService;
68 
69   @Inject
PersistFilter(UnitOfWork unitOfWork, PersistService persistService)70   public PersistFilter(UnitOfWork unitOfWork, PersistService persistService) {
71     this.unitOfWork = unitOfWork;
72     this.persistService = persistService;
73   }
74 
75   @Override
init(FilterConfig filterConfig)76   public void init(FilterConfig filterConfig) throws ServletException {
77     persistService.start();
78   }
79 
80   @Override
destroy()81   public void destroy() {
82     persistService.stop();
83   }
84 
85   @Override
doFilter( final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain)86   public void doFilter(
87       final ServletRequest servletRequest,
88       final ServletResponse servletResponse,
89       final FilterChain filterChain)
90       throws IOException, ServletException {
91 
92     unitOfWork.begin();
93     try {
94       filterChain.doFilter(servletRequest, servletResponse);
95     } finally {
96       unitOfWork.end();
97     }
98   }
99 }
100