commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1204229 - in /commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath: ClassLoaderBuilder.java ClassPathScanner.java DefaultErrorHandler.java ErrorHandlerBuilder.java HandlerConfigurationsBuilder.java
Date Sun, 20 Nov 2011 20:04:52 GMT
Author: simonetripodi
Date: Sun Nov 20 20:04:51 2011
New Revision: 1204229

URL: http://svn.apache.org/viewvc?rev=1204229&view=rev
Log:
reduced the proliferation of anonymous classes

Added:
    commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/DefaultErrorHandler.java
  (with props)
Modified:
    commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/ClassLoaderBuilder.java
    commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/ClassPathScanner.java
    commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/ErrorHandlerBuilder.java
    commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/HandlerConfigurationsBuilder.java

Modified: commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/ClassLoaderBuilder.java
URL: http://svn.apache.org/viewvc/commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/ClassLoaderBuilder.java?rev=1204229&r1=1204228&r2=1204229&view=diff
==============================================================================
--- commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/ClassLoaderBuilder.java
(original)
+++ commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/ClassLoaderBuilder.java
Sun Nov 20 20:04:51 2011
@@ -1,5 +1,7 @@
 package org.apache.commons.meiyo.classpath;
 
+import java.util.Collection;
+
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -22,16 +24,29 @@ package org.apache.commons.meiyo.classpa
 /**
  * Builder to set the {@link ClassLoader} to be used for resolving classes when required.
  */
-public interface ClassLoaderBuilder
+public final class ClassLoaderBuilder
 {
 
+    private final String[] paths;
+
+    private final Collection<ClassPathHandler> handlers;
+
+    ClassLoaderBuilder( String[] paths, Collection<ClassPathHandler> handlers )
+    {
+        this.paths = paths;
+        this.handlers = handlers;
+    }
+
     /**
      * Sets the Context ClassLoader (the one found by calling {@code Thread.currentThread().getContextClassLoader()})
      * to resolve/load classes.
      *
      * @return The builder to set error handlers
      */
-    ErrorHandlerBuilder usingContextClassLoader();
+    public ErrorHandlerBuilder usingContextClassLoader()
+    {
+        return usingClassLoader( Thread.currentThread().getContextClassLoader() );
+    }
 
     /**
      * Sets the input ClassLoader to resolve/load classes.
@@ -39,6 +54,14 @@ public interface ClassLoaderBuilder
      * @param classLoader the ClassLoader to resolve/load classes.
      * @return The builder to set error handlers
      */
-    ErrorHandlerBuilder usingClassLoader( ClassLoader classLoader );
+    public ErrorHandlerBuilder usingClassLoader( ClassLoader classLoader )
+    {
+        if ( classLoader == null )
+        {
+            throw new IllegalArgumentException( "Parameter 'classLoader' must not be null"
);
+        }
+
+        return new ErrorHandlerBuilder( paths, handlers, classLoader );
+    }
 
 }

Modified: commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/ClassPathScanner.java
URL: http://svn.apache.org/viewvc/commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/ClassPathScanner.java?rev=1204229&r1=1204228&r2=1204229&view=diff
==============================================================================
--- commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/ClassPathScanner.java
(original)
+++ commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/ClassPathScanner.java
Sun Nov 20 20:04:51 2011
@@ -20,12 +20,6 @@ package org.apache.commons.meiyo.classpa
  */
 
 import java.io.File;
-import java.io.IOException;
-import java.util.Collection;
-import java.util.Enumeration;
-import java.util.jar.JarEntry;
-import java.util.jar.JarFile;
-import java.util.regex.Pattern;
 
 /**
  * A ClassPath scanner allows users define ClassPath entries matching rule while scanning,
@@ -36,10 +30,6 @@ public final class ClassPathScanner
 
     private static final String JAVA_CLASS_PATH = "java.class.path";
 
-    private static final Pattern JAR_FILE = Pattern.compile( ".+\\.(jar|zip)", Pattern.CASE_INSENSITIVE
);
-
-    private static final String CLASS_EXTENSION = ".class";
-
     /**
      * This class can not be instantiated explicitly.
      */
@@ -90,160 +80,7 @@ public final class ClassPathScanner
             throw new IllegalArgumentException( "At least one path has to be specified" );
         }
 
-        return new HandlerConfigurationsBuilder()
-        {
-
-            public ClassLoaderBuilder withConfiguration( final HandlerConfiguration configuration
)
-            {
-                if ( configuration == null )
-                {
-                    throw new IllegalArgumentException( "At least one HandlerConfiguration
has to be specified" );
-                }
-
-                configuration.configure();
-
-                final Collection<ClassPathHandler> handlers = configuration.getHandlers();
-
-                return new ClassLoaderBuilder()
-                {
-
-                    public ErrorHandlerBuilder usingContextClassLoader()
-                    {
-                        return this.usingClassLoader( Thread.currentThread().getContextClassLoader()
);
-                    }
-
-                    public ErrorHandlerBuilder usingClassLoader( final ClassLoader classLoader
)
-                    {
-                        if ( classLoader == null )
-                        {
-                            throw new IllegalArgumentException( "Parameter 'classLoader'
must not be null" );
-                        }
-
-                        return new ErrorHandlerBuilder()
-                        {
-
-                            public void scan()
-                            {
-                                scan( new ErrorHandler()
-                                {
-
-                                    public void onJARReadingError( File file, IOException
e )
-                                    {
-                                        throw new RuntimeException( "An error occurred while
loading '" + file
-                                            + "' jar entry", e );
-                                    }
-
-                                    public void onClassNotFound( String className, Throwable
t )
-                                    {
-                                        // do nothing, just ignore it
-                                    }
-
-                                } );
-                            }
-
-                            public void scan( final ErrorHandler errorHandler )
-                            {
-                                if ( errorHandler == null )
-                                {
-                                    throw new IllegalArgumentException( "Parameter 'errorHandler'
must not be null" );
-                                }
-
-                                for ( String path : paths )
-                                {
-                                    File file = new File( path );
-                                    if ( JAR_FILE.matcher( path ).matches() )
-                                    {
-                                        try
-                                        {
-                                            JarFile jarFile = new JarFile( path );
-                                            Enumeration<JarEntry> enumeration = jarFile.entries();
-                                            while ( enumeration.hasMoreElements() )
-                                            {
-                                                JarEntry entry = enumeration.nextElement();
-                                                if ( !entry.isDirectory() )
-                                                {
-                                                    handleEntry( entry.getName(), path, handlers,
classLoader,
-                                                                 errorHandler );
-                                                }
-                                            }
-                                        }
-                                        catch ( IOException e )
-                                        {
-                                            errorHandler.onJARReadingError( file, e );
-                                        }
-                                    }
-                                    else
-                                    {
-                                        traverse( file, path, handlers, classLoader, errorHandler
);
-                                    }
-                                    // else ignore it
-                                }
-                            }
-                        };
-                    }
-
-                };
-            }
-        };
-    }
-
-    /**
-     * Traverses recursively a directory and handle children with the input handlers.
-     *
-     * @param file the directory has to be traversed
-     * @param path the path where the input directory has been found
-     * @param handlers ClassPath entry handlers have to be invoked when {@link Matcher} pattern
matches.
-     * @param classLoader the {@code ClassLoader} used to resolve/load classes
-     * @param errorHandler the {@link ErrorHandler} used to catch any scanning error
-     */
-    private static void traverse( final File file, final String path, final Collection<ClassPathHandler>
handlers,
-                                  final ClassLoader classLoader, final ErrorHandler errorHandler
)
-    {
-        if ( file.isDirectory() )
-        {
-
-            for ( File child : file.listFiles() )
-            {
-                traverse( child, path, handlers, classLoader, errorHandler );
-            }
-
-            return;
-        }
-
-        handleEntry( file.getAbsolutePath().substring( path.length() + 1 ), path, handlers,
classLoader, errorHandler );
-    }
-
-    /**
-     * Handles an entry found in the ClassPath.
-     *
-     * @param entry the ClassPath entry name found.
-     * @param path the path where the ClassPath entry name is found.
-     * @param handlers ClassPath entry handlers have to be invoked when {@link Matcher} pattern
matches.
-     * @param classLoader the {@code ClassLoader} used to resolve/load classes
-     * @param errorHandler the {@link ErrorHandler} used to catch any scanning error
-     */
-    private static void handleEntry( String entry, final String path, final Collection<ClassPathHandler>
handlers,
-                                     final ClassLoader classLoader, final ErrorHandler errorHandler
)
-    {
-        if ( !entry.endsWith( CLASS_EXTENSION ) )
-        {
-            return;
-        }
-
-        entry = entry.substring( 0, entry.lastIndexOf( '.' ) ).replace( '/', '.' );
-        try
-        {
-            Class<?> clazz = classLoader.loadClass( entry );
-
-            for ( ClassPathHandler classPathHandler : handlers )
-            {
-                classPathHandler.doHandle( path, clazz );
-            }
-        }
-        catch ( Throwable t )
-        {
-            errorHandler.onClassNotFound( entry, t );
-        }
+        return new HandlerConfigurationsBuilder( paths );
     }
 
 }

Added: commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/DefaultErrorHandler.java
URL: http://svn.apache.org/viewvc/commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/DefaultErrorHandler.java?rev=1204229&view=auto
==============================================================================
--- commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/DefaultErrorHandler.java
(added)
+++ commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/DefaultErrorHandler.java
Sun Nov 20 20:04:51 2011
@@ -0,0 +1,51 @@
+package org.apache.commons.meiyo.classpath;
+
+/*
+ * 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.
+ */
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * Default {@link ErrorHandler} implementation.
+ */
+final class DefaultErrorHandler
+    implements ErrorHandler
+{
+
+    /**
+     * {@inheritDoc}
+     */
+    public void onClassNotFound( String className, Throwable t )
+    {
+        // do nothing, just ignore it
+
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void onJARReadingError( File file, IOException e )
+    {
+        throw new RuntimeException( "An error occurred while loading '"
+                                    + file
+                                    + "' jar entry", e );
+    }
+
+}

Propchange: commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/DefaultErrorHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/DefaultErrorHandler.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/DefaultErrorHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/ErrorHandlerBuilder.java
URL: http://svn.apache.org/viewvc/commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/ErrorHandlerBuilder.java?rev=1204229&r1=1204228&r2=1204229&view=diff
==============================================================================
--- commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/ErrorHandlerBuilder.java
(original)
+++ commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/ErrorHandlerBuilder.java
Sun Nov 20 20:04:51 2011
@@ -19,22 +19,146 @@ package org.apache.commons.meiyo.classpa
  * under the License.
  */
 
+import static java.util.regex.Pattern.compile;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Enumeration;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.regex.Pattern;
+
 /**
  * Builder to perform a scan, recording eventual errors inside a proper handler.
  */
-public interface ErrorHandlerBuilder
+public final class ErrorHandlerBuilder
 {
 
+    private static final Pattern JAR_FILE = compile( ".+\\.(jar|zip)", Pattern.CASE_INSENSITIVE
);
+
+    private static final String CLASS_EXTENSION = ".class";
+
+    private final String[] paths;
+
+    private final Collection<ClassPathHandler> handlers;
+
+    private final ClassLoader classLoader;
+
+    private ErrorHandler errorHandler;
+
+    ErrorHandlerBuilder( String[] paths, Collection<ClassPathHandler> handlers, ClassLoader
classLoader )
+    {
+        this.paths = paths;
+        this.handlers = handlers;
+        this.classLoader = classLoader;
+    }
+
     /**
      * Performs a ClassPath scanning operation, using the default {@link ErrorHandler}.
      */
-    void scan();
+    public void scan()
+    {
+        scan( new DefaultErrorHandler() );
+    }
 
     /**
      * Performs a ClassPath scanning operation, using the input {@link ErrorHandler}.
      *
      * @param errorHandler the handler to record scanning errors.
      */
-    void scan( ErrorHandler errorHandler );
+    public void scan( ErrorHandler errorHandler )
+    {
+        if ( errorHandler == null )
+        {
+            throw new IllegalArgumentException( "Parameter 'errorHandler' must not be null"
);
+        }
+
+        this.errorHandler = errorHandler;
+
+        for ( String path : paths )
+        {
+            File file = new File( path );
+            if ( JAR_FILE.matcher( path ).matches() )
+            {
+                try
+                {
+                    JarFile jarFile = new JarFile( path );
+                    Enumeration<JarEntry> enumeration = jarFile.entries();
+                    while ( enumeration.hasMoreElements() )
+                    {
+                        JarEntry entry = enumeration.nextElement();
+                        if ( !entry.isDirectory() )
+                        {
+                            handleEntry( entry.getName(), path );
+                        }
+                    }
+                }
+                catch ( IOException e )
+                {
+                    errorHandler.onJARReadingError( file, e );
+                }
+            }
+            else
+            {
+                traverse( file, path );
+            }
+            // else ignore it
+        }
+    }
+
+    /**
+     * Traverses recursively a directory and handle children with the input handlers.
+     *
+     * @param file the directory has to be traversed
+     * @param path the path where the input directory has been found
+     */
+    private void traverse( final File file, final String path )
+    {
+        if ( file.isDirectory() )
+        {
+
+            for ( File child : file.listFiles() )
+            {
+                traverse( child, path );
+            }
+
+            return;
+        }
+
+        handleEntry( file.getAbsolutePath().substring( path.length() + 1 ), path );
+    }
+
+    /**
+     * Handles an entry found in the ClassPath.
+     *
+     * @param entry the ClassPath entry name found.
+     * @param path the path where the ClassPath entry name is found.
+     * @param handlers ClassPath entry handlers have to be invoked when {@link Matcher} pattern
matches.
+     * @param classLoader the {@code ClassLoader} used to resolve/load classes
+     * @param errorHandler the {@link ErrorHandler} used to catch any scanning error
+     */
+    private void handleEntry( String entry, String path )
+    {
+        if ( !entry.endsWith( CLASS_EXTENSION ) )
+        {
+            return;
+        }
+
+        entry = entry.substring( 0, entry.lastIndexOf( '.' ) ).replace( '/', '.' );
+        try
+        {
+            Class<?> clazz = classLoader.loadClass( entry );
+
+            for ( ClassPathHandler classPathHandler : handlers )
+            {
+                classPathHandler.doHandle( path, clazz );
+            }
+        }
+        catch ( Throwable t )
+        {
+            errorHandler.onClassNotFound( entry, t );
+        }
+    }
 
 }

Modified: commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/HandlerConfigurationsBuilder.java
URL: http://svn.apache.org/viewvc/commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/HandlerConfigurationsBuilder.java?rev=1204229&r1=1204228&r2=1204229&view=diff
==============================================================================
--- commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/HandlerConfigurationsBuilder.java
(original)
+++ commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classpath/HandlerConfigurationsBuilder.java
Sun Nov 20 20:04:51 2011
@@ -22,15 +22,32 @@ package org.apache.commons.meiyo.classpa
 /**
  * Builder to set-up the {@link ClassPathScanner} given the user defined configurations.
  */
-public interface HandlerConfigurationsBuilder
+public final class HandlerConfigurationsBuilder
 {
 
+    private final String[] paths;
+
+    HandlerConfigurationsBuilder( final String... paths )
+    {
+        this.paths = paths;
+    }
+
     /**
      * Initialize the scanner given one or more configuration.
      *
      * @param configurations configurations needed to set-up the scanner
      * @return the builder to set-up the scanner ClassLoader
      */
-    ClassLoaderBuilder withConfiguration( HandlerConfiguration configuration );
+    public ClassLoaderBuilder withConfiguration( HandlerConfiguration configuration )
+    {
+        if ( configuration == null )
+        {
+            throw new IllegalArgumentException( "At least one HandlerConfiguration has to
be specified" );
+        }
+
+        configuration.configure();
+
+        return new ClassLoaderBuilder( paths, configuration.getHandlers() );
+    }
 
 }



Mime
View raw message