| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (

This example Java source code file ( is included in the "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

filterchain, inject, ioexception, persistfilter, persistservice, servlet, servletexception, servletrequest, servletresponse, singleton, unitofwork

The Java example source code

 * Copyright (C) 2010 Google, Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.




import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

 * Apply this filter to enable the HTTP Request unit of work and to have
 * guice-persist manage the lifecycle of active units of work.
 * The filter automatically starts and stops the relevant {@link PersistService}
 * upon {@link javax.servlet.Filter#init(javax.servlet.FilterConfig)} and
 * {@link javax.servlet.Filter#destroy()} respectively.
 * <p> To be able to use the open session-in-view pattern (i.e. work per request),
 * register this filter <b>once in your Guice {@code ServletModule}. It is
 * important that you register this filter before any other filter.
 * For multiple providers, you should register this filter once per provider, inside
 * a private module for each persist module installed (this must be the same private
 * module where the specific persist module is itself installed).
 * <p>
 * Example configuration:
 * <pre>{@code
 *  public class MyModule extends ServletModule {
 *    public void configureServlets() {
 *      filter("/*").through(PersistFilter.class);
 *      serve("/index.html").with(MyHtmlServlet.class);
 *      // Etc.
 *    }
 *  }
 * }</pre>
 * <p>
 * This filter is thread safe and allows you to create injectors concurrently
 * and deploy multiple guice-persist modules within the same injector, or even
 * multiple injectors with persist modules withing the same JVM or web app.
 * <p>
 * This filter requires the Guice Servlet extension.
 * @author Dhanji R. Prasanna (
public final class PersistFilter implements Filter {
  private final UnitOfWork unitOfWork;
  private final PersistService persistService;

  public PersistFilter(UnitOfWork unitOfWork, PersistService persistService) {
    this.unitOfWork = unitOfWork;
    this.persistService = persistService;

  public void init(FilterConfig filterConfig) throws ServletException {

  public void destroy() {

  public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse,
      final FilterChain filterChain) throws IOException, ServletException {

    try {
      filterChain.doFilter(servletRequest, servletResponse);
    } finally {

Other Java examples (source code examples)

Here is a short list of links related to this Java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.