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  import javax.enterprise.util.AnnotationLiteral;
31  
32  import de.smartics.properties.api.config.app.ConfigurationPropertiesFactory;
33  import de.smartics.properties.api.config.domain.ConfigurationPropertiesManagement;
34  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
35  import de.smartics.properties.integration.cdi.DefaultManagement;
36  import de.smartics.properties.integration.cdi.Management;
37  import de.smartics.properties.spi.config.domain.key.ConfigurationKeyContext;
38  import de.smartics.properties.spi.config.domain.key.ConfigurationKeyContextManager;
39  
40  /**
41   * Cdi {@link Bean} implementation for the property descriptor types.
42   */
43  public class ConfigurationPropertiesCdiBean implements Bean<Object>,
44      Serializable
45  {
46  
47    // ********************************* Fields *********************************
48  
49    // --- constants ------------------------------------------------------------
50  
51    /**
52     * The class version identifier.
53     */
54    private static final long serialVersionUID = 1L;
55  
56    /**
57     * The configuration properties class.
58     */
59    private final Class<?> type;
60  
61    /**
62     * The cdi bean manager.
63     */
64    private final BeanManager beanManager;
65  
66    /**
67     * The factory that stores all configurations.
68     */
69    private final ConfigurationPropertiesFactory factory;
70  
71    /**
72     * Marker to define which key shall be used a static key or the full dynamic
73     * key.
74     */
75    private final boolean useOnlyStaticKeyParts;
76  
77    // --- members --------------------------------------------------------------
78  
79    // ****************************** Initializer *******************************
80  
81    // ****************************** Constructors ******************************
82  
83    /**
84     * Constructor.
85     *
86     * @param type the type which shall be constructed.
87     * @param beanManager the cdi bean manager.
88     * @param factory the factory to construct configurationProperties or
89     *          managements.
90     * @param useOnlyStaticKeyParts trigger to use only the static key parts of aa
91     *          key.
92     */
93    public ConfigurationPropertiesCdiBean(final Class<?> type,
94        final BeanManager beanManager,
95        final ConfigurationPropertiesFactory factory,
96        final boolean useOnlyStaticKeyParts)
97    {
98      this.type = type;
99      this.beanManager = beanManager;
100     this.factory = factory;
101     this.useOnlyStaticKeyParts = useOnlyStaticKeyParts;
102   }
103 
104   // ****************************** Inner Classes *****************************
105 
106   // ********************************* Methods ********************************
107 
108   // --- init -----------------------------------------------------------------
109 
110   // --- get&set --------------------------------------------------------------
111 
112   // --- business -------------------------------------------------------------
113 
114   @Override
115   public Class<?> getBeanClass()
116   {
117     return type;
118   }
119 
120   @Override
121   public Set<InjectionPoint> getInjectionPoints()
122   {
123     return Collections.emptySet();
124   }
125 
126   @Override
127   public String getName()
128   {
129     final StringBuilder beanName = new StringBuilder();
130     if (useOnlyStaticKeyParts)
131     {
132       beanName.append("Default");
133     }
134     beanName.append(type.getName());
135     return beanName.toString();
136   }
137 
138   @SuppressWarnings("serial")
139   @Override
140   public Set<Annotation> getQualifiers()
141   {
142     final Set<Annotation> qualifiers = new HashSet<Annotation>();
143     if (useOnlyStaticKeyParts)
144     {
145       qualifiers.add(new AnnotationLiteral<DefaultManagement>()
146       {
147       });
148     }
149     else
150     {
151       qualifiers.add(new AnnotationLiteral<Management>()
152       {
153       });
154     }
155     return qualifiers;
156   }
157 
158   @Override
159   public Class<? extends Annotation> getScope()
160   {
161     return RequestScoped.class;
162   }
163 
164   @Override
165   public Set<Class<? extends Annotation>> getStereotypes()
166   {
167     return Collections.emptySet();
168   }
169 
170   @Override
171   public Set<Type> getTypes()
172   {
173     final Set<Type> types = new HashSet<Type>();
174     types.add(type);
175     return types;
176   }
177 
178   @Override
179   public boolean isAlternative()
180   {
181     return false;
182   }
183 
184   @Override
185   public boolean isNullable()
186   {
187     return false;
188   }
189 
190   @Override
191   public Object create(final CreationalContext<Object> ctx)
192   {
193     final ConfigurationKey<?> actualKey =
194         CdiBeanHelper.find(beanManager, ConfigurationKey.class).get(0);
195     final ConfigurationKey<?> keyToUse;
196     if (useOnlyStaticKeyParts)
197     {
198       final ConfigurationKeyContext context =
199           ConfigurationKeyContextManager.INSTANCE.context();
200       final de.smartics.properties.api.config.domain.key.ConfigurationKeyFactory<?> ckfactory =
201           context.configurationKeyFactory();
202       final ConfigurationKey<?> defaultKey =
203           ckfactory.createStaticKey(actualKey);
204       keyToUse = defaultKey;
205     }
206     else
207     {
208       keyToUse = actualKey;
209     }
210     final ConfigurationPropertiesManagement configuration =
211         factory.createManagement(keyToUse);
212     // configuration.validate(false);
213     return configuration;
214   }
215 
216   @Override
217   public void destroy(final Object instance, final CreationalContext<Object> ctx)
218   {
219 
220   }
221   // --- object basics --------------------------------------------------------
222 
223 }