|
1
|
|
|
package io.mcarle.strix; |
|
2
|
|
|
|
|
3
|
|
|
import org.slf4j.Logger; |
|
4
|
|
|
import org.slf4j.LoggerFactory; |
|
5
|
|
|
|
|
6
|
|
|
import javax.persistence.EntityManager; |
|
7
|
|
|
import java.util.Map; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Provides methods to start and stop strix, as well as getting the {@link EntityManager}. |
|
11
|
|
|
*/ |
|
12
|
1 |
|
public final class Strix { |
|
13
|
|
|
|
|
14
|
1 |
|
private static final Logger LOG = LoggerFactory.getLogger(Strix.class); |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Starts strix with default settings |
|
18
|
|
|
*/ |
|
19
|
|
|
public static void startup() { |
|
20
|
1 |
|
startup(null, null); |
|
21
|
1 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Starts strix with additional properties, which will override the properties of the persistence unit from the |
|
25
|
|
|
* persistence.xml |
|
26
|
|
|
* |
|
27
|
|
|
* @param persistenceProperties Map of persistence unit names to a map of persistence properties. |
|
28
|
|
|
*/ |
|
29
|
|
|
public static void startup(Map<String, Map<String, String>> persistenceProperties) { |
|
30
|
1 |
|
startup(persistenceProperties, null); |
|
31
|
1 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Starts strix with a default persistence unit, which will be used in all methods annotated with |
|
35
|
|
|
* {@link io.mcarle.strix.annotation.Transactional}, when that annotation defines no persistence unit. |
|
36
|
|
|
* |
|
37
|
|
|
* @param defaultPersistenceUnit The default persistence unit which should be used, when no other unit is specified |
|
38
|
|
|
*/ |
|
39
|
|
|
public static void startup(String defaultPersistenceUnit) { |
|
40
|
1 |
|
startup(null, defaultPersistenceUnit); |
|
41
|
1 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Starts strix. Combines {@link #startup(Map)} and {@link #startup(String)}. |
|
45
|
|
|
* |
|
46
|
|
|
* @param persistenceProperties see {@link #startup(Map)} |
|
47
|
|
|
* @param defaultPersistenceUnit see {@link #startup(String)} |
|
48
|
|
|
*/ |
|
49
|
|
|
public static void startup( |
|
50
|
|
|
Map<String, Map<String, String>> persistenceProperties, |
|
51
|
|
|
String defaultPersistenceUnit |
|
52
|
|
|
) { |
|
53
|
1 |
|
LOG.info( |
|
54
|
1 |
|
"Starts strix with default persistence unit '{}' and custom persistence properties '{}'", |
|
55
|
1 |
|
defaultPersistenceUnit, |
|
56
|
1 |
|
persistenceProperties |
|
57
|
|
|
); |
|
58
|
1 |
|
StrixManager.startup(persistenceProperties, defaultPersistenceUnit); |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Shutdown strix and closes any open {@link javax.persistence.EntityManagerFactory} |
|
63
|
|
|
*/ |
|
64
|
|
|
public static void shutdown() { |
|
65
|
1 |
|
LOG.info("Shutdown strix"); |
|
66
|
1 |
|
StrixManager.shutdown(); |
|
67
|
1 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get the {@link EntityManager} for the current thread. |
|
71
|
|
|
* |
|
72
|
|
|
* @return If a transaction is active, returns the current {@link EntityManager}, otherwise {@code null} |
|
73
|
|
|
*/ |
|
74
|
|
|
public static EntityManager em() { |
|
75
|
1 |
|
LOG.trace("Get EntityManager"); |
|
76
|
1 |
|
return PersistenceManager.getEntityManager(); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|