Example usage for java.util.concurrent Callable call

List of usage examples for java.util.concurrent Callable call

Introduction

In this page you can find the example usage for java.util.concurrent Callable call.

Prototype

V call() throws Exception;

Source Link

Document

Computes a result, or throws an exception if unable to do so.

Usage

From source file:io.fabric8.forge.camel.commands.project.CamelGetComponentsCommand.java

@Override
public Result execute(UIExecutionContext context) throws Exception {
    Project project = getSelectedProject(context);
    boolean excludeProjectComponents = isValueTrue(excludeProject);
    Callable<Iterable<ComponentDto>> callable = CamelCommandsHelper.createAllComponentDtoValues(project,
            getCamelCatalog(), filter, excludeProjectComponents);
    Iterable<ComponentDto> results = callable.call();
    String result = formatResult(results);
    return Results.success(result);
}

From source file:com.streamsets.pipeline.stage.destination.mapreduce.MapReduceExecutor.java

private Job createAndSubmitJob(final Configuration configuration) throws IOException, InterruptedException {
    return mapReduceConfig.getUGI().doAs((PrivilegedExceptionAction<Job>) () -> {
        // Create new mapreduce job object
        Callable<Job> jobCreator = ReflectionUtils.newInstance(jobConfig.getJobCreator(), configuration);
        Job job = jobCreator.call();

        // In trace mode, dump all the configuration that we're using for the job
        if (LOG.isTraceEnabled()) {
            LOG.trace("Using the following configuration object for mapreduce job.");
            for (Map.Entry<String, String> entry : configuration) {
                LOG.trace("  Config: {}={}", entry.getKey(), entry.getValue());
            }/*from   w  ww.  ja  v  a  2s  .  co  m*/
        }

        // Submit it for processing. Blocking mode is only for testing.
        job.submit();
        if (waitForCompletition) {
            job.waitForCompletion(true);
        }

        return job;
    });
}

From source file:org.apache.ambari.view.hive.resources.jobs.ResultsPaginationController.java

private Cursor getResultsSet(String key, Callable<Cursor> makeResultsSet) {
    if (!getResultsCache().containsKey(key)) {
        Cursor resultSet = null;//from w  w w . j  a  v  a2s . c  o  m
        try {
            resultSet = makeResultsSet.call();
        } catch (Exception ex) {
            throw new ServiceFormattedException(ex.getMessage(), ex);
        }
        getResultsCache().put(key, resultSet);
    }

    return getResultsCache().get(key);
}

From source file:com.haulmont.cuba.core.app.scheduling.RunnerBean.java

protected Object executeTask(ScheduledTask task) {
    switch (task.getDefinedBy()) {
    case BEAN: {/*  w  w w  .  j ava2s  .co  m*/
        log.trace("{}: invoking bean", task);
        Object bean = AppBeans.get(task.getBeanName());
        try {
            List<MethodParameterInfo> methodParams = task.getMethodParameters();
            Class[] paramTypes = new Class[methodParams.size()];
            Object[] paramValues = new Object[methodParams.size()];

            for (int i = 0; i < methodParams.size(); i++) {
                paramTypes[i] = methodParams.get(i).getType();
                paramValues[i] = methodParams.get(i).getValue();
            }

            Method method = bean.getClass().getMethod(task.getMethodName(), paramTypes);
            return method.invoke(bean, paramValues);
        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    case CLASS: {
        try {
            Class taskClass = scripting.loadClass(task.getClassName());
            Callable callable = (Callable) taskClass.newInstance();
            return callable.call();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(
                    String.format("An error occurred while instantiating class %s.", task.getClassName()), e);
        } catch (Exception e) {
            throw new RuntimeException(String.format(
                    "An error occurred while running method call() of class %s.", task.getClassName()), e);
        }
    }

    case SCRIPT: {
        return scripting.runGroovyScript(task.getScriptName(), Collections.<String, Object>emptyMap());
    }

    default: {
        throw new IllegalStateException(
                String.format("\"Defined by\" field has illegal value: %s. Task id: [%s].", task.getDefinedBy(),
                        task.getId()));
    }
    }
}

From source file:com.fitbur.testify.junit.system.internal.SpringBootInterceptor.java

public EmbeddedServletContainerFactory getEmbeddedServletContainerFactory(
        @SuperCall Callable<EmbeddedServletContainerFactory> zuper, @This Object object) throws Exception {
    EmbeddedServletContainerFactory containerFactory = zuper.call();
    ConfigurableEmbeddedServletContainer configurableServletContainer = (ConfigurableEmbeddedServletContainer) containerFactory;
    configurableServletContainer.setPort(0);

    SpringBootDescriptor descriptor = getDescriptor(object);
    descriptor.setContainerFactory(containerFactory);
    descriptor.setConfigurableServletContainer(configurableServletContainer);
    TestContext testContext = descriptor.getTestContext();

    testContext.getConfigMethod(ConfigurableEmbeddedServletContainer.class).map(m -> m.getMethod())
            .ifPresent(m -> {/*from w  w  w  .  j  ava 2s . com*/
                AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
                    try {
                        m.setAccessible(true);
                        m.invoke(testContext.getTestInstance(), configurableServletContainer);
                    } catch (Exception e) {
                        checkState(false, "Call to config method '%s' in test class '%s' failed.", m.getName(),
                                testContext.getTestClassName());
                        throw Throwables.propagate(e);
                    }

                    return null;
                });
            });

    return containerFactory;
}

From source file:com.meltmedia.dropwizard.crypto.DecryptCommandTest.java

@Test
public void testDecryption() throws Exception {
    mockInput(namespace,/*  w  ww.  j av  a2 s .com*/
            "secret:\n" + "  salt: \"02n2NA==\"\n" + "  iv: \"WsiLMqSgKqTiURkqbuOnZw==\"\n"
                    + "  value: \"1ww7k45AN+XvZHaEPrlztA==\"\n" + "  cipher: \"aes-256-cbc\"\n"
                    + "  keyDerivation: \"pbkdf2\"\n" + "  keyLength: 256\n" + "  iterations: 2000\n"
                    + "  encrypted: true\n");

    Callable<String> output = mockOutput(namespace);
    mockPointer(namespace, "/secret");

    Commands.Decrypt command = new Commands.Decrypt("decrypt", "desc", service);

    command.run(null, namespace);

    JsonNode result = yamlMapper.readValue(output.call(), JsonNode.class);
    assertThat(result.at("/secret").asText(), equalTo("value"));
}

From source file:com.meltmedia.dropwizard.crypto.DecryptCommandTest.java

@Test
public void testDecryptionWithOtherKeys() throws Exception {
    mockInput(namespace,/*w w  w . j  a  v  a 2 s  .  c  o  m*/
            "before: before value\n" + "secret:\n" + "  salt: \"02n2NA==\"\n"
                    + "  iv: \"WsiLMqSgKqTiURkqbuOnZw==\"\n" + "  value: \"1ww7k45AN+XvZHaEPrlztA==\"\n"
                    + "  cipher: \"aes-256-cbc\"\n" + "  keyDerivation: \"pbkdf2\"\n" + "  keyLength: 256\n"
                    + "  iterations: 2000\n" + "  encrypted: true\n" + "after: after value\n");

    Callable<String> output = mockOutput(namespace);
    mockPointer(namespace, "/secret");

    Commands.Decrypt command = new Commands.Decrypt("decrypt", "desc", service);

    command.run(null, namespace);

    JsonNode result = yamlMapper.readValue(output.call(), JsonNode.class);
    assertThat(result.at("/before").asText(), equalTo("before value"));
    assertThat(result.at("/secret").asText(), equalTo("value"));
    assertThat(result.at("/after").asText(), equalTo("after value"));

}

From source file:org.polymap.rhei.batik.toolkit.BusyIndicator.java

@Override
public <T> T showWhile(final Callable<T> task) throws Exception {
    start();//from  ww  w. j a  v  a  2  s. c  om

    final AtomicReference resultRef = new AtomicReference();
    display().asyncExec(new Runnable() {
        public void run() {
            try {
                resultRef.set(task.call());
            } catch (Exception e) {
                resultRef.set(e);
            } finally {
                end();
            }
        }
    });
    if (resultRef.get() instanceof Exception) {
        throw (Exception) resultRef.get();
    } else {
        return (T) resultRef.get();
    }
}

From source file:com.meltmedia.dropwizard.crypto.EncryptCommandTest.java

@Test
public void testEncryption() throws Exception {
    mockInput(namespace, "secret: value");
    Callable<String> output = mockOutput(namespace);
    mockPointer(namespace, "/secret");

    Commands.Encrypt encryptCommand = new Commands.Encrypt("encrypt", "desc", service);

    encryptCommand.run(null, namespace);

    JsonNode result = yamlMapper.readValue(output.call(), JsonNode.class);
    assertThat(result.at("/secret/value").isMissingNode(), equalTo(false));
}

From source file:org.obiba.mica.search.csvexport.SpecificStudyReportGenerator.java

private String arrayField(final Callable<List<String>> field) {
    return field(() -> {
        List<String> fields = field.call();
        return StringUtils.arrayToDelimitedString(fields.toArray(new String[fields.size()]), ", ");
    });/*from   w w  w  .ja v a  2  s  .c  o  m*/
}