1 package com.atlassian.plugin.spring.scanner.dynamic.contexts;
2
3 import com.atlassian.event.api.EventPublisher;
4 import com.atlassian.plugin.event.events.PluginContainerRefreshedEvent;
5 import com.atlassian.plugin.osgi.spring.DefaultSpringContainerAccessor;
6 import org.eclipse.gemini.blueprint.context.support.OsgiBundleXmlApplicationContext;
7 import org.osgi.framework.BundleContext;
8 import org.springframework.context.ApplicationContext;
9 import org.springframework.context.ConfigurableApplicationContext;
10
11 import java.util.Date;
12
13
14
15
16 public class ApplicationContextInstallerImpl implements DynamicContext.Installer {
17 private final EventPublisher eventPublisher;
18 private final BundleContext bundleContext;
19 private final String pluginKey;
20
21 ApplicationContextInstallerImpl(final EventPublisher eventPublisher, final BundleContext bundleContext, final String pluginKey) {
22 this.eventPublisher = eventPublisher;
23 this.bundleContext = bundleContext;
24 this.pluginKey = pluginKey;
25 }
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 @Override
44 public ConfigurableApplicationContext useContext(String[] springConfigPaths, ApplicationContext parentContext) {
45 final Date now = new Date();
46 final OsgiBundleXmlApplicationContext newContext = new GeminiOsgiBundleXmlApplicationContext(springConfigPaths, parentContext);
47
48 newContext.setDisplayName(bundleContext.getBundle().getSymbolicName());
49 newContext.setId(bundleContext.getBundle().getBundleId() + "." + now.toString());
50 newContext.setBundleContext(bundleContext);
51 newContext.setPublishContextAsService(false);
52 newContext.refresh();
53 newContext.start();
54
55 publishNewContext(newContext);
56
57 return newContext;
58 }
59
60
61
62
63
64
65
66
67
68 @Override
69 public void closeAndUseContext(ConfigurableApplicationContext childContext, ApplicationContext replacementContext) {
70 childContext.close();
71 if (replacementContext != null) {
72 publishNewContext(replacementContext);
73 }
74 }
75
76 private void publishNewContext(final ApplicationContext newContext) {
77
78
79
80 DefaultSpringContainerAccessor containerAccessor = new DefaultSpringContainerAccessor(newContext);
81 PluginContainerRefreshedEvent event = new PluginContainerRefreshedEvent(containerAccessor, pluginKey);
82
83 eventPublisher.publish(event);
84 }
85 }