From e0c5ce719b9eb9ed21baad8a7b4bb876b549d9a6 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Thu, 29 Jul 2010 13:06:54 +0000 Subject: [PATCH] Introduce Eclipse Commons git-svn-id: https://svn.argeo.org/commons/trunk@3730 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../plugins/org.argeo.eclipse.ui/.classpath | 7 ++ eclipse/plugins/org.argeo.eclipse.ui/.project | 28 +++++ .../.settings/org.eclipse.jdt.core.prefs | 8 ++ .../.settings/org.eclipse.pde.core.prefs | 3 + .../org.argeo.eclipse.ui/META-INF/MANIFEST.MF | 16 +++ .../org.argeo.eclipse.ui/build.properties | 4 + .../spring/ApplicationContextTracker.java | 66 +++++++++++ .../spring/SpringExtensionFactory.java | 105 ++++++++++++++++++ .../org/argeo/eclipse/ui/ArgeoUiPlugin.java | 74 ++++++++++++ .../java/org/argeo/eclipse/ui/TreeObject.java | 26 +++++ .../java/org/argeo/eclipse/ui/TreeParent.java | 44 ++++++++ 11 files changed, 381 insertions(+) create mode 100644 eclipse/plugins/org.argeo.eclipse.ui/.classpath create mode 100644 eclipse/plugins/org.argeo.eclipse.ui/.project create mode 100644 eclipse/plugins/org.argeo.eclipse.ui/.settings/org.eclipse.jdt.core.prefs create mode 100644 eclipse/plugins/org.argeo.eclipse.ui/.settings/org.eclipse.pde.core.prefs create mode 100644 eclipse/plugins/org.argeo.eclipse.ui/META-INF/MANIFEST.MF create mode 100644 eclipse/plugins/org.argeo.eclipse.ui/build.properties create mode 100644 eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/ApplicationContextTracker.java create mode 100644 eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/SpringExtensionFactory.java create mode 100644 eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/ArgeoUiPlugin.java create mode 100644 eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/TreeObject.java create mode 100644 eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/TreeParent.java diff --git a/eclipse/plugins/org.argeo.eclipse.ui/.classpath b/eclipse/plugins/org.argeo.eclipse.ui/.classpath new file mode 100644 index 000000000..92f19d2ff --- /dev/null +++ b/eclipse/plugins/org.argeo.eclipse.ui/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/eclipse/plugins/org.argeo.eclipse.ui/.project b/eclipse/plugins/org.argeo.eclipse.ui/.project new file mode 100644 index 000000000..53e9b324c --- /dev/null +++ b/eclipse/plugins/org.argeo.eclipse.ui/.project @@ -0,0 +1,28 @@ + + + org.argeo.eclipse.ui + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + diff --git a/eclipse/plugins/org.argeo.eclipse.ui/.settings/org.eclipse.jdt.core.prefs b/eclipse/plugins/org.argeo.eclipse.ui/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..f441c9045 --- /dev/null +++ b/eclipse/plugins/org.argeo.eclipse.ui/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,8 @@ +#Thu Jul 29 10:54:11 CEST 2010 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.5 diff --git a/eclipse/plugins/org.argeo.eclipse.ui/.settings/org.eclipse.pde.core.prefs b/eclipse/plugins/org.argeo.eclipse.ui/.settings/org.eclipse.pde.core.prefs new file mode 100644 index 000000000..8e36e3304 --- /dev/null +++ b/eclipse/plugins/org.argeo.eclipse.ui/.settings/org.eclipse.pde.core.prefs @@ -0,0 +1,3 @@ +#Thu Jul 29 11:00:39 CEST 2010 +eclipse.preferences.version=1 +resolve.requirebundle=false diff --git a/eclipse/plugins/org.argeo.eclipse.ui/META-INF/MANIFEST.MF b/eclipse/plugins/org.argeo.eclipse.ui/META-INF/MANIFEST.MF new file mode 100644 index 000000000..f1109799e --- /dev/null +++ b/eclipse/plugins/org.argeo.eclipse.ui/META-INF/MANIFEST.MF @@ -0,0 +1,16 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Argeo Eclipse UI +Bundle-SymbolicName: org.argeo.eclipse.ui +Bundle-Version: 0.2.1.SNAPSHOT +Bundle-Activator: org.argeo.eclipse.ui.ArgeoUiPlugin +Bundle-Vendor: Argeo +Require-Bundle: org.eclipse.ui, + org.eclipse.core.runtime +Bundle-RequiredExecutionEnvironment: J2SE-1.5 +Bundle-ActivationPolicy: lazy +Import-Package: org.springframework.beans.factory;version="2.5.6.SEC01", + org.springframework.context;version="2.5.6.SEC01", + org.springframework.core.io.support;version="2.5.6.SEC01" +Export-Package: org.argeo.eclipse.spring, + org.argeo.eclipse.ui diff --git a/eclipse/plugins/org.argeo.eclipse.ui/build.properties b/eclipse/plugins/org.argeo.eclipse.ui/build.properties new file mode 100644 index 000000000..5fc538bc8 --- /dev/null +++ b/eclipse/plugins/org.argeo.eclipse.ui/build.properties @@ -0,0 +1,4 @@ +source.. = src/main/java/ +output.. = target/classes/ +bin.includes = META-INF/,\ + . 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 new file mode 100644 index 000000000..be53a97fc --- /dev/null +++ b/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/ApplicationContextTracker.java @@ -0,0 +1,66 @@ +package org.argeo.eclipse.spring; + +import static java.text.MessageFormat.format; + +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.FrameworkUtil; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.util.tracker.ServiceTracker; +import org.springframework.context.ApplicationContext; + +/** + * @author Heiko Seeberger + */ +public class ApplicationContextTracker { + + private static final String FILTER = "(&(objectClass=org.springframework.context.ApplicationContext)" //$NON-NLS-1$ + + "(org.springframework.context.service.name={0}))"; //$NON-NLS-1$ + + 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. + */ + 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 close() { + if (applicationContextServiceTracker != null) { + applicationContextServiceTracker.close(); + } + } + + public ApplicationContext getApplicationContext() { + ApplicationContext applicationContext = null; + if (applicationContextServiceTracker != null) { + try { + applicationContext = (ApplicationContext) applicationContextServiceTracker + .waitForService(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + return applicationContext; + } + + @Override + protected void finalize() throws Throwable { + close(); + super.finalize(); + } +} 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 new file mode 100644 index 000000000..f92b423e9 --- /dev/null +++ b/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/spring/SpringExtensionFactory.java @@ -0,0 +1,105 @@ +package org.argeo.eclipse.spring; + +import org.argeo.eclipse.ui.ArgeoUiPlugin; +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; + +/** + * 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. + * + * @author Martin Lippert + */ +public class SpringExtensionFactory implements IExecutableExtensionFactory, + IExecutableExtension { + + private Object bean; + + public Object create() throws CoreException { + 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 && appContext != null) { + 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(); + } + + // 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; + } + + 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; + } + +} diff --git a/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/ArgeoUiPlugin.java b/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/ArgeoUiPlugin.java new file mode 100644 index 000000000..c43017b5f --- /dev/null +++ b/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/ArgeoUiPlugin.java @@ -0,0 +1,74 @@ +package org.argeo.eclipse.ui; + +import org.eclipse.core.runtime.Platform; +import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; + +/** + * The activator class controls the plug-in life cycle + */ +public class ArgeoUiPlugin extends AbstractUIPlugin { + + // The plug-in ID + public static final String PLUGIN_ID = "org.argeo.eclipse.ui"; + + private final static String SPRING_OSGI_EXTENDER = "org.springframework.osgi.extender"; + + // The shared instance + private static ArgeoUiPlugin plugin; + + private BundleContext bundleContext; + + /** + * The constructor + */ + public ArgeoUiPlugin() { + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext + * ) + */ + public void start(BundleContext context) throws Exception { + super.start(context); + plugin = this; + bundleContext = context; + + // Make sure that the Spring OSGi extender is started + Bundle osgiExtBundle = Platform.getBundle(SPRING_OSGI_EXTENDER); + if (osgiExtBundle != null) + osgiExtBundle.start(); + else + throw new Exception("Spring OSGi Extender not found"); + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext + * ) + */ + public void stop(BundleContext context) throws Exception { + plugin = null; + super.stop(context); + } + + /** + * Returns the shared instance + * + * @return the shared instance + */ + public static ArgeoUiPlugin getDefault() { + return plugin; + } + + public BundleContext getBundleContext() { + return bundleContext; + } + +} diff --git a/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/TreeObject.java b/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/TreeObject.java new file mode 100644 index 000000000..d75db780e --- /dev/null +++ b/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/TreeObject.java @@ -0,0 +1,26 @@ +package org.argeo.eclipse.ui; + +public class TreeObject { + private String name; + private TreeParent parent; + + public TreeObject(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setParent(TreeParent parent) { + this.parent = parent; + } + + public TreeParent getParent() { + return parent; + } + + public String toString() { + return getName(); + } +} diff --git a/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/TreeParent.java b/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/TreeParent.java new file mode 100644 index 000000000..d45844de2 --- /dev/null +++ b/eclipse/plugins/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/TreeParent.java @@ -0,0 +1,44 @@ +package org.argeo.eclipse.ui; + +import java.util.ArrayList; +import java.util.List; + +public class TreeParent extends TreeObject { + private List children; + + private boolean loaded; + + public TreeParent(String name) { + super(name); + children = new ArrayList(); + loaded = false; + } + + public synchronized void addChild(TreeObject child) { + loaded = true; + children.add(child); + child.setParent(this); + } + + public synchronized void removeChild(TreeObject child) { + children.remove(child); + child.setParent(null); + } + + public synchronized void clearChildren() { + loaded = false; + children.clear(); + } + + public synchronized TreeObject[] getChildren() { + return (TreeObject[]) children.toArray(new TreeObject[children.size()]); + } + + public synchronized boolean hasChildren() { + return children.size() > 0; + } + + public synchronized Boolean isLoaded() { + return loaded; + } +} -- 2.30.2