X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=eclipse%2Fplugins%2Forg.argeo.eclipse.ui%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Feclipse%2Fspring%2FApplicationContextTracker.java;h=e29d5a05690e2974027c06184ff138570fb1f0c0;hb=6c587214d5c68fba28a3fbc0bf258c5e7c470aa1;hp=be53a97fcba764029ce9534753d0cce31b122891;hpb=e0c5ce719b9eb9ed21baad8a7b4bb876b549d9a6;p=lgpl%2Fargeo-commons.git diff --git a/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/ApplicationContextTracker.java b/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/ApplicationContextTracker.java index be53a97fc..e29d5a056 100644 --- a/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/ApplicationContextTracker.java +++ b/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/ApplicationContextTracker.java @@ -1,9 +1,30 @@ +/* + * Copyright (C) 2010 Mathieu Baudier + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.argeo.eclipse.spring; import static java.text.MessageFormat.format; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.argeo.eclipse.ui.ArgeoUiPlugin; +import org.eclipse.core.runtime.Platform; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; +import org.osgi.framework.BundleException; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.InvalidSyntaxException; import org.osgi.util.tracker.ServiceTracker; @@ -11,8 +32,11 @@ import org.springframework.context.ApplicationContext; /** * @author Heiko Seeberger + * @author Mathieu Baudier */ -public class ApplicationContextTracker { +class ApplicationContextTracker { + private final static Log log = LogFactory + .getLog(ApplicationContextTracker.class); private static final String FILTER = "(&(objectClass=org.springframework.context.ApplicationContext)" //$NON-NLS-1$ + "(org.springframework.context.service.name={0}))"; //$NON-NLS-1$ @@ -21,18 +45,21 @@ public class ApplicationContextTracker { /** * @param contributorBundle - * OSGi bundle for which the Spring application context is to be - * tracked. Must not be null! + * OSGi bundle for which the Spring application context is to be + * tracked. Must not be null! * @param factoryBundleContext - * BundleContext object which can be used to track services + * BundleContext object which can be used to track services * @throws IllegalArgumentException - * if the given bundle is null. + * if the given bundle is null. */ - public ApplicationContextTracker(final Bundle contributorBundle, final BundleContext factoryBundleContext) { - final String filter = format(FILTER, contributorBundle.getSymbolicName()); + public ApplicationContextTracker(final Bundle contributorBundle, + final BundleContext factoryBundleContext) { + final String filter = format(FILTER, contributorBundle + .getSymbolicName()); try { applicationContextServiceTracker = new ServiceTracker( - factoryBundleContext, FrameworkUtil.createFilter(filter), null); + factoryBundleContext, FrameworkUtil.createFilter(filter), + null); applicationContextServiceTracker.open(); } catch (final InvalidSyntaxException e) { e.printStackTrace(); @@ -63,4 +90,39 @@ public class ApplicationContextTracker { close(); super.finalize(); } + + static ApplicationContext getApplicationContext(String bundleSymbolicName) { + Bundle contributorBundle = Platform.getBundle(bundleSymbolicName); + return getApplicationContext(contributorBundle); + } + + static ApplicationContext getApplicationContext(Bundle contributorBundle) { + if (log.isTraceEnabled()) + log + .trace("Get application context for bundle " + + contributorBundle); + + if (contributorBundle.getState() != Bundle.ACTIVE + && contributorBundle.getState() != Bundle.STARTING) { + try { + log.info("Starting bundle: " + + contributorBundle.getSymbolicName()); + contributorBundle.start(); + } catch (BundleException e) { + e.printStackTrace(); + } + } + + final ApplicationContextTracker applicationContextTracker = new ApplicationContextTracker( + contributorBundle, ArgeoUiPlugin.getDefault() + .getBundleContext()); + ApplicationContext applicationContext = null; + try { + applicationContext = applicationContextTracker + .getApplicationContext(); + } finally { + applicationContextTracker.close(); + } + return applicationContext; + } }