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.util.Iterator;
19  import java.util.Set;
20  import java.util.logging.Level;
21  import java.util.logging.Logger;
22  
23  import javax.enterprise.event.Observes;
24  import javax.enterprise.inject.spi.AfterBeanDiscovery;
25  import javax.enterprise.inject.spi.BeanManager;
26  import javax.enterprise.inject.spi.Extension;
27  
28  import de.smartics.exceptions.i18n.AbstractMessageRuntimeException;
29  import de.smartics.properties.api.config.app.ConfigurationPropertiesFactory;
30  import de.smartics.properties.api.config.app.ConfigurationPropertiesFactoryFactory;
31  import de.smartics.properties.api.config.domain.ConfigurationProperties;
32  import de.smartics.properties.api.config.domain.ConfigurationPropertiesManagement;
33  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
34  
35  /**
36   * The extension that creates the cdi beans for the property types and adds them
37   * to the {@link BeanManager}.
38   */
39  public class SmarticsPropertiesCdiExtension implements Extension
40  {
41  
42    /**
43     * The logger for this class.
44     */
45    private final Logger log = Logger
46        .getLogger(SmarticsPropertiesCdiExtension.class.getName());
47  
48    /**
49     * Cdi extension method that gets the {@link AfterBeanDiscovery} event and the
50     * {@link BeanManager} injected.
51     *
52     * @param abd the {@link AfterBeanDiscovery} event.
53     * @param manager the {@link BeanManager}.
54     * @param configKey the actual (runtime dependent) configuration key.
55     */
56    public void addSmarticsPropertiesToBeanManager(
57        @Observes final AfterBeanDiscovery abd, final BeanManager manager,
58        final ConfigurationKey<?> configKey)
59    {
60      try
61      {
62  
63        final ConfigurationPropertiesFactory factory = createFactory();
64  
65        abd.addBean(new ConfigurationPropertiesFactoryCdiBean(
66            ConfigurationPropertiesFactory.class, manager, factory));
67  
68        addManagemenAsCdiBeanToBeanMnager(abd, manager, factory);
69        addConfigurationPropertiesAsCdiBeanToBeanManager(abd, manager, factory);
70        addPropertyTypesAsCdiBeansToBeanManager(abd, manager, factory, configKey);
71      }
72      catch (final AbstractMessageRuntimeException e)
73      {
74        log.log(Level.SEVERE, e.getMessage());
75        throw e;
76      }
77  
78    }
79  
80    private void addPropertyTypesAsCdiBeansToBeanManager(
81        final AfterBeanDiscovery abd, final BeanManager manager,
82        final ConfigurationPropertiesFactory factory,
83        final ConfigurationKey<?> configKey)
84    {
85      final ConfigurationPropertiesManagement configurationManagement =
86          factory.createManagement(configKey);
87      final Set<Class<?>> propertySetTypes =
88          fetchPropertyTypes(configurationManagement);
89  
90      for (final Iterator<Class<?>> iterator = propertySetTypes.iterator(); iterator
91          .hasNext();)
92      {
93        final Class<?> propertySetType = (Class<?>) iterator.next();
94        abd.addBean(new PropertySetTypeCdiBean(propertySetType, manager, factory));
95      }
96    }
97  
98    private Set<Class<?>> fetchPropertyTypes(
99        final ConfigurationPropertiesManagement configurationManagement)
100   {
101     final Set<Class<?>> propertySetTypes =
102         configurationManagement.getRegistry().getPropertySetTypes();
103     return propertySetTypes;
104   }
105 
106   private void addConfigurationPropertiesAsCdiBeanToBeanManager(
107       final AfterBeanDiscovery abd, final BeanManager manager,
108       final ConfigurationPropertiesFactory factory)
109   {
110     abd.addBean(new ConfigurationPropertiesCdiBean(
111         ConfigurationProperties.class, manager, factory, true));
112     abd.addBean(new ConfigurationPropertiesCdiBean(
113         ConfigurationProperties.class, manager, factory, false));
114   }
115 
116   private void addManagemenAsCdiBeanToBeanMnager(final AfterBeanDiscovery abd,
117       final BeanManager manager, final ConfigurationPropertiesFactory factory)
118   {
119     abd.addBean(new ConfigurationPropertiesCdiBean(
120         ConfigurationPropertiesManagement.class, manager, factory, true));
121     abd.addBean(new ConfigurationPropertiesCdiBean(
122         ConfigurationPropertiesManagement.class, manager, factory, false));
123   }
124 
125   private ConfigurationPropertiesFactory createFactory()
126   {
127     final ConfigurationPropertiesFactory factory =
128         ConfigurationPropertiesFactoryFactory.createDefaultFactory();
129     return factory;
130   }
131 }