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 16:24:32 UTC

svn commit: r604896 - in /incubator/sling/trunk/scripting/api/src/main/java: org/apache/sling/scripting/api/AbstractScriptEngineFactory.java sun/misc/Service.java

Author: fmeschbe
Date: Mon Dec 17 07:24:31 2007
New Revision: 604896

URL: http://svn.apache.org/viewvc?rev=604896&view=rev
Log:
Implement Service.providers method for the script engine factories
to be available for microsling and allow setXXX methods to get null
to initialize with an empty list.

Added:
    incubator/sling/trunk/scripting/api/src/main/java/sun/misc/Service.java
Modified:
    incubator/sling/trunk/scripting/api/src/main/java/org/apache/sling/scripting/api/AbstractScriptEngineFactory.java

Modified: incubator/sling/trunk/scripting/api/src/main/java/org/apache/sling/scripting/api/AbstractScriptEngineFactory.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/api/src/main/java/org/apache/sling/scripting/api/AbstractScriptEngineFactory.java?rev=604896&r1=604895&r2=604896&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/api/src/main/java/org/apache/sling/scripting/api/AbstractScriptEngineFactory.java (original)
+++ incubator/sling/trunk/scripting/api/src/main/java/org/apache/sling/scripting/api/AbstractScriptEngineFactory.java Mon Dec 17 07:24:31 2007
@@ -21,6 +21,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 import java.util.jar.Attributes;
 import java.util.jar.Manifest;
@@ -32,11 +33,15 @@
         ScriptEngineFactory {
 
     private String engineName;
+
     private String engineVersion;
+
     private List<String> extensions;
+
     private List<String> mimeTypes;
+
     private List<String> names;
-    
+
     protected AbstractScriptEngineFactory() {
         String name = null;
         String version = null;
@@ -75,7 +80,7 @@
         setEngineName(name);
         setEngineVersion(version);
     }
-    
+
     public String getEngineName() {
         return engineName;
     }
@@ -83,7 +88,7 @@
     protected void setEngineName(String engineName) {
         this.engineName = engineName;
     }
-    
+
     public String getEngineVersion() {
         return engineVersion;
     }
@@ -91,31 +96,43 @@
     protected void setEngineVersion(String engineVersion) {
         this.engineVersion = engineVersion;
     }
-    
+
     public List<String> getExtensions() {
         return extensions;
     }
 
     protected void setExtensions(String... extensions) {
-        this.extensions = Arrays.asList(extensions);
+        if (extensions == null) {
+            this.extensions = Collections.emptyList();
+        } else {
+            this.extensions = Arrays.asList(extensions);
+        }
     }
-    
+
     public List<String> getMimeTypes() {
         return mimeTypes;
     }
 
     protected void setMimeTypes(String... mimeTypes) {
-        this.mimeTypes = Arrays.asList(mimeTypes);
+        if (mimeTypes == null) {
+            this.mimeTypes = Collections.emptyList();
+        } else {
+            this.mimeTypes = Arrays.asList(mimeTypes);
+        }
     }
-    
+
     public List<String> getNames() {
         return names;
     }
 
     protected void setNames(String... names) {
-        this.names = Arrays.asList(names);
+        if (names == null) {
+            this.names = Collections.emptyList();
+        } else {
+            this.names = Arrays.asList(names);
+        }
     }
-    
+
     public String getMethodCallSyntax(String obj, String m, String[] args) {
         StringBuffer callSyntax = new StringBuffer();
         callSyntax.append(obj).append('.').append(m).append('(');

Added: incubator/sling/trunk/scripting/api/src/main/java/sun/misc/Service.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/api/src/main/java/sun/misc/Service.java?rev=604896&view=auto
==============================================================================
--- incubator/sling/trunk/scripting/api/src/main/java/sun/misc/Service.java (added)
+++ incubator/sling/trunk/scripting/api/src/main/java/sun/misc/Service.java Mon Dec 17 07:24:31 2007
@@ -0,0 +1,143 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 sun.misc;
+
+import java.awt.image.ImagingOpException;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+/**
+ * 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>
+ * This stub is provided because the original class is not part of the official
+ * Java API and may not be available on all platforms. In addition even if the
+ * 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>Service</code> method is not used.
+ */
+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 {
+        if (loader != null) {
+            try {
+                String name = PREFIX + type.getName();
+                Enumeration<?> files = loader.getResources(name);
+                return new NameIterator(files);
+            } catch (IOException ignore) {
+            }
+        }
+
+        return Collections.<String> emptyList().iterator();
+    }
+
+    private static class NameIterator implements Iterator<String> {
+        
+        private final Enumeration<?> files;
+
+        private Iterator<String> currentFile;
+        
+        private String nextName;
+        
+        public NameIterator(Enumeration<?> files) {
+            this.files = files;
+            seek();
+        }
+        
+        public boolean hasNext() {
+            return nextName != null;
+        }
+        
+        public String next() {
+            if (nextName == null) {
+                throw new NoSuchElementException();
+            }
+            
+            String result = nextName;
+            seek();
+            return result;
+        }
+        
+        public void remove() {
+            throw new UnsupportedOperationException();
+        }
+        
+        private void seek() {
+            if (currentFile == null || !currentFile.hasNext()) {
+                currentFile = getNames();
+            }
+            
+            nextName = (currentFile != null && currentFile.hasNext())
+                    ? currentFile.next()
+                    : null;
+        }
+        
+        private Iterator<String> getNames() {
+            while (files.hasMoreElements()) {
+                URL fileUrl = (URL) files.nextElement();
+                InputStream ins = null;
+                try {
+                    ArrayList<String> names = new ArrayList<String>();
+                    ins = fileUrl.openStream();
+                    BufferedReader br = new BufferedReader(new InputStreamReader(ins));
+                    String name;
+                    while ( (name = br.readLine()) != null) {
+                        int hash = name.indexOf('#');
+                        if (hash >= 0) {
+                            name = name.substring(0, hash);
+                        }
+                        name = name.trim();
+                        
+                        if (name.length() > 0) {
+                            names.add(name);
+                        }
+                    }
+                    
+                    return names.iterator();
+                } catch (IOException ioe) {
+                    
+                } finally {
+                    if (ins != null) {
+                        try {
+                            ins.close();
+                        } catch (IOException ignore) {
+                        }
+                    }
+                }
+            }
+
+            // exhausted search
+            return null;
+        }
+    }
+}