-
Notifications
You must be signed in to change notification settings - Fork 162
/
CommandScopedContext.java
177 lines (149 loc) · 5.13 KB
/
CommandScopedContext.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright 2012 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.shell.command;
import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Stack;
import java.util.concurrent.ConcurrentHashMap;
import javax.enterprise.context.ContextNotActiveException;
import javax.enterprise.context.spi.Context;
import javax.enterprise.context.spi.Contextual;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.event.Observes;
import javax.inject.Singleton;
import org.jboss.forge.shell.events.CommandExecuted;
import org.jboss.forge.shell.events.CommandVetoed;
import org.jboss.forge.shell.events.PreCommandExecution;
/**
* This class provides lifecycle management for {@link CommandScoped} objects
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*/
@Singleton
public class CommandScopedContext implements Context
{
private final static String COMPONENT_MAP_NAME = CommandScopedContext.class.getName() + ".componentInstanceMap";
private final static String CREATIONAL_MAP_NAME = CommandScopedContext.class.getName() + ".creationalInstanceMap";
private static final Stack<Map<Object, Object>> contextStack = new Stack<Map<Object, Object>>();
private void assertActive()
{
if (!isActive())
{
throw new ContextNotActiveException(
"Context with scope annotation @CommandScoped is not active since no command is in execution.");
}
}
public Map<Object, Object> getCurrentContext()
{
return contextStack.peek();
}
public void create(@Observes final PreCommandExecution execution)
{
contextStack.push(execution.getContext());
}
public void destroy(@Observes final CommandExecuted event)
{
destroyCurrentContext();
contextStack.pop();
}
public void destroy(@Observes final CommandVetoed event)
{
destroyCurrentContext();
contextStack.pop();
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private void destroyCurrentContext()
{
Map<Contextual<?>, Object> componentInstanceMap = getComponentInstanceMap();
Map<Contextual<?>, CreationalContext<?>> creationalContextMap = getCreationalContextMap();
if ((componentInstanceMap != null) && (creationalContextMap != null))
{
for (Entry<Contextual<?>, Object> componentEntry : componentInstanceMap.entrySet())
{
Contextual contextual = componentEntry.getKey();
Object instance = componentEntry.getValue();
CreationalContext creational = creationalContextMap.get(contextual);
contextual.destroy(instance, creational);
}
}
getCurrentContext().clear();
}
/*
* Context Methods
*/
@Override
public boolean isActive()
{
return !contextStack.isEmpty();
}
@Override
public Class<? extends Annotation> getScope()
{
return CommandScoped.class;
}
@Override
@SuppressWarnings("unchecked")
public <T> T get(final Contextual<T> component)
{
assertActive();
return (T) getComponentInstanceMap().get(component);
}
@Override
@SuppressWarnings("unchecked")
public <T> T get(final Contextual<T> component, final CreationalContext<T> creationalContext)
{
assertActive();
T instance = get(component);
if (instance == null)
{
Map<Contextual<?>, CreationalContext<?>> creationalContextMap = getCreationalContextMap();
Map<Contextual<?>, Object> componentInstanceMap = getComponentInstanceMap();
synchronized (componentInstanceMap)
{
instance = (T) componentInstanceMap.get(component);
if (instance == null)
{
instance = component.create(creationalContext);
if (instance != null)
{
componentInstanceMap.put(component, instance);
creationalContextMap.put(component, creationalContext);
}
}
}
}
return instance;
}
/*
* Helpers for manipulating the Component/Context maps.
*/
@SuppressWarnings("unchecked")
private Map<Contextual<?>, Object> getComponentInstanceMap()
{
ConcurrentHashMap<Contextual<?>, Object> map = (ConcurrentHashMap<Contextual<?>, Object>) getCurrentContext()
.get(COMPONENT_MAP_NAME);
if (map == null)
{
map = new ConcurrentHashMap<Contextual<?>, Object>();
getCurrentContext().put(COMPONENT_MAP_NAME, map);
}
return map;
}
@SuppressWarnings("unchecked")
private Map<Contextual<?>, CreationalContext<?>> getCreationalContextMap()
{
Map<Contextual<?>, CreationalContext<?>> map = (ConcurrentHashMap<Contextual<?>, CreationalContext<?>>) getCurrentContext()
.get(CREATIONAL_MAP_NAME);
if (map == null)
{
map = new ConcurrentHashMap<Contextual<?>, CreationalContext<?>>();
getCurrentContext().put(CREATIONAL_MAP_NAME, map);
}
return map;
}
}