Skip to content

Commit 8a27b7a

Browse files
committed
Polish code
1 parent 3a448bb commit 8a27b7a

File tree

3 files changed

+145
-15
lines changed

3 files changed

+145
-15
lines changed

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

+82-11
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import javax.el.ELResolver;
4444
import javax.el.ExpressionFactory;
45+
import javax.enterprise.context.ContextNotActiveException;
4546
import javax.enterprise.context.spi.Context;
4647
import javax.enterprise.context.spi.Contextual;
4748
import javax.enterprise.context.spi.CreationalContext;
@@ -57,8 +58,11 @@
5758
import java.lang.reflect.Method;
5859
import java.lang.reflect.Type;
5960
import java.util.*;
61+
import java.util.function.Function;
6062

63+
import static java.lang.String.format;
6164
import static java.lang.System.getProperty;
65+
import static java.util.Collections.unmodifiableList;
6266
import static java.util.Objects.requireNonNull;
6367
import static java.util.ServiceLoader.load;
6468
import static org.geektimes.commons.lang.util.ArrayUtils.iterate;
@@ -101,13 +105,14 @@ public class StandardBeanManager implements BeanManager, Instance<Object> {
101105

102106
private final ObserverMethodManager observerMethodsManager;
103107

108+
private final DisposerMethodManager disposerMethodManager;
109+
104110
private final InterceptorManager interceptorManager;
105111

106112
private final Map<Class<? extends Extension>, Extension> extensions;
107113

108-
private final Map<String, AnnotatedType<?>> beanTypes;
109114

110-
private final DisposerMethodManager disposerMethodManager;
115+
private final Map<String, AnnotatedType<?>> beanTypes;
111116

112117
private final Map<String, AnnotatedType<?>> alternativeTypes;
113118

@@ -127,9 +132,12 @@ public class StandardBeanManager implements BeanManager, Instance<Object> {
127132
private final List<Bean<?>> genericBeans;
128133

129134

130-
private Set<Throwable> definitionErrors;
135+
private final Set<DefinitionException> definitionErrors;
131136

132-
private Set<Throwable> deploymentProblems;
137+
private final Set<DeploymentException> deploymentProblems;
138+
139+
140+
private final Map<Class<? extends Annotation>, Context> contexts;
133141

134142
public StandardBeanManager() {
135143
this.classLoader = ClassLoaderUtils.getClassLoader(getClass());
@@ -154,10 +162,13 @@ public StandardBeanManager() {
154162

155163
this.definitionErrors = new LinkedHashSet<>();
156164
this.deploymentProblems = new LinkedHashSet<>();
165+
166+
this.contexts = new HashMap<>();
157167
}
158168

159169
@Override
160170
public Object getReference(Bean<?> bean, Type beanType, CreationalContext<?> ctx) {
171+
Class<? extends Annotation> scope = bean.getScope();
161172
// TODO
162173
return null;
163174
}
@@ -333,8 +344,11 @@ public int getInterceptorBindingHashCode(Annotation interceptorBinding) {
333344

334345
@Override
335346
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;
338352
}
339353

340354
@Override
@@ -487,16 +501,17 @@ public void initialize() {
487501
}
488502

489503
public void addDefinitionError(Throwable t) {
490-
this.definitionErrors.add(t);
504+
this.definitionErrors.add(new DefinitionException(t));
491505
}
492506

493507
/**
494508
* Registers a deployment problem with the container, causing the container to abort deployment
495509
* after all observers have been notified.
510+
*
496511
* @param t {@link Throwable}
497512
*/
498513
public void addDeploymentProblem(Throwable t) {
499-
this.deploymentProblems.add(t);
514+
this.deploymentProblems.add(new DeploymentException(t));
500515
}
501516

502517
private void initializeBeanArchiveManager() {
@@ -717,7 +732,7 @@ public void registerBean(Bean<?> bean) {
717732
registerInterceptorBean((Interceptor) bean);
718733
} else if (bean instanceof Decorator) {
719734
registerDecoratorBean((Decorator) bean);
720-
} else if (bean instanceof ManagedBean){
735+
} else if (bean instanceof ManagedBean) {
721736
registerManagedBean((ManagedBean) bean);
722737
} else {
723738
genericBeans.add(bean);
@@ -958,7 +973,7 @@ private void registerObserverMethods(Object beanInstance) {
958973
observerMethodsManager.registerObserverMethods(beanInstance);
959974
}
960975

961-
public void registerObserverMethod(ObserverMethod<?> observerMethod){
976+
public void registerObserverMethod(ObserverMethod<?> observerMethod) {
962977
observerMethodsManager.registerObserverMethod(observerMethod);
963978
}
964979

@@ -1054,4 +1069,60 @@ private Set<AnnotatedType<?>> createAnnotatedTypes(Iterable<Class<?>> classes) {
10541069
return annotatedTypes;
10551070
}
10561071

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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.context;
18+
19+
import javax.enterprise.context.ApplicationScoped;
20+
import javax.enterprise.context.spi.Context;
21+
import javax.enterprise.context.spi.Contextual;
22+
import javax.enterprise.context.spi.CreationalContext;
23+
import javax.enterprise.inject.spi.BeanManager;
24+
import java.lang.annotation.Annotation;
25+
26+
/**
27+
* The Context for {@link ApplicationScoped}
28+
*
29+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
30+
* @since 1.0.0
31+
*/
32+
public class ApplicationScopedContext implements Context {
33+
34+
private final BeanManager beanManager;
35+
36+
public ApplicationScopedContext(BeanManager beanManager) {
37+
this.beanManager = beanManager;
38+
}
39+
40+
@Override
41+
public Class<? extends Annotation> getScope() {
42+
return ApplicationScoped.class;
43+
}
44+
45+
@Override
46+
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
47+
return null;
48+
}
49+
50+
@Override
51+
public <T> T get(Contextual<T> contextual) {
52+
return get(contextual, beanManager.createCreationalContext(contextual));
53+
}
54+
55+
@Override
56+
public boolean isActive() {
57+
return false;
58+
}
59+
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public AfterBeanDiscoveryEvent(StandardBeanManager standardBeanManager) {
4444

4545
@Override
4646
public void addDefinitionError(Throwable t) {
47-
47+
standardBeanManager.addDefinitionError(t);
4848
}
4949

5050
@Override
@@ -71,16 +71,16 @@ public <T> ObserverMethodConfigurator<T> addObserverMethod() {
7171

7272
@Override
7373
public void addContext(Context context) {
74-
74+
standardBeanManager.addContext(context);
7575
}
7676

7777
@Override
7878
public <T> AnnotatedType<T> getAnnotatedType(Class<T> type, String id) {
79-
return null;
79+
return standardBeanManager.getAnnotatedType(type, id);
8080
}
8181

8282
@Override
8383
public <T> Iterable<AnnotatedType<T>> getAnnotatedTypes(Class<T> type) {
84-
return null;
84+
return standardBeanManager.getAnnotatedTypes(type);
8585
}
8686
}

0 commit comments

Comments
 (0)