Skip to content

Commit 3a448bb

Browse files
committed
Polish code
1 parent aac2910 commit 3a448bb

13 files changed

+214
-32
lines changed

projects/stage-1/middleware-frameworks/my-cdi/src/main/java/org/geektimes/enterprise/inject/standard/beans/interceptor/InterceptorBean.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,13 @@ public Object intercept(InterceptionType type, T instance, InvocationContext ctx
111111
@Override
112112
public T create(CreationalContext<T> creationalContext) {
113113
// TODO
114-
return null;
114+
return super.create(creationalContext);
115115
}
116116

117117
@Override
118118
public void destroy(T instance, CreationalContext<T> creationalContext) {
119119
// TODO
120+
super.destroy(instance,creationalContext);
120121
}
121122

122123
@Override

projects/stage-1/middleware-frameworks/my-cdi/src/main/java/org/geektimes/enterprise/inject/standard/beans/manager/StandardBeanManager.java

+67-16
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,20 @@ public class StandardBeanManager implements BeanManager, Instance<Object> {
117117

118118
private final Map<String, AnnotatedType<?>> syntheticTypes;
119119

120+
120121
private final List<ManagedBean<?>> managedBeans;
121122

122123
private final List<Interceptor<?>> interceptorBeans;
123124

124125
private final List<Decorator<?>> decoratorBeans;
125126

127+
private final List<Bean<?>> genericBeans;
128+
129+
130+
private Set<Throwable> definitionErrors;
131+
132+
private Set<Throwable> deploymentProblems;
133+
126134
public StandardBeanManager() {
127135
this.classLoader = ClassLoaderUtils.getClassLoader(getClass());
128136

@@ -142,6 +150,10 @@ public StandardBeanManager() {
142150
this.managedBeans = new LinkedList<>();
143151
this.interceptorBeans = new LinkedList<>();
144152
this.decoratorBeans = new LinkedList<>();
153+
this.genericBeans = new LinkedList<>();
154+
155+
this.definitionErrors = new LinkedHashSet<>();
156+
this.deploymentProblems = new LinkedHashSet<>();
145157
}
146158

147159
@Override
@@ -474,6 +486,19 @@ public void initialize() {
474486
// TODO
475487
}
476488

489+
public void addDefinitionError(Throwable t) {
490+
this.definitionErrors.add(t);
491+
}
492+
493+
/**
494+
* Registers a deployment problem with the container, causing the container to abort deployment
495+
* after all observers have been notified.
496+
* @param t {@link Throwable}
497+
*/
498+
public void addDeploymentProblem(Throwable t) {
499+
this.deploymentProblems.add(t);
500+
}
501+
477502
private void initializeBeanArchiveManager() {
478503
beanArchiveManager.setClassLoader(classLoader);
479504
beanArchiveManager.enableScanImplicit(isScanImplicitEnabled());
@@ -548,11 +573,6 @@ private void performBeanDiscovery() {
548573
determineAlternativeBeans();
549574
determineInterceptorBeans();
550575
determineDecoratorBeans();
551-
552-
// register the instances of enabled Beans, Interceptors and Decorator
553-
registerBeans();
554-
registerInterceptorBeans();
555-
registerDecoratorBeans();
556576
}
557577

558578
private void determineManagedBeans() {
@@ -687,12 +707,24 @@ private void determineInterceptorBean(AnnotatedType<?> interceptorType) {
687707
fireProcessBeanAttributesEvent(interceptorType, interceptorBean);
688708
if (!interceptorBean.isVetoed()) {
689709
fireProcessBeanEvent(interceptorType, interceptorBean);
690-
registerInterceptorBean(interceptorType, interceptorBean);
710+
registerInterceptorClass(interceptorType);
711+
registerInterceptorBean(interceptorBean);
691712
}
692713
}
693714

694-
private void registerInterceptorBean(AnnotatedType<?> interceptorType, Interceptor<?> interceptorBean) {
695-
registerInterceptorClass(interceptorType);
715+
public void registerBean(Bean<?> bean) {
716+
if (bean instanceof Interceptor) {
717+
registerInterceptorBean((Interceptor) bean);
718+
} else if (bean instanceof Decorator) {
719+
registerDecoratorBean((Decorator) bean);
720+
} else if (bean instanceof ManagedBean){
721+
registerManagedBean((ManagedBean) bean);
722+
} else {
723+
genericBeans.add(bean);
724+
}
725+
}
726+
727+
private void registerInterceptorBean(Interceptor<?> interceptorBean) {
696728
this.interceptorBeans.add(interceptorBean);
697729
}
698730

@@ -708,15 +740,15 @@ private void determineDecoratorBeans() {
708740
}
709741

710742
private void determineDecoratorBean(AnnotatedType<?> decoratorType) {
711-
DecoratorBean<?> decoratorBean = new DecoratorBean(decoratorType,this);
743+
DecoratorBean<?> decoratorBean = new DecoratorBean(decoratorType, this);
712744
fireProcessBeanAttributesEvent(decoratorType, decoratorBean);
713745
if (!decoratorBean.isVetoed()) { // vetoed if ProcessBeanAttributes.veto() method was invoked
714746
fireProcessBeanEvent(decoratorType, decoratorBean);
715747
registerDecoratorBean(decoratorBean);
716748
}
717749
}
718750

719-
private void registerDecoratorBean(DecoratorBean<?> decoratorBean) {
751+
private void registerDecoratorBean(Decorator<?> decoratorBean) {
720752
this.decoratorBeans.add(decoratorBean);
721753
}
722754

@@ -737,7 +769,7 @@ private void registerDecoratorBeans() {
737769
* initialization of the application if any observer registers a definition error.
738770
*/
739771
private void performAfterBeanDiscovery() {
740-
// TODO
772+
fireAfterBeanDiscoveryEvent();
741773
}
742774

743775
/**
@@ -754,7 +786,7 @@ private void performDeploymentValidation() {
754786
* and abort initialization of the application if any observer registers a deployment problem.
755787
*/
756788
private void performAfterDeploymentValidation() {
757-
// TODO
789+
fireAfterDeploymentValidationEvent();
758790
}
759791

760792
private StandardBeanManager discoverExtensions() {
@@ -833,6 +865,24 @@ private void fireProcessProducerEvent(AnnotatedMember annotatedMember, Producer
833865
fireEvent(new ProcessProducerEvent(annotatedMember, producer, this));
834866
}
835867

868+
/**
869+
* Fire fire an event when it has fully completed the bean discovery process, validated that there are
870+
* no definition errors relating to the discovered beans, and registered Bean and ObserverMethod objects
871+
* for the discovered beans.
872+
*/
873+
private void fireAfterBeanDiscoveryEvent() {
874+
fireEvent(new AfterBeanDiscoveryEvent(this));
875+
}
876+
877+
878+
/**
879+
* Fire an event after it has validated that there are no deployment problems and before creating contexts
880+
* or processing requests.
881+
*/
882+
private void fireAfterDeploymentValidationEvent() {
883+
fireEvent(new AfterDeploymentValidationEvent(this));
884+
}
885+
836886
private void fireEvent(Object event) {
837887
observerMethodsManager.fire(event);
838888
}
@@ -908,6 +958,10 @@ private void registerObserverMethods(Object beanInstance) {
908958
observerMethodsManager.registerObserverMethods(beanInstance);
909959
}
910960

961+
public void registerObserverMethod(ObserverMethod<?> observerMethod){
962+
observerMethodsManager.registerObserverMethod(observerMethod);
963+
}
964+
911965
public StandardBeanManager extensions(Extension... extensions) {
912966
iterate(extensions, this::addExtension);
913967
return this;
@@ -959,10 +1013,6 @@ public BeanArchiveManager getBeanArchiveManager() {
9591013
return beanArchiveManager;
9601014
}
9611015

962-
public void addBeanDiscoveryDefinitionError(Throwable t) {
963-
// TODO
964-
}
965-
9661016
public Boolean getScanImplicitProperty() {
9671017
Boolean value = (Boolean) properties.get(SCAN_IMPLICIT_PROPERTY_NAME);
9681018
if (value == null) {
@@ -1003,4 +1053,5 @@ private Set<AnnotatedType<?>> createAnnotatedTypes(Iterable<Class<?>> classes) {
10031053
}
10041054
return annotatedTypes;
10051055
}
1056+
10061057
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.geektimes.enterprise.inject.standard.event;
18+
19+
import org.geektimes.enterprise.inject.standard.beans.manager.StandardBeanManager;
20+
21+
import javax.enterprise.context.spi.Context;
22+
import javax.enterprise.inject.spi.AfterBeanDiscovery;
23+
import javax.enterprise.inject.spi.AnnotatedType;
24+
import javax.enterprise.inject.spi.Bean;
25+
import javax.enterprise.inject.spi.ObserverMethod;
26+
import javax.enterprise.inject.spi.configurator.BeanConfigurator;
27+
import javax.enterprise.inject.spi.configurator.ObserverMethodConfigurator;
28+
29+
/**
30+
* An event was raised when it has fully completed the bean discovery process, validated that there are
31+
* no definition errors relating to the discovered beans, and registered Bean and ObserverMethod
32+
* objects for the discovered beans.
33+
*
34+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
35+
* @since 1.0.0
36+
*/
37+
public class AfterBeanDiscoveryEvent implements AfterBeanDiscovery {
38+
39+
private final StandardBeanManager standardBeanManager;
40+
41+
public AfterBeanDiscoveryEvent(StandardBeanManager standardBeanManager) {
42+
this.standardBeanManager = standardBeanManager;
43+
}
44+
45+
@Override
46+
public void addDefinitionError(Throwable t) {
47+
48+
}
49+
50+
@Override
51+
public void addBean(Bean<?> bean) {
52+
standardBeanManager.registerBean(bean);
53+
}
54+
55+
@Override
56+
public <T> BeanConfigurator<T> addBean() {
57+
// TODO
58+
return null;
59+
}
60+
61+
@Override
62+
public void addObserverMethod(ObserverMethod<?> observerMethod) {
63+
standardBeanManager.registerObserverMethod(observerMethod);
64+
}
65+
66+
@Override
67+
public <T> ObserverMethodConfigurator<T> addObserverMethod() {
68+
// TODO
69+
return null;
70+
}
71+
72+
@Override
73+
public void addContext(Context context) {
74+
75+
}
76+
77+
@Override
78+
public <T> AnnotatedType<T> getAnnotatedType(Class<T> type, String id) {
79+
return null;
80+
}
81+
82+
@Override
83+
public <T> Iterable<AnnotatedType<T>> getAnnotatedTypes(Class<T> type) {
84+
return null;
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.geektimes.enterprise.inject.standard.event;
18+
19+
import org.geektimes.enterprise.inject.standard.beans.manager.StandardBeanManager;
20+
21+
import javax.enterprise.inject.spi.AfterDeploymentValidation;
22+
23+
/**
24+
* an event is raised after it has validated that there are no deployment problems and before creating
25+
* contexts or processing requests.
26+
*
27+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
28+
* @since 1.0.0
29+
*/
30+
public class AfterDeploymentValidationEvent implements AfterDeploymentValidation {
31+
32+
private final StandardBeanManager standardBeanManager;
33+
34+
public AfterDeploymentValidationEvent(StandardBeanManager standardBeanManager) {
35+
this.standardBeanManager = standardBeanManager;
36+
}
37+
38+
@Override
39+
public void addDeploymentProblem(Throwable t) {
40+
standardBeanManager.addDeploymentProblem(t);
41+
}
42+
}

projects/stage-1/middleware-frameworks/my-cdi/src/main/java/org/geektimes/enterprise/inject/standard/event/ObserverMethodManager.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ public void registerObserverMethods(Object beanInstance) {
7373
.forEach(this::registerObserverMethod);
7474
}
7575

76-
public void registerObserverMethods(Bean bean) {
76+
public void registerObserverMethods(Bean<?> bean) {
7777
observerMethodDiscoverer.getObserverMethods(bean, bean.getBeanClass())
7878
.forEach(this::registerObserverMethod);
7979
}
8080

81-
public void registerObserverMethod(ObserverMethod observerMethod) {
81+
public void registerObserverMethod(ObserverMethod<?> observerMethod) {
8282
Type observerType = observerMethod.getObservedType();
8383
Set<Type> eventTypes = getAllTypes(observerType);
8484
fireProcessInjectionPointEvents(observerMethod);
@@ -90,7 +90,7 @@ public void registerObserverMethod(ObserverMethod observerMethod) {
9090
});
9191
}
9292

93-
private void fireProcessInjectionPointEvents(ObserverMethod observerMethod) {
93+
private void fireProcessInjectionPointEvents(ObserverMethod<?> observerMethod) {
9494
List<InjectionPoint> injectionPoints = createInjectionPoints(observerMethod);
9595
injectionPoints.forEach(this::fireProcessInjectionPointEvent);
9696
}
@@ -103,7 +103,7 @@ public void fireProcessInjectionPointEvent(ProcessInjectionPoint processInjectio
103103
fire(processInjectionPoint);
104104
}
105105

106-
private void fireProcessObserverMethodEvent(ObserverMethod observerMethod) {
106+
private void fireProcessObserverMethodEvent(ObserverMethod<?> observerMethod) {
107107
fire(new ProcessObserverMethodEvent(observerMethod, standardBeanManager));
108108
}
109109

@@ -164,7 +164,7 @@ protected void fire(Object event, boolean async) {
164164
});
165165
}
166166

167-
private InjectionPoint createObservedInjectionPoint(ObserverMethod observerMethod) {
167+
private InjectionPoint createObservedInjectionPoint(ObserverMethod<?> observerMethod) {
168168
if (observerMethod instanceof ReflectiveObserverMethod) {
169169
ReflectiveObserverMethod reflectiveObserverMethod = (ReflectiveObserverMethod) observerMethod;
170170
ObserverMethodParameter observedParameter = reflectiveObserverMethod.getObservedParameter();
@@ -173,7 +173,7 @@ private InjectionPoint createObservedInjectionPoint(ObserverMethod observerMetho
173173
return null;
174174
}
175175

176-
private List<InjectionPoint> createInjectionPoints(ObserverMethod observerMethod) {
176+
private List<InjectionPoint> createInjectionPoints(ObserverMethod<?> observerMethod) {
177177
List<InjectionPoint> injectionPoints = new LinkedList<>();
178178
if (observerMethod instanceof ReflectiveObserverMethod) {
179179
ReflectiveObserverMethod method = (ReflectiveObserverMethod) observerMethod;
@@ -185,7 +185,7 @@ private List<InjectionPoint> createInjectionPoints(ObserverMethod observerMethod
185185
return injectionPoints;
186186
}
187187

188-
private InjectionPoint createInjectionPoint(ObserverMethodParameter observedParameter, ReflectiveObserverMethod observerMethod) {
188+
private InjectionPoint createInjectionPoint(ObserverMethodParameter observedParameter, ReflectiveObserverMethod<?> observerMethod) {
189189
Parameter parameter = observedParameter.getParameter();
190190
int index = observedParameter.getIndex();
191191
AnnotatedMethod method = new ReflectiveAnnotatedMethod(observerMethod.getMethod());

projects/stage-1/middleware-frameworks/my-cdi/src/main/java/org/geektimes/enterprise/inject/standard/event/ProcessBeanAttributesEvent.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public BeanAttributesConfigurator<T> configureBeanAttributes() {
9999

100100
@Override
101101
public void addDefinitionError(Throwable t) {
102-
standardBeanManager.addBeanDiscoveryDefinitionError(t);
102+
standardBeanManager.addDefinitionError(t);
103103
}
104104

105105
@Override

0 commit comments

Comments
 (0)