]> git.argeo.org Git - lgpl/argeo-commons.git/blob - spring/ApplicationContextTracker.java
Prepare next development cycle
[lgpl/argeo-commons.git] / spring / ApplicationContextTracker.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.eclipse.spring;
18
19 import static java.text.MessageFormat.format;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.argeo.eclipse.ui.ArgeoUiPlugin;
24 import org.eclipse.core.runtime.Platform;
25 import org.osgi.framework.Bundle;
26 import org.osgi.framework.BundleContext;
27 import org.osgi.framework.BundleException;
28 import org.osgi.framework.FrameworkUtil;
29 import org.osgi.framework.InvalidSyntaxException;
30 import org.osgi.util.tracker.ServiceTracker;
31 import org.springframework.context.ApplicationContext;
32
33 /**
34 * @author Heiko Seeberger
35 * @author Mathieu Baudier
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)" //$NON-NLS-1$
42 + "(org.springframework.context.service.name={0}))"; //$NON-NLS-1$
43
44 private ServiceTracker applicationContextServiceTracker;
45
46 /**
47 * @param contributorBundle
48 * OSGi bundle for which the Spring application context is to be
49 * tracked. Must not be null!
50 * @param factoryBundleContext
51 * BundleContext object which can be used to track services
52 * @throws IllegalArgumentException
53 * if the given bundle is null.
54 */
55 public ApplicationContextTracker(final Bundle contributorBundle,
56 final BundleContext factoryBundleContext) {
57 final String filter = format(FILTER, contributorBundle
58 .getSymbolicName());
59 try {
60 applicationContextServiceTracker = new ServiceTracker(
61 factoryBundleContext, FrameworkUtil.createFilter(filter),
62 null);
63 applicationContextServiceTracker.open();
64 } catch (final InvalidSyntaxException e) {
65 e.printStackTrace();
66 }
67 }
68
69 public void close() {
70 if (applicationContextServiceTracker != null) {
71 applicationContextServiceTracker.close();
72 }
73 }
74
75 public ApplicationContext getApplicationContext() {
76 ApplicationContext applicationContext = null;
77 if (applicationContextServiceTracker != null) {
78 try {
79 applicationContext = (ApplicationContext) applicationContextServiceTracker
80 .waitForService(5000);
81 } catch (InterruptedException e) {
82 e.printStackTrace();
83 }
84 }
85 return applicationContext;
86 }
87
88 @Override
89 protected void finalize() throws Throwable {
90 close();
91 super.finalize();
92 }
93
94 static ApplicationContext getApplicationContext(String bundleSymbolicName) {
95 Bundle contributorBundle = Platform.getBundle(bundleSymbolicName);
96 return getApplicationContext(contributorBundle);
97 }
98
99 static ApplicationContext getApplicationContext(Bundle contributorBundle) {
100 if (log.isTraceEnabled())
101 log
102 .trace("Get application context for bundle "
103 + contributorBundle);
104
105 if (contributorBundle.getState() != Bundle.ACTIVE
106 && contributorBundle.getState() != Bundle.STARTING) {
107 try {
108 log.info("Starting bundle: "
109 + contributorBundle.getSymbolicName());
110 contributorBundle.start();
111 } catch (BundleException e) {
112 e.printStackTrace();
113 }
114 }
115
116 final ApplicationContextTracker applicationContextTracker = new ApplicationContextTracker(
117 contributorBundle, ArgeoUiPlugin.getDefault()
118 .getBundleContext());
119 ApplicationContext applicationContext = null;
120 try {
121 applicationContext = applicationContextTracker
122 .getApplicationContext();
123 } finally {
124 applicationContextTracker.close();
125 }
126 return applicationContext;
127 }
128 }