harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hinde...@apache.org
Subject svn commit: r986136 - in /harmony/enhanced/java/trunk/classlib/modules/imageio: ./ src/main/java/javax/imageio/ src/main/java/javax/imageio/spi/ src/main/java/org/apache/harmony/x/imageio/internal/nls/ src/test/java/META-INF/ src/test/java/META-INF/ser...
Date Mon, 16 Aug 2010 21:59:28 GMT
Author: hindessm
Date: Mon Aug 16 21:59:27 2010
New Revision: 986136

URL: http://svn.apache.org/viewvc?rev=986136&view=rev
Log:
Apply patch from "[#HARMONY-6553] Implement
ServiceRegistry.lookupProviders(class)".

Add a <metainf> line to test jar ant task so that the test passes when
run from the hdk.

Added:
    harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/META-INF/
    harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/META-INF/services/
    harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/META-INF/services/javax.imageio.spi.CorrectProviderConfiguration
    harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/META-INF/services/javax.imageio.spi.IncorrectProviderConfiguration
    harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/javax/imageio/spi/CorrectProviderConfiguration.java
  (with props)
    harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/javax/imageio/spi/IncorrectProviderConfiguration.java
  (with props)
Modified:
    harmony/enhanced/java/trunk/classlib/modules/imageio/build.xml
    harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/javax/imageio/ImageIO.java
    harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/javax/imageio/spi/IIORegistry.java
    harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/javax/imageio/spi/ServiceRegistry.java
    harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/org/apache/harmony/x/imageio/internal/nls/messages.properties
    harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/javax/imageio/spi/ServiceRegistryTest.java

Modified: harmony/enhanced/java/trunk/classlib/modules/imageio/build.xml
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/imageio/build.xml?rev=986136&r1=986135&r2=986136&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/imageio/build.xml (original)
+++ harmony/enhanced/java/trunk/classlib/modules/imageio/build.xml Mon Aug 16 21:59:27 2010
@@ -178,6 +178,7 @@ ${line.separator}For Fedora try: yum ins
         <copy file="make/run-test.xml" tofile="${tests.hdk.dir}/build.xml" />
         
         <hy.jar.bin destfile="${tests.hdk.dir}/${hy.module}_tests.jar">
+            <metainf dir="src/test/java/META-INF" />
             <fileset dir="bin/test" />
         </hy.jar.bin>
 

Modified: harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/javax/imageio/ImageIO.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/javax/imageio/ImageIO.java?rev=986136&r1=986135&r2=986136&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/javax/imageio/ImageIO.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/javax/imageio/ImageIO.java
Mon Aug 16 21:59:27 2010
@@ -44,9 +44,8 @@ public final class ImageIO {
     private ImageIO() {}
     
 
-    public static void scanForPlugins() throws NotImplementedException {
-        // TODO: implement
-        throw new NotImplementedException();
+    public static void scanForPlugins() {
+        registry.registerApplicationClasspathSpis();
     }
 
     public static void setUseCache(boolean useCache) {

Modified: harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/javax/imageio/spi/IIORegistry.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/javax/imageio/spi/IIORegistry.java?rev=986136&r1=986135&r2=986136&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/javax/imageio/spi/IIORegistry.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/javax/imageio/spi/IIORegistry.java
Mon Aug 16 21:59:27 2010
@@ -19,7 +19,10 @@
  */
 package javax.imageio.spi;
 
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.Arrays;
+import java.util.Iterator;
 
 import org.apache.harmony.x.imageio.plugins.gif.GIFImageReaderSpi;
 import org.apache.harmony.x.imageio.plugins.jpeg.JPEGImageReaderSpi;
@@ -75,7 +78,17 @@ public final class IIORegistry extends S
         }
     }
 
-    public void registerApplicationClasspathSpis() {
-        // -- TODO implement for non-builtin plugins
+    @SuppressWarnings("unchecked")
+	public void registerApplicationClasspathSpis() {
+        AccessController.doPrivileged(new PrivilegedAction() {
+			public Object run() {
+				Iterator<Class<?>> categories = getCategories();
+				while (categories.hasNext()) {
+					Iterator providers = lookupProviders(categories.next());
+					registerServiceProviders(providers);
+				}
+				return this;
+			}
+        });
     }
 }

Modified: harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/javax/imageio/spi/ServiceRegistry.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/javax/imageio/spi/ServiceRegistry.java?rev=986136&r1=986135&r2=986136&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/javax/imageio/spi/ServiceRegistry.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/javax/imageio/spi/ServiceRegistry.java
Mon Aug 16 21:59:27 2010
@@ -19,6 +19,11 @@
  */
 package javax.imageio.spi;
 
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
 import java.util.*;
 import java.util.Map.Entry;
 
@@ -42,9 +47,8 @@ public class ServiceRegistry {
         }
     }
 
-    public static <T> Iterator<T> lookupProviders(Class<T> providerClass,
ClassLoader loader) throws NotImplementedException {
-        // TODO: implement
-        throw new NotImplementedException();
+    public static <T> Iterator<T> lookupProviders(Class<T> providerClass,
ClassLoader loader) {
+        return new LookupProvidersIterator(providerClass, loader);
     }
 
     public static <T> Iterator<T> lookupProviders(Class<T> providerClass)
{
@@ -549,4 +553,133 @@ public class ServiceRegistry {
             throw new UnsupportedOperationException();
         }
     }
+    
+    static class LookupProvidersIterator<T> implements Iterator {
+
+        private Set<String> providerNames = new HashSet<String>();
+        private Iterator<String> it = null;
+        private ClassLoader loader = null;
+        
+        public LookupProvidersIterator(Class<T> providerClass, ClassLoader loader)
{
+            this.loader = loader;
+            
+            Enumeration<URL> e = null;
+            try {
+                e = loader.getResources("META-INF/services/"+providerClass.getName()); //$NON-NLS-1$
+                while (e.hasMoreElements()) {
+                    Set<String> names = parse((URL)e.nextElement());
+                    providerNames.addAll(names);
+                }
+            } catch (IOException e1) {
+                // Ignored
+            }
+
+            it = providerNames.iterator();
+        }
+        
+        /*
+         * Parse the provider-configuration file as specified 
+         * @see <a href="http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Provider
Configuration File">JAR File Specification</a>
+         */
+        private Set<String> parse(URL u) {
+            InputStream input = null;
+            BufferedReader reader = null;
+            Set<String> names = new HashSet<String>();
+            try {
+                input = u.openStream();
+                reader = new BufferedReader(new InputStreamReader(input, "utf-8")); //$NON-NLS-1$
+                
+                String line;
+                while ((line = reader.readLine()) != null) {
+                    // The comment character is '#' (0x23)
+                    // on each line all characters following the first comment character
are ignored
+                    int sharpIndex = line.indexOf('#');
+                    if (sharpIndex>=0) {
+                        line = line.substring(0, sharpIndex);
+                    }
+                    
+                    // Whitespaces are ignored
+                    line = line.trim();
+                    
+                    if (line.length()>0) {
+                        // a java class name, check if identifier correct
+                        char[] namechars = line.toCharArray();
+                        for (int i = 0; i < namechars.length; i++) {
+                            if (!(Character.isJavaIdentifierPart(namechars[i]) || namechars[i]
== '.')) {
+                                throw new ServiceConfigurationError(Messages.getString("imageio.99",
line));
+                            }
+                        }
+                        names.add(line);
+                    }
+                }
+            } catch (IOException e) {
+                throw new ServiceConfigurationError(e.toString());
+            } finally {
+                try {
+                    if (reader != null) {
+                        reader.close();
+                    }
+                    if (input != null) {
+                        input.close();
+                    }
+                } catch (IOException e) {
+                    throw new ServiceConfigurationError(e.toString());
+                }
+            }
+            
+            return names;
+        }
+        
+        public boolean hasNext() {
+            return it.hasNext();
+        }
+
+        public Object next() {
+            if (!hasNext()) {
+                throw new NoSuchElementException();
+            }
+            
+            String name = (String)it.next();
+            try {
+                return Class.forName(name, true, loader).newInstance();
+            } catch (Exception e) {
+                throw new ServiceConfigurationError(e.toString());
+            }
+        }
+
+        public void remove() {
+            throw new UnsupportedOperationException();   
+        }
+    }
+    
+    /**
+     * An Error that can be thrown when something wrong occurs in loading a service
+     * provider.
+     */
+    static class ServiceConfigurationError extends Error {
+        
+        private static final long serialVersionUID = 74132770414881L;
+
+        /**
+         * The constructor
+         * 
+         * @param msg
+         *            the message of this error
+         */
+        public ServiceConfigurationError(String msg) {
+            super(msg);
+        }
+
+        /**
+         * The constructor
+         * 
+         * @param msg
+         *            the message of this error
+         * @param cause 
+         *            the cause of this error
+         */
+        public ServiceConfigurationError(String msg, Throwable cause) {
+            super(msg, cause);
+        }
+    }
 }

Modified: harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/org/apache/harmony/x/imageio/internal/nls/messages.properties
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/org/apache/harmony/x/imageio/internal/nls/messages.properties?rev=986136&r1=986135&r2=986136&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/org/apache/harmony/x/imageio/internal/nls/messages.properties
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/org/apache/harmony/x/imageio/internal/nls/messages.properties
Mon Aug 16 21:59:27 2010
@@ -167,4 +167,5 @@ imageio.95=Input is not an instance of j
 imageio.96=Writer cannot be null
 imageio.97=Reader cannot be null
 imageio.98=Ordered providers cannot be equal
+imageio.99=Illegal provider-class name: {0}
 

Added: harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/META-INF/services/javax.imageio.spi.CorrectProviderConfiguration
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/META-INF/services/javax.imageio.spi.CorrectProviderConfiguration?rev=986136&view=auto
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/META-INF/services/javax.imageio.spi.CorrectProviderConfiguration
(added)
+++ harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/META-INF/services/javax.imageio.spi.CorrectProviderConfiguration
Mon Aug 16 21:59:27 2010
@@ -0,0 +1,2 @@
+#This is comment
+javax.imageio.spi.CorrectProviderConfiguration #This is another comment
\ No newline at end of file

Added: harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/META-INF/services/javax.imageio.spi.IncorrectProviderConfiguration
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/META-INF/services/javax.imageio.spi.IncorrectProviderConfiguration?rev=986136&view=auto
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/META-INF/services/javax.imageio.spi.IncorrectProviderConfiguration
(added)
+++ harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/META-INF/services/javax.imageio.spi.IncorrectProviderConfiguration
Mon Aug 16 21:59:27 2010
@@ -0,0 +1 @@
+java%1234567
\ No newline at end of file

Added: harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/javax/imageio/spi/CorrectProviderConfiguration.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/javax/imageio/spi/CorrectProviderConfiguration.java?rev=986136&view=auto
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/javax/imageio/spi/CorrectProviderConfiguration.java
(added)
+++ harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/javax/imageio/spi/CorrectProviderConfiguration.java
Mon Aug 16 21:59:27 2010
@@ -0,0 +1,25 @@
+/*
+ *  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 javax.imageio.spi;
+
+/*
+ * Dummy class used by ServiceRegistryTest 
+ */
+public class CorrectProviderConfiguration {
+
+}

Propchange: harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/javax/imageio/spi/CorrectProviderConfiguration.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/javax/imageio/spi/IncorrectProviderConfiguration.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/javax/imageio/spi/IncorrectProviderConfiguration.java?rev=986136&view=auto
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/javax/imageio/spi/IncorrectProviderConfiguration.java
(added)
+++ harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/javax/imageio/spi/IncorrectProviderConfiguration.java
Mon Aug 16 21:59:27 2010
@@ -0,0 +1,25 @@
+/*
+ *  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 javax.imageio.spi;
+
+/*
+ * Dummy class used by ServiceRegistryTest 
+ */
+public class IncorrectProviderConfiguration {
+
+}

Propchange: harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/javax/imageio/spi/IncorrectProviderConfiguration.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/javax/imageio/spi/ServiceRegistryTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/javax/imageio/spi/ServiceRegistryTest.java?rev=986136&r1=986135&r2=986136&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/javax/imageio/spi/ServiceRegistryTest.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/javax/imageio/spi/ServiceRegistryTest.java
Mon Aug 16 21:59:27 2010
@@ -31,6 +31,22 @@ import javax.imageio.spi.ImageWriterSpi;
 
 public class ServiceRegistryTest extends TestCase {
 
+    public void testLookupProviders() {
+        // lookup from a correct provider-configuration file
+        Iterator it = ServiceRegistry.lookupProviders(CorrectProviderConfiguration.class);

+        assertEquals("Failed to find provider and instantiate it",
+                "class javax.imageio.spi.CorrectProviderConfiguration", 
+                it.next().getClass().toString());
+       
+        // lookup from incorrect provider-configuration file
+        try {
+            it = ServiceRegistry.lookupProviders(IncorrectProviderConfiguration.class);
+            fail("ServiceConfigurationError expected");
+        } catch (Error e) {
+            // Ok
+        }
+    }
+    
     public void testDeregisterAll() {
         Class[] CATEGORIES = new Class[] {
                 ImageReaderSpi.class };



Mime
View raw message