Skip to content

Commit aaf3b50

Browse files
committed
1 parent 1ec9448 commit aaf3b50

7 files changed

Lines changed: 203 additions & 17 deletions

File tree

pom.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
********************************************
1919
* version history
2020
********************************************
21+
1.1.0
22+
- #136 Allow `@With` annotation to be used on specific handler method
23+
- #135 Provides SqlDbService as a base class for all SQL based DbService solution
24+
- upgrade FastJson to 1.2.31
25+
2126
1.0.7
2227
- #70 Make it able to configure the number of network io threads and work threads
2328
- #120 configuration render.json.output_charset.enabled default value shall be false
@@ -117,7 +122,7 @@
117122
<groupId>org.actframework</groupId>
118123
<artifactId>act</artifactId>
119124
<packaging>jar</packaging>
120-
<version>1.0.8-SNAPSHOT</version>
125+
<version>1.1.0-SNAPSHOT</version>
121126

122127
<name>ACT Framework</name>
123128
<description>The ACT full stack MVC framework</description>
@@ -155,7 +160,7 @@
155160
<commons-codec.version>1.10</commons-codec.version>
156161
<commons-fileupload.version>1.3.2</commons-fileupload.version>
157162
<ecj.version>4.6.1</ecj.version>
158-
<fastjson.version>1.2.30</fastjson.version>
163+
<fastjson.version>1.2.31</fastjson.version>
159164
<bval.version>1.1.2</bval.version>
160165
<javax.inject.version>1</javax.inject.version>
161166
<javax.mail.version>1.5.0-b01</javax.mail.version>

src/main/java/act/app/DbServiceManager.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public class DbServiceManager extends AppServiceBase<DbServiceManager> implement
6161
@Inject
6262
public DbServiceManager(final App app) {
6363
super(app);
64+
EntityClassRepository.init(app);
6465
initServices(app.config());
6566
configureSequenceGenerator(app);
6667
app.eventBus().bind(AppEventId.SINGLETON_PROVISIONED, new AppEventListenerBase() {
@@ -154,14 +155,14 @@ private void initServices(AppConfig config) {
154155
return;
155156
}
156157
DbPlugin db = dbManager.theSolePlugin();
157-
Map<String, Object> dbConf = config.subSet("db.");
158+
Map<String, String> dbConf = config.subSet("db.");
158159
if (dbConf.isEmpty()) {
159160
if (null == db) {
160161
logger.warn("DB service not intialized: need to specify default db service implementation");
161162
return;
162163
} else {
163164
logger.warn("DB configuration not found. Will try to init default service with the sole db plugin: %s", db);
164-
DbService svc = db.initDbService(DEFAULT, app(), new HashMap<String, Object>());
165+
DbService svc = db.initDbService(DEFAULT, app(), new HashMap<String, String>());
165166
serviceMap.put(DEFAULT, svc);
166167
return;
167168
}
@@ -196,11 +197,11 @@ private void initServices(AppConfig config) {
196197
logger.warn("DB service not initialized: need to specify default db service implementation");
197198
} else {
198199
logger.warn("DB configuration not found. Will try to init default service with the sole db plugin: %s", db);
199-
Map<String, Object> svcConf = C.newMap();
200+
Map<String, String> svcConf = C.newMap();
200201
String prefix = "db.";
201202
for (String key : dbConf.keySet()) {
202203
if (key.startsWith(prefix)) {
203-
Object o = dbConf.get(key);
204+
String o = dbConf.get(key);
204205
svcConf.put(key.substring(prefix.length()), o);
205206
}
206207
}
@@ -214,12 +215,12 @@ private void initServices(AppConfig config) {
214215
}
215216
}
216217

217-
private void initService(String dbId, Map<String, Object> conf) {
218-
Map<String, Object> svcConf = C.newMap();
218+
private void initService(String dbId, Map<String, String> conf) {
219+
Map<String, String> svcConf = C.newMap();
219220
String prefix = "db." + (S.empty(dbId) ? "" : dbId + ".");
220221
for (String key : conf.keySet()) {
221222
if (key.startsWith(prefix)) {
222-
Object o = conf.get(key);
223+
String o = conf.get(key);
223224
svcConf.put(key.substring(prefix.length()), o);
224225
}
225226
}

src/main/java/act/db/DbPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public boolean equals(Object obj) {
4747
return obj == this || null != obj && getClass() == obj.getClass();
4848
}
4949

50-
public abstract DbService initDbService(String id, App app, Map<String, Object> conf);
50+
public abstract DbService initDbService(String id, App app, Map<String, String> conf);
5151

5252
public void afterDbServiceLoaded() {
5353
}

src/main/java/act/db/DbService.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,52 @@
2020
* #L%
2121
*/
2222

23+
import act.Act;
2324
import act.app.App;
2425
import act.app.AppHolderBase;
26+
import org.osgl.logging.LogManager;
27+
import org.osgl.logging.Logger;
2528
import org.osgl.util.E;
2629

2730
import java.lang.annotation.Annotation;
2831
import java.lang.reflect.Field;
32+
import java.util.Set;
2933

3034
public abstract class DbService extends AppHolderBase<DbService> {
3135

36+
protected static final Logger _logger = LogManager.get(DbService.class);
37+
3238
private String id;
3339

40+
/**
41+
* Construct a `DbService` with service ID and the current application
42+
* @param id the service ID
43+
* @param app the current application
44+
*/
3445
public DbService(String id, App app) {
3546
super(app);
3647
E.NPE(id);
3748
this.id = id;
3849
}
3950

51+
/**
52+
* Returns the DB ID of the service
53+
* @return the service ID
54+
*/
4055
public String id() {
4156
return id;
4257
}
4358

59+
/**
60+
* Returns all model classes registered on this datasource
61+
*
62+
* @return model classes talk to this datasource
63+
*/
64+
public Set<Class> modelClasses() {
65+
EntityClassRepository repo = Act.getInstance(EntityClassRepository.class);
66+
return repo.modelClasses(id());
67+
}
68+
4469
@Override
4570
protected abstract void releaseResources();
4671

@@ -50,6 +75,15 @@ public String id() {
5075

5176
public abstract Class<? extends Annotation> entityAnnotationType();
5277

78+
/**
79+
* Utility method to find the ID type from Model type. Could be used by sub class on {@link #defaultDao(Class)}
80+
* method implementation
81+
*
82+
* @param modelType the model type
83+
* @param idAnnotation the ID annotation
84+
* @return the ID type
85+
*/
86+
@SuppressWarnings("unused")
5387
protected static Class<?> findModelIdTypeByAnnotation(Class<?> modelType, Class<? extends Annotation> idAnnotation) {
5488
Class<?> curClass = modelType;
5589
while (Object.class != curClass && null != curClass) {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package act.db;
2+
3+
/*-
4+
* #%L
5+
* ACT Framework
6+
* %%
7+
* Copyright (C) 2014 - 2017 ActFramework
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
import act.app.App;
24+
import act.app.AppServiceBase;
25+
import act.app.DbServiceManager;
26+
27+
import java.util.HashSet;
28+
import java.util.Set;
29+
import java.util.concurrent.ConcurrentHashMap;
30+
import java.util.concurrent.ConcurrentMap;
31+
32+
/**
33+
* Keep track of SQL entity model classes
34+
*/
35+
public class EntityClassRepository extends AppServiceBase<EntityClassRepository> {
36+
37+
// map model class set to db service ID
38+
private ConcurrentMap<String, Set<Class>> modelClasses = new ConcurrentHashMap<>();
39+
40+
public EntityClassRepository(App app) {
41+
super(app);
42+
}
43+
44+
public void registerModelClass(Class<?> modelClass) {
45+
DB db = modelClass.getAnnotation(DB.class);
46+
String dbId = null == db ? DbServiceManager.DEFAULT : db.value();
47+
registerModelClass(dbId, modelClass);
48+
}
49+
50+
private void registerModelClass(String dbId, Class<?> modelClass) {
51+
Set<Class> set = modelClasses.get(dbId);
52+
if (null == set) {
53+
set = new HashSet<>();
54+
Set<Class> set0 = modelClasses.putIfAbsent(dbId, set);
55+
if (null != set0) {
56+
set.addAll(set0);
57+
}
58+
}
59+
set.add(modelClass);
60+
}
61+
62+
public Set<Class> modelClasses(String dbId) {
63+
return modelClasses.get(dbId);
64+
}
65+
66+
@Override
67+
protected void releaseResources() {
68+
modelClasses.clear();
69+
}
70+
71+
public static void init(App app) {
72+
new EntityClassRepository(app);
73+
}
74+
75+
}

src/main/java/act/util/AnnotationUtil.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,19 @@ public static <T extends Annotation> T declaredAnnotation(Class c, Class<T> anno
4747
* @return the annotation tagged on annotation of type `tagClass`
4848
*/
4949
public static <T extends Annotation> T annotation(Annotation annotation, Class<T> tagClass) {
50-
Class<?> c = classOf(annotation);
51-
for (Annotation a : c.getAnnotations()) {
52-
if (tagClass.isInstance(a)) {
53-
return (T) a;
54-
}
55-
}
56-
return null;
50+
Class<?> c = annotation.annotationType();
51+
return c.getAnnotation(tagClass);
5752
}
5853

5954
/**
55+
* **Note** this method is deprecated. Please use {@link Annotation#annotationType} instead
56+
*
6057
* Returns the class of an annotation instance
6158
* @param annotation the annotation instance
6259
* @param <T> the generic type of the annotation
6360
* @return the real annotation class
6461
*/
62+
@Deprecated
6563
public static <T extends Annotation> Class<T> classOf(Annotation annotation) {
6664
Class<?>[] ca = annotation.getClass().getInterfaces();
6765
for (Class<?> c: ca) {
@@ -71,4 +69,16 @@ public static <T extends Annotation> Class<T> classOf(Annotation annotation) {
7169
}
7270
throw new UnexpectedException("!!!");
7371
}
72+
73+
public static <T extends Annotation> T getAnnotation(Class<?> targetClass, Class<T> annotationClass) {
74+
if (Object.class == targetClass) {
75+
return null;
76+
}
77+
T annotation = targetClass.getAnnotation(annotationClass);
78+
if (null != annotation) {
79+
return annotation;
80+
}
81+
targetClass = targetClass.getSuperclass();
82+
return null != targetClass ? getAnnotation(targetClass, annotationClass) : null;
83+
}
7484
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package act.util;
2+
3+
/*-
4+
* #%L
5+
* ACT Framework
6+
* %%
7+
* Copyright (C) 2014 - 2017 ActFramework
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
import act.TestBase;
24+
import org.junit.Test;
25+
import org.osgl.mvc.annotation.GetAction;
26+
27+
import javax.inject.Singleton;
28+
import java.lang.annotation.Retention;
29+
import java.lang.annotation.RetentionPolicy;
30+
31+
public class AnnotationUtilTest extends TestBase {
32+
33+
@Singleton
34+
static class Base {}
35+
36+
@Async
37+
static class Derived extends Base {}
38+
39+
@Test
40+
public void testClassOf() {
41+
Async async = Derived.class.getAnnotation(Async.class);
42+
eq(Async.class, AnnotationUtil.classOf(async));
43+
eq(Async.class, async.annotationType());
44+
}
45+
46+
@Test
47+
public void testAnnotation() {
48+
Async async = Derived.class.getAnnotation(Async.class);
49+
Retention retention = AnnotationUtil.annotation(async, Retention.class);
50+
eq(RetentionPolicy.RUNTIME, retention.value());
51+
}
52+
53+
@Test
54+
public void testGetAnnotation() {
55+
Singleton singleton = AnnotationUtil.getAnnotation(Derived.class, Singleton.class);
56+
assertNotNull(singleton);
57+
GetAction getAction = AnnotationUtil.getAnnotation(Derived.class, GetAction.class);
58+
assertNull(getAction);
59+
}
60+
61+
}

0 commit comments

Comments
 (0)