Commit 1be04f3feceecf0bc4449c32d4142218f41aa9fe
1 parent
29dee7a4
Exists in
master
Criação do escopo estático.
Showing
3 changed files
with
200 additions
and
14 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/StaticScoped.java
0 → 100644
@@ -0,0 +1,56 @@ | @@ -0,0 +1,56 @@ | ||
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.annotation; | ||
38 | + | ||
39 | +import static java.lang.annotation.ElementType.FIELD; | ||
40 | +import static java.lang.annotation.ElementType.METHOD; | ||
41 | +import static java.lang.annotation.ElementType.TYPE; | ||
42 | +import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
43 | + | ||
44 | +import java.lang.annotation.Inherited; | ||
45 | +import java.lang.annotation.Retention; | ||
46 | +import java.lang.annotation.Target; | ||
47 | + | ||
48 | +import javax.enterprise.context.NormalScope; | ||
49 | + | ||
50 | +@Inherited | ||
51 | +@Target({ METHOD, TYPE, FIELD }) | ||
52 | +@Retention(RUNTIME) | ||
53 | +@NormalScope | ||
54 | +public @interface StaticScoped { | ||
55 | + | ||
56 | +} |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/CoreBootstrap.java
@@ -39,6 +39,7 @@ package br.gov.frameworkdemoiselle.internal.bootstrap; | @@ -39,6 +39,7 @@ package br.gov.frameworkdemoiselle.internal.bootstrap; | ||
39 | import java.util.Locale; | 39 | import java.util.Locale; |
40 | 40 | ||
41 | import javax.enterprise.event.Observes; | 41 | import javax.enterprise.event.Observes; |
42 | +import javax.enterprise.inject.spi.AfterBeanDiscovery; | ||
42 | import javax.enterprise.inject.spi.AfterDeploymentValidation; | 43 | import javax.enterprise.inject.spi.AfterDeploymentValidation; |
43 | import javax.enterprise.inject.spi.BeanManager; | 44 | import javax.enterprise.inject.spi.BeanManager; |
44 | import javax.enterprise.inject.spi.BeforeBeanDiscovery; | 45 | import javax.enterprise.inject.spi.BeforeBeanDiscovery; |
@@ -47,6 +48,8 @@ import javax.enterprise.inject.spi.Extension; | @@ -47,6 +48,8 @@ import javax.enterprise.inject.spi.Extension; | ||
47 | 48 | ||
48 | import org.slf4j.Logger; | 49 | import org.slf4j.Logger; |
49 | 50 | ||
51 | +import br.gov.frameworkdemoiselle.internal.context.Contexts; | ||
52 | +import br.gov.frameworkdemoiselle.internal.context.StaticContext; | ||
50 | import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer; | 53 | import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer; |
51 | import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; | 54 | import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; |
52 | import br.gov.frameworkdemoiselle.util.Beans; | 55 | import br.gov.frameworkdemoiselle.util.Beans; |
@@ -58,6 +61,8 @@ public class CoreBootstrap implements Extension { | @@ -58,6 +61,8 @@ public class CoreBootstrap implements Extension { | ||
58 | 61 | ||
59 | private ResourceBundle bundle; | 62 | private ResourceBundle bundle; |
60 | 63 | ||
64 | + private AfterBeanDiscovery afterBeanDiscovery; | ||
65 | + | ||
61 | private Logger getLogger() { | 66 | private Logger getLogger() { |
62 | if (this.logger == null) { | 67 | if (this.logger == null) { |
63 | this.logger = LoggerProducer.create(CoreBootstrap.class); | 68 | this.logger = LoggerProducer.create(CoreBootstrap.class); |
@@ -75,29 +80,23 @@ public class CoreBootstrap implements Extension { | @@ -75,29 +80,23 @@ public class CoreBootstrap implements Extension { | ||
75 | } | 80 | } |
76 | 81 | ||
77 | public void engineOn(@Observes final BeforeBeanDiscovery event, BeanManager beanManager) { | 82 | public void engineOn(@Observes final BeforeBeanDiscovery event, BeanManager beanManager) { |
78 | - String description; | ||
79 | - Logger log = getLogger(); | ||
80 | - | ||
81 | - description = getBundle().getString("engine-on"); | ||
82 | - log.info(description); | 83 | + getLogger().info(getBundle().getString("engine-on")); |
83 | 84 | ||
84 | Beans.setBeanManager(beanManager); | 85 | Beans.setBeanManager(beanManager); |
86 | + getLogger().info(getBundle().getString("setting-up-bean-manager", Beans.class.getCanonicalName())); | ||
87 | + } | ||
85 | 88 | ||
86 | - description = getBundle().getString("setting-up-bean-manager", Beans.class.getCanonicalName()); | ||
87 | - log.info(description); | 89 | + public void afterBeanDiscovery(@Observes final AfterBeanDiscovery event) { |
90 | + this.afterBeanDiscovery = event; | ||
88 | } | 91 | } |
89 | 92 | ||
90 | public void takeOff(@Observes final AfterDeploymentValidation event) { | 93 | public void takeOff(@Observes final AfterDeploymentValidation event) { |
91 | - String description = getBundle().getString("taking-off"); | 94 | + Contexts.add(new StaticContext(), this.afterBeanDiscovery); |
92 | 95 | ||
93 | - Logger log = getLogger(); | ||
94 | - log.info(description); | 96 | + getLogger().info(getBundle().getString("taking-off")); |
95 | } | 97 | } |
96 | 98 | ||
97 | public void engineOff(@Observes final BeforeShutdown event) { | 99 | public void engineOff(@Observes final BeforeShutdown event) { |
98 | - String description = getBundle().getString("engine-off"); | ||
99 | - | ||
100 | - Logger log = getLogger(); | ||
101 | - log.info(description); | 100 | + getLogger().info(getBundle().getString("engine-off")); |
102 | } | 101 | } |
103 | } | 102 | } |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/StaticContext.java
0 → 100644
@@ -0,0 +1,131 @@ | @@ -0,0 +1,131 @@ | ||
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 | +/* | ||
38 | + * Demoiselle Framework Copyright (c) 2010 Serpro and other contributors as indicated by the @author tag. See the | ||
39 | + * copyright.txt in the distribution for a full listing of contributors. Demoiselle Framework is an open source Java EE | ||
40 | + * library designed to accelerate the development of transactional database Web applications. Demoiselle Framework is | ||
41 | + * released under the terms of the LGPL license 3 http://www.gnu.org/licenses/lgpl.html LGPL License 3 This file is part | ||
42 | + * of Demoiselle Framework. Demoiselle Framework is free software: you can redistribute it and/or modify it under the | ||
43 | + * terms of the GNU Lesser General Public License 3 as published by the Free Software Foundation. Demoiselle Framework | ||
44 | + * is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
45 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You | ||
46 | + * should have received a copy of the GNU Lesser General Public License along with Demoiselle Framework. If not, see | ||
47 | + * <http://www.gnu.org/licenses/>. | ||
48 | + */ | ||
49 | +package br.gov.frameworkdemoiselle.internal.context; | ||
50 | + | ||
51 | +import java.lang.annotation.Annotation; | ||
52 | + | ||
53 | +import javax.enterprise.context.ContextNotActiveException; | ||
54 | +import javax.enterprise.context.spi.Contextual; | ||
55 | +import javax.enterprise.context.spi.CreationalContext; | ||
56 | +import javax.enterprise.inject.spi.Bean; | ||
57 | + | ||
58 | +import br.gov.frameworkdemoiselle.annotation.StaticScoped; | ||
59 | + | ||
60 | +public class StaticContext implements CustomContext { | ||
61 | + | ||
62 | + private final static ContextStore store = new ContextStore(); | ||
63 | + | ||
64 | + // private final Map<Class<?>, Object> cache = Collections.synchronizedMap(new HashMap<Class<?>, Object>()); | ||
65 | + | ||
66 | + private boolean active; | ||
67 | + | ||
68 | + private final Class<? extends Annotation> scope; | ||
69 | + | ||
70 | + public StaticContext(final Class<? extends Annotation> scope) { | ||
71 | + this(scope, true); | ||
72 | + } | ||
73 | + | ||
74 | + public StaticContext() { | ||
75 | + this(StaticScoped.class, true); | ||
76 | + } | ||
77 | + | ||
78 | + public StaticContext(final Class<? extends Annotation> scope, boolean active) { | ||
79 | + this.scope = scope; | ||
80 | + this.active = active; | ||
81 | + } | ||
82 | + | ||
83 | + @Override | ||
84 | + public <T> T get(final Contextual<T> contextual) { | ||
85 | + return get(contextual, null); | ||
86 | + } | ||
87 | + | ||
88 | + @Override | ||
89 | + @SuppressWarnings("unchecked") | ||
90 | + public <T> T get(final Contextual<T> contextual, final CreationalContext<T> creationalContext) { | ||
91 | + T instance = null; | ||
92 | + | ||
93 | + if (!isActive()) { | ||
94 | + throw new ContextNotActiveException(); | ||
95 | + } | ||
96 | + | ||
97 | + String id = getId(contextual); | ||
98 | + if (getStore().contains(id)) { | ||
99 | + instance = (T) getStore().get(id); | ||
100 | + | ||
101 | + } else if (creationalContext != null) { | ||
102 | + instance = contextual.create(creationalContext); | ||
103 | + getStore().put(id, instance); | ||
104 | + } | ||
105 | + | ||
106 | + return instance; | ||
107 | + } | ||
108 | + | ||
109 | + private <T> String getId(final Contextual<T> contextual) { | ||
110 | + Bean<T> bean = (Bean<T>) contextual; | ||
111 | + return bean.getBeanClass().getCanonicalName(); | ||
112 | + } | ||
113 | + | ||
114 | + @Override | ||
115 | + public Class<? extends Annotation> getScope() { | ||
116 | + return this.scope; | ||
117 | + } | ||
118 | + | ||
119 | + private ContextStore getStore() { | ||
120 | + return store; | ||
121 | + } | ||
122 | + | ||
123 | + @Override | ||
124 | + public boolean isActive() { | ||
125 | + return this.active; | ||
126 | + } | ||
127 | + | ||
128 | + public void setActive(final boolean active) { | ||
129 | + this.active = active; | ||
130 | + } | ||
131 | +} |