diff options
author | Eike Stepper | 2011-12-10 09:23:23 +0000 |
---|---|---|
committer | Eike Stepper | 2011-12-10 09:23:23 +0000 |
commit | 030fea1eb3d590c95dc75813e51c31975074fbd0 (patch) | |
tree | 0df32567d7396920b00c69c4a393ad0bda2d574e /plugins/org.eclipse.net4j.util/src/org/eclipse | |
parent | a9b8b8d3831d1729cc067edf91544df3f43fa05d (diff) | |
download | cdo-030fea1eb3d590c95dc75813e51c31975074fbd0.tar.gz cdo-030fea1eb3d590c95dc75813e51c31975074fbd0.tar.xz cdo-030fea1eb3d590c95dc75813e51c31975074fbd0.zip |
[366290] Provide an OMBundle.getClasses() method
https://bugs.eclipse.org/bugs/show_bug.cgi?id=366290
Diffstat (limited to 'plugins/org.eclipse.net4j.util/src/org/eclipse')
7 files changed, 270 insertions, 12 deletions
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/bundle/AbstractBundle.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/bundle/AbstractBundle.java index 1cfa82440e..13111b433c 100644 --- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/bundle/AbstractBundle.java +++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/bundle/AbstractBundle.java @@ -40,6 +40,8 @@ import java.util.concurrent.ConcurrentHashMap; */ public abstract class AbstractBundle implements OMBundle, OMBundle.DebugSupport, OMBundle.TranslationSupport { + private static final String CLASS_EXTENSION = ".class"; + private AbstractPlatform platform; private String bundleID; @@ -95,6 +97,7 @@ public abstract class AbstractBundle implements OMBundle, OMBundle.DebugSupport, return bundleContext; } + @Deprecated public void setBundleContext(Object bundleContext) { this.bundleContext = bundleContext; @@ -374,6 +377,28 @@ public abstract class AbstractBundle implements OMBundle, OMBundle.DebugSupport, return bundleID + ".properties"; //$NON-NLS-1$ } + protected final Class<?> getClassFromBundle(String path) + { + if (path.endsWith(CLASS_EXTENSION)) + { + int start = path.startsWith("/") ? 1 : 0; + int end = path.length() - CLASS_EXTENSION.length(); + String className = path.substring(start, end).replace('/', '.'); + + try + { + ClassLoader classLoader = getAccessor().getClassLoader(); + return classLoader.loadClass(className); + } + catch (ClassNotFoundException ex) + { + //$FALL-THROUGH$ + } + } + + return null; + } + private void invokeMethod(String name) throws Exception { try diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/bundle/OM.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/bundle/OM.java index 908ff9398b..30222f89ea 100644 --- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/bundle/OM.java +++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/bundle/OM.java @@ -63,7 +63,7 @@ public abstract class OM public void start(BundleContext context) throws Exception { AbstractPlatform.systemContext = context; - OM.BUNDLE.setBundleContext(context); + setBundleContext(context); ((OSGiBundle)OM.BUNDLE).start(); // TODO Make configurable @@ -92,8 +92,14 @@ public abstract class OM OSGiActivator.traceStop(context); ((OSGiBundle)OM.BUNDLE).stop(); PluginContainer.dispose(); - OM.BUNDLE.setBundleContext(null); + setBundleContext(null); AbstractPlatform.systemContext = null; } + + @SuppressWarnings("deprecation") + private void setBundleContext(BundleContext context) + { + OM.BUNDLE.setBundleContext(context); + } } } diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/LegacyBundle.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/LegacyBundle.java index cabc83b314..b031cfbd41 100644 --- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/LegacyBundle.java +++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/LegacyBundle.java @@ -14,6 +14,7 @@ import org.eclipse.net4j.internal.util.bundle.AbstractBundle; import org.eclipse.net4j.internal.util.bundle.AbstractPlatform; import org.eclipse.net4j.util.ReflectUtil; import org.eclipse.net4j.util.StringUtil; +import org.eclipse.net4j.util.WrappedException; import org.eclipse.net4j.util.io.IOUtil; import java.io.File; @@ -21,9 +22,15 @@ import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.Iterator; +import java.util.List; import java.util.Map.Entry; import java.util.MissingResourceException; import java.util.Properties; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; /** * @author Eike Stepper @@ -62,6 +69,87 @@ public class LegacyBundle extends AbstractBundle return baseURL; } + public Iterator<Class<?>> getClasses() + { + List<Class<?>> result = new ArrayList<Class<?>>(); + + if (isArchiveProtocol(baseURL.getProtocol())) + { + JarFile jarFile = null; + + try + { + jarFile = new JarFile(baseURL.getFile()); + + Enumeration<JarEntry> entries = jarFile.entries(); + while (entries.hasMoreElements()) + { + JarEntry jarEntry = entries.nextElement(); + if (!jarEntry.isDirectory()) + { + Class<?> c = getClassFromBundle(jarEntry.getName()); + if (c != null) + { + result.add(c); + } + } + } + } + catch (IOException ex) + { + throw WrappedException.wrap(ex); + } + finally + { + IOUtil.close(jarFile); + } + } + else + { + try + { + URL url = getClassesURL(getAccessor()); + File folder = new File(url.getFile()); + collectFileClasses(folder, null, result); + } + catch (MalformedURLException ex) + { + throw WrappedException.wrap(ex); + } + } + + return result.iterator(); + } + + private void collectFileClasses(File folder, String path, List<Class<?>> result) + { + File file = folder; + if (path == null) + { + path = ""; + } + else + { + file = new File(folder, path); + } + + if (file.isDirectory()) + { + for (String child : file.list()) + { + collectFileClasses(folder, path + "/" + child, result); + } + } + else + { + Class<?> c = getClassFromBundle(path); + if (c != null) + { + result.add(c); + } + } + } + private void loadOptions() { InputStream inputStream = null; @@ -125,11 +213,7 @@ public class LegacyBundle extends AbstractBundle // file:/D:/sandbox/unpackage1-3.1M7/eclipse/plugins/org.eclipse.emf.common/bin/org/eclipse/emf/common/CommonPlugin. // class - String className = accessor.getName(); - URL url = accessor.getResource(ReflectUtil.getSimpleName(accessor) + ".class"); //$NON-NLS-1$ - - int segmentsToTrim = 1 + StringUtil.occurrences(className, '.'); - url = trimSegments(url, segmentsToTrim); + URL url = getClassesURL(accessor); // For an archive URI, check for the plugin.properties in the archive. if (isArchiveProtocol(url.getProtocol())) @@ -138,8 +222,18 @@ public class LegacyBundle extends AbstractBundle { // If we can open an input stream, then the plugin.properties is there, // and we have a good base URL. - InputStream inputStream = new URL(url.toString() + "plugin.properties").openStream(); //$NON-NLS-1$ - inputStream.close(); + URL u = new URL(url.toString() + "plugin.properties"); + InputStream inputStream = null; + + try + { + inputStream = u.openStream(); + } + finally + { + IOUtil.close(inputStream); + } + baseURL = url; } catch (IOException exception) @@ -183,6 +277,15 @@ public class LegacyBundle extends AbstractBundle } } + private static URL getClassesURL(Class<?> accessor) throws MalformedURLException + { + String className = accessor.getName(); + URL url = accessor.getResource(ReflectUtil.getSimpleName(accessor) + ".class"); //$NON-NLS-1$ + + int segmentsToTrim = 1 + StringUtil.occurrences(className, '.'); + return trimSegments(url, segmentsToTrim); + } + private static String lastSegment(URL url) { String path = url.getPath(); diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/OSGiBundle.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/OSGiBundle.java index 0b86e98890..8090a07251 100644 --- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/OSGiBundle.java +++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/OSGiBundle.java @@ -13,6 +13,7 @@ package org.eclipse.net4j.internal.util.om; import org.eclipse.net4j.internal.util.bundle.AbstractBundle; import org.eclipse.net4j.internal.util.bundle.AbstractPlatform; import org.eclipse.net4j.util.WrappedException; +import org.eclipse.net4j.util.collection.AbstractIterator; import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.Platform; @@ -22,6 +23,8 @@ import org.osgi.framework.BundleContext; import java.io.IOException; import java.net.URL; +import java.util.Enumeration; +import java.util.Iterator; /** * @author Eike Stepper @@ -59,6 +62,32 @@ public class OSGiBundle extends AbstractBundle } } + public Iterator<Class<?>> getClasses() + { + final Bundle bundle = getBundleContext().getBundle(); + + return new AbstractIterator<Class<?>>() + { + private Enumeration<String> entryPaths = bundle.getEntryPaths("/"); + + @Override + protected Object computeNextElement() + { + while (entryPaths.hasMoreElements()) + { + String entryPath = entryPaths.nextElement(); + Class<?> c = getClassFromBundle(entryPath); + if (c != null) + { + return c; + } + } + + return END_OF_DATA; + } + }; + } + public String getStateLocation() { Bundle bundle = getBundleContext().getBundle(); diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/AbstractIterator.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/AbstractIterator.java new file mode 100644 index 0000000000..897a487bb0 --- /dev/null +++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/AbstractIterator.java @@ -0,0 +1,79 @@ +/*
+ * Copyright (c) 2004 - 2011 Eike Stepper (Berlin, Germany) and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ */
+package org.eclipse.net4j.util.collection;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+/**
+ * An abstract base class for custom iterators that only requires to implement a single {@link #computeNextElement()}
+ * method.
+ *
+ * @author Eike Stepper
+ * @since 3.2
+ */
+public abstract class AbstractIterator<T> implements Iterator<T>
+{
+ /**
+ * The token to be used in {@link #computeNextElement()} to indicate the end of the iteration.
+ */
+ protected static final Object END_OF_DATA = new Object();
+
+ private boolean computed;
+
+ private T next;
+
+ public AbstractIterator()
+ {
+ }
+
+ public final boolean hasNext()
+ {
+ if (computed)
+ {
+ return true;
+ }
+
+ Object object = computeNextElement();
+ computed = true;
+
+ if (object == END_OF_DATA)
+ {
+ return false;
+ }
+
+ @SuppressWarnings("unchecked")
+ T cast = (T)object;
+ next = cast;
+ return true;
+ }
+
+ public final T next()
+ {
+ if (!hasNext())
+ {
+ throw new NoSuchElementException();
+ }
+
+ computed = false;
+ return next;
+ }
+
+ public void remove()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * Returns the next iteration element, or {@link #END_OF_DATA} if the end of the iteration has been reached.
+ */
+ protected abstract Object computeNextElement();
+}
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/OMBundle.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/OMBundle.java index 28b5862871..252912edff 100644 --- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/OMBundle.java +++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/OMBundle.java @@ -22,6 +22,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.util.Iterator; import java.util.Properties; import java.util.ResourceBundle; @@ -47,7 +48,10 @@ public interface OMBundle public URL getBaseURL(); - public void setBundleContext(Object bundleContext); + /** + * @since 3.2 + */ + public Iterator<Class<?>> getClasses(); public OMTracer tracer(String name); @@ -68,6 +72,12 @@ public interface OMBundle public TranslationSupport getTranslationSupport(); /** + * @deprecated For internal use only. + */ + @Deprecated + public void setBundleContext(Object bundleContext); + + /** * A facility for accessing OSGi {@link DebugOptions debug options}, whether OSGi {@link OMPlatform#isOSGiRunning() is * running} or not. * diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/OSGiActivator.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/OSGiActivator.java index 50968f839e..2d41ba444b 100644 --- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/OSGiActivator.java +++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/OSGiActivator.java @@ -61,7 +61,7 @@ public abstract class OSGiActivator implements BundleActivator try { - omBundle.setBundleContext(context); + setBundleContext(context); ((AbstractBundle)omBundle).start(); doStart(); } @@ -89,7 +89,7 @@ public abstract class OSGiActivator implements BundleActivator { doStop(); ((AbstractBundle)omBundle).stop(); - omBundle.setBundleContext(null); + setBundleContext(null); } catch (Error error) { @@ -147,6 +147,12 @@ public abstract class OSGiActivator implements BundleActivator { } + @SuppressWarnings("deprecation") + private void setBundleContext(BundleContext context) + { + omBundle.setBundleContext(context); + } + /** * @since 2.0 */ |