package org.romaframework.aspect.view.echo2;
import java.io.File;
import java.util.HashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.romaframework.core.Utility;
import org.romaframework.core.config.RomaApplicationContext;
public class WebResourseResolver {
private HashMap<String, String> cache = new HashMap<String, String>();
public static String BASE_FOLDER_NAME = "base";
public static String CUSTOM_FOLDER_NAME = "custom";
public static final char SEPARATOR = '/';
private String basePath;
private static Log log = LogFactory.getLog(WebResourseResolver.class);
public String getResource(String iName) {
String path = cache.get(iName);
if (path != null)
return path;
synchronized (cache) {
if (basePath == null) {
basePath = RomaApplicationContext.getApplicationPath();
if (basePath.charAt(basePath.length() - 1) != SEPARATOR)
// APPEND THE SEPARATOR
basePath += SEPARATOR;
}
int pos = iName.indexOf(SEPARATOR);
String first = pos > -1 ? iName.substring(0, pos) : "";
String second = pos > -1 ? iName.substring(pos + 1) : iName;
File file = new File(basePath + first + "/" + CUSTOM_FOLDER_NAME + "/" + second);
log.info("[WebResourseResolver.getResource] Try to resolving path: " + iName + " to: " + file.getAbsolutePath());
if (!file.exists()) {
file = new File(basePath + first + "/" + BASE_FOLDER_NAME + "/" + second);
log.info("[WebResourseResolver.getResource] Try to resolving path: " + iName + " to: " + file.getAbsolutePath());
if (!file.exists())
return null;
}
log.info("[WebResourseResolver.getResource] Resolved path: " + iName + " to: " + file.getAbsolutePath());
path = file.getAbsolutePath().substring(basePath.length());
path = Utility.getUniversalResourcePath(path);
cache.put(iName, path);
}
return path;
}
}
|