Java Code Examples for org.apache.uima.cas.CAS#setCurrentComponentInfo()

The following examples show how to use org.apache.uima.cas.CAS#setCurrentComponentInfo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CasPool.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Checks in a CAS to the pool. This automatically calls the {@link CAS#reset()} method, to ensure
 * that when the CAS is later retrieved from the pool it will be ready to use. Also notifies other
 * Threads that may be waiting for an instance to become available.
 * 
 * Synchronized on the CAS to avoid the unnatural case where 
 * multiple threads attempt to return the same CAS to the pool
 * at the same time. 
 * 
 * @param aCas
 *          the Cas to release
 */
public void releaseCas(CAS aCas) {
  // note the pool stores references to the InitialView of each CAS
  aCas.setCurrentComponentInfo(null);  // https://issues.apache.org/jira/browse/UIMA-3655
  CAS cas = aCas.getView(CAS.NAME_DEFAULT_SOFA);

  // make sure this CAS actually belongs to this pool and is checked out
  // synchronize to avoid the same CAS being released on 2 threads
  synchronized (cas) {
    if (!mAllInstances.contains(cas) || mFreeInstances.contains(cas)) {
      UIMAFramework.getLogger(CLASS_NAME).logrb(Level.WARNING, CLASS_NAME.getName(), "releaseCas",
              LOG_RESOURCE_BUNDLE, "UIMA_return_cas_to_pool__WARNING");
    } else {
      // restore the ClassLoader and unlock the CAS, since release() can be called 
      // from within a CAS Multiplier.
      ((CASImpl)cas).restoreClassLoaderUnlockCas(); 
      
      // reset CAS
      cas.reset();
      
      // Add the CAS to the end of the free instances List
      mFreeInstances.add(cas);
      permits.release();  // should follow adding cas back to mFreeInstances
    }
  }

  // Notify any threads waiting on this object
  // not needed by UIMA Core - other users may need.
  synchronized (this) {
    notifyAll();
  }
}
 
Example 2
Source File: FlowContainer.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public FlowContainer newCasProduced(final CAS newCAS, String producedBy)
        throws AnalysisEngineProcessException {
  mTimer.startIt();
  CAS view = null;
  try {
    view = Util.getStartingView(   
        newCAS, 
        mSofaAware, 
        mFlowControllerContainer.getUimaContextAdmin().getComponentInfo());
    // now get the right interface(e.g. CAS or JCAS)
    // must be done before call to switchClassLoader
    Class<? extends AbstractCas> requiredInterface = mFlowControllerContainer.getRequiredCasInterface();
    AbstractCas casToPass = getCasManager().getCasInterface(view, requiredInterface);

    ((CASImpl)view).switchClassLoaderLockCasCL(getFlowClassLoader());
    Flow flow = mFlow.newCasProduced(casToPass, producedBy);
    if (flow instanceof CasFlow_ImplBase) {
      ((CasFlow_ImplBase)flow).setCas(view);
    }
    if (flow instanceof JCasFlow_ImplBase) {
      ((JCasFlow_ImplBase)flow).setJCas(view.getJCas());
    }
    return new FlowContainer(flow, mFlowControllerContainer, newCAS);
  } catch (CASException e) {
    throw new AnalysisEngineProcessException(e);
  } finally {
    newCAS.setCurrentComponentInfo(null);
    if (null != view) {
      ((CASImpl)view).restoreClassLoaderUnlockCas();
    }
    mTimer.stopIt();
    getMBean().reportAnalysisTime(mTimer.getDuration());
    getMBean().incrementCASesProcessed();
  }
}
 
Example 3
Source File: FlowControllerContainer.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Invokes the FlowController's computeFlow method, returning a Flow object that routes the given
 * CAS through this aggregate. This method makes sure to provide the FlowController with its
 * required CAS interface (e.g. JCas).
 * 
 * @param aCAS
 *          the CAS to pass to the FlowController
 * @return a Flow object that routes this CAS
 * 
 * @throws AnalysisEngineProcessException
 *           if the FlowController failed
 */
public FlowContainer computeFlow(final CAS aCAS) throws AnalysisEngineProcessException {
  mTimer.startIt();
  CAS view = null;
  UimaContext prevContext = setContextHolder();  // for use by POJOs
  try {
    // throws if _InitialView is mapped to non-existent sofa https://issues.apache.org/jira/browse/UIMA-5097
    view = Util.getStartingView(aCAS, mSofaAware, getUimaContextAdmin().getComponentInfo());     
    // now get the right interface(e.g. CAS or JCAS)
    Class<? extends AbstractCas> requiredInterface = mFlowController.getRequiredCasInterface();
    AbstractCas casToPass = getCasManager().getCasInterface(view, requiredInterface);    
    ((CASImpl)view).switchClassLoaderLockCasCL(this.getResourceManager().getExtensionClassLoader());
    Flow flow = mFlowController.computeFlow(casToPass);
    if (flow instanceof CasFlow_ImplBase) {
      ((CasFlow_ImplBase)flow).setCas(view);
    }
    if (flow instanceof JCasFlow_ImplBase) {
      ((JCasFlow_ImplBase)flow).setJCas(view.getJCas());
    }
    return new FlowContainer(flow, this, aCAS);
  } catch (CASException e) {
    throw new AnalysisEngineProcessException(e);
  } finally {
    aCAS.setCurrentComponentInfo(null); // https://issues.apache.org/jira/browse/UIMA-5097
    if (view != null) {
      ((CASImpl)view).restoreClassLoaderUnlockCas();      
    }
    mTimer.stopIt();
    getMBean().reportAnalysisTime(mTimer.getDuration());
    getMBean().incrementCASesProcessed();
    UimaContextHolder.setContext(prevContext);
  }
}
 
Example 4
Source File: CasPoolTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testPool() throws Exception {
  try {
         
    casManager.defineCasPool("uniqueString", 2, null);
    
    CAS c1 = casManager.getCas("uniqueString");
    CAS c2 = casManager.getCas("uniqueString");
    c1.getJCas();
    
    CAS c1v2 = c1.createView("view2");
    CAS c2v2 = c2.createView("view3");
    c2v2.getJCas();
    
    TypeSystem ts = c1.getTypeSystem();
    
    Assert.assertTrue(ts == c2.getTypeSystem());
    Assert.assertTrue(ts == c1v2.getTypeSystem());
    Assert.assertTrue(ts == c2v2.getTypeSystem());
    
    casManager.releaseCas(c1v2);
    casManager.releaseCas(c2);
    
    c1 = casManager.getCas("uniqueString");
    c1.createView("mappedName");
    RootUimaContext_impl rootContext = new RootUimaContext_impl();
    ChildUimaContext_impl context = new ChildUimaContext_impl(rootContext, "abc", Collections.singletonMap(CAS.NAME_DEFAULT_SOFA, "mappedName"));
    c1.setCurrentComponentInfo(context.getComponentInfo());
    casManager.releaseCas(c1);

  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}