Skip to content

Method: getProviderUtil()

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library 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 GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.model;
19:
20: import org.slf4j.Logger;
21: import org.slf4j.LoggerFactory;
22:
23: import java.io.IOException;
24: import java.util.Collections;
25: import java.util.HashSet;
26: import java.util.Map;
27: import java.util.Optional;
28: import java.util.Properties;
29: import java.util.Set;
30:
31: public class JOPAPersistenceProvider implements PersistenceProvider, ProviderUtil {
32:
33: private static final Logger LOG = LoggerFactory.getLogger(JOPAPersistenceProvider.class);
34:
35: private static final Set<EntityManagerFactoryImpl> EMFS = Collections.synchronizedSet(new HashSet<>());
36:
37: public JOPAPersistenceProvider() {
38: logVersionInfo();
39: }
40:
41: @Override
42: public EntityManagerFactoryImpl createEntityManagerFactory(String emName, Map<String, String> properties) {
43: final EntityManagerFactoryImpl emf = new EntityManagerFactoryImpl(properties, this::emfClosed);
44: EMFS.add(emf);
45: return emf;
46: }
47:
48: private static void logVersionInfo() {
49: try {
50: final Properties props = new Properties();
51: props.load(JOPAPersistenceProvider.class.getClassLoader().getResourceAsStream("jopa.properties"));
52: assert props.containsKey("cz.cvut.jopa.version");
53: assert props.containsKey("cz.cvut.jopa.build.timestamp");
54: LOG.info("This is JOPA {}, built on {}...", props.get("cz.cvut.jopa.version"), props.get("cz.cvut.jopa.build.timestamp"));
55: } catch (IOException e) {
56: LOG.warn("Unable to load properties file to log version info.", e);
57: }
58: }
59:
60: void emfClosed(EntityManagerFactoryImpl emf) {
61: EMFS.remove(emf);
62: }
63:
64: @Override
65: public ProviderUtil getProviderUtil() {
66: return this;
67: }
68:
69: @Override
70: public LoadState isLoaded(Object entity) {
71: final Optional<EntityManagerFactoryImpl> found = EMFS.stream().filter(emf -> emf.isLoaded(entity)).findAny();
72: return found.map(entityManagerFactory -> LoadState.LOADED).orElse(LoadState.UNKNOWN);
73: }
74:
75: @Override
76: public LoadState isLoadedWithReference(Object entity, String attributeName) {
77: return isLoadedWithoutReference(entity, attributeName);
78: }
79:
80: @Override
81: public LoadState isLoadedWithoutReference(Object entity, String attributeName) {
82: final Optional<EntityManagerFactoryImpl> found = EMFS.stream()
83: .filter(emf -> emf.isLoaded(entity, attributeName))
84: .findAny();
85: return found.map(entityManagerFactory -> LoadState.LOADED).orElse(LoadState.UNKNOWN);
86: }
87: }