1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.ogf.graap.wsag.server.persistence;
36
37 import java.text.MessageFormat;
38
39 import javax.persistence.EntityManager;
40 import javax.persistence.EntityManagerFactory;
41 import javax.persistence.Persistence;
42
43 import org.apache.log4j.Logger;
44 import org.ogf.graap.wsag.api.logging.LogMessage;
45
46
47
48
49 public class EmfRegistry
50 {
51
52 private static final Logger LOG = Logger.getLogger( EmfRegistry.class );
53
54
55
56
57 public static final String WSAG4J_DATAPATH = "org.wsag4j.persistence.datapath";
58
59
60
61
62 public static final String WSAG4J_DATAPATH_DEFAULT = System.getProperty( "java.io.tmpdir" )
63 + "/wsag-data";
64
65
66
67
68
69
70
71
72
73
74
75 public static final String PERSISTENCE_MODE_MEM = "MEM";
76
77
78
79
80 public static final String PERSISTENCE_MODE_MEM_PU_NAME = "wsag4j_mem";
81
82
83
84
85
86
87
88
89
90
91 public static final String PERSISTENCE_MODE_MEM_SERVER = "MEM_SERVER";
92
93
94
95
96
97 public static final String PERSISTENCE_MODE_MEM_SERVER_PU_NAME = "wsag4j_mem_server";
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 public static final String PERSISTENCE_MODE_FILE = "FILE";
115
116
117
118
119 public static final String PERSISTENCE_MODE_FILE_PU_NAME = "wsag4j_file";
120
121 private static EntityManagerFactory emf;
122
123 private static String persistenceMode = PERSISTENCE_MODE_FILE;
124
125
126
127
128
129
130
131 public static void setPersistenceMode( String persistenceMode )
132 {
133 EmfRegistry.persistenceMode = persistenceMode;
134
135 LOG.debug( LogMessage.getMessage( "Set wsag4j persistence mode to: {0}", persistenceMode ) );
136 }
137
138
139
140
141
142
143 private static synchronized EntityManagerFactory getEntityManagerFactory()
144 {
145
146
147 if ( emf == null )
148 {
149 createEmf();
150
151 LOG.info( "No EMF instance exists. Created new instance." );
152
153 return emf;
154 }
155
156
157 if ( !emf.isOpen() )
158 {
159 createEmf();
160
161 if ( LOG.isInfoEnabled() )
162 {
163 LOG.info( "Former EMF instance is/was closed. Created a new instance." );
164 }
165 }
166
167 return emf;
168 }
169
170 private static void createEmf()
171 {
172
173
174
175
176
177
178
179
180
181
182 String configuredPersistenceMode = System.getProperty( "wsag4j.persistence.mode" );
183 if ( configuredPersistenceMode != null )
184 {
185 persistenceMode = configuredPersistenceMode;
186
187 if ( LOG.isInfoEnabled() )
188 {
189 LOG.info( LogMessage.getMessage(
190 "Set wsag4j persistence mode to: {0} (by System.getProperty)", persistenceMode ) );
191 }
192 }
193
194 if ( persistenceMode.equals( PERSISTENCE_MODE_MEM ) )
195 {
196 emf = Persistence.createEntityManagerFactory( PERSISTENCE_MODE_MEM_PU_NAME );
197 }
198 if ( persistenceMode.equals( PERSISTENCE_MODE_MEM_SERVER ) )
199 {
200 emf = Persistence.createEntityManagerFactory( PERSISTENCE_MODE_MEM_SERVER_PU_NAME );
201 }
202 else if ( persistenceMode.equals( PERSISTENCE_MODE_FILE ) )
203 {
204
205 String configuredDataPath =
206 System.getProperties().getProperty( WSAG4J_DATAPATH, WSAG4J_DATAPATH_DEFAULT );
207 if ( !"".equals( configuredDataPath ) )
208 {
209
210 if ( !( configuredDataPath.endsWith( "/" ) || configuredDataPath.endsWith( "\\" ) ) )
211 {
212 configuredDataPath += "/";
213 }
214
215 LOG.debug( LogMessage.getMessage( "WSAG4J persistence data path configuration: {0}",
216 configuredDataPath ) );
217 }
218 else
219 {
220 String message =
221 MessageFormat.format(
222 "No wsag4j persistence data path configured. Use default location {0}",
223 WSAG4J_DATAPATH_DEFAULT );
224 LOG.warn( message );
225 }
226
227
228
229
230
231 System.setProperty( WSAG4J_DATAPATH, configuredDataPath );
232
233
234
235
236 emf = Persistence.createEntityManagerFactory( PERSISTENCE_MODE_FILE_PU_NAME );
237 }
238 else
239 {
240 LOG.error( "No persistence operation mode configured." );
241 }
242 }
243
244
245
246
247
248
249 public static EntityManager getEntityManager()
250 {
251 return EmfRegistry.getEntityManagerFactory().createEntityManager();
252 }
253
254
255
256
257
258
259 public static String printInfo()
260 {
261 return MessageFormat.format(
262 "EmfRegistry [EmfRegistry.persistenceMode: ''{0}'', WSAG4J_DATAPATH: ''{1}'']",
263 EmfRegistry.persistenceMode, WSAG4J_DATAPATH );
264 }
265
266
267
268
269 public static synchronized void finalizeEmfRegistry()
270 {
271 if ( emf != null )
272 {
273 synchronized ( emf )
274 {
275 if ( emf.isOpen() )
276 {
277 emf.close();
278 }
279 emf = null;
280 }
281 }
282 }
283 }