package fr.synchrotron.soleil.ica.ci.service.legacymavenproxy.pommetadata;

import org.vertx.java.core.Vertx;
import org.vertx.java.core.shareddata.ConcurrentSharedMap;
import org.vertx.java.core.shareddata.SharedData;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author Gregory Boissinot
 */
public class POMCache {

    private static final String KEY_CACHE_POM_CONTENT = "pomContent";
    private static final String KEY_CACHE_POM_SHA1 = "pomContentSha1";

    private final ConcurrentSharedMap<String, String> pomContentMap;
    private final ConcurrentSharedMap<String, String> pomSha1Map;

    public POMCache(Vertx vertx) {
        final SharedData sharedData = vertx.sharedData();
        pomContentMap = sharedData.getMap(KEY_CACHE_POM_CONTENT);
        pomSha1Map = sharedData.getMap(KEY_CACHE_POM_SHA1);
    }

    public String loadPomContentFromCache(Vertx vertx, String pomPath) {
        return pomContentMap.get(pomPath);
    }

    public String getSha1(String pomSha1Path) {
        return pomSha1Map.get(pomSha1Path);
    }

    public void putPomContent(String pomPath, String pomContent) {

        pomContentMap.put(pomPath, pomContent);

        final String sha1Path = pomPath + ".sha1";

        try {
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(pomContent.getBytes("UTF-8"));

            String sha1 = new BigInteger(1, crypt.digest()).toString(16);
            pomSha1Map.put(sha1Path, sha1);

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }

    }
}

@@ -262,37 +262,8 @@
 on;%0A
-import java.math.BigInteger;%0A
 impo
@@ -294,16 +294,16 @@
 Digest;%0A
+
 import j
@@ -912,131 +912,8 @@
  %7D%0A%0A
-    public String loadPomContentFromCache(Vertx vertx, String pomPath) %7B%0A        return pomContentMap.get(pomPath);%0A    %7D%0A%0A
     
@@ -1343,17 +1343,16 @@
 F-8%22));%0A
-%0A
         
@@ -1359,111 +1359,63 @@
     
-String sha1 = new BigInteger(1, crypt.digest()).toString(16);%0A            pomSha1Map.put(sha1Path, sha1
+pomSha1Map.put(sha1Path, String.valueOf(crypt.digest())
 );%0A%0A

Fix sha1