You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by sc...@apache.org on 2016/10/21 15:29:23 UTC

svn commit: r1766047 - in /uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource: ResourceManager.java impl/ResourceManager_impl.java

Author: schor
Date: Fri Oct 21 15:29:23 2016
New Revision: 1766047

URL: http://svn.apache.org/viewvc?rev=1766047&view=rev
Log:
[UIMA-2903][UIMA-2977] uniformly change to not require that the class-specified-by-implementationName in external resources implement Resource.  Change the signature of getExternalResources to return list of Class<?>, change destroy() method to only call destroy on implementations that implement Resource.  Change ResourceManager API for returning the class of a resource from return type Class<? extends Resource> to <N> Class<N>. 

Modified:
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java?rev=1766047&r1=1766046&r2=1766047&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java Fri Oct 21 15:29:23 2016
@@ -74,7 +74,6 @@ public interface ResourceManager {
 
   /**
    * Gets the instance of the implementation object for a resource that has been registered under the specified name.
-   * These objects all implement the Resource API.
    * 
    * @param aName
    *          the name of the resource to retrieve
@@ -92,10 +91,10 @@ public interface ResourceManager {
 
   /**
    * Returns one of two kinds of objects (or null):
-   *   * an instance of the implementation object for a resource, that has 
+   *   - an instance of the implementation object for a resource, that has 
    *     been loaded with a DataResource resource produced by the resource given the aParms
    *     
-   *   * (if there is no implementation defined for this resource) 
+   *   - (if there is no implementation defined for this resource) 
    *     returns an instance of the DataResource, itself, produced by the resource given the aParms
    *    
    *   An example of a parameterized Resource is a
@@ -146,7 +145,7 @@ public interface ResourceManager {
    * @return the Class for the resource named <code>aName</code>, <code>null</code> if there is
    *         no resource registered under that name.
    */
-  public Class<? extends Resource> getResourceClass(String aName);
+  public <N> Class<N> getResourceClass(String aName);
 
   /**
    * Retrieves the URL to the named resource. This can be used, for example, to locate configuration
@@ -242,19 +241,16 @@ public interface ResourceManager {
    *   Initialization should be done once, on the first call
    * 
    * External resources have a Container class representing the resource, 
-   * which are instances of Resource, and they may also have implementation classes
-   * also instances of Resource, and often implementing SharedResourceObject.
+   * which are instances of Resource.
    * 
-   * As part of the initialization, the External Resource Bindings are processed to hook them up
-   * with defined External Resources.
+   * This may act as the implementation class, or they may also have a
+   * separately specified implementation class, which may or may not implement Resource.
    * 
-   *   If a binding specifies a non-existing resource, the key is interpreted as a file name, 
-   *   and looked up using the current context for relative path resolution.  
-   *     - If found, a FilewResourceSpecifier is created using the file 
-   * 
-   *   If no resource can be found at all, then unless the dependency is marked "optional", 
-   *   an ResourceInitializationException is thrown.
-   *   
+   * As part of the initialization of the Container class, 
+   * by default, External Resource Bindings are processed to hook them up
+   * with defined External Resources, using the default implementation
+   * of resolveAndValidateResourceDependencies.
+   *     
    * @param aConfiguration
    *          the ResourceManagerConfiguration containing resource declarations and bindings
    * @param aQualifiedContextName
@@ -271,17 +267,21 @@ public interface ResourceManager {
           throws ResourceInitializationException;
 
   /**
-   * Resolves a component's external resource dependencies (bindings) using this resource manager. 
+   * Resolves a component's external resource dependencies (bindings) using this resource manager.
+   * 
+   * The default implementation has special defaulting logic:
    * 
-   *   If a binding specifies a non-existing resource, the key is interpreted as a file name, 
-   *   and looked up using the current context for relative path resolution.  
-   *     - If found, a FilewResourceSpecifier is created using the file 
+   *   If a binding specifies a non-existing resource, 
+   *   an attempt is made to interpret the key as a file name, looked up 
+   *   using the current context for relative path resolution.  
+   *     - If successfully found, a FileResourceSpecifier is created using the file
+   *       and used as the implementing class. 
    * 
    *   If no resource can be found at all, then unless the dependency is marked "optional", 
    *   an ResourceInitializationException is thrown.
    * 
    * Multi-threading: may be called on multiple threads, repeatedly for the same set of resources.
-   * Implementations should avoid wasting time do this work.
+   * Implementations should recognize this and skip repeated resolutions.
    * 
    * @param aDependencies
    *          declarations of a component's dependencies on external resources
@@ -395,5 +395,5 @@ public interface ResourceManager {
    *         For parameterized resources, those which have been asked for (having unique parameter sets) 
    *         are included.
    */
-  public List<Resource> getExternalResources();
+  public List<Object> getExternalResources();
 }

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java?rev=1766047&r1=1766046&r2=1766047&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java Fri Oct 21 15:29:23 2016
@@ -67,7 +67,7 @@ public class ResourceManager_impl implem
    *     -- name
    *     -- textual description
    *     -- a ResourceSpecifier describing how to create it
-   *     -- the String name of the Java class that implements the resource)
+   *     -- (optional) the String name of the Java class that implements the resource)
    *   - its defining UIMA Context
    *   
    *   These are used to validate multiple declarations, and to get
@@ -75,11 +75,12 @@ public class ResourceManager_impl implem
    */
   static protected class ResourceRegistration { // make protected https://issues.apache.org/jira/browse/UIMA-2102
     /**
-     * For ParameterizedDataResources, is a Resource
-     * For DataResources, is the implementation object, which is 
+     * For ParameterizedDataResources or DataResources, is the implementation object, which is 
      *   an arbitrary Java class implementing SharedDataResource (which has the "load" method)
+     *   
+     * If the external resource specification omitted the implementation class, a default FileResource  
      */
-    Resource resource;  
+    Object resource;  
 
     ExternalResourceDescription description;
 
@@ -138,29 +139,29 @@ public class ResourceManager_impl implem
    * Map from String keys to Class objects. For ParameterizedResources only, stores the
    * implementation class (not a Resource) corresponding to each resource name.
    * 
-   * These class objects are not Resource instances, but rather "implementations" of Resources.
+   * These class objects may or may not be Resource instances.
    *   They may be arbitrary classes, except that they must implement SharedResourceObject.
    * 
    * This is a many to one map; many keys may refer to the same class
    * 
    * key = aQualifiedContextName + the key name in an external resource binding
    */
-  final protected Map<String, Class<? extends Resource>> mParameterizedResourceImplClassMap;
+  final protected Map<String, Class<?>> mParameterizedResourceImplClassMap;
 
   /**
    * Internal map from resource names (declared in resource declaration XML) to Class objects
    * for parameterized Resource.
    * 
-   *   These class objects are not Resource instances, but rather "implementations" of Resources.
+   *   These class objects may or may not be Resource instances.  
    *   They may be arbitrary classes, except that they must implement SharedResourceObject.
    *   
-   * These are potentially "customized" when referenced, by 
+   * These are "customized" when referenced, by 
    * parameter strings (such as language, for a Dictionary resource). Used
    * internally during resource initialization.
    * 
    * key = external resource declared name.
    */
-  final protected Map<String, Class<? extends Resource>> mInternalParameterizedResourceImplClassMap;
+  final protected Map<String, Class<?>> mInternalParameterizedResourceImplClassMap;
 
   /**
    * Map from ParameterizedResourceKey to SharedResourceObject or DataResource instances.
@@ -172,7 +173,7 @@ public class ResourceManager_impl implem
    * This map is for ParameterizedResources only, and 
    * stores the SharedResourceObjects or DataResource objects that have already been instantiated and loaded.
    */
-  final protected Map<List<Object>, Resource> mParameterizedResourceInstanceMap;
+  final protected Map<List<Object>, Object> mParameterizedResourceInstanceMap;
 
   /**
    * UIMA extension ClassLoader. ClassLoader is created if an extension classpath is specified at
@@ -223,9 +224,9 @@ public class ResourceManager_impl implem
   public ResourceManager_impl() {
     mResourceMap = Collections.synchronizedMap(new HashMap<String, Object>());
     mInternalResourceRegistrationMap = new ConcurrentHashMap<String, ResourceRegistration>();
-    mParameterizedResourceImplClassMap =  new ConcurrentHashMap<String, Class<? extends Resource>>();
-    mInternalParameterizedResourceImplClassMap = new ConcurrentHashMap<String, Class<? extends Resource>>();
-    mParameterizedResourceInstanceMap =  new ConcurrentHashMap<List<Object>, Resource>();
+    mParameterizedResourceImplClassMap =  new ConcurrentHashMap<String, Class<?>>();
+    mInternalParameterizedResourceImplClassMap = new ConcurrentHashMap<String, Class<?>>();
+    mParameterizedResourceInstanceMap =  new ConcurrentHashMap<List<Object>, Object>();
     mRelativePathResolver = new RelativePathResolver_impl(); 
   }
 
@@ -237,9 +238,9 @@ public class ResourceManager_impl implem
   public ResourceManager_impl(ClassLoader aClassLoader) {
     mResourceMap = Collections.synchronizedMap(new HashMap<String, Object>());
     mInternalResourceRegistrationMap = new ConcurrentHashMap<String, ResourceRegistration>();
-    mParameterizedResourceImplClassMap =  new ConcurrentHashMap<String, Class<? extends Resource>>();
-    mInternalParameterizedResourceImplClassMap = new ConcurrentHashMap<String, Class<? extends Resource>>();
-    mParameterizedResourceInstanceMap =  new ConcurrentHashMap<List<Object>, Resource>();
+    mParameterizedResourceImplClassMap =  new ConcurrentHashMap<String, Class<?>>();
+    mInternalParameterizedResourceImplClassMap = new ConcurrentHashMap<String, Class<?>>();
+    mParameterizedResourceInstanceMap =  new ConcurrentHashMap<List<Object>, Object>();
     mRelativePathResolver = new RelativePathResolver_impl(aClassLoader);
   }
 
@@ -249,9 +250,9 @@ public class ResourceManager_impl implem
   public ResourceManager_impl(
       Map<String, Object> resourceMap,
       Map<String, ResourceRegistration> internalResourceRegistrationMap,
-      Map<String, Class<? extends Resource>> parameterizedResourceImplClassMap,
-      Map<String, Class<? extends Resource>> internalParameterizedResourceImplClassMap,
-      Map<List<Object>, Resource> parameterizedResourceInstanceMap) {
+      Map<String, Class<?>> parameterizedResourceImplClassMap,
+      Map<String, Class<?>> internalParameterizedResourceImplClassMap,
+      Map<List<Object>, Object> parameterizedResourceInstanceMap) {
     mResourceMap = resourceMap;
     mInternalResourceRegistrationMap = internalResourceRegistrationMap;
     mParameterizedResourceImplClassMap =  parameterizedResourceImplClassMap;
@@ -435,11 +436,11 @@ public class ResourceManager_impl implem
       }
       // We haven't encountered this before. See if we need to instantiate a
       // SharedResourceObject
-      Class<? extends Resource> sharedResourceObjectClass = mParameterizedResourceImplClassMap.get(aName);
+      Class<?> sharedResourceObjectClass = mParameterizedResourceImplClassMap.get(aName);
       if (sharedResourceObjectClass != EMPTY_RESOURCE_CLASS) {
         try {
-          Resource sro = sharedResourceObjectClass.newInstance();
-          ((SharedResourceObject)sro).load(dr);
+          SharedResourceObject sro = (SharedResourceObject) sharedResourceObjectClass.newInstance();
+          sro.load(dr);
           mParameterizedResourceInstanceMap.put(nameAndResource, sro);
           return sro;
         } catch (InstantiationException e) {
@@ -463,7 +464,7 @@ public class ResourceManager_impl implem
    */
   @Override
   @SuppressWarnings("unchecked")
-  public Class<? extends Resource> getResourceClass(String aName) {
+  public Class<?> getResourceClass(String aName) {
     Object r = mResourceMap.get(aName);
     if (r == null) // no such resource
     {
@@ -472,7 +473,7 @@ public class ResourceManager_impl implem
 
     // if this is a ParameterizedDataResource, look up its class
     if (r instanceof ParameterizedDataResource) {
-      Class<? extends Resource> customResourceClass = (Class<? extends Resource>) mParameterizedResourceImplClassMap.get(aName);
+      Class<?> customResourceClass = mParameterizedResourceImplClassMap.get(aName);
       if (customResourceClass == EMPTY_RESOURCE_CLASS) {
         // return the default class
         return DataResource_impl.class;
@@ -617,7 +618,7 @@ public class ResourceManager_impl implem
       }
       mResourceMap.put(aQualifiedContextName + bindings[i].getKey(), registration.resource);
       // record the link from key to resource class (for parameterized resources only)
-      Class<? extends Resource> impl = mInternalParameterizedResourceImplClassMap.get(bindings[i].getResourceName()); 
+      Class<?> impl = mInternalParameterizedResourceImplClassMap.get(bindings[i].getResourceName()); 
       mParameterizedResourceImplClassMap.put(aQualifiedContextName + bindings[i].getKey(),
                                              (impl == null) ? EMPTY_RESOURCE_CLASS : impl);
     }
@@ -641,9 +642,9 @@ public class ResourceManager_impl implem
     for (int i = 0; i < aDependencies.length; i++) {
       // get resource
       String qname = aQualifiedContextName + aDependencies[i].getKey();
-      Resource resource = (Resource) mResourceMap.get(qname);
+      Object resourceImpl = mResourceMap.get(qname);  // may or may not implement Resource, may implement SharedResourceObject
       
-      if (resource == null) {
+      if (resourceImpl == null) {
         // no resource found
         // try to look up in classpath/datapath
         URL relativeUrl;
@@ -657,12 +658,13 @@ public class ResourceManager_impl implem
           // found - create a DataResource object and store it in the mResourceMap
           FileResourceSpecifier spec = new FileResourceSpecifier_impl();
           spec.setFileUrl(absUrl.toString());
-          resource = UIMAFramework.produceResource(spec, null);
-          mResourceMap.put(qname, resource);
+          // produces an instance of DataResourceImpl
+          resourceImpl = UIMAFramework.produceResource(spec, null);
+          mResourceMap.put(qname, resourceImpl);
         }
       }
       
-      if (resource == null) { // still no resource found - throw exception if required
+      if (resourceImpl == null) { // still no resource found - throw exception if required
 
         if (!aDependencies[i].isOptional()) {
           throw new ResourceInitializationException(
@@ -678,7 +680,7 @@ public class ResourceManager_impl implem
           if (name != null && name.length() > 0) {
             Class<?> theInterface = loadUserClass(name);
 
-            Class<? extends Resource> resourceClass = getResourceClass(qname);
+            Class<?> resourceClass = getResourceClass(qname);
             if (!theInterface.isAssignableFrom(resourceClass)) {
               throw new ResourceInitializationException(
                       ResourceInitializationException.RESOURCE_DOES_NOT_IMPLEMENT_INTERFACE,
@@ -716,8 +718,8 @@ public class ResourceManager_impl implem
     // load implementation class (if any) and ensure that it implements
     // SharedResourceObject
     String implementationName = aResourceDescription.getImplementationName();
-    Class<? extends Resource> implClass = null;
-    Resource implInstance = r;  // what will be registered, might be the Resource, or its implementation
+    Class<?> implClass = null;  // might or might not impl Resource
+    Object implInstance = r;  // what will be registered, might be the Resource, or its implementation
     if (implementationName != null && implementationName.length() > 0) {
       try {
         implClass = loadUserClass(implementationName);
@@ -738,9 +740,9 @@ public class ResourceManager_impl implem
       // instantiate and load the resource object if there is one
       if (implClass != null) {
         try {
-          Resource sro = implClass.newInstance();
+          SharedResourceObject sro = (SharedResourceObject) implClass.newInstance();
           if (!verificationMode) {
-            ((SharedResourceObject)sro).load((DataResource) r);
+            sro.load((DataResource) r);
           }
           implInstance = sro;   // so the implementation is registered, for DataResources
         } catch (InstantiationException e) {
@@ -864,12 +866,16 @@ public class ResourceManager_impl implem
       return;
     }
     
-    for (ResourceRegistration r : mInternalResourceRegistrationMap.values()) {
-        r.resource.destroy();
+    for (ResourceRegistration rr : mInternalResourceRegistrationMap.values()) {
+      if (rr.resource instanceof Resource) {
+        ((Resource)rr.resource).destroy();
+      }
     }
     
-    for (Resource r : mParameterizedResourceInstanceMap.values()) {
-      r.destroy();
+    for (Object r : mParameterizedResourceInstanceMap.values()) {
+      if (r instanceof Resource) {
+        ((Resource)r).destroy();
+      }
     }
     
     // no destroy of caspool at this time
@@ -880,16 +886,16 @@ public class ResourceManager_impl implem
    * @see org.apache.uima.resource.ResourceManager#getExternalResources()
    */
   @Override
-  public List<Resource> getExternalResources() {
+  public List<Object> getExternalResources() {
     
-    List<Resource> rs = new ArrayList<>();
+    List<Object> rs = new ArrayList<>();
     for (ResourceRegistration r : mInternalResourceRegistrationMap.values()) {
       if (!(r instanceof ParameterizedDataResource)) {
         rs.add(r.resource);
       } 
     }
     
-    for (Resource r : mParameterizedResourceInstanceMap.values()) {
+    for (Object r : mParameterizedResourceInstanceMap.values()) {
       rs.add(r);
     }