2 * Copyright 2005-2013 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.photos.model.plumbing;
18 import java.util.ArrayList;
19 import java.util.List;
21 import javax.enterprise.context.ApplicationScoped;
22 import javax.enterprise.context.spi.Context;
23 import javax.enterprise.context.spi.CreationalContext;
24 import javax.enterprise.event.Observes;
25 import javax.enterprise.inject.spi.AfterDeploymentValidation;
26 import javax.enterprise.inject.spi.Bean;
27 import javax.enterprise.inject.spi.BeanManager;
28 import javax.enterprise.inject.spi.Extension;
29 import javax.enterprise.inject.spi.ProcessBean;
31 public class EagerExtension implements Extension {
33 private List<Bean<?>> eagerBeansList = new ArrayList<Bean<?>>();
35 public <T> void collect(@Observes ProcessBean<T> event) {
36 if (event.getAnnotated().isAnnotationPresent(Eager.class) &&
37 event.getAnnotated().isAnnotationPresent(ApplicationScoped.class)) {
38 eagerBeansList.add(event.getBean());
42 public void load(@Observes AfterDeploymentValidation event,
43 BeanManager beanManager) {
44 for (Bean<?> bean : eagerBeansList) {
45 // note: toString() is important to instantiate the bean
47 Class main = Object.class;
48 for (Type type : bean.getTypes()) {
49 if (type instanceof Class) {
50 Class clazz = (Class) type;
51 if (main.isAssignableFrom(clazz)) {
57 bean.create((CreationalContext) beanManager
58 .createCreationalContext(bean));*/
59 /* beanManager.getReference(bean, main,
60 beanManager.createCreationalContext(null)).toString();
61 beanManager.getReference(bean, main,
62 beanManager.createCreationalContext(null)).toString(); */
64 beanManager.getReference(bean, bean.getBeanClass(),
65 beanManager.createCreationalContext(bean)).toString();
68 Context context = beanManager.getContext(ApplicationScoped.class);
70 (CreationalContext) beanManager.createCreationalContext(bean))