Commit db2d2cbb755ff2928b98bb777d4a9cf492606605
1 parent
79df3bfb
Exists in
master
Criação da abstração para bootstraps de seleção de estratégias.
Showing
1 changed file
with
118 additions
and
0 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractStrategyBootstrap.java
0 → 100644
... | ... | @@ -0,0 +1,118 @@ |
1 | +/* | |
2 | + * Demoiselle Framework | |
3 | + * Copyright (C) 2010 SERPRO | |
4 | + * ---------------------------------------------------------------------------- | |
5 | + * This file is part of Demoiselle Framework. | |
6 | + * | |
7 | + * Demoiselle Framework is free software; you can redistribute it and/or | |
8 | + * modify it under the terms of the GNU Lesser General Public License version 3 | |
9 | + * as published by the Free Software Foundation. | |
10 | + * | |
11 | + * This program is distributed in the hope that it will be useful, | |
12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | + * GNU General Public License for more details. | |
15 | + * | |
16 | + * You should have received a copy of the GNU Lesser General Public License version 3 | |
17 | + * along with this program; if not, see <http://www.gnu.org/licenses/> | |
18 | + * or write to the Free Software Foundation, Inc., 51 Franklin Street, | |
19 | + * Fifth Floor, Boston, MA 02110-1301, USA. | |
20 | + * ---------------------------------------------------------------------------- | |
21 | + * Este arquivo é parte do Framework Demoiselle. | |
22 | + * | |
23 | + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | |
24 | + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | |
25 | + * do Software Livre (FSF). | |
26 | + * | |
27 | + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | |
28 | + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | |
29 | + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | |
30 | + * para maiores detalhes. | |
31 | + * | |
32 | + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | |
33 | + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | |
34 | + * ou escreva para a Fundação do Software Livre (FSF) Inc., | |
35 | + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | |
36 | + */ | |
37 | +package br.gov.frameworkdemoiselle.internal.bootstrap; | |
38 | + | |
39 | +import javax.enterprise.event.Observes; | |
40 | +import javax.enterprise.inject.spi.BeforeBeanDiscovery; | |
41 | +import javax.enterprise.inject.spi.ProcessAnnotatedType; | |
42 | + | |
43 | +import org.apache.commons.configuration.Configuration; | |
44 | +import org.apache.commons.configuration.PropertiesConfiguration; | |
45 | + | |
46 | +import br.gov.frameworkdemoiselle.configuration.ConfigurationException; | |
47 | +import br.gov.frameworkdemoiselle.util.Reflections; | |
48 | +import br.gov.frameworkdemoiselle.util.Strings; | |
49 | + | |
50 | +public abstract class AbstractStrategyBootstrap<T, D extends T> extends AbstractBootstrap { | |
51 | + | |
52 | + private Class<T> type; | |
53 | + | |
54 | + private Class<D> defaultClass; | |
55 | + | |
56 | + private Class<? extends T> selected; | |
57 | + | |
58 | + private Class<T> getType() { | |
59 | + if (this.type == null) { | |
60 | + this.type = Reflections.getGenericTypeArgument(this.getClass(), 0); | |
61 | + } | |
62 | + | |
63 | + return this.type; | |
64 | + } | |
65 | + | |
66 | + private Class<D> getDefaultClass() { | |
67 | + if (this.defaultClass == null) { | |
68 | + this.defaultClass = Reflections.getGenericTypeArgument(this.getClass(), 1); | |
69 | + } | |
70 | + | |
71 | + return this.defaultClass; | |
72 | + } | |
73 | + | |
74 | + public void beforeBeanDiscovery(@Observes final BeforeBeanDiscovery event) { | |
75 | + selected = loadSelected(); | |
76 | + } | |
77 | + | |
78 | + public <A> void processAnnotatedType(@Observes final ProcessAnnotatedType<A> event) { | |
79 | + Class<A> annotated = event.getAnnotatedType().getJavaClass(); | |
80 | + | |
81 | + if (Reflections.isOfType(annotated, getType()) && annotated != selected) { | |
82 | + event.veto(); | |
83 | + } | |
84 | + } | |
85 | + | |
86 | + @SuppressWarnings("unchecked") | |
87 | + private Class<T> loadSelected() { | |
88 | + Class<T> result = null; | |
89 | + String canonicalName = null; | |
90 | + String typeName = getType().getSimpleName().toLowerCase(); | |
91 | + String key = null; | |
92 | + | |
93 | + try { | |
94 | + Configuration config = new PropertiesConfiguration("demoiselle.properties"); | |
95 | + canonicalName = config.getString(getConfigurationKey(), getDefaultClass().getCanonicalName()); | |
96 | + | |
97 | + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); | |
98 | + result = (Class<T>) Class.forName(canonicalName, false, classLoader); | |
99 | + result.asSubclass(getType()); | |
100 | + | |
101 | + } catch (org.apache.commons.configuration.ConfigurationException cause) { | |
102 | + throw new ConfigurationException(getBundle().getString("file-not-found", "demoiselle.properties")); | |
103 | + | |
104 | + } catch (ClassNotFoundException cause) { | |
105 | + key = Strings.getString("{0}-class-not-found", typeName); | |
106 | + throw new ConfigurationException(getBundle().getString(key, canonicalName)); | |
107 | + | |
108 | + } catch (ClassCastException cause) { | |
109 | + key = Strings.getString("{0}-class-must-be-of-type", typeName); | |
110 | + throw new ConfigurationException(getBundle().getString(key, canonicalName, getType())); | |
111 | + } | |
112 | + | |
113 | + return result; | |
114 | + } | |
115 | + | |
116 | + public abstract String getConfigurationKey(); | |
117 | + | |
118 | +} | ... | ... |