42
42
43
43
import javax .el .ELResolver ;
44
44
import javax .el .ExpressionFactory ;
45
+ import javax .enterprise .context .ContextNotActiveException ;
45
46
import javax .enterprise .context .spi .Context ;
46
47
import javax .enterprise .context .spi .Contextual ;
47
48
import javax .enterprise .context .spi .CreationalContext ;
57
58
import java .lang .reflect .Method ;
58
59
import java .lang .reflect .Type ;
59
60
import java .util .*;
61
+ import java .util .function .Function ;
60
62
63
+ import static java .lang .String .format ;
61
64
import static java .lang .System .getProperty ;
65
+ import static java .util .Collections .unmodifiableList ;
62
66
import static java .util .Objects .requireNonNull ;
63
67
import static java .util .ServiceLoader .load ;
64
68
import static org .geektimes .commons .lang .util .ArrayUtils .iterate ;
@@ -101,13 +105,14 @@ public class StandardBeanManager implements BeanManager, Instance<Object> {
101
105
102
106
private final ObserverMethodManager observerMethodsManager ;
103
107
108
+ private final DisposerMethodManager disposerMethodManager ;
109
+
104
110
private final InterceptorManager interceptorManager ;
105
111
106
112
private final Map <Class <? extends Extension >, Extension > extensions ;
107
113
108
- private final Map <String , AnnotatedType <?>> beanTypes ;
109
114
110
- private final DisposerMethodManager disposerMethodManager ;
115
+ private final Map < String , AnnotatedType <?>> beanTypes ;
111
116
112
117
private final Map <String , AnnotatedType <?>> alternativeTypes ;
113
118
@@ -127,9 +132,12 @@ public class StandardBeanManager implements BeanManager, Instance<Object> {
127
132
private final List <Bean <?>> genericBeans ;
128
133
129
134
130
- private Set <Throwable > definitionErrors ;
135
+ private final Set <DefinitionException > definitionErrors ;
131
136
132
- private Set <Throwable > deploymentProblems ;
137
+ private final Set <DeploymentException > deploymentProblems ;
138
+
139
+
140
+ private final Map <Class <? extends Annotation >, Context > contexts ;
133
141
134
142
public StandardBeanManager () {
135
143
this .classLoader = ClassLoaderUtils .getClassLoader (getClass ());
@@ -154,10 +162,13 @@ public StandardBeanManager() {
154
162
155
163
this .definitionErrors = new LinkedHashSet <>();
156
164
this .deploymentProblems = new LinkedHashSet <>();
165
+
166
+ this .contexts = new HashMap <>();
157
167
}
158
168
159
169
@ Override
160
170
public Object getReference (Bean <?> bean , Type beanType , CreationalContext <?> ctx ) {
171
+ Class <? extends Annotation > scope = bean .getScope ();
161
172
// TODO
162
173
return null ;
163
174
}
@@ -333,8 +344,11 @@ public int getInterceptorBindingHashCode(Annotation interceptorBinding) {
333
344
334
345
@ Override
335
346
public Context getContext (Class <? extends Annotation > scopeType ) {
336
- // TODO
337
- return null ;
347
+ Context context = contexts .get (scopeType );
348
+ if (!context .isActive ()){
349
+ throw new ContextNotActiveException (format ("The context[scope : @%s] is not active!" ,scopeType .getName ()));
350
+ }
351
+ return context ;
338
352
}
339
353
340
354
@ Override
@@ -487,16 +501,17 @@ public void initialize() {
487
501
}
488
502
489
503
public void addDefinitionError (Throwable t ) {
490
- this .definitionErrors .add (t );
504
+ this .definitionErrors .add (new DefinitionException ( t ) );
491
505
}
492
506
493
507
/**
494
508
* Registers a deployment problem with the container, causing the container to abort deployment
495
509
* after all observers have been notified.
510
+ *
496
511
* @param t {@link Throwable}
497
512
*/
498
513
public void addDeploymentProblem (Throwable t ) {
499
- this .deploymentProblems .add (t );
514
+ this .deploymentProblems .add (new DeploymentException ( t ) );
500
515
}
501
516
502
517
private void initializeBeanArchiveManager () {
@@ -717,7 +732,7 @@ public void registerBean(Bean<?> bean) {
717
732
registerInterceptorBean ((Interceptor ) bean );
718
733
} else if (bean instanceof Decorator ) {
719
734
registerDecoratorBean ((Decorator ) bean );
720
- } else if (bean instanceof ManagedBean ){
735
+ } else if (bean instanceof ManagedBean ) {
721
736
registerManagedBean ((ManagedBean ) bean );
722
737
} else {
723
738
genericBeans .add (bean );
@@ -958,7 +973,7 @@ private void registerObserverMethods(Object beanInstance) {
958
973
observerMethodsManager .registerObserverMethods (beanInstance );
959
974
}
960
975
961
- public void registerObserverMethod (ObserverMethod <?> observerMethod ){
976
+ public void registerObserverMethod (ObserverMethod <?> observerMethod ) {
962
977
observerMethodsManager .registerObserverMethod (observerMethod );
963
978
}
964
979
@@ -1054,4 +1069,60 @@ private Set<AnnotatedType<?>> createAnnotatedTypes(Iterable<Class<?>> classes) {
1054
1069
return annotatedTypes ;
1055
1070
}
1056
1071
1057
- }
1072
+ public <T > AnnotatedType <T > getAnnotatedType (Class <T > type , String id ) {
1073
+ return id == null ? getAnnotatedType (type .getName ()) : getAnnotatedType (id );
1074
+ }
1075
+
1076
+ public <T > AnnotatedType <T > getAnnotatedType (String id ) {
1077
+ AnnotatedType <T > annotatedType = getAnnotatedType (id , beanTypes ::get );
1078
+
1079
+ if (annotatedType == null ) {
1080
+ annotatedType = getAnnotatedType (id , alternativeTypes ::get );
1081
+ }
1082
+
1083
+ if (annotatedType == null ) {
1084
+ annotatedType = getAnnotatedType (id , interceptorTypes ::get );
1085
+ }
1086
+
1087
+ if (annotatedType == null ) {
1088
+ annotatedType = getAnnotatedType (id , decoratorTypes ::get );
1089
+ }
1090
+
1091
+ return annotatedType ;
1092
+ }
1093
+
1094
+ private <T > AnnotatedType <T > getAnnotatedType (String id , Function <Object , AnnotatedType <?>> typeMapping ) {
1095
+ return (AnnotatedType <T >) typeMapping .apply (id );
1096
+ }
1097
+
1098
+ public <T > Iterable <AnnotatedType <T >> getAnnotatedTypes (Class <T > type ) {
1099
+ List <AnnotatedType <T >> annotatedTypes = new LinkedList <>();
1100
+ addAnnotatedType (type , beanTypes , annotatedTypes );
1101
+ addAnnotatedType (type , alternativeTypes , annotatedTypes );
1102
+ addAnnotatedType (type , interceptorTypes , annotatedTypes );
1103
+ addAnnotatedType (type , decoratorTypes , annotatedTypes );
1104
+ return unmodifiableList (annotatedTypes );
1105
+ }
1106
+
1107
+ private <T > void addAnnotatedType (Class <T > type , Map <String , AnnotatedType <?>> types ,
1108
+ List <AnnotatedType <T >> annotatedTypes ) {
1109
+ AnnotatedType <T > annotatedType = getAnnotatedType (type .getName (), types ::get );
1110
+ if (annotatedType != null ) {
1111
+ annotatedTypes .add (annotatedType );
1112
+ }
1113
+ }
1114
+
1115
+ /**
1116
+ *
1117
+ * @param context {@link Context}
1118
+ * @throws DeploymentException If the scope of specified {@link Context} has been registered
1119
+ */
1120
+ public void addContext (Context context ) throws DeploymentException {
1121
+ Class <? extends Annotation > scope = context .getScope ();
1122
+ if (contexts .containsKey (scope )){
1123
+ throw new DeploymentException (format ("The context[scope : @%s] has been registered!" ,scope .getName ()));
1124
+ }
1125
+ contexts .put (scope ,context );
1126
+ }
1127
+
1128
+ }
0 commit comments