Hi,
I have thought a bit of how dependency injection in a non-distributed scenario
could possibly be improved without introducing a global context. I'd be curious
about any feedback. The solution is based on uimaFIT annotations and employs a
custom ResourceManager. It could be used without uimaFIT if you are willing to
hand-craft the necessary descriptor.
-- Begin of example --
class FooAE extends org.uimafit.component.JCasAnnotator_ImplBase {
static final String RES_INJECTED_POJO = "InjectedPojo";
@ExternalResource(key = RES_INJECTED_POJO)
Number pojo;
public void process(JCas aJCas) throws AnalysisEngineProcessException {
...
}
}
// Set up external context - for now simply a map.
final Map<String, Object> externalContext = new HashMap<String, Object>();
externalContext.put("externalPojoName", new AtomicInteger(5));
// Set up custom resource manager that uses the above map as context.
// This could easily be changed to access e.g. a Spring ApplicationContext.
SimpleNamedResourceManager resMgr = new SimpleNamedResourceManager();
resMgr.setExternalContext(externalContext);
// Set up analysis engine
AnalysisEngineDescription desc = createPrimitiveDescription(FooAE.class);
bindExternalResource(desc, FooAE.RES_INJECTED_POJO, "externalPojoName");
// Instantiate
AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(desc, resMgr, null);
-- End of example --
The SimpleNamedResourceManager [1] and a test case [2] are currently available
in uimaFIT trunk. There is also an auto-wire mode in which the explicit
bindExternalResource() is not required. In this mode it is assumed that the
names used in the externalContext and the external resource keys in the
components are the same.
Unfortunately I had to apply some rather heavy reflection-based hacking to be
able to implement the SimpleNamedResourceManager as a subclass of
ResourceManager_impl, because an important inner class is package private
instead of protected [3].
For simple cases I could imagine to use e.g. the EJB @Resource [4] annotation
instead of the uimaFIT @ExternalResource annotation in conjunction with
SimpleNamedResourceManager. At least the possibility to mark a resource
optional or mandatory would be missing though.
Cheers,
Richard
[1]
http://code.google.com/p/uimafit/source/browse/trunk/uimaFIT/src/main/java/org/uimafit/util/SimpleNamedResourceManager.java?r=575
[2]
http://code.google.com/p/uimafit/source/browse/trunk/uimaFIT/src/test/java/org/uimafit/factory/ExternalResourceFactoryTest.java?r=574
[3] https://issues.apache.org/jira/browse/UIMA-2102
[4] http://download.oracle.com/javaee/5/api/javax/annotation/Resource.html
--
-------------------------------------------------------------------
Richard Eckart de Castilho
Technical Lead
Ubiquitous Knowledge Processing Lab
FB 20 Computer Science Department
Technische Universität Darmstadt
Hochschulstr. 10, D-64289 Darmstadt, Germany
phone +49 (6151) 16-7477, fax -5455, room S2/02/E225
[email protected]
www.ukp.tu-darmstadt.de
-------------------------------------------------------------------