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/20 21:30:32 UTC
svn commit: r1765888 - in
/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/resource:
ResourceManager.java impl/ResourceManager_impl.java
Author: schor
Date: Thu Oct 20 21:30:32 2016
New Revision: 1765888
URL: http://svn.apache.org/viewvc?rev=1765888&view=rev
Log:
[UIMA-5146] [UIMA-2977] merging from trunk the same change - add getExternalResources
Modified:
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java (contents, props changed)
uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java (contents, props changed)
Modified: uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java
URL: http://svn.apache.org/viewvc/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java?rev=1765888&r1=1765887&r2=1765888&view=diff
==============================================================================
--- uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java (original)
+++ uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java Thu Oct 20 21:30:32 2016
@@ -22,8 +22,8 @@ package org.apache.uima.resource;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
+import java.util.List;
import java.util.Map;
-import java.util.Set;
import org.apache.uima.resource.metadata.ResourceManagerConfiguration;
import org.apache.uima.util.XMLizable;
@@ -73,13 +73,14 @@ public interface ResourceManager {
public URL resolveRelativePath(String aRelativePath) throws MalformedURLException;
/**
- * Gets the Resource that has been registered under the specified name.
+ * 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
*
- * @return the Resource registered under <code>aName</code>, <code>null</code> if none
- * exists.
+ * @return the instance of the implementation object for the resource, registered under <code>aName</code>,
+ * <code>null</code> if none exists.
*
* @throws ResourceAccessException
* if the requested resource could not be initialized. A common cause is that it
@@ -90,16 +91,42 @@ public interface ResourceManager {
public Object getResource(String aName) throws ResourceAccessException;
/**
- * Gets an instance of a parameterized Resource. An example of a parameterized Resource is a
- * dictionary whose data depends on a specified language identifier.
+ * Returns one of two kinds of objects (or null):
+ * * 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)
+ * returns an instance of the DataResource, itself, produced by the resource given the aParms
+ *
+ * An example of a parameterized Resource is a
+ * dictionary whose data depend on a specified language identifier.
+ *
+ * If the implementation object class exists, but no instance has been
+ * created (yet) for the particular data resource corresponding to the parameters,
+ * then this method will create and register a new instance and call its
+ * load() api using the data resource corresponding to the parameters, and
+ * return that.
*
* @param aName
- * the name of the resource to retrieve
+ * the name of the parameterized resource to retrieve
* @param aParams
* the parameters determining which particular instance is returned
+ * and specifying a particular DataResource instance to use in initializing
+ * the implementation of the resource (if there is an implementation).
+ *
+ * If there is no implementation, the DataResource instance
+ * produced from the named Resource given these
+ * parameters is returned instead.
+ *
+ * @return one of two kinds of objects (or null):
+ * an instance of the requested implementation of the named resource where that instance
+ * has been initialized by calling its load method with the DataResource instance produced
+ * from the Resource given aParams,
+ *
+ * or, (if the named resource has no implementation) the DataResource instance
+ * corresponding to the named Resource, given aParams,
*
- * @return the requested Resource, <code>null</code> if there is no resource registered under
- * the name <code>aName</code>.
+ * or if no resource with this name exists, <code>null</code>.
*
* @throws ResourceAccessException
* if there is a resource registered under <code>aName</code> but it could not be
@@ -111,6 +138,8 @@ public interface ResourceManager {
* Gets the Class of the Resource that will be returned by a call to {@link #getResource(String)}
* or {@link #getResource(String,String[])}.
*
+ * For those resource specifications which include an implementation class, this call returns that class.
+ *
* @param aName
* the name of a resource
*
@@ -209,8 +238,23 @@ public interface ResourceManager {
/**
* Initializes all external resources declared in a ResourceCreationSpecifier.
* Multi-threading: may be called on multiple threads.
+ *
* 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.
+ *
+ * As part of the initialization, the External Resource Bindings are processed to hook them up
+ * with defined External Resources.
+ *
+ * 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.
+ *
* @param aConfiguration
* the ResourceManagerConfiguration containing resource declarations and bindings
* @param aQualifiedContextName
@@ -227,8 +271,14 @@ public interface ResourceManager {
throws ResourceInitializationException;
/**
- * Resolves a component's external resource dependencies using this resource manager. Throws an
- * exception if any required dependencies are not satisfied.
+ * Resolves a component's external resource dependencies (bindings) using this resource manager.
+ *
+ * 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.
*
* Multi-threading: may be called on multiple threads, repeatedly for the same set of resources.
* Implementations should avoid wasting time do this work.
@@ -323,7 +373,7 @@ public interface ResourceManager {
* @return the class
* @throws ClassNotFoundException -
*/
- public Class<?> loadUserClass(String name) throws ClassNotFoundException;
+ public <N> Class<N> loadUserClass(String name) throws ClassNotFoundException;
/**
* Frees all resources held by this ResourceManager, and marks the ResourceManager as having been destroyed.
@@ -338,4 +388,12 @@ public interface ResourceManager {
* knowledgeable source; for example a single ResourceManager might be used for multiple UIMA Pipelines.
*/
public void destroy();
+
+ /**
+ *
+ * @return a List of External Shared Resource instances instantiated by this Resource Manager.
+ * For parameterized resources, those which have been asked for (having unique parameter sets)
+ * are included.
+ */
+ public List<Resource> getExternalResources();
}
Propchange: uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java
------------------------------------------------------------------------------
--- svn:mergeinfo (added)
+++ svn:mergeinfo Thu Oct 20 21:30:32 2016
@@ -0,0 +1,8 @@
+/incubator/uima/uimaj/branches/mavenAlign/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java:932400-933272
+/uima/uimaj/branches/2.6.0-json/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java:1616936-1617592
+/uima/uimaj/branches/depend-on-july-9-build-tools/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java:963167-964468
+/uima/uimaj/branches/depend-on-parent-pom-4/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java:961329-961745
+/uima/uimaj/branches/filteredCompress-uima-2498/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java:1436573-1462257
+/uima/uimaj/branches/mavenAlign/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java:933273-944396
+/uima/uimaj/branches/test-parent-pom-6/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java:1024030
+/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/ResourceManager.java:1690273-1765881
Modified: uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java
URL: http://svn.apache.org/viewvc/uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java?rev=1765888&r1=1765887&r2=1765888&view=diff
==============================================================================
--- uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java (original)
+++ uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java Thu Oct 20 21:30:32 2016
@@ -74,15 +74,20 @@ public class ResourceManager_impl implem
* a resource to tie it to a binding
*/
static protected class ResourceRegistration { // make protected https://issues.apache.org/jira/browse/UIMA-2102
- Resource resource;
+ /**
+ * For ParameterizedDataResources, is a Resource
+ * For DataResources, is the implementation object, which is
+ * an arbitrary Java class implementing SharedDataResource (which has the "load" method)
+ */
+ Resource resource;
ExternalResourceDescription description;
String definingContext;
- public ResourceRegistration(Resource resource, ExternalResourceDescription description,
+ public ResourceRegistration(Object resourceOrImplementation, ExternalResourceDescription description,
String definingContext) {
- this.resource = resource;
+ this.resource = (Resource) resourceOrImplementation;
this.description = description;
this.definingContext = definingContext;
}
@@ -107,16 +112,23 @@ public class ResourceManager_impl implem
private final RelativePathResolver mRelativePathResolver;
/**
- * Map from qualified key names (declared in resource dependency XML) to Resource objects.
+ * Map from qualified key names (declared in resource dependency XML) to
+ * either DataResource objects or SharedResourceObjects that impl Resource.
+ *
* This map is many to one (multiple keys may refer to the same Resource object)
*
* Can't be concurrentMap because it (currently) depends on storing nulls
+ *
+ * Keeping the Map's 2nd argument as Object, although it could be Resource,
+ * for backwards compatibility
*/
final protected Map<String, Object> mResourceMap;
/**
* Internal map from resource names (declared in resource declaration XML) to ResourceRegistration
- * objects. Used during initialization only.
+ * objects.
+ * Uses: for detecting if multiple declarations are made for the same resource name
+ * .values() for list of all resources that have been registered
*
* This is a one-to-one map.
*/
@@ -124,28 +136,41 @@ public class ResourceManager_impl implem
/**
* Map from String keys to Class objects. For ParameterizedResources only, stores the
- * implementation class corresponding to each resource name.
+ * implementation class (not a Resource) corresponding to each resource name.
+ *
+ * These class objects are not Resource instances, but rather "implementations" of Resources.
+ * 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<?>> mParameterizedResourceImplClassMap;
+ final protected Map<String, Class<? extends Resource>> mParameterizedResourceImplClassMap;
/**
* Internal map from resource names (declared in resource declaration XML) to Class objects
- * for parameterized Resource. These are potentially "customized" when referenced, by
+ * for parameterized Resource.
+ *
+ * These class objects are not Resource instances, but rather "implementations" of Resources.
+ * They may be arbitrary classes, except that they must implement SharedResourceObject.
+ *
+ * These are potentially "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<?>> mInternalParameterizedResourceImplClassMap;
+ final protected Map<String, Class<? extends Resource>> mInternalParameterizedResourceImplClassMap;
/**
- * Map from ParameterizedResourceKey to Resource objects.
- * For ParameterizedResources only, stores the DataResources that have already been encountered, and
- * the Resources that have been instantiated therefrom.
+ * Map from ParameterizedResourceKey to SharedResourceObject or DataResource instances.
+ *
+ * Note: SharedResourceObjects are not "Resource" instances, but rather arbitrary Java classes that
+ * are the "implementations" of Resources. The only requirement on these Java classes is
+ * that they implement SharedResourceObject interface.
+ *
+ * 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;
@@ -198,8 +223,8 @@ 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<?>>();
- mInternalParameterizedResourceImplClassMap = new ConcurrentHashMap<String, Class<?>>();
+ mParameterizedResourceImplClassMap = new ConcurrentHashMap<String, Class<? extends Resource>>();
+ mInternalParameterizedResourceImplClassMap = new ConcurrentHashMap<String, Class<? extends Resource>>();
mParameterizedResourceInstanceMap = new ConcurrentHashMap<List<Object>, Resource>();
mRelativePathResolver = new RelativePathResolver_impl();
}
@@ -212,8 +237,8 @@ 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<?>>();
- mInternalParameterizedResourceImplClassMap = new ConcurrentHashMap<String, Class<?>>();
+ mParameterizedResourceImplClassMap = new ConcurrentHashMap<String, Class<? extends Resource>>();
+ mInternalParameterizedResourceImplClassMap = new ConcurrentHashMap<String, Class<? extends Resource>>();
mParameterizedResourceInstanceMap = new ConcurrentHashMap<List<Object>, Resource>();
mRelativePathResolver = new RelativePathResolver_impl(aClassLoader);
}
@@ -224,8 +249,8 @@ public class ResourceManager_impl implem
public ResourceManager_impl(
Map<String, Object> resourceMap,
Map<String, ResourceRegistration> internalResourceRegistrationMap,
- Map<String, Class<?>> parameterizedResourceImplClassMap,
- Map<String, Class<?>> internalParameterizedResourceImplClassMap,
+ Map<String, Class<? extends Resource>> parameterizedResourceImplClassMap,
+ Map<String, Class<? extends Resource>> internalParameterizedResourceImplClassMap,
Map<List<Object>, Resource> parameterizedResourceInstanceMap) {
mResourceMap = resourceMap;
mInternalResourceRegistrationMap = internalResourceRegistrationMap;
@@ -410,12 +435,12 @@ public class ResourceManager_impl implem
}
// We haven't encountered this before. See if we need to instantiate a
// SharedResourceObject
- Class<?> sharedResourceObjectClass = mParameterizedResourceImplClassMap.get(aName);
+ Class<? extends Resource> sharedResourceObjectClass = mParameterizedResourceImplClassMap.get(aName);
if (sharedResourceObjectClass != EMPTY_RESOURCE_CLASS) {
try {
- SharedResourceObject sro = (SharedResourceObject) sharedResourceObjectClass.newInstance();
- sro.load(dr);
- mParameterizedResourceInstanceMap.put(nameAndResource, (Resource) sro);
+ Resource sro = sharedResourceObjectClass.newInstance();
+ ((SharedResourceObject)sro).load(dr);
+ mParameterizedResourceInstanceMap.put(nameAndResource, sro);
return sro;
} catch (InstantiationException e) {
throw new ResourceAccessException(e);
@@ -455,6 +480,7 @@ public class ResourceManager_impl implem
return customResourceClass;
} else {
// return r's Class
+ // could be, for
return (Class<? extends Resource>) r.getClass();
}
}
@@ -540,13 +566,24 @@ public class ResourceManager_impl implem
throws ResourceInitializationException {
// register resources
checkDestroyed();
+
+ // set up aAdditionalParams to have this resource manager if not already set
+ // so that External Resource instances created from this use this creating/owning
+ // resource manager as the value returned from their getResourceManager call
+ // see https://issues.apache.org/jira/browse/UIMA-5148
+ if (null == aAdditionalParams) {
+ aAdditionalParams = new HashMap<String, Object>();
+ }
+ if (!aAdditionalParams.containsKey(Resource.PARAM_RESOURCE_MANAGER)) {
+ aAdditionalParams.put(Resource.PARAM_RESOURCE_MANAGER, this);
+ }
+
ExternalResourceDescription[] resources = aConfiguration.getExternalResources();
for (int i = 0; i < resources.length; i++) {
String name = resources[i].getName();
// check for existing resource registration under this name
- ResourceRegistration registration = mInternalResourceRegistrationMap
- .get(name);
+ ResourceRegistration registration = mInternalResourceRegistrationMap.get(name);
if (registration == null) {
registerResource(name, resources[i], aQualifiedContextName, aAdditionalParams);
} else {
@@ -580,7 +617,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<?> impl = mInternalParameterizedResourceImplClassMap.get(bindings[i].getResourceName());
+ Class<? extends Resource> impl = mInternalParameterizedResourceImplClassMap.get(bindings[i].getResourceName());
mParameterizedResourceImplClassMap.put(aQualifiedContextName + bindings[i].getKey(),
(impl == null) ? EMPTY_RESOURCE_CLASS : impl);
}
@@ -604,7 +641,8 @@ public class ResourceManager_impl implem
for (int i = 0; i < aDependencies.length; i++) {
// get resource
String qname = aQualifiedContextName + aDependencies[i].getKey();
- Object resource = mResourceMap.get(qname);
+ Resource resource = (Resource) mResourceMap.get(qname);
+
if (resource == null) {
// no resource found
// try to look up in classpath/datapath
@@ -623,14 +661,17 @@ public class ResourceManager_impl implem
mResourceMap.put(qname, resource);
}
}
- if (resource == null) // still no resource found - throw exception if required
- {
+
+ if (resource == null) { // still no resource found - throw exception if required
+
if (!aDependencies[i].isOptional()) {
throw new ResourceInitializationException(
ResourceInitializationException.RESOURCE_DEPENDENCY_NOT_SATISFIED, new Object[] {
aDependencies[i].getKey(), aDependencies[i].getSourceUrlString() });
}
- } else {
+
+ } else { // resource not null
+
// make sure resource exists and implements the correct interface
try {
String name = aDependencies[i].getInterfaceName();
@@ -674,7 +715,8 @@ public class ResourceManager_impl implem
// load implementation class (if any) and ensure that it implements
// SharedResourceObject
String implementationName = aResourceDescription.getImplementationName();
- Class<?> implClass = null;
+ Class<? extends Resource> implClass = null;
+ Resource implInstance = r; // what will be registered, might be the Resource, or its implementation
if (implementationName != null && implementationName.length() > 0) {
try {
implClass = loadUserClass(implementationName);
@@ -695,11 +737,12 @@ public class ResourceManager_impl implem
// instantiate and load the resource object if there is one
if (implClass != null) {
try {
- SharedResourceObject sro = (SharedResourceObject) implClass.newInstance();
+ Resource sro = implClass.newInstance();
if (!verificationMode) {
- sro.load((DataResource) r);
+ ((SharedResourceObject)sro).load((DataResource) r);
}
r = (Resource) sro;
+ implInstance = sro; // so the implementation is registered, for DataResources
} catch (InstantiationException e) {
throw new ResourceInitializationException(
ResourceInitializationException.COULD_NOT_INSTANTIATE, new Object[] {
@@ -716,9 +759,8 @@ public class ResourceManager_impl implem
// we can't load the SharedResourceObject now, but we need to remember
// which class it is for later when we get a request with parameters
mInternalParameterizedResourceImplClassMap.put(aName, (null == implClass) ? EMPTY_RESOURCE_CLASS : implClass);
- } else
+ } else {
// it is some other type of Resource
- {
// it is an error to specify an implementation class in this case
if (implClass != null) {
throw new ResourceInitializationException(
@@ -728,8 +770,8 @@ public class ResourceManager_impl implem
}
}
- // put resource in internal map for later retrieval
- ResourceRegistration registration = new ResourceRegistration(r, aResourceDescription,
+ // put resource or its implementation (for DataResources) in internal map for later retrieval
+ ResourceRegistration registration = new ResourceRegistration(implInstance, aResourceDescription,
aDefiningContext);
mInternalResourceRegistrationMap.put(aName, registration);
}
@@ -785,12 +827,12 @@ public class ResourceManager_impl implem
}
@Override
- public Class<?> loadUserClass(String name) throws ClassNotFoundException {
+ public <N> Class<N> loadUserClass(String name) throws ClassNotFoundException {
ClassLoader cl = getExtensionClassLoader();
if (cl == null) {
cl = this.getClass().getClassLoader();
}
- return Class.forName(name, true, cl);
+ return (Class<N>) Class.forName(name, true, cl);
}
public static Class<?> loadUserClass(String name, ResourceManager rm) throws ClassNotFoundException {
@@ -823,7 +865,7 @@ public class ResourceManager_impl implem
}
for (ResourceRegistration r : mInternalResourceRegistrationMap.values()) {
- r.resource.destroy();
+ r.resource.destroy();
}
for (Resource r : mParameterizedResourceInstanceMap.values()) {
@@ -834,6 +876,24 @@ public class ResourceManager_impl implem
}
-
+ /* (non-Javadoc)
+ * @see org.apache.uima.resource.ResourceManager#getExternalResources()
+ */
+ @Override
+ public List<Resource> getExternalResources() {
+
+ List<Resource> rs = new ArrayList<>();
+ for (ResourceRegistration r : mInternalResourceRegistrationMap.values()) {
+ if (!(r instanceof ParameterizedDataResource)) {
+ rs.add(r.resource);
+ }
+ }
+
+ for (Resource r : mParameterizedResourceInstanceMap.values()) {
+ rs.add(r);
+ }
+
+ return rs;
+ }
}
Propchange: uima/uimaj/branches/experiment-v3-jcas/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Oct 20 21:30:32 2016
@@ -5,4 +5,4 @@
/uima/uimaj/branches/filteredCompress-uima-2498/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java:1436573-1462257
/uima/uimaj/branches/mavenAlign/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java:933273-944396
/uima/uimaj/branches/test-parent-pom-6/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java:1024030
-/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java:1690273-1765718
+/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/impl/ResourceManager_impl.java:1690273-1765883