View Javadoc
1   package org.apache.onami.persist;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import javax.inject.Inject;
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  import java.io.IOException;
29  
30  import static org.apache.onami.persist.Preconditions.checkNotNull;
31  
32  /**
33   * Implementation of {@link PersistenceFilter}.
34   */
35  class PersistenceFilterImpl
36      implements PersistenceFilter
37  {
38  
39      /**
40       * Container of all known persistence services.
41       */
42      private final AllPersistenceServices allPersistenceServices;
43  
44      /**
45       * Container of all known units of work.
46       */
47      private final AllUnitsOfWork allUnitsOfWork;
48  
49      /**
50       * Constructor.
51       *
52       * @param allPersistenceServices container of all known persistence services.
53       * @param allUnitsOfWork container of all known units of work.
54       */
55      @Inject
56      PersistenceFilterImpl( AllPersistenceServices allPersistenceServices, AllUnitsOfWork allUnitsOfWork  )
57      {
58          this.allPersistenceServices = checkNotNull( allPersistenceServices, "allPersistenceServices is mandatory!" );
59          this.allUnitsOfWork = checkNotNull( allUnitsOfWork, "allUnitsOfWork is mandatory!" );
60      }
61  
62      /**
63       * {@inheritDoc}
64       */
65      // @Override
66      public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain )
67          throws IOException, ServletException
68      {
69          try
70          {
71              allUnitsOfWork.beginAllInactiveUnitsOfWork();
72              chain.doFilter( request, response );
73          }
74          finally
75          {
76              allUnitsOfWork.endAllUnitsOfWork();
77          }
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      // @Override
84      public void init( FilterConfig filterConfig )
85          throws ServletException
86      {
87          allPersistenceServices.startAllStoppedPersistenceServices();
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      // @Override
94      public void destroy()
95      {
96          allPersistenceServices.stopAllPersistenceServices();
97      }
98  
99  }