X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=eclipse%2Fplugins%2Forg.argeo.eclipse.ui%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Feclipse%2Fspring%2FSpringExtensionFactory.java;h=49a226d39339bf15d5a7dffb3884f79709896897;hb=f41458fe3c21479bf614b3cf03cedf621a91e36c;hp=f92b423e9799e3f7e510301ee93999d508f3d089;hpb=e0c5ce719b9eb9ed21baad8a7b4bb876b549d9a6;p=lgpl%2Fargeo-commons.git diff --git a/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/SpringExtensionFactory.java b/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/SpringExtensionFactory.java index f92b423e9..49a226d39 100644 --- a/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/SpringExtensionFactory.java +++ b/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/SpringExtensionFactory.java @@ -1,14 +1,27 @@ +/* + * 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 org.argeo.eclipse.ui.ArgeoUiPlugin; +import org.argeo.ArgeoException; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IExecutableExtension; import org.eclipse.core.runtime.IExecutableExtensionFactory; import org.eclipse.core.runtime.IExtension; -import org.eclipse.core.runtime.Platform; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleException; import org.springframework.context.ApplicationContext; /** @@ -29,7 +42,12 @@ import org.springframework.context.ApplicationContext; * spring extension factory uses the id of the extension itself to identify the * bean. * + * original code from: Blog entry + * * @author Martin Lippert + * @author mbaudier */ public class SpringExtensionFactory implements IExecutableExtensionFactory, IExecutableExtension { @@ -37,25 +55,35 @@ public class SpringExtensionFactory implements IExecutableExtensionFactory, private Object bean; public Object create() throws CoreException { + if (bean == null) + throw new ArgeoException("No underlying bean for extension"); return bean; } public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException { String beanName = getBeanName(data, config); - ApplicationContext appContext = getApplicationContext(config); + if (beanName == null) + throw new ArgeoException("Cannot find bean name for extension " + + config); - if (beanName != null && appContext != null) { - this.bean = appContext.getBean(beanName); - if (this.bean instanceof IExecutableExtension) { - ((IExecutableExtension) this.bean).setInitializationData( - config, propertyName, data); - } + String bundleSymbolicName = config.getContributor().getName(); + ApplicationContext appContext = ApplicationContextTracker + .getApplicationContext(bundleSymbolicName); + if (appContext == null) + throw new ArgeoException( + "Cannot find application context for bundle " + + bundleSymbolicName); + + this.bean = appContext.getBean(beanName); + if (this.bean instanceof IExecutableExtension) { + ((IExecutableExtension) this.bean).setInitializationData(config, + propertyName, data); } } private String getBeanName(Object data, IConfigurationElement config) { - + // try the specific bean id the extension defines if (data != null && data.toString().length() > 0) { return data.toString(); @@ -76,30 +104,4 @@ public class SpringExtensionFactory implements IExecutableExtensionFactory, return null; } - private ApplicationContext getApplicationContext( - IConfigurationElement config) { - String contributorName = config.getContributor().getName(); - Bundle contributorBundle = Platform.getBundle(contributorName); - - if (contributorBundle.getState() != Bundle.ACTIVE && contributorBundle.getState() != Bundle.STARTING) { - try { - System.out.println("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; - } - }