Repository navigation
Expand file tree
/
Copy pathDbServiceManager.java
More file actions
217 lines (200 loc) · 8.54 KB
/
Copy pathDbServiceManager.java
File metadata and controls
217 lines (200 loc) · 8.54 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package act.app;
import act.Act;
import act.ActComponent;
import act.Destroyable;
import act.app.event.AppEventId;
import act.conf.AppConfig;
import act.db.*;
import act.db.util.SequenceNumberGenerator;
import act.db.util._SequenceNumberGenerator;
import act.event.AppEventListenerBase;
import act.util.ClassNode;
import act.util.General;
import org.osgl.$;
import org.osgl.exception.ConfigurationException;
import org.osgl.logging.LogManager;
import org.osgl.logging.Logger;
import org.osgl.util.C;
import org.osgl.util.E;
import org.rythmengine.utils.S;
import java.lang.annotation.Annotation;
import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;
@ActComponent
public class DbServiceManager extends AppServiceBase<DbServiceManager> implements DaoLocator {
private static Logger logger = LogManager.get(DbServiceManager.class);
public static final String DEFAULT = "default";
// map service id to service instance
private Map<String, DbService> serviceMap = C.newMap();
// map model class to dao class
private Map<Class<?>, Dao> modelDaoMap = C.newMap();
protected DbServiceManager(final App app) {
super(app, true);
initServices(app.config());
configureSequenceGenerator(app);
app.eventBus().bind(AppEventId.SINGLETON_PROVISIONED, new AppEventListenerBase() {
@Override
public void on(EventObject event) throws Exception {
ClassNode node = app.classLoader().classInfoRepository().node(Dao.class.getName());
node.findPublicNotAbstract(new $.Visitor<ClassNode>() {
private boolean isGeneral(Class c) {
Annotation[] aa = c.getDeclaredAnnotations();
for (Annotation a : aa) {
if (a instanceof General) {
return true;
}
}
return false;
}
@Override
public void visit(ClassNode classNode) throws $.Break {
Class<? extends Dao> daoType = $.classForName(classNode.name(), app.classLoader());
if (isGeneral(daoType)) {
return;
}
try {
Dao dao = $.cast(app.newInstance(daoType));
Class<?> modelType = dao.modelType();
DB db = modelType.getAnnotation(DB.class);
String svcId = null == db ? DEFAULT : db.value();
DbService dbService = dbService(svcId);
E.invalidConfigurationIf(null == dbService, "cannot find db service by id: %s", svcId);
dao = dbService.newDaoInstance(daoType);
modelDaoMap.put(modelType, dao);
} catch (Exception e) {
logger.warn(e, "error loading DAO: %s", daoType);
}
}
});
}
});
}
private void configureSequenceGenerator(final App app) {
app.jobManager().on(AppEventId.DEPENDENCY_INJECTOR_PROVISIONED, new Runnable() {
@Override
public void run() {
_SequenceNumberGenerator seqGen = app.config().sequenceNumberGenerator();
seqGen.configure(app.config(), DbServiceManager.this);
SequenceNumberGenerator.registerImpl(seqGen);
}
});
}
@Override
protected void releaseResources() {
Destroyable.Util.tryDestroyAll(serviceMap.values());
serviceMap.clear();
Destroyable.Util.tryDestroyAll(modelDaoMap.values());
modelDaoMap.clear();
}
@Override
public Dao dao(Class<?> modelClass) {
Dao dao = modelDaoMap.get(modelClass);
if (null == dao) {
String svcId = DEFAULT;
Annotation[] aa = modelClass.getDeclaredAnnotations();
for (Annotation a : aa) {
if (a instanceof DB) {
svcId = ((DB)a).value();
}
}
DbService dbService = dbService(svcId);
dao = dbService.defaultDao(modelClass);
modelDaoMap.put(modelClass, dao);
}
return dao;
}
public <T extends DbService> T dbService(String id) {
return (T)serviceMap.get(id);
}
public Iterable<DbService> registeredServices() {
return serviceMap.values();
}
private void initServices(AppConfig config) {
DbManager dbManager = Act.dbManager();
if (!dbManager.hasPlugin()) {
logger.warn("DB service not initialized: No DB plugin found");
return;
}
DbPlugin db = dbManager.theSolePlugin();
Map<String, Object> dbConf = config.subSet("db.");
if (dbConf.isEmpty()) {
if (null == db) {
logger.warn("DB service not intialized: need to specify default db service implementation");
return;
} else {
logger.warn("DB configuration not found. Will try to init default service with the sole db plugin: %s", db);
DbService svc = db.initDbService(DEFAULT, app(), new HashMap<String, Object>());
serviceMap.put(DEFAULT, svc);
return;
}
}
String firstInstance = null;
if (dbConf.containsKey("db.instances")) {
String instances = dbConf.get("db.instances").toString();
String[] sa = instances.split("[,\\s;:]+");
for (String dbId: sa) {
initService(dbId, dbConf);
}
firstInstance = sa[0];
}
if (serviceMap.containsKey(DEFAULT)) return;
// try init default service if conf found
String dbId = null;
if (dbConf.containsKey("db." + DEFAULT +".impl")) {
dbId = DEFAULT;
} else if (dbConf.containsKey("db.impl")) {
dbId = "";
}
if (null != dbId) {
initService(dbId, dbConf);
} else if (serviceMap.size() == 1) {
DbService svc = serviceMap.values().iterator().next();
serviceMap.put(DEFAULT, svc);
logger.warn("db service configuration not found. Use the sole one db service[%s] as default service", svc.id());
} else {
if (serviceMap.isEmpty()) {
if (null == db) {
logger.warn("DB service not initialized: need to specify default db service implementation");
} else {
logger.warn("DB configuration not found. Will try to init default service with the sole db plugin: %s", db);
Map<String, Object> svcConf = C.newMap();
String prefix = "db.";
for (String key : dbConf.keySet()) {
if (key.startsWith(prefix)) {
Object o = dbConf.get(key);
svcConf.put(key.substring(prefix.length()), o);
}
}
DbService svc = db.initDbService(DEFAULT, app(), svcConf);
serviceMap.put(DEFAULT, svc);
}
} else {
logger.warn("Default service not specified. Use the first db instance as default service: %s", firstInstance);
serviceMap.put(DEFAULT, serviceMap.get(firstInstance));
}
}
}
private void initService(String dbId, Map<String, Object> conf) {
Map<String, Object> svcConf = C.newMap();
String prefix = "db." + (S.empty(dbId) ? "" : dbId + ".");
for (String key : conf.keySet()) {
if (key.startsWith(prefix)) {
Object o = conf.get(key);
svcConf.put(key.substring(prefix.length()), o);
}
}
Object impl = svcConf.remove("impl");
String svcId = S.empty(dbId) ? DEFAULT : dbId;
if (null == impl) {
throw new ConfigurationException("Cannot init db service[%s]: implementation not specified", svcId);
}
DbPlugin plugin = Act.dbManager().plugin(impl.toString());
if (null == plugin) {
throw new ConfigurationException("Cannot init db service[%s]: implementation not found", svcId);
}
DbService svc = plugin.initDbService(dbId, app(), svcConf);
serviceMap.put(svcId, svc);
logger.info("db service[%s] initialized", svcId);
}
}