package org.graphwalker.io.factory;

/*
 * #%L
 * GraphWalker Input/Output
 * %%
 * Copyright (C) 2011 - 2014 GraphWalker
 * %%
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * #L%
 */

import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;

import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Nils Olsson
 */
public final class ContextFactoryScanner {

    private static Map<Class<? extends ContextFactory>, ContextFactory> factories = new HashMap<>();

    private static Reflections reflections = new Reflections(new ConfigurationBuilder()
            .addUrls(ClasspathHelper.forJavaClassPath())
            .addUrls(ClasspathHelper.forClassLoader())
            .addScanners(new SubTypesScanner()));

    public static ContextFactory get(Path path) {
        for (Class<? extends ContextFactory> factoryClass: reflections.getSubTypesOf(ContextFactory.class)) {
            ContextFactory factory = create(factoryClass);
            if (null != factory && factory.accept(path)) {
                return factory;
            }
        }
        throw new ContextFactoryException("No suitable context factory found");
    }

    private static ContextFactory create(Class<? extends ContextFactory> factoryClass) {
        if (!factories.containsKey(factoryClass)) {
            try {
                factories.put(factoryClass, factoryClass.newInstance());
            } catch (InstantiationException | IllegalAccessException e) {
                return null;
            }
        }
        return factories.get(factoryClass);
    }
}

@@ -1404,21 +1404,36 @@
 ava.
-n
 io.
-file.Path
+File;%0Aimport java.net.URL
 ;%0Aim
@@ -1446,20 +1446,21 @@
 ava.
-util.HashMap
+nio.file.Path
 ;%0Aim
@@ -1474,19 +1474,17 @@
 va.util.
-Map
+*
 ;%0A%0A/**%0A 
@@ -1678,76 +1678,450 @@
 tic 
-Reflections reflect
+String getExtension(String path) %7B%0A        int position = path.lastIndexOf('.');%0A        return path.lastIndexOf(File.separator)%3Eposition?%22%22:path.substring(position+1);%0A    %7D%0A%0A    private static boolean valid(URL url) %7B%0A        String extens
 ion
-s
  = 
-new Reflections(new ConfigurationBuild
+getExtension(url.getPath());%0A        return %22%22.equals(extension) %7C%7C %22jar%22.equals(extension);%0A    %7D%0A%0A    private static Collection%3CURL%3E getUrls() %7B%0A        Set%3CURL%3E filt
 er
+edUrls = new HashSet%3C%3E
 ()
+;
 %0A   
@@ -2121,36 +2121,131 @@
 ();%0A        
-    .addUrls
+Set%3CURL%3E urls = new HashSet%3C%3E();%0A        urls.addAll(ClasspathHelper.forClassLoader());%0A        urls.addAll
 (ClasspathHe
@@ -2268,16 +2268,17 @@
 sPath())
+;
 %0A       
@@ -2282,51 +2282,275 @@
     
-    .addUrls(ClasspathHelper.forClassLoader
+for (URL url: urls) %7B%0A            if (valid(url)) %7B%0A                filteredUrls.add(url);%0A            %7D%0A        %7D%0A        return filteredUrls;%0A    %7D%0A%0A    private static Reflections reflections = new Reflections(new ConfigurationBuilder()%0A            .addUrls(getUrls
 ())%0A

Upgrade reflections library and filter unusable classpath entities