View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
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 de.smartics.properties.integration.cdi.extension;
17  
18  import java.io.Serializable;
19  import java.lang.annotation.Annotation;
20  import java.lang.reflect.Type;
21  import java.util.Collections;
22  import java.util.HashSet;
23  import java.util.Set;
24  
25  import javax.enterprise.context.RequestScoped;
26  import javax.enterprise.context.spi.CreationalContext;
27  import javax.enterprise.inject.spi.Bean;
28  import javax.enterprise.inject.spi.BeanManager;
29  import javax.enterprise.inject.spi.InjectionPoint;
30  
31  import de.smartics.properties.api.config.app.ConfigurationPropertiesFactory;
32  
33  /**
34   * Cdi {@link Bean} implementation for the property descriptor types.
35   */
36  public class ConfigurationPropertiesFactoryCdiBean implements Bean<Object>,
37      Serializable
38  {
39  
40    // ********************************* Fields *********************************
41  
42    // --- constants ------------------------------------------------------------
43  
44    /**
45     * The class version identifier.
46     */
47    private static final long serialVersionUID = 1L;
48  
49    /**
50     * The configuration properties class.
51     */
52    private final Class<?> type;
53  
54    /**
55     * The cdi bean manager.
56     */
57    private final BeanManager beanManager;
58  
59    /**
60     * The factory that stores all configurations.
61     */
62    private final ConfigurationPropertiesFactory factory;
63  
64    // --- members --------------------------------------------------------------
65  
66    // ****************************** Initializer *******************************
67  
68    // ****************************** Constructors ******************************
69  
70    /**
71     * Constructor.
72     *
73     * @param type the type which shall be constructed.
74     * @param beanManager the cdi bean manager.
75     * @param factory the factory to construct configurationProperties or
76     *          managements.
77     */
78    public ConfigurationPropertiesFactoryCdiBean(final Class<?> type,
79        final BeanManager beanManager,
80        final ConfigurationPropertiesFactory factory)
81    {
82      this.type = type;
83      this.beanManager = beanManager;
84      this.factory = factory;
85    }
86  
87    // ****************************** Inner Classes *****************************
88  
89    // ********************************* Methods ********************************
90  
91    // --- init -----------------------------------------------------------------
92  
93    // --- get&set --------------------------------------------------------------
94  
95    // --- business -------------------------------------------------------------
96  
97    @Override
98    public Class<?> getBeanClass()
99    {
100     return type;
101   }
102 
103   @Override
104   public Set<InjectionPoint> getInjectionPoints()
105   {
106     return Collections.emptySet();
107   }
108 
109   @Override
110   public String getName()
111   {
112     final StringBuilder beanName = new StringBuilder();
113     beanName.append(type.getName());
114     return beanName.toString();
115   }
116 
117   @Override
118   public Set<Annotation> getQualifiers()
119   {
120     final Set<Annotation> qualifiers = new HashSet<Annotation>();
121     return qualifiers;
122   }
123 
124   @Override
125   public Class<? extends Annotation> getScope()
126   {
127     return RequestScoped.class;
128   }
129 
130   @Override
131   public Set<Class<? extends Annotation>> getStereotypes()
132   {
133     return Collections.emptySet();
134   }
135 
136   @Override
137   public Set<Type> getTypes()
138   {
139     final Set<Type> types = new HashSet<Type>();
140     types.add(type);
141     return types;
142   }
143 
144   @Override
145   public boolean isAlternative()
146   {
147     return false;
148   }
149 
150   @Override
151   public boolean isNullable()
152   {
153     return false;
154   }
155 
156   @Override
157   public Object create(final CreationalContext<Object> ctx)
158   {
159     return factory;
160   }
161 
162   @Override
163   public void destroy(final Object instance, final CreationalContext<Object> ctx)
164   {
165 
166   }
167   // --- object basics --------------------------------------------------------
168 
169 }