Initialization of authorization service from the code is now working.
[photos] / src / main / java / org / wamblee / photos / model / plumbing / EagerExtension.java
1 /*
2  * Copyright 2005-2013 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.photos.model.plumbing;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
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;
30
31 public class EagerExtension implements Extension {
32
33     private List<Bean<?>> eagerBeansList = new ArrayList<Bean<?>>();
34
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());
39         }
40     }
41
42     public void load(@Observes AfterDeploymentValidation event,
43         BeanManager beanManager) {
44         for (Bean<?> bean : eagerBeansList) {
45             // note: toString() is important to instantiate the bean
46             /*
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)) {
52                         main = clazz;
53                     }
54                 }
55             }*/
56             /*
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(); */
63             /*
64                         beanManager.getReference(bean, bean.getBeanClass(),
65                             beanManager.createCreationalContext(bean)).toString();
66                             */
67
68             Context context = beanManager.getContext(ApplicationScoped.class);
69             context.get(bean,
70                 (CreationalContext) beanManager.createCreationalContext(bean))
71                 .toString();
72         }
73     }
74 }