String markupLanguageName,
String programmingLanguageName,
SourceResolver resolver)
throws Exception {
Source source = resolver.resolve(fileName);
try {
// Set filenames
StringBuffer contextFilename = new StringBuffer(this.rootPackage.replace('.', File.separatorChar));
contextFilename.append(File.separator);
String id = source.getSystemId();
if(id.startsWith(this.contextDir)) {
// VG: File is located under contextDir, using relative file name
contextFilename.append(id.substring(this.contextDir.length()));
} else {
// VG: File is located outside of contextDir, using systemId
getLogger().debug("Loading from external source " + id);
contextFilename.append(id);
}
String normalizedName = IOUtils.normalizedFilename(contextFilename.toString());
// Ensure no 2 requests for the same file overlap
Class program = null;
CompiledComponent programInstance = null;
// Attempt to load program object from cache
try {
programInstance = (CompiledComponent) select(normalizedName);
} catch (Exception e) {
getLogger().debug("The instance was not accessible from the internal cache. Proceeding.");
}
if ((programInstance == null) && this.preload) {
String className = normalizedName.replace(File.separatorChar, '.');
try {
program = this.classManager.loadClass(className);
this.addCompiledComponent(newManager, normalizedName, program);
programInstance = (CompiledComponent) select(normalizedName);
} catch (Exception e) {
getLogger().debug("The class was not preloaded");
}
}
if (programInstance == null) {
programInstance =
this.createResource(
newManager, fileName, normalizedName,
markupLanguageName, programmingLanguageName, resolver
);
}
if (this.autoReload == false) {
return programInstance;
}
/*
* FIXME: It's the program (not the instance) that must
* be queried for changes!!!
*/
if (programInstance != null && programInstance.modifiedSince(source.getLastModified())) {
// Release the component.
release(programInstance);
// Unload program
ProgrammingLanguage programmingLanguage = (ProgrammingLanguage)this.languageSelector.select(programmingLanguageName);
programmingLanguage.setLanguageName(programmingLanguageName);
programmingLanguage.unload(program, normalizedName, this.workDir);
this.cache.removeGenerator(normalizedName);
// Invalidate previous program/instance pair
program = null;
programInstance = null;
}
if (programInstance == null) {
if (program == null) {
programInstance =
this.createResource(
newManager, fileName, normalizedName,
markupLanguageName, programmingLanguageName,
resolver
);
} else
programInstance = (CompiledComponent) select(normalizedName);
}
return programInstance;
} finally {
source.recycle();
}
}