// Inject members into the listener via Guice.
inj.getMembersInjector(DiffableListener.class).injectMembers(this);
// Initialize the diffable context
DiffableContext diffableCtx = new DiffableContext();
ctx.setAttribute(Constants.DIFFABLE_CONTEXT, diffableCtx);
// Initialize the resource manager. For testing, the DiffableListener
// is retrieved via Guice with a mock ResourceManager injected already,
// which is why this conditional check is performed.
if (mgr == null) {
mgr = inj.getInstance(ResourceManager.class);
}
// Initialize the resource.
try {
String webAppBaseDir = ctx.getRealPath("/");
mgr.initialize(webAppBaseDir, diffableCtx);
} catch (ResourceManagerException exc) {
provider.error(logger, "resourcestore.problem");
printer.print(exc);
}
// Retrieve the directories containing resources to be managed and
// crawl them for resources, adding them to the resource manager as
// they are found. If the path begins with a slash, it will be
// interpreted as absolute. Otherwise, it will be interpreted as
// relative to the web app directory. If multiple folders are specified,
// they should be separated by commas.
String currentPath = ctx.getRealPath("");
String resourceFolders = ctx.getInitParameter("ResourceFolders");
ArrayList<File> foundFolders = new ArrayList<File>();
if (resourceFolders == null) {
String error =
provider.getMessage(
"ErrorMessages", "resourcefolders.none");
logger.error(error);
} else {
String[] folders = resourceFolders.split(",");
for (String path : folders) {
File folder = path.startsWith("/") ?
new File(path) :
new File(currentPath + File.separator + path);
if (!folder.exists()) {
provider.error(logger, "resourcefolders.nonexistent",
folder.getAbsolutePath());
} else if (!folder.isDirectory()) {
provider.error(logger, "resourcefolders.notfolder",
folder.getAbsolutePath());
} else {
foundFolders.add(folder);
}
}
// Set the folders on the DiffableTag so it can locate resources.
//DiffableResourceTag.setFolder(foundFolders);
diffableCtx.setFolder(foundFolders);
// Start up the monitoring thread.
monitor.setFolderAndManager(foundFolders, mgr);
timer.schedule(monitor, 0, interval);
}
// Get the servlet prefix initialization parameter. This is mandatory
// for correctly linking to managed resources, and an exception is
// thrown if this parameter is not defined.
String servletPrefix = ctx.getInitParameter("ServletPrefix");
if (servletPrefix != null) {
diffableCtx.setServletPrefix(servletPrefix);
} else {
provider.error(logger, "servlet.noservletprefix");
}
}