1 package com.atlassian.plugin.spring.scanner.test.dynamic;
2
3 import com.atlassian.event.api.EventPublisher;
4 import com.atlassian.plugin.Plugin;
5 import com.atlassian.plugin.PluginAccessor;
6 import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
7 import com.atlassian.plugin.spring.scanner.dynamic.contexts.DynamicContext;
8 import com.google.common.base.Joiner;
9 import org.osgi.framework.BundleContext;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.context.ApplicationContext;
12 import org.springframework.context.ConfigurableApplicationContext;
13 import org.springframework.stereotype.Component;
14
15
16
17
18 @SuppressWarnings({"SpringJavaAutowiringInspection", "FieldCanBeLocal", "UnusedDeclaration"})
19 @Component
20 public class DynamicContextManager {
21 private static final String[] CONFIG_PATHS = {"/dynamicProfile/dynamic-context-spring-scanner.xml"};
22 private static final Joiner INTERFACE_NAME_JOINER = Joiner.on(", ");
23
24 private final ApplicationContext parentContext;
25 private final BundleContext bundleContext;
26 private final DynamicContext.Installer dynamicContextInstaller;
27 private ConfigurableApplicationContext internalContext;
28
29 @Autowired
30 public DynamicContextManager(@ComponentImport EventPublisher eventPublisher, @ComponentImport PluginAccessor pluginAccessor, ApplicationContext parentContext, BundleContext bundleContext) {
31 this.parentContext = parentContext;
32 this.bundleContext = bundleContext;
33
34 final String pluginKey = "com.atlassian.plugin.atlassian-spring-scanner-maven-test";
35 Plugin plugin = pluginAccessor.getPlugin(pluginKey);
36 if (plugin == null) {
37 throw new IllegalStateException("Plugin with key '" + pluginKey + "' was not found");
38 }
39
40 dynamicContextInstaller = DynamicContext.installer(eventPublisher, bundleContext, plugin.getKey());
41 }
42
43 public Result bootstrapTheRestOfTheApplication() {
44 long then = System.currentTimeMillis();
45 if (internalContext == null) {
46 this.internalContext = dynamicContextInstaller.useContext(CONFIG_PATHS, parentContext);
47
48 return new Result(true, then);
49 } else {
50 return new Result(false, then);
51 }
52 }
53
54 public Result shutdownNewContext() {
55 long then = System.currentTimeMillis();
56 if (internalContext != null) {
57 dynamicContextInstaller.closeAndUseContext(internalContext, internalContext.getParent());
58 internalContext = null;
59 return new Result(true, then);
60 }
61 return new Result(false, then);
62 }
63
64 public ConfigurableApplicationContext getInternalContext() {
65 return internalContext;
66 }
67
68 public static class Result {
69 boolean tookPlace;
70 long timeTaken;
71
72 Result(final boolean tookPlace, final long then) {
73 this.tookPlace = tookPlace;
74 this.timeTaken = System.currentTimeMillis() - then;
75 }
76
77 public boolean tookPlace() {
78 return tookPlace;
79 }
80
81 public long getTimeTaken() {
82 return timeTaken;
83 }
84 }
85 }