Author: schor
Date: Wed Apr 16 15:24:41 2014
New Revision: 1587942

URL: http://svn.apache.org/r1587942
Log:
[UIMA-1249] allow initialization of additional instances of a pipeline with the 
same resourcemanager, after the cas pool has been defined. Otherwise, report an 
error.

Modified:
    
uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/UIMARuntimeException.java
    
uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/impl/CasManager_impl.java
    
uima/uimaj/trunk/uimaj-core/src/main/resources/org/apache/uima/UIMAException_Messages.properties

Modified: 
uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/UIMARuntimeException.java
URL: 
http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/UIMARuntimeException.java?rev=1587942&r1=1587941&r2=1587942&view=diff
==============================================================================
--- 
uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/UIMARuntimeException.java
 (original)
+++ 
uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/UIMARuntimeException.java
 Wed Apr 16 15:24:41 2014
@@ -136,6 +136,14 @@ public class UIMARuntimeException extend
   public static final String ILLEGAL_CAS_COPY_TO_SAME_CAS = 
"illegal_cas_copy_to_same_cas";
   
   /**
+   * Message key for a standard UIMA exception message: "Illegal adding of 
additional MetaData after
+   * CASes have been defined.  Likely cause is the reuse of a Resource Manager 
object for a different
+   * pipeline, after it has already been initialized."
+   */
+  public static final String ILLEGAL_ADDING_OF_NEW_META_INFO_AFTER_CAS_DEFINED 
= "illegal_adding_of_new_meta_info";
+
+  
+  /**
    * Creates a new exception with a null message.
    */
   public UIMARuntimeException() {

Modified: 
uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/impl/CasManager_impl.java
URL: 
http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/impl/CasManager_impl.java?rev=1587942&r1=1587941&r2=1587942&view=diff
==============================================================================
--- 
uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/impl/CasManager_impl.java
 (original)
+++ 
uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/resource/impl/CasManager_impl.java
 Wed Apr 16 15:24:41 2014
@@ -40,7 +40,10 @@ import org.apache.uima.resource.CasDefin
 import org.apache.uima.resource.CasManager;
 import org.apache.uima.resource.ResourceInitializationException;
 import org.apache.uima.resource.ResourceManager;
+import org.apache.uima.resource.metadata.FsIndexCollection;
 import org.apache.uima.resource.metadata.ProcessingResourceMetaData;
+import org.apache.uima.resource.metadata.TypePriorities;
+import org.apache.uima.resource.metadata.TypeSystemDescription;
 import org.apache.uima.util.CasCreationUtils;
 import org.apache.uima.util.CasPool;
 import org.apache.uima.util.impl.CasPoolManagementImpl;
@@ -60,8 +63,10 @@ public class CasManager_impl implements 
    * accumulates the metadata needed for shared CASes for this resource manager
    * Starts out "empty" when this is created; is added to (but never removed)
    * Duplicates may be in the list.
+   * 
+   * Threading: This list is always accessed under the class instance lock.
    */
-  private final List<ProcessingResourceMetaData> mMetaDataList = 
Collections.synchronizedList(new ArrayList<ProcessingResourceMetaData>());
+  private final List<ProcessingResourceMetaData> mMetaDataList = new 
ArrayList<ProcessingResourceMetaData>();
 
   private final Map<String, CasPool> mRequestorToCasPoolMap = 
Collections.synchronizedMap(new HashMap<String, CasPool>());
 
@@ -91,12 +96,55 @@ public class CasManager_impl implements 
    */
   public synchronized void addMetaData(ProcessingResourceMetaData aMetaData) {
     if (mCasDefinition != null) {
-      throw new UIMARuntimeException();  // internal error  UIMA-1249
+      if (containsSameTypeAndIndexInfo(aMetaData)) { //UIMA-1249
+        return;
+      }
+      throw new 
UIMARuntimeException(UIMARuntimeException.ILLEGAL_ADDING_OF_NEW_META_INFO_AFTER_CAS_DEFINED,
 new Object[] {});  // internal error  UIMA-1249    
     }
     mMetaDataList.add(aMetaData);
 //    mCasDefinition = null; // mark this stale
 //    mCurrentTypeSystem = null; //this too
   }
+  
+  /**
+   * This comparison is needed to avoid throwing errors in the use case:
+   *   a) the pipeline has been fully initialized, and one or more CASes have 
been drawn from this pool
+   *   b) Another instance of an annotator using the same resource manager is 
initialized.
+   *   
+   * In this case there is no change to the metadata, and we do not want to 
disturb anything.
+   * 
+   * @param md the metadata to see if its in the list already
+   * @return true if the type system description, the type priority 
description, and the index collection are
+   * in the list already.
+   */
+  private boolean containsSameTypeAndIndexInfo(ProcessingResourceMetaData md) {
+    final TypeSystemDescription tsd = md.getTypeSystem();
+    final TypePriorities tsp = md.getTypePriorities();
+    final FsIndexCollection ic = md.getFsIndexCollection();
+    
+    for (ProcessingResourceMetaData item : mMetaDataList) {
+      final TypeSystemDescription otsd = item.getTypeSystem();
+      final TypePriorities otsp = item.getTypePriorities();
+      final FsIndexCollection oic = item.getFsIndexCollection();
+      if (equalsWithNulls(tsp, otsp) &&
+          equalsWithNulls(tsd, otsd) &&
+          equalsWithNulls(ic , oic )) {
+        return true;
+      }
+    }
+    return false;
+  }
+  
+  private boolean equalsWithNulls(Object a, Object b) {
+    if (a == null && b == null) {
+      return true;
+    }
+    if (a != null) {
+      return a.equals(b);
+    } else {
+      return b.equals(a);
+    }
+  }
 
   /*
    * (non-Javadoc)

Modified: 
uima/uimaj/trunk/uimaj-core/src/main/resources/org/apache/uima/UIMAException_Messages.properties
URL: 
http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/resources/org/apache/uima/UIMAException_Messages.properties?rev=1587942&r1=1587941&r2=1587942&view=diff
==============================================================================
--- 
uima/uimaj/trunk/uimaj-core/src/main/resources/org/apache/uima/UIMAException_Messages.properties
 (original)
+++ 
uima/uimaj/trunk/uimaj-core/src/main/resources/org/apache/uima/UIMAException_Messages.properties
 Wed Apr 16 15:24:41 2014
@@ -53,6 +53,8 @@ illegal_cas_copy_to_same_cas = It is not
 
 unsupported_cas_copy_view_base_cas = Unsupported invocation of CasCopier 
copyCasView, specifying a source or destination as a base CAS.
 
+illegal_adding_of_new_meta_info = Illegal adding of additional MetaData after 
CASes have been defined.  Likely cause is the reuse of a Resource Manager 
object for a different pipeline, after it has already been initialized.
+
 #--------------------------
 #UIMA_IllegalStateException
 #--------------------------          


Reply via email to