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.inject.Singleton;
24  import javax.persistence.EntityManagerFactory;
25  
26  import static org.apache.onami.persist.Preconditions.checkNotNull;
27  
28  /**
29   * Implementation of {@link PersistenceService} and {@link EntityManagerFactoryProvider} for
30   * application managed persistence units.
31   */
32  @Singleton
33  class ApplicationManagedEntityManagerFactoryProvider
34      implements EntityManagerFactoryProvider, PersistenceService
35  {
36  
37      /**
38       * Factory for creating the {@link EntityManagerFactory}.
39       */
40      private final EntityManagerFactoryFactory emfFactory;
41  
42      /**
43       * Currently active entity manager factory.
44       * Is {@code null} when the persistence service is not running.
45       */
46      private EntityManagerFactory emf;
47  
48      /**
49       * Constructor.
50       *
51       * @param emfFactory the factory for the  {@link EntityManagerFactory}. Must not be {@code null}.
52       */
53      @Inject
54      ApplicationManagedEntityManagerFactoryProvider( EntityManagerFactoryFactory emfFactory )
55      {
56          this.emfFactory = checkNotNull( emfFactory, "emfFactory is mandatory!" );
57      }
58  
59      /**
60       * {@inheritDoc}
61       */
62      // @Override
63      public EntityManagerFactory get()
64      {
65          if ( isRunning() )
66          {
67              return emf;
68          }
69  
70          throw new IllegalStateException( "PersistenceService is not running." );
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      // @Override
77      public void start()
78      {
79          if ( isRunning() )
80          {
81              throw new IllegalStateException( "PersistenceService is already running." );
82          }
83          emf = emfFactory.createApplicationManagedEntityManagerFactory();
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      // @Override
90      public boolean isRunning()
91      {
92          return null != emf;
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      // @Override
99      public void stop()
100     {
101         if ( isRunning() )
102         {
103             try
104             {
105                 emf.close();
106             }
107             finally
108             {
109                 emf = null;
110             }
111         }
112     }
113 
114 }