1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.eclipse.spring;
17
18 import static java.text.MessageFormat.format;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.eclipse.core.runtime.Platform;
23 import org.osgi.framework.Bundle;
24 import org.osgi.framework.BundleContext;
25 import org.osgi.framework.BundleException;
26 import org.osgi.framework.FrameworkUtil;
27 import org.osgi.framework.InvalidSyntaxException;
28 import org.osgi.util.tracker.ServiceTracker;
29 import org.springframework.context.ApplicationContext;
30
31
32
33
34
35
36
37 class ApplicationContextTracker {
38 private final static Log log = LogFactory
39 .getLog(ApplicationContextTracker.class);
40
41 private static final String FILTER = "(&(objectClass=org.springframework.context.ApplicationContext)"
42 + "(org.springframework.context.service.name={0}))";
43
44 public final static String APPLICATION_CONTEXT_TRACKER_TIMEOUT = "org.argeo.eclipse.spring.applicationContextTrackerTimeout";
45
46 private static Long defaultTimeout = Long.parseLong(System.getProperty(
47 APPLICATION_CONTEXT_TRACKER_TIMEOUT, "30000"));
48
49 @SuppressWarnings("rawtypes")
50 private ServiceTracker applicationContextServiceTracker;
51
52
53
54
55
56
57
58
59
60
61 @SuppressWarnings({ "unchecked", "rawtypes" })
62 public ApplicationContextTracker(final Bundle contributorBundle,
63 final BundleContext factoryBundleContext) {
64 final String filter = format(FILTER,
65 contributorBundle.getSymbolicName());
66 try {
67 applicationContextServiceTracker = new ServiceTracker(
68 factoryBundleContext, FrameworkUtil.createFilter(filter),
69 null);
70
71 } catch (final InvalidSyntaxException e) {
72 e.printStackTrace();
73 }
74 }
75
76 public void open() {
77 if (applicationContextServiceTracker != null) {
78 applicationContextServiceTracker.open();
79 }
80 }
81
82 public void close() {
83 if (applicationContextServiceTracker != null) {
84 applicationContextServiceTracker.close();
85 }
86 }
87
88 public ApplicationContext getApplicationContext() {
89 ApplicationContext applicationContext = null;
90 if (applicationContextServiceTracker != null) {
91 try {
92 applicationContext = (ApplicationContext) applicationContextServiceTracker
93 .waitForService(defaultTimeout);
94 } catch (InterruptedException e) {
95 e.printStackTrace();
96 }
97 }
98 return applicationContext;
99 }
100
101 @Override
102 protected void finalize() throws Throwable {
103 close();
104 super.finalize();
105 }
106
107 static ApplicationContext getApplicationContext(String bundleSymbolicName) {
108 Bundle contributorBundle = Platform.getBundle(bundleSymbolicName);
109 return getApplicationContext(contributorBundle);
110 }
111
112 static ApplicationContext getApplicationContext(
113 final Bundle contributorBundle) {
114 if (log.isTraceEnabled())
115 log.trace("Get application context for bundle " + contributorBundle);
116
117
118 if (contributorBundle.getState() != Bundle.ACTIVE) {
119 if (log.isTraceEnabled())
120 log.trace("Starting bundle: "
121 + contributorBundle.getSymbolicName());
122
123
124
125 try {
126 contributorBundle.start();
127 } catch (BundleException e) {
128 log.error("Cannot start bundle " + contributorBundle, e);
129 }
130
131
132
133
134
135
136
137
138 }
139
140 final ApplicationContextTracker applicationContextTracker = new ApplicationContextTracker(
141 contributorBundle, contributorBundle.getBundleContext());
142 ApplicationContext applicationContext = null;
143 try {
144 applicationContextTracker.open();
145 applicationContext = applicationContextTracker
146 .getApplicationContext();
147 } finally {
148 applicationContextTracker.close();
149 }
150 return applicationContext;
151 }
152 }