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 |
|
package info.magnolia.module.blossom.context; |
35 |
|
|
36 |
|
import javax.jcr.Node; |
37 |
|
import javax.jcr.RepositoryException; |
38 |
|
import javax.jcr.observation.EventIterator; |
39 |
|
import javax.jcr.observation.EventListener; |
40 |
|
|
41 |
|
import org.slf4j.Logger; |
42 |
|
import org.slf4j.LoggerFactory; |
43 |
|
import org.springframework.aop.TargetSource; |
44 |
|
import org.springframework.aop.framework.ProxyFactoryBean; |
45 |
|
import org.springframework.beans.factory.BeanFactory; |
46 |
|
import org.springframework.beans.factory.BeanFactoryAware; |
47 |
|
import org.springframework.beans.factory.BeanNameAware; |
48 |
|
import org.springframework.beans.factory.DisposableBean; |
49 |
|
import org.springframework.beans.factory.InitializingBean; |
50 |
|
import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory; |
51 |
|
import org.springframework.context.ApplicationContext; |
52 |
|
import org.springframework.context.ApplicationContextAware; |
53 |
|
import org.springframework.context.ApplicationEventPublisher; |
54 |
|
import org.springframework.context.ApplicationEventPublisherAware; |
55 |
|
|
56 |
|
import info.magnolia.cms.util.ObservationUtil; |
57 |
|
import info.magnolia.context.SystemContext; |
58 |
|
import info.magnolia.jcr.node2bean.Node2BeanException; |
59 |
|
import info.magnolia.jcr.node2bean.Node2BeanProcessor; |
60 |
|
import info.magnolia.module.blossom.node2bean.SpringNode2BeanTransformer; |
61 |
|
import info.magnolia.module.blossom.support.BeanFactoryUtils; |
62 |
|
import info.magnolia.objectfactory.Components; |
63 |
|
import info.magnolia.repository.RepositoryConstants; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
@since |
78 |
|
|
|
|
| 95.6% |
Uncovered Elements: 2 (45) |
Complexity: 16 |
Complexity Density: 0.57 |
|
79 |
|
public class ObservedBeanFactoryBean extends ProxyFactoryBean implements InitializingBean, BeanNameAware, ApplicationContextAware, BeanFactoryAware, EventListener, DisposableBean, ApplicationEventPublisherAware { |
80 |
|
|
81 |
|
private static final Logger logger = LoggerFactory.getLogger(ObservedBeanFactoryBean.class); |
82 |
|
|
83 |
|
private static final String DEFAULT_WORKSPACE = RepositoryConstants.CONFIG; |
84 |
|
private static final int DEFAULT_MAX_DELAY = 60000; |
85 |
|
private static final int DEFAULT_DELAY = 15000; |
86 |
|
|
87 |
|
private ApplicationContext applicationContext; |
88 |
|
private ApplicationEventPublisher applicationEventPublisher; |
89 |
|
private AbstractAutowireCapableBeanFactory beanFactory; |
90 |
|
private String beanName; |
91 |
|
private String workspace = DEFAULT_WORKSPACE; |
92 |
|
private String path; |
93 |
|
private int maxObservationDelay = DEFAULT_MAX_DELAY; |
94 |
|
private int observationDelay = DEFAULT_DELAY; |
95 |
|
private Class<?> defaultClass; |
96 |
|
|
97 |
|
private volatile Object target; |
98 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
99 |
3 |
public ObservedBeanFactoryBean() {... |
100 |
3 |
super.setSingleton(true); |
101 |
|
|
102 |
3 |
super.setTargetSource(new TargetSource() { |
103 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
104 |
18 |
@Override... |
105 |
|
public Class<?> getTargetClass() { |
106 |
18 |
Object local = target; |
107 |
18 |
return local != null ? local.getClass() : null; |
108 |
|
} |
109 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
110 |
|
@Override... |
111 |
|
public boolean isStatic() { |
112 |
|
return false; |
113 |
|
} |
114 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
115 |
|
@Override... |
116 |
|
public Object getTarget() throws Exception { |
117 |
|
return target; |
118 |
|
} |
119 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
120 |
17 |
@Override... |
121 |
|
public void releaseTarget(Object target) throws Exception { |
122 |
|
} |
123 |
|
}); |
124 |
|
} |
125 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
126 |
|
public Class<?> getDefaultClass() {... |
127 |
|
return defaultClass; |
128 |
|
} |
129 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
130 |
|
public void setDefaultClass(Class<?> defaultClass) {... |
131 |
|
this.defaultClass = defaultClass; |
132 |
|
} |
133 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
134 |
|
@Deprecated... |
135 |
|
public String getRepository() { |
136 |
|
return workspace; |
137 |
|
} |
138 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
139 |
|
@Deprecated... |
140 |
|
public void setRepository(String repository) { |
141 |
|
this.workspace = repository; |
142 |
|
} |
143 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
144 |
|
public String getWorkspace() {... |
145 |
|
return workspace; |
146 |
|
} |
147 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
148 |
|
public void setWorkspace(String workspace) {... |
149 |
|
this.workspace = workspace; |
150 |
|
} |
151 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
152 |
|
public String getPath() {... |
153 |
|
return path; |
154 |
|
} |
155 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
156 |
|
public void setPath(String path) {... |
157 |
|
this.path = path; |
158 |
|
} |
159 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
160 |
|
public void setMaxObservationDelay(int maxObservationDelay) {... |
161 |
|
this.maxObservationDelay = maxObservationDelay; |
162 |
|
} |
163 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
164 |
|
public void setObservationDelay(int observationDelay) {... |
165 |
|
this.observationDelay = observationDelay; |
166 |
|
} |
167 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
168 |
3 |
@Override... |
169 |
|
public void afterPropertiesSet() throws Exception { |
170 |
3 |
this.target = createInstance(); |
171 |
3 |
startObservation(); |
172 |
|
} |
173 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
174 |
1 |
@Override... |
175 |
|
public void destroy() throws Exception { |
176 |
1 |
stopObservation(); |
177 |
1 |
Object instance = target; |
178 |
1 |
target = null; |
179 |
1 |
destroyInstance(instance); |
180 |
|
} |
181 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
182 |
1 |
@Override... |
183 |
|
public void onEvent(EventIterator events) { |
184 |
1 |
try { |
185 |
1 |
reloadInstance(); |
186 |
|
} catch (Exception e) { |
187 |
0 |
logger.error("Failed to reload observed bean [" + beanName + "] configured at path [" + path + "] in workspace [" + workspace + "] on change event", e); |
188 |
|
} |
189 |
|
} |
190 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
191 |
|
@Override... |
192 |
|
public void setApplicationContext(ApplicationContext applicationContext) { |
193 |
|
this.applicationContext = applicationContext; |
194 |
|
} |
195 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
196 |
|
@Override... |
197 |
|
public void setBeanFactory(BeanFactory beanFactory) { |
198 |
|
this.beanFactory = (AbstractAutowireCapableBeanFactory) beanFactory; |
199 |
|
} |
200 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
201 |
|
@Override... |
202 |
|
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { |
203 |
|
this.applicationEventPublisher = applicationEventPublisher; |
204 |
|
} |
205 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
206 |
|
@Override... |
207 |
|
public void setBeanName(String beanName) { |
208 |
|
this.beanName = beanName; |
209 |
|
} |
210 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
211 |
4 |
protected Object createInstance() throws Node2BeanException, RepositoryException {... |
212 |
4 |
Node node = getConfigurationNode(); |
213 |
4 |
return transformNode(node); |
214 |
|
} |
215 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
216 |
2 |
protected void destroyInstance(Object instance) {... |
217 |
2 |
if (instance != null) { |
218 |
2 |
BeanFactoryUtils.destroyBean(instance, beanName, beanFactory); |
219 |
|
} |
220 |
|
} |
221 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
222 |
1 |
protected synchronized void reloadInstance() throws Node2BeanException, RepositoryException {... |
223 |
1 |
Object previousInstance = this.target; |
224 |
1 |
this.target = createInstance(); |
225 |
1 |
destroyInstance(previousInstance); |
226 |
|
|
227 |
1 |
applicationEventPublisher.publishEvent(new ObservedBeanReloadedEvent(applicationContext, target, beanName, workspace, path)); |
228 |
|
} |
229 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
230 |
4 |
protected Object transformNode(Node node) throws Node2BeanException, RepositoryException {... |
231 |
4 |
SpringNode2BeanTransformer transformer = new SpringNode2BeanTransformer(beanFactory); |
232 |
4 |
transformer.setTopLevelBeanName(beanName); |
233 |
4 |
transformer.setDefaultClass(defaultClass); |
234 |
4 |
return Components.getComponent(Node2BeanProcessor.class).toBean(node, true, transformer, Components.getComponentProvider()); |
235 |
|
} |
236 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
237 |
4 |
protected Node getConfigurationNode() throws RepositoryException {... |
238 |
4 |
return Components.getComponent(SystemContext.class).getJCRSession(workspace).getNode(path); |
239 |
|
} |
240 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
241 |
3 |
protected void startObservation() {... |
242 |
3 |
ObservationUtil.registerDeferredChangeListener(workspace, path, this, observationDelay, maxObservationDelay); |
243 |
|
} |
244 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
245 |
1 |
protected void stopObservation() {... |
246 |
1 |
ObservationUtil.unregisterChangeListener(workspace, this); |
247 |
|
} |
248 |
|
} |