Skip to content

Commit 4fe1251

Browse files
committed
Allow specify the app env (e.g. PROD or DEV) when loading a Genie module
1 parent 8269498 commit 4fe1251

4 files changed

Lines changed: 120 additions & 0 deletions

File tree

src/main/java/act/Act.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import act.conf.ActConfLoader;
1010
import act.conf.ActConfig;
1111
import act.conf.AppConfig;
12+
import act.conf.ConfLoader;
1213
import act.controller.meta.ActionMethodMetaInfo;
1314
import act.controller.meta.CatchMethodMetaInfo;
1415
import act.controller.meta.InterceptorMethodMetaInfo;
@@ -172,6 +173,13 @@ public static boolean isDev() {
172173
return mode.isDev();
173174
}
174175

176+
/**
177+
* Return the current profile name
178+
*/
179+
public static String profile() {
180+
return ConfLoader.confSetName();
181+
}
182+
175183
public static ActConfig conf() {
176184
return conf;
177185
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package act.inject;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* Mark a class as dependency injector module
10+
*/
11+
@Target(ElementType.TYPE)
12+
@Retention(RetentionPolicy.RUNTIME)
13+
public @interface ModuleTag {
14+
}

src/main/java/act/inject/genie/GenieInjector.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import act.inject.ActProviders;
88
import act.inject.DependencyInjectionBinder;
99
import act.inject.DependencyInjectorBase;
10+
import act.inject.ModuleTag;
11+
import act.sys.Env;
1012
import act.util.AnnotatedClassFinder;
1113
import act.util.SubClassFinder;
1214
import org.osgl.$;
@@ -19,6 +21,7 @@
1921
import org.osgl.mvc.annotation.Param;
2022
import org.osgl.util.C;
2123
import org.osgl.util.E;
24+
import org.osgl.util.S;
2225

2326
import javax.inject.Inject;
2427
import javax.inject.Provider;
@@ -163,11 +166,24 @@ public Void apply(Class aClass, Provider provider) throws NotAppliedException, O
163166

164167
@SubClassFinder(value = Module.class, callOn = AppEventId.DEPENDENCY_INJECTOR_LOADED)
165168
public static void foundModule(Class<? extends Module> moduleClass) {
169+
if (!isModuleAllowed(moduleClass)) {
170+
return;
171+
}
166172
App app = App.instance();
167173
GenieInjector genieInjector = app.injector();
168174
genieInjector.addModule($.newInstance(moduleClass));
169175
}
170176

177+
@AnnotatedClassFinder(value = ModuleTag.class, callOn = AppEventId.DEPENDENCY_INJECTOR_LOADED, noAbstract = false)
178+
public static void foundTaggedModule(Class<?> taggedModuleClass) {
179+
if (!isModuleAllowed(taggedModuleClass)) {
180+
return;
181+
}
182+
App app = App.instance();
183+
GenieInjector genieInjector = app.injector();
184+
genieInjector.addModule(taggedModuleClass);
185+
}
186+
171187
@AnnotatedClassFinder(value = LoadValue.class, noAbstract = false, callOn = AppEventId.DEPENDENCY_INJECTOR_LOADED)
172188
public static void foundValueLoader(Class<? extends Annotation> valueLoader) {
173189
App app = App.instance();
@@ -191,4 +207,28 @@ public static void foundGenericTypedBeanLoader(Class<? extends GenericTypedBeanL
191207
}
192208
}
193209

210+
private static boolean isModuleAllowed(Class<?> moduleClass) {
211+
Env.Profile profile = moduleClass.getAnnotation(Env.Profile.class);
212+
if (null != profile) {
213+
return isProfileMatched(profile);
214+
}
215+
Env.Mode mode = moduleClass.getAnnotation(Env.Mode.class);
216+
if (null != mode) {
217+
return isModeMatched(mode);
218+
}
219+
return true;
220+
}
221+
222+
private static boolean isProfileMatched(Env.Profile profile) {
223+
boolean unless = profile.unless();
224+
String required = profile.value();
225+
return unless ^ S.eq(required, Act.profile());
226+
}
227+
228+
private static boolean isModeMatched(Env.Mode mode) {
229+
boolean unless = mode.unless();
230+
Act.Mode required = mode.value();
231+
return unless ^ (required == Act.mode());
232+
}
233+
194234
}

src/main/java/act/sys/Env.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package act.sys;
2+
3+
import act.Act;
4+
5+
import java.lang.annotation.ElementType;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
/**
11+
* Mark a module should only be loaded in certain environment
12+
*/
13+
public final class Env {
14+
15+
private Env() {}
16+
17+
/**
18+
* Used to mark a dependency injector module that
19+
* should be load only in specified profile
20+
*/
21+
@Target(ElementType.TYPE)
22+
@Retention(RetentionPolicy.RUNTIME)
23+
public @interface Profile {
24+
25+
/**
26+
* The profile specification
27+
*/
28+
String value();
29+
30+
/**
31+
* If unless is `true` then the module should be load
32+
* unless the current profile is the value specified
33+
*/
34+
boolean unless() default false;
35+
}
36+
37+
/**
38+
* Used to mark a dependency injector module
39+
* that should be load only in specified mode
40+
*/
41+
@Target(ElementType.TYPE)
42+
@Retention(RetentionPolicy.RUNTIME)
43+
public @interface Mode {
44+
45+
/**
46+
* The mode specification
47+
*/
48+
Act.Mode value();
49+
50+
/**
51+
* If unless is `true` then the module should be load
52+
* unless the current mode is the value specified
53+
*/
54+
boolean unless() default false;
55+
}
56+
57+
58+
}

0 commit comments

Comments
 (0)