You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2007/12/17 21:56:20 UTC

svn commit: r605001 - in /incubator/sling/trunk/scripting/api/src/main/java/sun/misc: Service.java ServiceX.java

Author: fmeschbe
Date: Mon Dec 17 12:56:19 2007
New Revision: 605001

URL: http://svn.apache.org/viewvc?rev=605001&view=rev
Log:
Rename ServiceX to Service again. This class was implemented incorrectly.
The returned Iterator is expected to return SPI instances and not the
fully qualified class names of the SPI classes. Now it works correctly.

Added:
    incubator/sling/trunk/scripting/api/src/main/java/sun/misc/Service.java
      - copied, changed from r604984, incubator/sling/trunk/scripting/api/src/main/java/sun/misc/ServiceX.java
Removed:
    incubator/sling/trunk/scripting/api/src/main/java/sun/misc/ServiceX.java

Copied: incubator/sling/trunk/scripting/api/src/main/java/sun/misc/Service.java (from r604984, incubator/sling/trunk/scripting/api/src/main/java/sun/misc/ServiceX.java)
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/api/src/main/java/sun/misc/Service.java?p2=incubator/sling/trunk/scripting/api/src/main/java/sun/misc/Service.java&p1=incubator/sling/trunk/scripting/api/src/main/java/sun/misc/ServiceX.java&r1=604984&r2=605001&rev=605001&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/api/src/main/java/sun/misc/ServiceX.java (original)
+++ incubator/sling/trunk/scripting/api/src/main/java/sun/misc/Service.java Mon Dec 17 12:56:19 2007
@@ -31,8 +31,8 @@
 import java.util.NoSuchElementException;
 
 /**
- * The <code>ServiceX</code> class is a primitive stub of the original
- * <code>sun.misc.ServiceX</code> class used by the
+ * The <code>Service</code> class is a primitive stub of the original
+ * <code>sun.misc.Service</code> class used by the
  * <code>javax.script.ScriptEngineManager</code> to find script engine
  * factories in factory service files.
  * <p>
@@ -41,49 +41,52 @@
  * class would be available on the Java platform, it may not be visible inside
  * the OSGi framework. Finally, the <em>org.apache.sling.scripting.resolver</em>
  * bundle implements its own resolution of script engine factories and thus the
- * <code>ServiceX</code> method is not used.
+ * <code>Service</code> method is not used.
  */
-public class ServiceX {
+public class Service {
 
     private static final String PREFIX = "META-INF/services/";
     
     /** Returns an empty iterator */
-    public static Iterator<String> providers(Class<?> type, ClassLoader loader) throws IOException {
+    public static <ProviderType> Iterator<ProviderType> providers(Class<ProviderType> type, ClassLoader loader) throws IOException {
         if (loader != null) {
             try {
                 String name = PREFIX + type.getName();
                 Enumeration<?> files = loader.getResources(name);
-                return new NameIterator(files);
+                return new NameIterator<ProviderType>(loader, files);
             } catch (IOException ignore) {
             }
         }
 
-        return Collections.<String> emptyList().iterator();
+        return Collections.<ProviderType> emptyList().iterator();
     }
 
-    private static class NameIterator implements Iterator<String> {
+    private static class NameIterator<ProviderType> implements Iterator<ProviderType> {
+        
+        private final ClassLoader loader;
         
         private final Enumeration<?> files;
 
         private Iterator<String> currentFile;
         
-        private String nextName;
+        private ProviderType nextProvider;
         
-        public NameIterator(Enumeration<?> files) {
+        public NameIterator(ClassLoader loader, Enumeration<?> files) {
+            this.loader = loader;
             this.files = files;
             seek();
         }
         
         public boolean hasNext() {
-            return nextName != null;
+            return nextProvider != null;
         }
         
-        public String next() {
-            if (nextName == null) {
+        public ProviderType next() {
+            if (nextProvider == null) {
                 throw new NoSuchElementException();
             }
             
-            String result = nextName;
+            ProviderType result = nextProvider;
             seek();
             return result;
         }
@@ -97,9 +100,7 @@
                 currentFile = getNames();
             }
             
-            nextName = (currentFile != null && currentFile.hasNext())
-                    ? currentFile.next()
-                    : null;
+            nextProvider = getClass(currentFile);
         }
         
         private Iterator<String> getNames() {
@@ -137,6 +138,21 @@
             }
 
             // exhausted search
+            return null;
+        }
+        
+        @SuppressWarnings("unchecked")
+        private ProviderType getClass(Iterator<String> currentFile) {
+            if (currentFile != null && currentFile.hasNext()) {
+                String name = currentFile.next();
+                try {
+                    Class<?> clazz = Class.forName(name, true, loader);
+                    return (ProviderType) clazz.newInstance();
+                } catch (Throwable t) {
+                    // 
+                }
+            }
+            
             return null;
         }
     }