Package org.apache.uima.resource

Examples of org.apache.uima.resource.Resource


                new Object[] { className, aResourceClass.getName(),
                    aSpecifier.getSourceUrlString() });
      }
     
      // instantiate this Resource Class
      Resource resource;
      try {
        resource = (Resource) resourceClass.newInstance();
      } catch (InstantiationException e) {
        throw new ResourceInitializationException(
                ResourceInitializationException.COULD_NOT_INSTANTIATE, new Object[] { className,
                    aSpecifier.getSourceUrlString() }, e);
      } catch (IllegalAccessException e) {
        throw new ResourceInitializationException(
                ResourceInitializationException.COULD_NOT_INSTANTIATE, new Object[] { className,
                    aSpecifier.getSourceUrlString() }, e);
      }
      // attempt to initialize it
      if (resource.initialize(aSpecifier, aAdditionalParams)) {
        // success!
        return resource;
      } else
      // failure, for some unknown reason :(
      {
View Full Code Here


        ProcessingResourceMetaData md = new ProcessingResourceMetaData_impl();
        md.setTypePriorities((TypePriorities) current);
        mdList.add(md);
      } else if (current instanceof ResourceSpecifier) {
        //try to instantiate the resource
        Resource resource = null;
        Map resourceMgrInMap = new HashMap();
        resourceMgrInMap.put(Resource.PARAM_RESOURCE_MANAGER, aResourceManager);
        try {
          resource = UIMAFramework.produceResource((ResourceSpecifier) current,
              (null == aResourceManager) ? Collections.EMPTY_MAP : resourceMgrInMap);
        } catch (Exception e) {
          //failed.  If aOutputFailedRemotes is non-null, add an entry to it to it, else throw the exception.
          if (aOutputFailedRemotes != null) {
            aOutputFailedRemotes.put(aContextName,e);
          }
          else {
            if (e instanceof ResourceInitializationException)
              throw (ResourceInitializationException)e;
            else if (e instanceof RuntimeException)
              throw (RuntimeException)e;
            else
              throw new RuntimeException(e);
          }
        }
        if (resource != null) {
          ResourceMetaData metadata = resource.getMetaData();
          if (metadata instanceof ProcessingResourceMetaData) {
            mdList.add(metadata);
          }
          resource.destroy();
        }
      } else {
        throw new ResourceInitializationException(
                ResourceInitializationException.UNSUPPORTED_OBJECT_TYPE_IN_CREATE_CAS,
                new Object[] { current.getClass().getName() });
View Full Code Here

   * @throws ResourceInitializationException
   *           if a failure occurred during production of the resource.
   */
  public static Resource produceResource(Class<? extends Resource> aResourceClass, ResourceSpecifier aSpecifier,
          Map<String, Object> aAdditionalParams) throws ResourceInitializationException {
    Resource resource = getResourceFactory().produceResource(aResourceClass, aSpecifier,
            aAdditionalParams);
    if (resource == null) {
      throw new ResourceInitializationException(ResourceInitializationException.DO_NOT_KNOW_HOW,
              new Object[] { aResourceClass.getName(), aSpecifier.getSourceUrlString() });
    }
View Full Code Here

    // destroy component AnalysisEngines that have been successfully initialized
    //   unsuccessful initializations are not put into the Map
    Iterator<Map.Entry<String, AnalysisEngine>> i = mComponentAnalysisEngineMap.entrySet().iterator();
    while (i.hasNext()) {
      Map.Entry<String, AnalysisEngine> entry = i.next();
      Resource delegate = entry.getValue();
      delegate.destroy();
    }
   
    if (mFlowControllerContainer != null &&
        // the container might be non-null, but the initialization could have failed
        mFlowControllerContainer.isInitialized()) {
View Full Code Here

          }
        }
       
        // try to instantiate the resource
       
        Resource resource = null;
        Map<String, Object> prParams = new HashMap<String, Object>();
        if (aResourceManager != null) {
          prParams.put(Resource.PARAM_RESOURCE_MANAGER, aResourceManager);
        }
        prParams.put(AnalysisEngineImplBase.PARAM_VERIFICATION_MODE, Boolean.TRUE);
        try {
          resource = UIMAFramework.produceResource((ResourceSpecifier) current, prParams);
//              (null == aResourceManager) ? Collections.<String, Object>emptyMap() : resourceMgrInMap);
        } catch (Exception e) {
          // record failure, so we don't ask for this again, for a while
          synchronized (metaDataCache) {
            if (cacheDebug) {
              System.err.format("GetMetaDataCache: saving entry in cache%n");
            }
            metaDataCache.put(metaDataCacheKey, new MetaDataCacheEntry(null));
          }
          // failed. If aOutputFailedRemotes is non-null, add an entry to it to it, else throw the
          // exception.
          if (aOutputFailedRemotes != null) {
            aOutputFailedRemotes.put(aContextName, e);
          } else {
            if (e instanceof ResourceInitializationException)
              throw (ResourceInitializationException) e;
            else if (e instanceof RuntimeException)
              throw (RuntimeException) e;
            else
              throw new RuntimeException(e);
          }
        }
        ResourceMetaData metadata = (resource == null) ? null : resource.getMetaData();

        synchronized (metaDataCache) {
          if (cacheDebug) {
            System.err.format("GetMetaDataCache: saving entry in cache%n");
          }
          metaDataCache.put(metaDataCacheKey, new MetaDataCacheEntry(metadata));
        }

        if (resource != null) {
          if (metadata instanceof ProcessingResourceMetaData) {
            mdList.add((ProcessingResourceMetaData) metadata);
          }
          resource.destroy();
        }
      } else {
        throw new ResourceInitializationException(
            ResourceInitializationException.UNSUPPORTED_OBJECT_TYPE_IN_CREATE_CAS,
            new Object[] { current.getClass().getName() });
View Full Code Here

  public void testGetResource() throws Exception {
    try {
      Assert.assertEquals(3, pool1.getFreeInstances().size());

      // get two resources
      Resource foo = pool1.getResource();
      Assert.assertNotNull(foo);
      Assert.assertEquals(2, pool1.getFreeInstances().size());

      Resource bar = pool1.getResource();
      Assert.assertNotNull(bar);
      Assert.assertTrue(!foo.equals(bar));
      Assert.assertEquals(1, pool1.getFreeInstances().size());

      // get two more resources (should exhaust pool)
      Resource a = pool1.getResource();
      Assert.assertNotNull(a);
      Assert.assertEquals(0, pool1.getFreeInstances().size());

      Resource b = pool1.getResource();
      Assert.assertNull(b);
      Assert.assertEquals(0, pool1.getFreeInstances().size());
    } catch (Exception e) {
      JUnitExtension.handleException(e);
    }
View Full Code Here

    try {
      // ask for resources with timeout of 2 seconds. should respond quickly
      // until resources are exhausted, then it will pause 2 seconds before
      // returning null
      long startTime = System.currentTimeMillis();
      Resource foo = pool1.getResource(2000);
      Assert.assertNotNull(foo);
      Assert.assertTrue(System.currentTimeMillis() - startTime < 1000);

      startTime = System.currentTimeMillis();
      Resource bar = pool1.getResource(2000);
      Assert.assertNotNull(bar);
      Assert.assertTrue(!foo.equals(bar));
      Assert.assertTrue(System.currentTimeMillis() - startTime < 1000);

      startTime = System.currentTimeMillis();
      Resource a = pool1.getResource(2000);
      Assert.assertNotNull(a);
      Assert.assertTrue(System.currentTimeMillis() - startTime < 1000);

      startTime = System.currentTimeMillis();
      Resource b = pool1.getResource(2000);
      Assert.assertNull(b);
      Assert.assertTrue(System.currentTimeMillis() - startTime >= 2000);

      // Start a thread that will release "foo" in 1 second. Demonstrate that
      // getResource() will not acquire a resource but getResource(2000) will.
View Full Code Here

  public void testReleaseResource() throws Exception {
    try {
      // acquire all the resources
      Assert.assertEquals(3, pool1.getFreeInstances().size());
      Resource foo = pool1.getResource();
      Resource bar = pool1.getResource();
      Resource blah = pool1.getResource();
      Assert.assertEquals(0, pool1.getFreeInstances().size());

      // release one
      pool1.releaseResource(foo);
      Assert.assertEquals(1, pool1.getFreeInstances().size());

      // try to release "foo" again - should not change the free instances count
      // this will log a warning - first we log that this is expected
      UIMAFramework.getLogger().log(Level.WARNING, "Unit test is expecting to log ResourcePool warning.");
      pool1.releaseResource(foo);
      Assert.assertEquals(1, pool1.getFreeInstances().size());

      // show that we can then check out a new one
      Resource test = pool1.getResource();
      Assert.assertNotNull(test);
      Assert.assertEquals(0, pool1.getFreeInstances().size());

      // release the others
      pool1.releaseResource(test);
View Full Code Here

  }

  public void testDestroy() throws Exception {
    try {
      // do some stuff
      Resource foo = pool1.getResource();
      Resource bar = pool1.getResource();
      Resource a = pool1.getResource();
      pool1.releaseResource(foo);
      Resource b = pool1.getResource();
      pool1.releaseResource(b);

      // now some stuff should be recorded in the pool
      Assert.assertTrue(!pool1.getFreeInstances().isEmpty());
View Full Code Here

    parameters[1] = UIMAFramework.getResourceSpecifierFactory().createParameter();
    parameters[1].setName("param2");
    parameters[1].setValue("val2");
    specifier.setParameters(parameters);   
   
    Resource res = crFactory.produceResource(Resource.class, specifier, Collections.EMPTY_MAP);  
    assertTrue(res instanceof SomeCustomResource);
    assertEquals("val1", ((SomeCustomResource)res).paramMap.get("param1"));
    assertEquals("val2", ((SomeCustomResource)res).paramMap.get("param2"));
   
    //also UIMAFramework.produceResource should do the same thing
View Full Code Here

TOP

Related Classes of org.apache.uima.resource.Resource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.