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 * Tracks Spring application context published as services.
35 * @author Heiko Seeberger
36 * @author Mathieu Baudier
38 class ApplicationContextTracker
{
39 private final static Log log
= LogFactory
40 .getLog(ApplicationContextTracker
.class);
42 private static final String FILTER
= "(&(objectClass=org.springframework.context.ApplicationContext)" //$NON-NLS-1$
43 + "(org.springframework.context.service.name={0}))"; //$NON-NLS-1$
45 public final static String APPLICATION_CONTEXT_TRACKER_TIMEOUT
= "org.argeo.eclipse.spring.applicationContextTrackerTimeout";
47 private static Long defaultTimeout
= Long
.parseLong(System
.getProperty(
48 APPLICATION_CONTEXT_TRACKER_TIMEOUT
, "30000"));
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 public ApplicationContextTracker(final Bundle contributorBundle
,
62 final BundleContext factoryBundleContext
) {
63 final String filter
= format(FILTER
,
64 contributorBundle
.getSymbolicName());
66 applicationContextServiceTracker
= new ServiceTracker(
67 factoryBundleContext
, FrameworkUtil
.createFilter(filter
),
69 // applicationContextServiceTracker.open();
70 } catch (final InvalidSyntaxException e
) {
76 if (applicationContextServiceTracker
!= null) {
77 applicationContextServiceTracker
.open();
82 if (applicationContextServiceTracker
!= null) {
83 applicationContextServiceTracker
.close();
87 public ApplicationContext
getApplicationContext() {
88 ApplicationContext applicationContext
= null;
89 if (applicationContextServiceTracker
!= null) {
91 applicationContext
= (ApplicationContext
) applicationContextServiceTracker
92 .waitForService(defaultTimeout
);
93 } catch (InterruptedException e
) {
97 return applicationContext
;
101 protected void finalize() throws Throwable
{
106 static ApplicationContext
getApplicationContext(String bundleSymbolicName
) {
107 Bundle contributorBundle
= Platform
.getBundle(bundleSymbolicName
);
108 return getApplicationContext(contributorBundle
);
111 static ApplicationContext
getApplicationContext(Bundle contributorBundle
) {
112 if (log
.isTraceEnabled())
113 log
.trace("Get application context for bundle " + contributorBundle
);
115 if (contributorBundle
.getState() != Bundle
.ACTIVE
116 && contributorBundle
.getState() != Bundle
.STARTING
) {
118 if (log
.isTraceEnabled())
119 log
.trace("Starting bundle: "
120 + contributorBundle
.getSymbolicName());
121 contributorBundle
.start();
122 } catch (BundleException e
) {
127 final ApplicationContextTracker applicationContextTracker
= new ApplicationContextTracker(
128 contributorBundle
, contributorBundle
.getBundleContext());
129 ApplicationContext applicationContext
= null;
131 applicationContextTracker
.open();
132 applicationContext
= applicationContextTracker
133 .getApplicationContext();
135 applicationContextTracker
.close();
137 return applicationContext
;