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 @SuppressWarnings("rawtypes")
50 private ServiceTracker applicationContextServiceTracker
;
53 * @param contributorBundle
54 * OSGi bundle for which the Spring application context is to be
55 * tracked. Must not be null!
56 * @param factoryBundleContext
57 * BundleContext object which can be used to track services
58 * @throws IllegalArgumentException
59 * if the given bundle is null.
61 @SuppressWarnings({ "unchecked", "rawtypes" })
62 public ApplicationContextTracker(final Bundle contributorBundle
,
63 final BundleContext factoryBundleContext
) {
64 final String filter
= format(FILTER
,
65 contributorBundle
.getSymbolicName());
67 applicationContextServiceTracker
= new ServiceTracker(
68 factoryBundleContext
, FrameworkUtil
.createFilter(filter
),
70 // applicationContextServiceTracker.open();
71 } catch (final InvalidSyntaxException e
) {
77 if (applicationContextServiceTracker
!= null) {
78 applicationContextServiceTracker
.open();
83 if (applicationContextServiceTracker
!= null) {
84 applicationContextServiceTracker
.close();
88 public ApplicationContext
getApplicationContext() {
89 ApplicationContext applicationContext
= null;
90 if (applicationContextServiceTracker
!= null) {
92 applicationContext
= (ApplicationContext
) applicationContextServiceTracker
93 .waitForService(defaultTimeout
);
94 } catch (InterruptedException e
) {
98 return applicationContext
;
102 protected void finalize() throws Throwable
{
107 static ApplicationContext
getApplicationContext(String bundleSymbolicName
) {
108 Bundle contributorBundle
= Platform
.getBundle(bundleSymbolicName
);
109 return getApplicationContext(contributorBundle
);
112 static ApplicationContext
getApplicationContext(
113 final Bundle contributorBundle
) {
114 if (log
.isTraceEnabled())
115 log
.trace("Get application context for bundle " + contributorBundle
);
117 // Start if not yet started (also if in STARTING state, may be lazy)
118 if (contributorBundle
.getState() != Bundle
.ACTIVE
) {
119 if (log
.isTraceEnabled())
120 log
.trace("Starting bundle: "
121 + contributorBundle
.getSymbolicName());
122 // Thread startBundle = new Thread("Start bundle "
123 // + contributorBundle.getSymbolicName()) {
124 // public void run() {
126 contributorBundle
.start();
127 } catch (BundleException e
) {
128 log
.error("Cannot start bundle " + contributorBundle
, e
);
132 // startBundle.start();
134 // startBundle.join(10 * 1000l);
135 // } catch (InterruptedException e) {
140 final ApplicationContextTracker applicationContextTracker
= new ApplicationContextTracker(
141 contributorBundle
, contributorBundle
.getBundleContext());
142 ApplicationContext applicationContext
= null;
144 applicationContextTracker
.open();
145 applicationContext
= applicationContextTracker
146 .getApplicationContext();
148 applicationContextTracker
.close();
150 return applicationContext
;