View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.persistence.jpa.dao;
20  
21  import java.util.Map;
22  import java.util.Optional;
23  import java.util.concurrent.ConcurrentHashMap;
24  import javax.persistence.EntityManager;
25  import javax.persistence.EntityManagerFactory;
26  import org.apache.openjpa.jdbc.meta.MappingRepository;
27  import org.apache.openjpa.jdbc.sql.OracleDictionary;
28  import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
29  import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
30  import org.apache.openjpa.persistence.OpenJPAPersistence;
31  import org.apache.syncope.core.persistence.api.dao.DAO;
32  import org.apache.syncope.core.persistence.api.entity.Entity;
33  import org.apache.syncope.core.spring.ApplicationContextProvider;
34  import org.apache.syncope.core.spring.security.AuthContextUtils;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  import org.springframework.orm.jpa.EntityManagerFactoryUtils;
38  
39  public abstract class AbstractDAO<E extends Entity> implements DAO<E> {
40  
41      protected static final Logger LOG = LoggerFactory.getLogger(DAO.class);
42  
43      private static final Map<String, Boolean> IS_ORACLE = new ConcurrentHashMap<>();
44  
45      protected EntityManagerFactory entityManagerFactory() {
46          return EntityManagerFactoryUtils.findEntityManagerFactory(
47                  ApplicationContextProvider.getBeanFactory(), AuthContextUtils.getDomain());
48      }
49  
50      protected EntityManager entityManager() {
51          return Optional.ofNullable(EntityManagerFactoryUtils.getTransactionalEntityManager(entityManagerFactory())).
52                  orElseThrow(() -> new IllegalStateException(
53                  "Could not find EntityManager for domain " + AuthContextUtils.getDomain()));
54      }
55  
56      @Override
57      public void refresh(final E entity) {
58          entityManager().refresh(entity);
59      }
60  
61      @Override
62      public void detach(final E entity) {
63          entityManager().detach(entity);
64      }
65  
66      protected boolean isOracle() {
67          Boolean isOracle = IS_ORACLE.get(AuthContextUtils.getDomain());
68          if (isOracle == null) {
69              OpenJPAEntityManagerFactory emf = OpenJPAPersistence.cast(entityManagerFactory());
70              OpenJPAEntityManagerFactorySPI emfspi = (OpenJPAEntityManagerFactorySPI) OpenJPAPersistence.cast(emf);
71              isOracle = ((MappingRepository) emfspi.getConfiguration()
72                      .getMetaDataRepositoryInstance()).getDBDictionary() instanceof OracleDictionary;
73              IS_ORACLE.put(AuthContextUtils.getDomain(), isOracle);
74          }
75          return isOracle;
76      }
77  }