@Override
@SuppressWarnings("unchecked")
public void init() throws ServletException
{
// Run in servlet container, load config from config.xml.
ResourceLookup webInfLookup = new ResourceLookup(new PrefixDecoratorLocator(
new ServletContextLocator(getServletContext()), "/WEB-INF/"));
try
{
config = DcsConfig.deserialize(webInfLookup.getFirst("dcs-config.xml"));
}
catch (Exception e)
{
throw new ServletException("Could not read 'config.xml' resource.", e);
}
// Initialize XSLT
initXslt(config, webInfLookup);
// Load component suite. Use classpath too (for JUnit tests).
try
{
List<IResourceLocator> resourceLocators = Lists.newArrayList();
resourceLocators.add(new PrefixDecoratorLocator(new ServletContextLocator(
getServletContext()), "/WEB-INF/suites/"));
if (Boolean.getBoolean(ENABLE_CLASSPATH_LOCATOR)) resourceLocators
.add(Location.CONTEXT_CLASS_LOADER.locator);
ResourceLookup suitesLookup = new ResourceLookup(resourceLocators);
IResource suiteResource = suitesLookup
.getFirst(config.componentSuiteResource);
if (suiteResource == null)
{
throw new Exception(
"Suite file not found in servlet context's /WEB-INF/suites: "
+ config.componentSuiteResource);
}
componentSuite = ProcessingComponentSuite.deserialize(suiteResource,
suitesLookup);
}
catch (Exception e)
{
throw new ServletException("Could initialize component suite.", e);
}
// Initialize defaults.
if (componentSuite.getAlgorithms().size() == 0)
{
throw new ServletException("Component suite has no algorithms.");
}
defaultAlgorithmId = componentSuite.getAlgorithms().get(0).getId();
// Initialize controller
final List<Class<? extends IProcessingComponent>> cachedComponentClasses = Lists
.newArrayListWithExpectedSize(2);
if (config.cacheDocuments)
{
cachedComponentClasses.add(IDocumentSource.class);
}
if (config.cacheClusters)
{
cachedComponentClasses.add(IClusteringAlgorithm.class);
}
controller = ControllerFactory.createCachingPooling(cachedComponentClasses
.toArray(new Class [cachedComponentClasses.size()]));
List<IResourceLocator> locators = Lists.newArrayList();
locators.add(new PrefixDecoratorLocator(new ServletContextLocator(
getServletContext()), "/WEB-INF/resources/"));
if (Boolean.getBoolean(ENABLE_CLASSPATH_LOCATOR)) locators
.add(Location.CONTEXT_CLASS_LOADER.locator);
// Allow multiple resource lookup paths for different component configurations.
String resourceLookupAttrKey = AttributeUtils.getKey(DefaultLexicalDataFactory.class, "resourceLookup");
String altResourceLookupAttrKey = "dcs.resource-lookup";
ProcessingComponentConfiguration [] configurations = componentSuite.getComponentConfigurations();
for (int i = 0; i < configurations.length; i++) {
ProcessingComponentConfiguration config = configurations[i];
Object location = config.attributes.get(altResourceLookupAttrKey);
if (location != null && location instanceof String) {
File resourceDir = new File((String) location);
if (!resourceDir.isDirectory()) {
Logger.getRootLogger().warn("Not a resource folder, ignored: " + resourceDir);
} else {
HashMap<String,Object> mutableMap = new HashMap<String,Object>(config.attributes);
mutableMap.put(resourceLookupAttrKey,
new ResourceLookup(new DirLocator(resourceDir)));
config = configurations[i] = new ProcessingComponentConfiguration(
config.componentClass,
config.componentId,
mutableMap);
}
}
}
controller.init(
ImmutableMap.<String, Object> of(resourceLookupAttrKey, new ResourceLookup(locators)),
configurations);
}