|
20 | 20 | * #L% |
21 | 21 | */ |
22 | 22 |
|
| 23 | +import act.Act; |
23 | 24 | import act.app.App; |
24 | 25 | import act.app.AppClassLoader; |
| 26 | +import act.conf.AppConfig; |
| 27 | +import act.inject.DependencyInjector; |
25 | 28 | import act.util.ClassNode; |
26 | 29 | import org.osgl.$; |
27 | 30 | import org.osgl.Osgl; |
| 31 | +import org.osgl.inject.BeanSpec; |
| 32 | +import org.osgl.inject.InjectException; |
28 | 33 | import org.osgl.inject.loader.AnnotatedElementLoader; |
29 | 34 | import org.osgl.inject.loader.ConfigurationValueLoader; |
30 | 35 | import org.osgl.inject.loader.TypedElementLoader; |
31 | 36 | import org.osgl.util.C; |
| 37 | +import org.osgl.util.S; |
32 | 38 |
|
33 | 39 | import javax.inject.Provider; |
34 | 40 | import java.lang.annotation.Annotation; |
| 41 | +import java.lang.reflect.Type; |
| 42 | +import java.util.Collection; |
35 | 43 | import java.util.List; |
| 44 | +import java.util.Map; |
36 | 45 |
|
37 | 46 | /** |
38 | 47 | * Integrate Genie with ActFramework |
@@ -80,10 +89,74 @@ private GenieProviders() { |
80 | 89 | @Override |
81 | 90 | public ConfigurationValueLoader get() { |
82 | 91 | return new ConfigurationValueLoader() { |
| 92 | + |
| 93 | + @Override |
| 94 | + public Object get() { |
| 95 | + final AppConfig appConfig = app().config(); |
| 96 | + DependencyInjector injector = Act.injector(); |
| 97 | + final String confKey = value().toString(); |
| 98 | + boolean isImpl = confKey.endsWith(".impl"); |
| 99 | + if (this.spec.isInstanceOf(Map.class)) { |
| 100 | + String prefix = confKey.toString(); |
| 101 | + Map<String, Object> confMap = appConfig.subSet(prefix); |
| 102 | + Map retVal = (Map) injector.get(spec.rawType()); |
| 103 | + List<Type> typeParams = spec.typeParams(); |
| 104 | + Type valType = null != typeParams && typeParams.size() > 1 ? typeParams.get(1) : Object.class; |
| 105 | + BeanSpec valSpec = BeanSpec.of(valType, injector); |
| 106 | + int pos = prefix.length() + 1; |
| 107 | + for (String key : confMap.keySet()) { |
| 108 | + Object val = confMap.get(key); |
| 109 | + retVal.put(key.substring(pos), null == val ? null : cast(S.string(val), valSpec, isImpl)); |
| 110 | + } |
| 111 | + return retVal; |
| 112 | + } else if (this.spec.isInstanceOf(Collection.class)) { |
| 113 | + Object val; |
| 114 | + try { |
| 115 | + val = appConfig.get(confKey); |
| 116 | + } catch (Exception e) { |
| 117 | + val = appConfig.rawConfiguration().get(confKey); |
| 118 | + } |
| 119 | + if (spec.isInstance(val)) { |
| 120 | + return val; |
| 121 | + } |
| 122 | + return cast(null == val ? null : val.toString(), spec, isImpl); |
| 123 | + } |
| 124 | + return super.get(); |
| 125 | + } |
| 126 | + |
83 | 127 | @Override |
84 | 128 | protected Object conf(String s) { |
85 | 129 | return app().config().get(s); |
86 | 130 | } |
| 131 | + |
| 132 | + private Object cast(String val, BeanSpec spec, boolean isImpl) { |
| 133 | + if (null == val) { |
| 134 | + return null; |
| 135 | + } |
| 136 | + if (spec.isInstanceOf(Collection.class)) { |
| 137 | + Collection retVal = (Collection<?>) Act.getInstance(spec.rawType()); |
| 138 | + List<Type> typeParams = spec.typeParams(); |
| 139 | + Type itemType = (null != typeParams && typeParams.size() > 0) ? typeParams.get(0) : Object.class; |
| 140 | + BeanSpec itemSpec = BeanSpec.of(itemType, Act.injector()); |
| 141 | + for (String itemVal : S.fastSplit(S.string(val), ",")) { |
| 142 | + retVal.add(cast(itemVal, itemSpec, isImpl)); |
| 143 | + } |
| 144 | + return retVal; |
| 145 | + } else { |
| 146 | + Class<?> type = spec.rawType(); |
| 147 | + if (type.isInstance(val)) { |
| 148 | + return val; |
| 149 | + } |
| 150 | + if (isImpl) { |
| 151 | + return $.newInstance(val, Act.app().classLoader()); |
| 152 | + } |
| 153 | + if ($.isSimpleType(type)) { |
| 154 | + return Act.app().resolverManager().resolve(val, type); |
| 155 | + } |
| 156 | + } |
| 157 | + throw new InjectException("Cannot cast value type[%s] to required type[%]", val.getClass(), spec); |
| 158 | + } |
| 159 | + |
87 | 160 | }; |
88 | 161 | } |
89 | 162 | }; |
|
0 commit comments