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.Any;
28  import javax.enterprise.inject.Default;
29  import javax.enterprise.inject.spi.Bean;
30  import javax.enterprise.inject.spi.BeanManager;
31  import javax.enterprise.inject.spi.InjectionPoint;
32  import javax.enterprise.util.AnnotationLiteral;
33  
34  import de.smartics.properties.api.config.app.ConfigurationPropertiesFactory;
35  import de.smartics.properties.api.config.domain.ConfigurationPropertiesManagement;
36  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
37  
38  /**
39   * Cdi {@link Bean} implementation for the property descriptor types.
40   */
41  public class PropertySetTypeCdiBean implements Bean<Object>, Serializable
42  {
43  
44    // ********************************* Fields *********************************
45  
46    // --- constants ------------------------------------------------------------
47  
48    /**
49     * The class version identifier.
50     */
51    private static final long serialVersionUID = 1L;
52  
53    /**
54     * The properties class.
55     */
56    private final Class<?> type;
57  
58    /**
59     * The cdi bean manager.
60     */
61    private final BeanManager beanManager;
62  
63    /**
64     * The smartics properties factory that stores / caches all configurations.
65     */
66    private final ConfigurationPropertiesFactory factory;
67  
68    // --- members --------------------------------------------------------------
69  
70    // ****************************** Initializer *******************************
71  
72    // ****************************** Constructors ******************************
73  
74    /**
75     * The Constructor.
76     *
77     * @param type the properties type.
78     * @param beanManager the cdi {@link BeanManager}
79     * @param factory the configuration factory.
80     */
81    public PropertySetTypeCdiBean(final Class<?> type,
82        final BeanManager beanManager,
83        final ConfigurationPropertiesFactory factory)
84    {
85      this.type = type;
86      this.beanManager = beanManager;
87      this.factory = factory;
88    }
89  
90    // ****************************** Inner Classes *****************************
91  
92    // ********************************* Methods ********************************
93  
94    // --- init -----------------------------------------------------------------
95  
96    // --- get&set --------------------------------------------------------------
97  
98    // --- business -------------------------------------------------------------
99  
100   @Override
101   public Class<?> getBeanClass()
102   {
103     return type;
104   }
105 
106   @Override
107   public Set<InjectionPoint> getInjectionPoints()
108   {
109     return Collections.emptySet();
110   }
111 
112   @Override
113   public String getName()
114   {
115     return type.getName();
116   }
117 
118   @SuppressWarnings("serial")
119   @Override
120   public Set<Annotation> getQualifiers()
121   {
122     final Set<Annotation> qualifiers = new HashSet<Annotation>();
123     qualifiers.add(new AnnotationLiteral<Default>()
124     {
125     });
126     qualifiers.add(new AnnotationLiteral<Any>()
127     {
128     });
129     return qualifiers;
130   }
131 
132   @Override
133   public Class<? extends Annotation> getScope()
134   {
135     return RequestScoped.class;
136   }
137 
138   @Override
139   public Set<Class<? extends Annotation>> getStereotypes()
140   {
141     return Collections.emptySet();
142   }
143 
144   @Override
145   public Set<Type> getTypes()
146   {
147     final Set<Type> types = new HashSet<Type>();
148     types.add(type);
149     return types;
150   }
151 
152   @Override
153   public boolean isAlternative()
154   {
155     return false;
156   }
157 
158   @Override
159   public boolean isNullable()
160   {
161     return false;
162   }
163 
164   @Override
165   public Object create(final CreationalContext<Object> ctx)
166   {
167     final ConfigurationKey<?> configKey =
168         CdiBeanHelper.find(beanManager, ConfigurationKey.class).get(0);
169     final ConfigurationPropertiesManagement configuration =
170         factory.createManagement(configKey);
171     // configuration.validate(false);
172 
173     return configuration.getProperties(type);
174   }
175 
176   @Override
177   public void destroy(final Object instance, final CreationalContext<Object> ctx)
178   {
179 
180   }
181 
182   // --- object basics --------------------------------------------------------
183 
184 }