package utils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;

import org.webdsl.logging.Logger;

import com.google.common.hash.Hashing;
import com.google.common.io.Files;

public class CachedResourceFileNameHelper {
  private static Map<String, String> resources = new HashMap<>();
  private static String resourcePathRoot = null;
  public static void init( ServletContext ctx){
    resourcePathRoot = ctx.getRealPath( File.separator );
  }
  
  //Given a name of a resource (with or without existing query param), it returns the name with the file hash (md5) as query param
  //This way, the browser will never use its client-cached resource when the resource changed. This method caches the hashed filenames, so
  //they will only be read from disk once during the life cycle of the application servlet.
  public static String getNameWithHash(String resourceDirName, String name){
    String hashname = resources.get( name );
    if( hashname == null){
      if( resourcePathRoot == null ){
        Logger.warn( CachedResourceFileNameHelper.class.getName() + " is not initialized by the servlet context. It won't add file-hashes to resource file names." );
        hashname = name;
      } else {
        String nameNoSuffix = name.replaceAll("(\\?.*)$", "");
        String realPath = resourcePathRoot + "/" + resourceDirName + "/" + nameNoSuffix;
        try {
          String hashStr = Files.asByteSource( new File(realPath)).hash(Hashing.md5()).toString();
          hashname = nameNoSuffix + "?" + hashStr;
        } catch (IOException e) {
          Logger.error(e);
          hashname = name;
        }        
        resources.put(name, hashname);
      }
    }
    return hashname;  
  }
}

@@ -414,16 +414,63 @@
 = null;%0A
+  private static boolean warningShown = false;%0A
   public
@@ -506,16 +506,17 @@
 text ctx
+ 
 )%7B%0A    r
@@ -574,16 +574,18 @@
 ;%0A  %7D%0A  
+  
 %0A  //Giv
@@ -1122,24 +1122,53 @@
  == null )%7B%0A
+        if(!warningShown)%7B%0A  
         Logg
@@ -1266,16 +1266,109 @@
  context
+ or the servlet is not deployed in an exploded way (e.g. when serving directly from war-file)
 . It won
@@ -1410,24 +1410,65 @@
  names.%22 );%0A
+          warningShown = true;%0A        %7D%0A
         hash

Improve error message + only show it once