You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by re...@apache.org on 2013/03/30 16:52:14 UTC
svn commit: r1462785 - in /uima/sandbox/uimafit/trunk:
uimafit-legacy-support/src/main/java/org/apache/uima/fit/legacy/converter/
uimafit/src/main/java/org/apache/uima/fit/component/initialize/
uimafit/src/main/java/org/apache/uima/fit/descriptor/ uima...
Author: rec
Date: Sat Mar 30 15:52:13 2013
New Revision: 1462785
URL: http://svn.apache.org/r1462785
Log:
[UIMA-2586] @ExternalResource - key should default to name of annotated field instead of the field type name
https://issues.apache.org/jira/browse/UIMA-2586
Modified:
uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/main/java/org/apache/uima/fit/legacy/converter/ExternalResourceConverter.java
uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/component/initialize/ExternalResourceInitializer.java
uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/descriptor/ExternalResource.java
uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/factory/ExternalResourceFactory.java
uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/factory/ExternalResourceConfiguratorTest.java
uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/factory/ExternalResourceFactoryTest.java
Modified: uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/main/java/org/apache/uima/fit/legacy/converter/ExternalResourceConverter.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/main/java/org/apache/uima/fit/legacy/converter/ExternalResourceConverter.java?rev=1462785&r1=1462784&r2=1462785&view=diff
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/main/java/org/apache/uima/fit/legacy/converter/ExternalResourceConverter.java (original)
+++ uima/sandbox/uimafit/trunk/uimafit-legacy-support/src/main/java/org/apache/uima/fit/legacy/converter/ExternalResourceConverter.java Sat Mar 30 15:52:13 2013
@@ -18,43 +18,63 @@
*/
package org.apache.uima.fit.legacy.converter;
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Field;
+
import org.apache.uima.fit.descriptor.ExternalResource;
+import org.apache.uima.fit.legacy.AnnotationConverter;
import org.apache.uima.resource.Resource;
public class ExternalResourceConverter
- extends
- ContextlessAnnotationConverterBase<org.uimafit.descriptor.ExternalResource, org.apache.uima.fit.descriptor.ExternalResource> {
+ implements
+ AnnotationConverter<org.uimafit.descriptor.ExternalResource, org.apache.uima.fit.descriptor.ExternalResource> {
public ExternalResourceConverter() {
// Nothing to do
}
- @Override
- public ExternalResource convert(
- final org.uimafit.descriptor.ExternalResource aAnnotation) {
- return new ExternalResourceSubstitute(aAnnotation);
+ public ExternalResource convert(AccessibleObject aContext,
+ org.uimafit.descriptor.ExternalResource aAnnotation) {
+ return new ExternalResourceSubstitute(aAnnotation, (Field) aContext);
+ }
+
+ public ExternalResource convert(Class<?> aContext,
+ org.uimafit.descriptor.ExternalResource aAnnotation) {
+ throw new UnsupportedOperationException("Annotation is not permitted on classes");
}
public Class<org.uimafit.descriptor.ExternalResource> getLegacyType() {
return org.uimafit.descriptor.ExternalResource.class;
}
-
+
public Class<ExternalResource> getModernType() {
return ExternalResource.class;
}
-
+
@SuppressWarnings("serial")
- public class ExternalResourceSubstitute extends
- AnnotationLiteral<ExternalResource> implements ExternalResource {
+ public class ExternalResourceSubstitute extends AnnotationLiteral<ExternalResource> implements
+ ExternalResource {
private org.uimafit.descriptor.ExternalResource legacyAnnotation;
-
- public ExternalResourceSubstitute(org.uimafit.descriptor.ExternalResource aAnnotation) {
+
+ private Field field;
+
+ public ExternalResourceSubstitute(org.uimafit.descriptor.ExternalResource aAnnotation,
+ Field aField) {
legacyAnnotation = aAnnotation;
+ field = aField;
}
+ /**
+ * Legacy uimaFIT used the field class name as default value.
+ */
public String key() {
- return legacyAnnotation.key();
+ if (legacyAnnotation.key().equals("")) {
+ return field.getType().getName();
+ }
+ else {
+ return legacyAnnotation.key();
+ }
}
public Class<? extends Resource> api() {
Modified: uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/component/initialize/ExternalResourceInitializer.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/component/initialize/ExternalResourceInitializer.java?rev=1462785&r1=1462784&r2=1462785&view=diff
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/component/initialize/ExternalResourceInitializer.java (original)
+++ uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/component/initialize/ExternalResourceInitializer.java Sat Mar 30 15:52:13 2013
@@ -261,7 +261,7 @@ public class ExternalResourceInitializer
}
/**
- * Get the binding key for the specified field. If no key is set, use the field class name as key.
+ * Get the binding key for the specified field. If no key is set, use the field name as key.
*
* @param field
* the field to bind.
@@ -271,7 +271,7 @@ public class ExternalResourceInitializer
ExternalResource cpa = ReflectionUtil.getAnnotation(field, ExternalResource.class);
String key = cpa.key();
if (key.length() == 0) {
- key = field.getType().getName();
+ key = field.getName();
}
return key;
}
Modified: uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/descriptor/ExternalResource.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/descriptor/ExternalResource.java?rev=1462785&r1=1462784&r2=1462785&view=diff
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/descriptor/ExternalResource.java (original)
+++ uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/descriptor/ExternalResource.java Sat Mar 30 15:52:13 2013
@@ -34,8 +34,8 @@ import org.apache.uima.resource.Resource
@Target(ElementType.FIELD)
public @interface ExternalResource {
/**
- * The key to which external resources bind to. If no key is set, the class name of the annotated
- * field will be used.
+ * The key to which external resources bind to. If no key is set, the name of the annotated field
+ * will be used.
*
* @return the key;
*/
Modified: uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/factory/ExternalResourceFactory.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/factory/ExternalResourceFactory.java?rev=1462785&r1=1462784&r2=1462785&view=diff
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/factory/ExternalResourceFactory.java (original)
+++ uima/sandbox/uimafit/trunk/uimafit/src/main/java/org/apache/uima/fit/factory/ExternalResourceFactory.java Sat Mar 30 15:52:13 2013
@@ -403,7 +403,7 @@ public final class ExternalResourceFacto
/**
* Scan the given resource specifier for external resource dependencies and whenever a dependency
- * is encounter that has a key equal to the resource class name, the resource will be bound.
+ * with a compatible type is found, the resource will be bound.
*
* @param aDesc
* a description.
@@ -414,13 +414,13 @@ public final class ExternalResourceFacto
* @see CustomResourceSpecifier
*/
public static void bindResource(ResourceSpecifier aDesc, Class<? extends Resource> aRes,
- String... aParams) throws InvalidXMLException {
+ String... aParams) throws InvalidXMLException, ClassNotFoundException {
bindResource(aDesc, aRes, aRes, aParams);
}
/**
* Scan the given resource specifier for external resource dependencies and whenever a dependency
- * is encounter that has a key equal to the API class name, the resource will be bound.
+ * with a compatible type is found, the resource will be bound.
*
* @param aDesc
* a description.
@@ -433,17 +433,18 @@ public final class ExternalResourceFacto
* @see CustomResourceSpecifier
*/
public static void bindResource(ResourceSpecifier aDesc, Class<?> aApi,
- Class<? extends Resource> aRes, String... aParams) throws InvalidXMLException {
+ Class<? extends Resource> aRes, String... aParams) throws InvalidXMLException,
+ ClassNotFoundException {
// Appending a disambiguation suffix it possible to have multiple instances of the same
// resource with different settings to different keys.
ExternalResourceDescription extRes = createExternalResourceDescription(
uniqueResourceKey(aRes.getName()), aRes, (Object[]) aParams);
- bindResource(aDesc, aApi.getName(), extRes);
+ bindResource(aDesc, extRes);
}
/**
* Scan the given resource specifier for external resource dependencies and whenever a dependency
- * is encountered that has a key equal to the resource class name, the resource will be bound.
+ * with a compatible type is found, the resource will be bound.
*
* @param aDesc
* a description.
@@ -457,8 +458,10 @@ public final class ExternalResourceFacto
*/
public static void bindResource(ResourceSpecifier aDesc,
Class<? extends SharedResourceObject> aRes, String aUrl, Object... aParams)
- throws InvalidXMLException {
- bindResource(aDesc, aRes, aRes, aUrl, aParams);
+ throws InvalidXMLException, ClassNotFoundException {
+ ExternalResourceDescription extRes = createExternalResourceDescription(
+ uniqueResourceKey(aRes.getName()), aRes, aUrl, aParams);
+ bind((AnalysisEngineDescription) aDesc, extRes);
}
/**
@@ -536,6 +539,23 @@ public final class ExternalResourceFacto
/**
* Scan the given resource specifier for external resource dependencies and whenever a dependency
+ * with a compatible type is found, the given resource is bound to it.
+ *
+ * @param aDesc
+ * a description.
+ * @param aResDesc
+ * the resource description.
+ */
+ public static void bindResource(ResourceSpecifier aDesc, ExternalResourceDescription aResDesc)
+ throws InvalidXMLException, ClassNotFoundException {
+ // Dispatch
+ if (aDesc instanceof AnalysisEngineDescription) {
+ bind((AnalysisEngineDescription) aDesc, aResDesc);
+ }
+ }
+
+ /**
+ * Scan the given resource specifier for external resource dependencies and whenever a dependency
* with the given key is encountered, the given resource is bound to it.
*
* @param aDesc
@@ -702,6 +722,40 @@ public final class ExternalResourceFacto
/**
* Scan the given resource specifier for external resource dependencies and whenever a dependency
+ * a compatible type is found, the given resource is bound to it.
+ *
+ * @param aDesc
+ * a description.
+ * @param aResDesc
+ * the resource description.
+ */
+ private static void bind(AnalysisEngineDescription aDesc, ExternalResourceDescription aResDesc)
+ throws InvalidXMLException, ClassNotFoundException {
+ // Recursively address delegates
+ if (!aDesc.isPrimitive()) {
+ for (Object delegate : aDesc.getDelegateAnalysisEngineSpecifiers().values()) {
+ bindResource((ResourceSpecifier) delegate, aResDesc);
+ }
+ }
+
+ // Bind if necessary
+ Class<?> resClass = Class.forName(getImplementationName(aResDesc));
+ for (ExternalResourceDependency dep : aDesc.getExternalResourceDependencies()) {
+ Class<?> apiClass = Class.forName(dep.getInterfaceName());
+
+ // Never bind fields of type Object. See also ExternalResourceInitializer#getApi()
+ if (apiClass.equals(Object.class)) {
+ continue;
+ }
+
+ if (apiClass.isAssignableFrom(resClass)) {
+ bindExternalResource(aDesc, dep.getKey(), aResDesc);
+ }
+ }
+ }
+
+ /**
+ * Scan the given resource specifier for external resource dependencies and whenever a dependency
* with the given key is encountered, the given resource is bound to it.
*
* @param aDesc
@@ -929,6 +983,24 @@ public final class ExternalResourceFacto
}
/**
+ * Find the name of the class implementing this resource. The location where this name is stored
+ * varies, depending if the resource extends {@link SharedResourceObject} or implements
+ * {@link Resource}.
+ *
+ * @param aDesc the external resource description.
+ * @return the implementation name.
+ */
+ protected static String getImplementationName(ExternalResourceDescription aDesc)
+ {
+ if (aDesc.getResourceSpecifier() instanceof CustomResourceSpecifier) {
+ return ((CustomResourceSpecifier) aDesc.getResourceSpecifier()).getResourceClassName();
+ }
+ else {
+ return aDesc.getImplementationName();
+ }
+ }
+
+ /**
* Extracts the external resource from the configuration parameters and nulls out these
* parameters. Mind that the array passed to this method is modified by the method.
*
Modified: uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/factory/ExternalResourceConfiguratorTest.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/factory/ExternalResourceConfiguratorTest.java?rev=1462785&r1=1462784&r2=1462785&view=diff
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/factory/ExternalResourceConfiguratorTest.java (original)
+++ uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/factory/ExternalResourceConfiguratorTest.java Sat Mar 30 15:52:13 2013
@@ -22,6 +22,7 @@ package org.apache.uima.fit.factory;
import static org.apache.uima.fit.component.initialize.ExternalResourceInitializer.getResourceDeclarations;
import static org.apache.uima.fit.factory.AnalysisEngineFactory.createPrimitiveDescription;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
import java.util.HashMap;
import java.util.Map;
@@ -61,23 +62,26 @@ public class ExternalResourceConfigurato
private void verify(Map<String, ExternalResourceDependency> deps) {
assertEquals(3, deps.size());
- String key = ParameterizedAE2.DummyResource.class.getName();
+ String key1 = "res"; // This is the name of the field
String api = ParameterizedAE2.DummyResource.class.getName();
- ExternalResourceDependency d = deps.get(key);
- assertEquals(key, d.getKey());
- assertEquals(api, d.getInterfaceName());
- assertEquals(false, d.isOptional());
-
- key = ParameterizedAE2.RES_OTHER;
- d = deps.get(key);
- assertEquals(key, d.getKey());
- assertEquals(api, d.getInterfaceName());
- assertEquals(false, d.isOptional());
-
- key = ParameterizedAE2.RES_OPTIONAL;
- d = deps.get(key);
- assertEquals(key, d.getKey());
- assertEquals(api, d.getInterfaceName());
- assertEquals(true, d.isOptional());
+ ExternalResourceDependency d1 = deps.get(key1);
+ assertNotNull(d1);
+ assertEquals(key1, d1.getKey());
+ assertEquals(api, d1.getInterfaceName());
+ assertEquals(false, d1.isOptional());
+
+ String key2 = ParameterizedAE2.RES_OTHER;
+ ExternalResourceDependency d2 = deps.get(key2);
+ assertNotNull(d2);
+ assertEquals(key2, d2.getKey());
+ assertEquals(api, d2.getInterfaceName());
+ assertEquals(false, d2.isOptional());
+
+ String key3 = ParameterizedAE2.RES_OPTIONAL;
+ ExternalResourceDependency d3 = deps.get(key3);
+ assertNotNull(3d);
+ assertEquals(key3, d3.getKey());
+ assertEquals(api, d3.getInterfaceName());
+ assertEquals(true, d3.isOptional());
}
}
Modified: uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/factory/ExternalResourceFactoryTest.java
URL: http://svn.apache.org/viewvc/uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/factory/ExternalResourceFactoryTest.java?rev=1462785&r1=1462784&r2=1462785&view=diff
==============================================================================
--- uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/factory/ExternalResourceFactoryTest.java (original)
+++ uima/sandbox/uimafit/trunk/uimafit/src/test/java/org/apache/uima/fit/factory/ExternalResourceFactoryTest.java Sat Mar 30 15:52:13 2013
@@ -134,7 +134,7 @@ public class ExternalResourceFactoryTest
@Test
public void testDirectInjectionAutowire() throws Exception {
- // Create analysis enginge description
+ // Create analysis engine description
AnalysisEngineDescription desc = createPrimitiveDescription(DummyAE2.class);
// Bind external resources for DummyAE