1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.core.framework.persistence.jpa; |
17 | |
|
18 | |
import java.io.File; |
19 | |
import java.io.FileInputStream; |
20 | |
import java.io.FileOutputStream; |
21 | |
import java.io.IOException; |
22 | |
import java.io.ObjectInputStream; |
23 | |
import java.io.ObjectOutputStream; |
24 | |
import java.util.Map; |
25 | |
|
26 | |
import javax.persistence.EntityManagerFactory; |
27 | |
import javax.persistence.spi.PersistenceUnitInfo; |
28 | |
|
29 | |
import org.apache.log4j.Logger; |
30 | |
import org.hibernate.ejb.Ejb3Configuration; |
31 | |
import org.hibernate.ejb.HibernatePersistence; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 0 | public class DevHibernatePersistence extends HibernatePersistence { |
39 | |
|
40 | 0 | private static final Logger LOG = Logger.getLogger(DevHibernatePersistence.class); |
41 | |
|
42 | |
private String serializationFilename; |
43 | |
|
44 | |
private boolean useSerialization; |
45 | |
|
46 | |
|
47 | |
|
48 | |
public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map) { |
49 | |
|
50 | |
|
51 | |
|
52 | 0 | EntityManagerFactory factory = null; |
53 | 0 | Ejb3Configuration cfg = null; |
54 | 0 | ObjectInputStream in = null; |
55 | |
try { |
56 | 0 | if (useSerialization && new File(serializationFilename).exists()) { |
57 | 0 | in = new ObjectInputStream(new FileInputStream(serializationFilename)); |
58 | 0 | cfg = (Ejb3Configuration) in.readObject(); |
59 | |
} else { |
60 | 0 | cfg = configure(info, map); |
61 | |
} |
62 | 0 | } catch (Exception e) { |
63 | 0 | LOG.error(e.getMessage(), e); |
64 | |
} finally { |
65 | 0 | if (in != null) { |
66 | |
try { |
67 | 0 | in.close(); |
68 | 0 | } catch (IOException e) { |
69 | 0 | LOG.error(e.getMessage(), e); |
70 | 0 | } |
71 | |
} |
72 | |
} |
73 | |
|
74 | 0 | if (cfg != null) { |
75 | 0 | factory = cfg.buildEntityManagerFactory(); |
76 | |
} else { |
77 | 0 | LOG.error("Error creating Ejb3Configuration"); |
78 | |
} |
79 | 0 | return factory; |
80 | |
} |
81 | |
|
82 | |
private Ejb3Configuration configure(PersistenceUnitInfo info, Map map) throws IOException { |
83 | 0 | Ejb3Configuration configured = new Ejb3Configuration().configure(info, map); |
84 | 0 | if (useSerialization) { |
85 | 0 | ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(serializationFilename)); |
86 | 0 | oos.writeObject(configured); |
87 | 0 | oos.close(); |
88 | |
} |
89 | 0 | return configured; |
90 | |
} |
91 | |
|
92 | |
public void setSerializationFilename(String serializationFilename) { |
93 | 0 | this.serializationFilename = serializationFilename; |
94 | 0 | } |
95 | |
|
96 | |
public void setUseSerialization(boolean useSerialization) { |
97 | 0 | this.useSerialization = useSerialization; |
98 | 0 | } |
99 | |
|
100 | |
} |