2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.eclipse
.spring
;
19 import static java
.text
.MessageFormat
.format
;
21 import org
.apache
.commons
.logging
.Log
;
22 import org
.apache
.commons
.logging
.LogFactory
;
23 import org
.eclipse
.core
.runtime
.Platform
;
24 import org
.osgi
.framework
.Bundle
;
25 import org
.osgi
.framework
.BundleContext
;
26 import org
.osgi
.framework
.BundleException
;
27 import org
.osgi
.framework
.FrameworkUtil
;
28 import org
.osgi
.framework
.InvalidSyntaxException
;
29 import org
.osgi
.util
.tracker
.ServiceTracker
;
30 import org
.springframework
.context
.ApplicationContext
;
33 * @author Heiko Seeberger
34 * @author Mathieu Baudier
36 class ApplicationContextTracker
{
37 private final static Log log
= LogFactory
38 .getLog(ApplicationContextTracker
.class);
40 private static final String FILTER
= "(&(objectClass=org.springframework.context.ApplicationContext)" //$NON-NLS-1$
41 + "(org.springframework.context.service.name={0}))"; //$NON-NLS-1$
43 private ServiceTracker applicationContextServiceTracker
;
46 * @param contributorBundle
47 * OSGi bundle for which the Spring application context is to be
48 * tracked. Must not be null!
49 * @param factoryBundleContext
50 * BundleContext object which can be used to track services
51 * @throws IllegalArgumentException
52 * if the given bundle is null.
54 public ApplicationContextTracker(final Bundle contributorBundle
,
55 final BundleContext factoryBundleContext
) {
56 final String filter
= format(FILTER
,
57 contributorBundle
.getSymbolicName());
59 applicationContextServiceTracker
= new ServiceTracker(
60 factoryBundleContext
, FrameworkUtil
.createFilter(filter
),
62 applicationContextServiceTracker
.open();
63 } catch (final InvalidSyntaxException e
) {
69 if (applicationContextServiceTracker
!= null) {
70 applicationContextServiceTracker
.close();
74 public ApplicationContext
getApplicationContext() {
75 ApplicationContext applicationContext
= null;
76 if (applicationContextServiceTracker
!= null) {
78 applicationContext
= (ApplicationContext
) applicationContextServiceTracker
79 .waitForService(5000);
80 } catch (InterruptedException e
) {
84 return applicationContext
;
88 protected void finalize() throws Throwable
{
93 static ApplicationContext
getApplicationContext(String bundleSymbolicName
) {
94 Bundle contributorBundle
= Platform
.getBundle(bundleSymbolicName
);
95 return getApplicationContext(contributorBundle
);
98 static ApplicationContext
getApplicationContext(Bundle contributorBundle
) {
99 if (log
.isTraceEnabled())
100 log
.trace("Get application context for bundle " + contributorBundle
);
102 if (contributorBundle
.getState() != Bundle
.ACTIVE
103 && contributorBundle
.getState() != Bundle
.STARTING
) {
105 log
.info("Starting bundle: "
106 + contributorBundle
.getSymbolicName());
107 contributorBundle
.start();
108 } catch (BundleException e
) {
113 final ApplicationContextTracker applicationContextTracker
= new ApplicationContextTracker(
114 contributorBundle
, contributorBundle
.getBundleContext());
115 ApplicationContext applicationContext
= null;
117 applicationContext
= applicationContextTracker
118 .getApplicationContext();
120 applicationContextTracker
.close();
122 return applicationContext
;