package com.bazaarvoice.dropwizard.webjars;

import com.google.common.cache.CacheLoader;
import com.google.common.io.Closer;
import com.google.common.io.Resources;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

/**
 * Determines the version of the webjar that's in the classpath for a particular library.
 * <p/>
 * The version of a webjar can be determined by looking at the <code>pom.properties</code> file located at
 * {@code META-INF/maven/<group>/<library>/pom.properties}.
 * <p/>
 * Where {@code <group>} is the name of the maven group the webjar artifact is part of, and {@code <library>} is the
 * name of the library.
 */
class VersionLoader extends CacheLoader<String, String> {
    public static final String NOT_FOUND = "VERSION-NOT-FOUND";

    private final Iterable<String> packages;

    VersionLoader(Iterable<String> packages) {
        this.packages = packages;
    }

    @Override
    public String load(String library) throws Exception {
        for (String pkg : packages) {
            String found = tryToLoadFrom("META-INF/maven/%s/%s/pom.properties", pkg, library);
            if (found != null) {
                return found;
            }
        }

        return NOT_FOUND;
    }

    private String tryToLoadFrom(String format, String searchPackage, String library) {
        String path = String.format(format, searchPackage, library);
        URL url;
        try {
            url = Resources.getResource(path);
        } catch (IllegalArgumentException e) {
            return null;
        }

        try {
            Closer closer = Closer.create();
            InputStream in = closer.register(url.openStream());
            try {
                Properties props = new Properties();
                props.load(in);

                return props.getProperty("version");
            } finally {
                closer.close();
            }
        } catch (IOException e) {
            return null;
        }
    }
}
@@ -845,23 +845,21 @@
 String%3E 
-package
+group
 s;%0A%0A    
@@ -889,23 +889,21 @@
 String%3E 
-package
+group
 s) %7B%0A   
@@ -916,26 +916,22 @@
 his.
-packages = package
+groups = group
 s;%0A 
@@ -1032,21 +1032,21 @@
 ing 
-pkg : package
+group : group
 s) %7B
@@ -1130,11 +1130,13 @@
 s%22, 
-pkg
+group
 , li
@@ -1319,29 +1319,21 @@
  String 
-searchPackage
+group
 , String
@@ -1392,21 +1392,13 @@
 at, 
-searchPackage
+group
 , li

Rename package to group, since resources are loaded based on maven artifact groups, not java packages.