From: Mathieu Baudier Date: Wed, 14 Sep 2016 16:53:22 +0000 (+0000) Subject: Improve workbench layer X-Git-Tag: argeo-commons-2.1.46~33 X-Git-Url: http://git.argeo.org/?a=commitdiff_plain;h=86cb759ad0ce612b138006d2c12445a3d89fce1b;hp=972528f4de2d00690362c01d3ce843ca9cd10250;p=lgpl%2Fargeo-commons.git Improve workbench layer git-svn-id: https://svn.argeo.org/commons/trunk@9143 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- diff --git a/org.argeo.cms.ui.workbench.rap/bnd.bnd b/org.argeo.cms.ui.workbench.rap/bnd.bnd index 31a99179c..e80b9ecae 100644 --- a/org.argeo.cms.ui.workbench.rap/bnd.bnd +++ b/org.argeo.cms.ui.workbench.rap/bnd.bnd @@ -1,6 +1,7 @@ Bundle-SymbolicName: org.argeo.cms.ui.workbench.rap;singleton:=true Bundle-Activator: org.argeo.security.ui.rap.SecureRapActivator Bundle-ActivationPolicy: lazy + Require-Bundle: org.eclipse.rap.ui,org.eclipse.core.runtime Import-Package: org.argeo.eclipse.spring,\ diff --git a/org.argeo.cms.ui.workbench/bnd.bnd b/org.argeo.cms.ui.workbench/bnd.bnd index 2a5b6bf67..3e0d1c69c 100644 --- a/org.argeo.cms.ui.workbench/bnd.bnd +++ b/org.argeo.cms.ui.workbench/bnd.bnd @@ -2,7 +2,8 @@ Bundle-SymbolicName: org.argeo.cms.ui.workbench;singleton:=true Bundle-Activator: org.argeo.security.ui.SecurityUiPlugin Bundle-ActivationPolicy: lazy -Require-Bundle: org.eclipse.core.runtime +Require-Bundle: org.eclipse.core.runtime,\ +org.eclipse.core.commands Import-Package: org.argeo.cms.auth,\ org.argeo.eclipse.spring,\ @@ -13,6 +14,10 @@ org.eclipse.swt.widgets,\ org.eclipse.ui.services,\ org.osgi.framework,\ org.springframework.core,\ +org.osgi.util.tracker,\ +org.springframework.beans.factory,\ +org.springframework.core.io.support,\ +!org.eclipse.core.runtime,\ * diff --git a/org.argeo.cms.ui.workbench/src/org/argeo/eclipse/spring/ApplicationContextTracker.java b/org.argeo.cms.ui.workbench/src/org/argeo/eclipse/spring/ApplicationContextTracker.java new file mode 100644 index 000000000..1d3df4304 --- /dev/null +++ b/org.argeo.cms.ui.workbench/src/org/argeo/eclipse/spring/ApplicationContextTracker.java @@ -0,0 +1,152 @@ +/* + * Copyright (C) 2007-2012 Argeo GmbH + * + * 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.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; +import org.springframework.context.ApplicationContext; + +/** + * Tracks Spring application context published as services. + * + * @author Heiko Seeberger + * @author Mathieu Baudier + */ +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$ + + public final static String APPLICATION_CONTEXT_TRACKER_TIMEOUT = "org.argeo.eclipse.spring.applicationContextTrackerTimeout"; + + private static Long defaultTimeout = Long.parseLong(System.getProperty( + APPLICATION_CONTEXT_TRACKER_TIMEOUT, "30000")); + + @SuppressWarnings("rawtypes") + private ServiceTracker applicationContextServiceTracker; + + /** + * @param contributorBundle + * 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 + * @throws IllegalArgumentException + * if the given bundle is null. + */ + @SuppressWarnings({ "unchecked", "rawtypes" }) + public ApplicationContextTracker(final Bundle contributorBundle, + final BundleContext factoryBundleContext) { + final String filter = format(FILTER, + contributorBundle.getSymbolicName()); + try { + applicationContextServiceTracker = new ServiceTracker( + factoryBundleContext, FrameworkUtil.createFilter(filter), + null); + // applicationContextServiceTracker.open(); + } catch (final InvalidSyntaxException e) { + e.printStackTrace(); + } + } + + public void open() { + if (applicationContextServiceTracker != null) { + applicationContextServiceTracker.open(); + } + } + + public void close() { + if (applicationContextServiceTracker != null) { + applicationContextServiceTracker.close(); + } + } + + public ApplicationContext getApplicationContext() { + ApplicationContext applicationContext = null; + if (applicationContextServiceTracker != null) { + try { + applicationContext = (ApplicationContext) applicationContextServiceTracker + .waitForService(defaultTimeout); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + return applicationContext; + } + + @Override + protected void finalize() throws Throwable { + close(); + super.finalize(); + } + + static ApplicationContext getApplicationContext(String bundleSymbolicName) { + Bundle contributorBundle = Platform.getBundle(bundleSymbolicName); + return getApplicationContext(contributorBundle); + } + + static ApplicationContext getApplicationContext( + final Bundle contributorBundle) { + if (log.isTraceEnabled()) + log.trace("Get application context for bundle " + contributorBundle); + + // Start if not yet started (also if in STARTING state, may be lazy) + if (contributorBundle.getState() != Bundle.ACTIVE) { + if (log.isTraceEnabled()) + log.trace("Starting bundle: " + + contributorBundle.getSymbolicName()); + // Thread startBundle = new Thread("Start bundle " + // + contributorBundle.getSymbolicName()) { + // public void run() { + try { + contributorBundle.start(); + } catch (BundleException e) { + log.error("Cannot start bundle " + contributorBundle, e); + } + // } + // }; + // startBundle.start(); + // try { + // startBundle.join(10 * 1000l); + // } catch (InterruptedException e) { + // // silent + // } + } + + final ApplicationContextTracker applicationContextTracker = new ApplicationContextTracker( + contributorBundle, contributorBundle.getBundleContext()); + ApplicationContext applicationContext = null; + try { + applicationContextTracker.open(); + applicationContext = applicationContextTracker + .getApplicationContext(); + } finally { + applicationContextTracker.close(); + } + return applicationContext; + } +} diff --git a/org.argeo.cms.ui.workbench/src/org/argeo/eclipse/spring/SpringCommandHandler.java b/org.argeo.cms.ui.workbench/src/org/argeo/eclipse/spring/SpringCommandHandler.java new file mode 100644 index 000000000..1bbf9cbed --- /dev/null +++ b/org.argeo.cms.ui.workbench/src/org/argeo/eclipse/spring/SpringCommandHandler.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2007-2012 Argeo GmbH + * + * 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.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.argeo.eclipse.ui.EclipseUiException; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.commands.IHandler; +import org.eclipse.core.commands.IHandlerListener; +import org.springframework.context.ApplicationContext; + +/** Allows to declare Eclipse commands as Spring beans */ +public class SpringCommandHandler implements IHandler { + private final static Log log = LogFactory + .getLog(SpringCommandHandler.class); + + public void addHandlerListener(IHandlerListener handlerListener) { + } + + public void dispose() { + } + + public Object execute(ExecutionEvent event) throws ExecutionException { + String commandId = event.getCommand().getId(); + String bundleSymbolicName = commandId.substring(0, + commandId.lastIndexOf('.')); + try { + if (log.isTraceEnabled()) + log.trace("Execute " + event + " via spring command handler " + + this); + // TODO: make it more flexible and robust + ApplicationContext applicationContext = ApplicationContextTracker + .getApplicationContext(bundleSymbolicName); + if (applicationContext == null) + throw new EclipseUiException("No application context found for " + + bundleSymbolicName); + + // retrieve the command via its id + String beanName = event.getCommand().getId(); + + if (!applicationContext.containsBean(beanName)) { + if (beanName.startsWith(bundleSymbolicName)) + beanName = beanName + .substring(bundleSymbolicName.length() + 1); + } + + if (!applicationContext.containsBean(beanName)) + throw new ExecutionException("No bean found with name " + + beanName + " in bundle " + bundleSymbolicName); + Object bean = applicationContext.getBean(beanName); + + if (!(bean instanceof IHandler)) + throw new ExecutionException("Bean with name " + beanName + + " and class " + bean.getClass() + + " does not implement the IHandler interface."); + + IHandler handler = (IHandler) bean; + return handler.execute(event); + } catch (Exception e) { + // TODO: use eclipse error management + // log.error(e); + throw new ExecutionException("Cannot execute Spring command " + + commandId + " in bundle " + bundleSymbolicName, e); + } + } + + public boolean isEnabled() { + return true; + } + + public boolean isHandled() { + return true; + } + + public void removeHandlerListener(IHandlerListener handlerListener) { + } + +} diff --git a/org.argeo.cms.ui.workbench/src/org/argeo/eclipse/spring/SpringExtensionFactory.java b/org.argeo.cms.ui.workbench/src/org/argeo/eclipse/spring/SpringExtensionFactory.java new file mode 100644 index 000000000..6a5626625 --- /dev/null +++ b/org.argeo.cms.ui.workbench/src/org/argeo/eclipse/spring/SpringExtensionFactory.java @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2007-2012 Argeo GmbH + * + * 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.EclipseUiException; +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.springframework.context.ApplicationContext; + +/** + * The Spring Extension Factory builds a bridge between the Eclipse Extension + * Registry and the Spring Framework (especially Spring Dynamic Modules). + * + * It allows you to define your extension as a spring bean within the spring + * application context of your bundle. If you would like to use this bean as an + * instance of an extension (an Eclipse RCP view, for example) you define the + * extension with this spring extension factory as the class to be created. + * + * To let the spring extension factory pick the right bean from your application + * context you need to set the bean id to the same value as the id of the view + * within the view definition, for example. This is important if your extension + * definition contains more than one element, where each element has its own id. + * + * If the extension definition elements themselves have no id attribute the + * 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 { + + private Object bean; + + public Object create() throws CoreException { + if (bean == null) + throw new EclipseUiException("No underlying bean for extension"); + return bean; + } + + public void setInitializationData(IConfigurationElement config, + String propertyName, Object data) throws CoreException { + String bundleSymbolicName = config.getContributor().getName(); + ApplicationContext applicationContext = ApplicationContextTracker + .getApplicationContext(bundleSymbolicName); + if (applicationContext == null) + throw new EclipseUiException( + "Cannot find application context for bundle " + + bundleSymbolicName); + + String beanName = getBeanName(data, config); + if (beanName == null) + throw new EclipseUiException("Cannot find bean name for extension " + + config); + + if (!applicationContext.containsBean(beanName)) { + if (beanName.startsWith(bundleSymbolicName)) + beanName = beanName.substring(bundleSymbolicName.length() + 1); + } + + if (!applicationContext.containsBean(beanName)) + throw new EclipseUiException("No bean with name '" + beanName + "'"); + + this.bean = applicationContext.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(); + } + + // try the id of the config element + if (config.getAttribute("id") != null) { + return config.getAttribute("id"); + } + + // try the id of the extension element itself + if (config.getParent() != null + && config.getParent() instanceof IExtension) { + IExtension extensionDefinition = (IExtension) config.getParent(); + return extensionDefinition.getSimpleIdentifier(); + } + + return null; + } + +} diff --git a/org.argeo.eclipse.ui.workbench/bnd.bnd b/org.argeo.eclipse.ui.workbench/bnd.bnd index e2e226812..26b6cf263 100644 --- a/org.argeo.eclipse.ui.workbench/bnd.bnd +++ b/org.argeo.eclipse.ui.workbench/bnd.bnd @@ -4,15 +4,14 @@ Bundle-Activator: org.argeo.eclipse.ui.workbench.WorkbenchUiPlugin Require-Bundle: org.eclipse.ui;resolution:=optional,\ org.eclipse.core.runtime,\ -org.eclipse.rap.ui;resolution:=optional,\ -org.eclipse.rap.ui.workbench;resolution:=optional +#org.eclipse.rap.ui;resolution:=optional,\ +#org.eclipse.rap.ui.workbench;resolution:=optional Import-Package: org.argeo.eclipse.ui.specific,\ org.eclipse.swt.widgets,\ org.osgi.framework,\ org.osgi.service.packageadmin,\ -org.osgi.util.tracker,\ -org.springframework.beans.factory,\ -org.springframework.core.io.support,\ !org.eclipse.core.runtime,\ +org.eclipse.swt,\ +org.eclipse.jface.window,\ * diff --git a/org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/spring/ApplicationContextTracker.java b/org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/spring/ApplicationContextTracker.java deleted file mode 100644 index 1d3df4304..000000000 --- a/org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/spring/ApplicationContextTracker.java +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Copyright (C) 2007-2012 Argeo GmbH - * - * 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.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; -import org.springframework.context.ApplicationContext; - -/** - * Tracks Spring application context published as services. - * - * @author Heiko Seeberger - * @author Mathieu Baudier - */ -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$ - - public final static String APPLICATION_CONTEXT_TRACKER_TIMEOUT = "org.argeo.eclipse.spring.applicationContextTrackerTimeout"; - - private static Long defaultTimeout = Long.parseLong(System.getProperty( - APPLICATION_CONTEXT_TRACKER_TIMEOUT, "30000")); - - @SuppressWarnings("rawtypes") - private ServiceTracker applicationContextServiceTracker; - - /** - * @param contributorBundle - * 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 - * @throws IllegalArgumentException - * if the given bundle is null. - */ - @SuppressWarnings({ "unchecked", "rawtypes" }) - public ApplicationContextTracker(final Bundle contributorBundle, - final BundleContext factoryBundleContext) { - final String filter = format(FILTER, - contributorBundle.getSymbolicName()); - try { - applicationContextServiceTracker = new ServiceTracker( - factoryBundleContext, FrameworkUtil.createFilter(filter), - null); - // applicationContextServiceTracker.open(); - } catch (final InvalidSyntaxException e) { - e.printStackTrace(); - } - } - - public void open() { - if (applicationContextServiceTracker != null) { - applicationContextServiceTracker.open(); - } - } - - public void close() { - if (applicationContextServiceTracker != null) { - applicationContextServiceTracker.close(); - } - } - - public ApplicationContext getApplicationContext() { - ApplicationContext applicationContext = null; - if (applicationContextServiceTracker != null) { - try { - applicationContext = (ApplicationContext) applicationContextServiceTracker - .waitForService(defaultTimeout); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - return applicationContext; - } - - @Override - protected void finalize() throws Throwable { - close(); - super.finalize(); - } - - static ApplicationContext getApplicationContext(String bundleSymbolicName) { - Bundle contributorBundle = Platform.getBundle(bundleSymbolicName); - return getApplicationContext(contributorBundle); - } - - static ApplicationContext getApplicationContext( - final Bundle contributorBundle) { - if (log.isTraceEnabled()) - log.trace("Get application context for bundle " + contributorBundle); - - // Start if not yet started (also if in STARTING state, may be lazy) - if (contributorBundle.getState() != Bundle.ACTIVE) { - if (log.isTraceEnabled()) - log.trace("Starting bundle: " - + contributorBundle.getSymbolicName()); - // Thread startBundle = new Thread("Start bundle " - // + contributorBundle.getSymbolicName()) { - // public void run() { - try { - contributorBundle.start(); - } catch (BundleException e) { - log.error("Cannot start bundle " + contributorBundle, e); - } - // } - // }; - // startBundle.start(); - // try { - // startBundle.join(10 * 1000l); - // } catch (InterruptedException e) { - // // silent - // } - } - - final ApplicationContextTracker applicationContextTracker = new ApplicationContextTracker( - contributorBundle, contributorBundle.getBundleContext()); - ApplicationContext applicationContext = null; - try { - applicationContextTracker.open(); - applicationContext = applicationContextTracker - .getApplicationContext(); - } finally { - applicationContextTracker.close(); - } - return applicationContext; - } -} diff --git a/org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/spring/SpringCommandHandler.java b/org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/spring/SpringCommandHandler.java deleted file mode 100644 index 1bbf9cbed..000000000 --- a/org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/spring/SpringCommandHandler.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (C) 2007-2012 Argeo GmbH - * - * 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.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.argeo.eclipse.ui.EclipseUiException; -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.core.commands.IHandler; -import org.eclipse.core.commands.IHandlerListener; -import org.springframework.context.ApplicationContext; - -/** Allows to declare Eclipse commands as Spring beans */ -public class SpringCommandHandler implements IHandler { - private final static Log log = LogFactory - .getLog(SpringCommandHandler.class); - - public void addHandlerListener(IHandlerListener handlerListener) { - } - - public void dispose() { - } - - public Object execute(ExecutionEvent event) throws ExecutionException { - String commandId = event.getCommand().getId(); - String bundleSymbolicName = commandId.substring(0, - commandId.lastIndexOf('.')); - try { - if (log.isTraceEnabled()) - log.trace("Execute " + event + " via spring command handler " - + this); - // TODO: make it more flexible and robust - ApplicationContext applicationContext = ApplicationContextTracker - .getApplicationContext(bundleSymbolicName); - if (applicationContext == null) - throw new EclipseUiException("No application context found for " - + bundleSymbolicName); - - // retrieve the command via its id - String beanName = event.getCommand().getId(); - - if (!applicationContext.containsBean(beanName)) { - if (beanName.startsWith(bundleSymbolicName)) - beanName = beanName - .substring(bundleSymbolicName.length() + 1); - } - - if (!applicationContext.containsBean(beanName)) - throw new ExecutionException("No bean found with name " - + beanName + " in bundle " + bundleSymbolicName); - Object bean = applicationContext.getBean(beanName); - - if (!(bean instanceof IHandler)) - throw new ExecutionException("Bean with name " + beanName - + " and class " + bean.getClass() - + " does not implement the IHandler interface."); - - IHandler handler = (IHandler) bean; - return handler.execute(event); - } catch (Exception e) { - // TODO: use eclipse error management - // log.error(e); - throw new ExecutionException("Cannot execute Spring command " - + commandId + " in bundle " + bundleSymbolicName, e); - } - } - - public boolean isEnabled() { - return true; - } - - public boolean isHandled() { - return true; - } - - public void removeHandlerListener(IHandlerListener handlerListener) { - } - -} diff --git a/org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/spring/SpringExtensionFactory.java b/org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/spring/SpringExtensionFactory.java deleted file mode 100644 index 6a5626625..000000000 --- a/org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/spring/SpringExtensionFactory.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (C) 2007-2012 Argeo GmbH - * - * 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.EclipseUiException; -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.springframework.context.ApplicationContext; - -/** - * The Spring Extension Factory builds a bridge between the Eclipse Extension - * Registry and the Spring Framework (especially Spring Dynamic Modules). - * - * It allows you to define your extension as a spring bean within the spring - * application context of your bundle. If you would like to use this bean as an - * instance of an extension (an Eclipse RCP view, for example) you define the - * extension with this spring extension factory as the class to be created. - * - * To let the spring extension factory pick the right bean from your application - * context you need to set the bean id to the same value as the id of the view - * within the view definition, for example. This is important if your extension - * definition contains more than one element, where each element has its own id. - * - * If the extension definition elements themselves have no id attribute the - * 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 { - - private Object bean; - - public Object create() throws CoreException { - if (bean == null) - throw new EclipseUiException("No underlying bean for extension"); - return bean; - } - - public void setInitializationData(IConfigurationElement config, - String propertyName, Object data) throws CoreException { - String bundleSymbolicName = config.getContributor().getName(); - ApplicationContext applicationContext = ApplicationContextTracker - .getApplicationContext(bundleSymbolicName); - if (applicationContext == null) - throw new EclipseUiException( - "Cannot find application context for bundle " - + bundleSymbolicName); - - String beanName = getBeanName(data, config); - if (beanName == null) - throw new EclipseUiException("Cannot find bean name for extension " - + config); - - if (!applicationContext.containsBean(beanName)) { - if (beanName.startsWith(bundleSymbolicName)) - beanName = beanName.substring(bundleSymbolicName.length() + 1); - } - - if (!applicationContext.containsBean(beanName)) - throw new EclipseUiException("No bean with name '" + beanName + "'"); - - this.bean = applicationContext.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(); - } - - // try the id of the config element - if (config.getAttribute("id") != null) { - return config.getAttribute("id"); - } - - // try the id of the extension element itself - if (config.getParent() != null - && config.getParent() instanceof IExtension) { - IExtension extensionDefinition = (IExtension) config.getParent(); - return extensionDefinition.getSimpleIdentifier(); - } - - return null; - } - -} diff --git a/org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/jcr/JcrPreferenceStore.java b/org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/jcr/JcrPreferenceStore.java index 27349f95d..c90eec067 100644 --- a/org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/jcr/JcrPreferenceStore.java +++ b/org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/jcr/JcrPreferenceStore.java @@ -34,7 +34,6 @@ import org.argeo.jcr.ArgeoTypes; import org.argeo.jcr.JcrUtils; import org.argeo.jcr.UserJcrUtils; import org.eclipse.jface.preference.PreferenceStore; -import org.eclipse.ui.preferences.ScopedPreferenceStore; import org.osgi.framework.BundleContext; /** @@ -42,7 +41,7 @@ import org.osgi.framework.BundleContext; * TODO: better integrate JCR and Eclipse:
* - typing
* - use eclipse preferences
- * - better integrate with {@link ScopedPreferenceStore} provided by RAP + * - better integrate with ScopedPreferenceStore provided by RAP */ public class JcrPreferenceStore extends PreferenceStore implements ArgeoNames { private static final long serialVersionUID = 1854011367784598758L;