View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.backend.jpa;
20  
21  import jakarta.persistence.EntityManager;
22  import jakarta.persistence.EntityManagerFactory;
23  import jakarta.persistence.PersistenceException;
24  
25  public class MCREntityManagerProvider {
26  
27      private static EntityManagerFactory factory;
28  
29      private static MCRSessionContext context;
30  
31      private static PersistenceException initException;
32  
33      public static EntityManagerFactory getEntityManagerFactory() {
34          return factory;
35      }
36  
37      public static EntityManager getCurrentEntityManager() {
38          if (context == null && initException != null) {
39              throw initException;
40          }
41          return context.getCurrentEntityManager();
42      }
43  
44      static void init(EntityManagerFactory factory) {
45          MCREntityManagerProvider.factory = factory;
46          context = new MCRSessionContext(factory);
47      }
48  
49      static void init(PersistenceException e) {
50          initException = e;
51      }
52  
53  }