-
Notifications
You must be signed in to change notification settings - Fork 158
/
SessionFactoryCreator.java
145 lines (122 loc) · 4.13 KB
/
SessionFactoryCreator.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package org.mamute.providers;
import java.net.URL;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.annotation.Priority;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Alternative;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.interceptor.Interceptor;
import javax.validation.ValidatorFactory;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.mamute.model.Answer;
import org.mamute.model.AnswerInformation;
import org.mamute.model.Comment;
import org.mamute.model.Flag;
import org.mamute.model.LoginMethod;
import org.mamute.model.News;
import org.mamute.model.NewsInformation;
import org.mamute.model.NewsletterSentLog;
import org.mamute.model.Question;
import org.mamute.model.QuestionInformation;
import org.mamute.model.ReputationEvent;
import org.mamute.model.Tag;
import org.mamute.model.TagPage;
import org.mamute.model.User;
import org.mamute.model.UserSession;
import org.mamute.model.Vote;
import org.mamute.model.watch.Watcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import br.com.caelum.vraptor.environment.Environment;
@ApplicationScoped
@Alternative
@Priority(Interceptor.Priority.APPLICATION)
public class SessionFactoryCreator {
public static final String JODA_TIME_TYPE = "org.jadira.usertype.dateandtime.joda.PersistentDateTime";
public static final String DATABASE_PROPERTY = "mamute.database";
private static final Logger LOGGER = LoggerFactory
.getLogger(SessionFactoryCreator.class);
private Configuration cfg;
private SessionFactory factory;
private Environment env;
private ValidatorFactory vf;
@Deprecated
public SessionFactoryCreator() {
}
@Inject
public SessionFactoryCreator(Environment env, ValidatorFactory vf) {
this.env = env;
this.vf = vf;
}
@PostConstruct
public void init() {
//TODO: should probably be in a config file of some sort rather than a system prop
String db = System.getProperty(DATABASE_PROPERTY, "mysql");
String hibernateCfg = "/hibernate-" + db + ".cfg.xml";
URL xml = env.getResource(hibernateCfg);
LOGGER.info("Loading hibernate xml from " + xml);
this.cfg = new Configuration().configure(xml);
if (this.vf != null) {
Map<Object, Object> properties = cfg.getProperties();
properties.put("javax.persistence.validation.factory", this.vf);
}
String url = System.getenv("JDBC_URL");
if (url != null) {
String user = System.getenv("USER");
String password = System.getenv("PASSWORD");
LOGGER.info("reading database config from environment: " + url);
cfg.setProperty("hibernate.connection.url", url);
cfg.setProperty("hibernate.connection.username", user);
cfg.setProperty("hibernate.connection.password", password);
}
cfg.addAnnotatedClass(User.class);
cfg.addAnnotatedClass(Question.class);
cfg.addAnnotatedClass(AnswerInformation.class);
cfg.addAnnotatedClass(Answer.class);
cfg.addAnnotatedClass(Tag.class);
cfg.addAnnotatedClass(Vote.class);
cfg.addAnnotatedClass(Comment.class);
cfg.addAnnotatedClass(QuestionInformation.class);
cfg.addAnnotatedClass(Flag.class);
cfg.addAnnotatedClass(LoginMethod.class);
cfg.addAnnotatedClass(UserSession.class);
cfg.addAnnotatedClass(Watcher.class);
cfg.addAnnotatedClass(ReputationEvent.class);
cfg.addAnnotatedClass(News.class);
cfg.addAnnotatedClass(NewsInformation.class);
cfg.addAnnotatedClass(NewsletterSentLog.class);
cfg.addAnnotatedClass(TagPage.class);
this.factory = cfg.buildSessionFactory();
}
@Produces
@javax.enterprise.context.ApplicationScoped
public SessionFactory getInstance() {
return factory;
}
void destroy(@Disposes SessionFactory factory) {
if (!factory.isClosed()) {
factory.close();
}
factory = null;
}
public void dropAndCreate() {
destroy(this.factory);
new SchemaExport(cfg).drop(true, true);
new SchemaExport(cfg).create(true, true);
init();
}
public void drop() {
factory.close();
factory = null;
new SchemaExport(cfg).drop(true, true);
init();
}
public Configuration getCfg() {
return cfg;
}
}