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
.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
;
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 private ServiceTracker applicationContextServiceTracker
;
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.
55 public ApplicationContextTracker(final Bundle contributorBundle
,
56 final BundleContext factoryBundleContext
) {
57 final String filter
= format(FILTER
, contributorBundle
60 applicationContextServiceTracker
= new ServiceTracker(
61 factoryBundleContext
, FrameworkUtil
.createFilter(filter
),
63 applicationContextServiceTracker
.open();
64 } catch (final InvalidSyntaxException e
) {
70 if (applicationContextServiceTracker
!= null) {
71 applicationContextServiceTracker
.close();
75 public ApplicationContext
getApplicationContext() {
76 ApplicationContext applicationContext
= null;
77 if (applicationContextServiceTracker
!= null) {
79 applicationContext
= (ApplicationContext
) applicationContextServiceTracker
80 .waitForService(5000);
81 } catch (InterruptedException e
) {
85 return applicationContext
;
89 protected void finalize() throws Throwable
{
94 static ApplicationContext
getApplicationContext(String bundleSymbolicName
) {
95 Bundle contributorBundle
= Platform
.getBundle(bundleSymbolicName
);
96 return getApplicationContext(contributorBundle
);
99 static ApplicationContext
getApplicationContext(Bundle contributorBundle
) {
100 if (log
.isTraceEnabled())
102 .trace("Get application context for bundle "
103 + contributorBundle
);
105 if (contributorBundle
.getState() != Bundle
.ACTIVE
106 && contributorBundle
.getState() != Bundle
.STARTING
) {
108 log
.info("Starting bundle: "
109 + contributorBundle
.getSymbolicName());
110 contributorBundle
.start();
111 } catch (BundleException e
) {
116 final ApplicationContextTracker applicationContextTracker
= new ApplicationContextTracker(
117 contributorBundle
, ArgeoUiPlugin
.getDefault()
118 .getBundleContext());
119 ApplicationContext applicationContext
= null;
121 applicationContext
= applicationContextTracker
122 .getApplicationContext();
124 applicationContextTracker
.close();
126 return applicationContext
;