1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }