2 * Copyright (C) 2007-2012 Argeo GmbH
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.
16 package org
.argeo
.eclipse
.spring
;
18 import static java
.text
.MessageFormat
.format
;
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
;
32 * Tracks Spring application context published as services.
34 * @author Heiko Seeberger
35 * @author Mathieu Baudier
37 class ApplicationContextTracker
{
38 private final static Log log
= LogFactory
39 .getLog(ApplicationContextTracker
.class);
41 private static final String FILTER
= "(&(objectClass=org.springframework.context.ApplicationContext)" //$NON-NLS-1$
42 + "(org.springframework.context.service.name={0}))"; //$NON-NLS-1$
44 public final static String APPLICATION_CONTEXT_TRACKER_TIMEOUT
= "org.argeo.eclipse.spring.applicationContextTrackerTimeout";
46 private static Long defaultTimeout
= Long
.parseLong(System
.getProperty(
47 APPLICATION_CONTEXT_TRACKER_TIMEOUT
, "30000"));
49 private ServiceTracker applicationContextServiceTracker
;
52 * @param contributorBundle
53 * OSGi bundle for which the Spring application context is to be
54 * tracked. Must not be null!
55 * @param factoryBundleContext
56 * BundleContext object which can be used to track services
57 * @throws IllegalArgumentException
58 * if the given bundle is null.
60 public ApplicationContextTracker(final Bundle contributorBundle
,
61 final BundleContext factoryBundleContext
) {
62 final String filter
= format(FILTER
,
63 contributorBundle
.getSymbolicName());
65 applicationContextServiceTracker
= new ServiceTracker(
66 factoryBundleContext
, FrameworkUtil
.createFilter(filter
),
68 // applicationContextServiceTracker.open();
69 } catch (final InvalidSyntaxException e
) {
75 if (applicationContextServiceTracker
!= null) {
76 applicationContextServiceTracker
.open();
81 if (applicationContextServiceTracker
!= null) {
82 applicationContextServiceTracker
.close();
86 public ApplicationContext
getApplicationContext() {
87 ApplicationContext applicationContext
= null;
88 if (applicationContextServiceTracker
!= null) {
90 applicationContext
= (ApplicationContext
) applicationContextServiceTracker
91 .waitForService(defaultTimeout
);
92 } catch (InterruptedException e
) {
96 return applicationContext
;
100 protected void finalize() throws Throwable
{
105 static ApplicationContext
getApplicationContext(String bundleSymbolicName
) {
106 Bundle contributorBundle
= Platform
.getBundle(bundleSymbolicName
);
107 return getApplicationContext(contributorBundle
);
110 static ApplicationContext
getApplicationContext(
111 final Bundle contributorBundle
) {
112 if (log
.isTraceEnabled())
113 log
.trace("Get application context for bundle " + contributorBundle
);
115 // Start if not yet started (also if in STARTING state, may be lazy)
116 if (contributorBundle
.getState() != Bundle
.ACTIVE
) {
117 if (log
.isTraceEnabled())
118 log
.trace("Starting bundle: "
119 + contributorBundle
.getSymbolicName());
120 // Thread startBundle = new Thread("Start bundle "
121 // + contributorBundle.getSymbolicName()) {
122 // public void run() {
124 contributorBundle
.start();
125 } catch (BundleException e
) {
126 log
.error("Cannot start bundle " + contributorBundle
, e
);
130 // startBundle.start();
132 // startBundle.join(10 * 1000l);
133 // } catch (InterruptedException e) {
138 final ApplicationContextTracker applicationContextTracker
= new ApplicationContextTracker(
139 contributorBundle
, contributorBundle
.getBundleContext());
140 ApplicationContext applicationContext
= null;
142 applicationContextTracker
.open();
143 applicationContext
= applicationContextTracker
144 .getApplicationContext();
146 applicationContextTracker
.close();
148 return applicationContext
;