Search in sources :

Example 46 with IOException

use of java.io.IOException in project camel by apache.

the class DefaultPackageScanClassResolver method doLoadJarClassEntries.

/**
     * Loads all the class entries from the JAR.
     *
     * @param stream  the inputstream of the jar file to be examined for classes
     * @param urlPath the url of the jar file to be examined for classes
     * @return all the .class entries from the JAR
     */
protected List<String> doLoadJarClassEntries(InputStream stream, String urlPath) {
    List<String> entries = new ArrayList<String>();
    JarInputStream jarStream = null;
    try {
        jarStream = new JarInputStream(stream);
        JarEntry entry;
        while ((entry = jarStream.getNextJarEntry()) != null) {
            String name = entry.getName();
            if (name != null) {
                name = name.trim();
                if (!entry.isDirectory() && name.endsWith(".class")) {
                    entries.add(name);
                }
            }
        }
    } catch (IOException ioe) {
        log.warn("Cannot search jar file '" + urlPath + " due to an IOException: " + ioe.getMessage(), ioe);
    } finally {
        IOHelper.close(jarStream, urlPath, log);
    }
    return entries;
}
Also used : JarInputStream(java.util.jar.JarInputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry)

Example 47 with IOException

use of java.io.IOException in project camel by apache.

the class FileIdempotentRepository method appendToStore.

/**
     * Appends the given message id to the file store
     *
     * @param messageId  the message id
     */
protected void appendToStore(final String messageId) {
    LOG.debug("Appending {} to idempotent filestore: {}", messageId, fileStore);
    FileOutputStream fos = null;
    try {
        // create store parent directory if missing
        File storeParentDirectory = fileStore.getParentFile();
        if (storeParentDirectory != null && !storeParentDirectory.exists()) {
            LOG.info("Parent directory of file store {} doesn't exist. Creating.", fileStore);
            if (fileStore.getParentFile().mkdirs()) {
                LOG.info("Parent directory of file store {} successfully created.", fileStore);
            } else {
                LOG.warn("Parent directory of file store {} cannot be created.", fileStore);
            }
        }
        // create store if missing
        if (!fileStore.exists()) {
            FileUtil.createNewFile(fileStore);
        }
        // append to store
        fos = new FileOutputStream(fileStore, true);
        fos.write(messageId.getBytes());
        fos.write(STORE_DELIMITER.getBytes());
    } catch (IOException e) {
        throw ObjectHelper.wrapRuntimeCamelException(e);
    } finally {
        IOHelper.close(fos, "Appending to file idempotent repository", LOG);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 48 with IOException

use of java.io.IOException in project camel by apache.

the class FileIdempotentRepository method trunkStore.

/**
     * Trunks the file store when the max store size is hit by rewriting the 1st level cache
     * to the file store.
     */
protected void trunkStore() {
    LOG.info("Trunking idempotent filestore: {}", fileStore);
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(fileStore);
        for (String key : cache.keySet()) {
            fos.write(key.getBytes());
            fos.write(STORE_DELIMITER.getBytes());
        }
    } catch (IOException e) {
        throw ObjectHelper.wrapRuntimeCamelException(e);
    } finally {
        IOHelper.close(fos, "Trunking file idempotent repository", LOG);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException)

Example 49 with IOException

use of java.io.IOException in project camel by apache.

the class WireTapProcessor method configureExchange.

protected Exchange configureExchange(Exchange exchange, ExchangePattern pattern) throws IOException {
    Exchange answer;
    if (copy) {
        // use a copy of the original exchange
        answer = configureCopyExchange(exchange);
    } else {
        // use a new exchange
        answer = configureNewExchange(exchange);
    }
    // prepare the exchange
    if (newExchangeExpression != null) {
        Object body = newExchangeExpression.evaluate(answer, Object.class);
        if (body != null) {
            answer.getIn().setBody(body);
        }
    }
    if (newExchangeProcessors != null) {
        for (Processor processor : newExchangeProcessors) {
            try {
                processor.process(answer);
            } catch (Exception e) {
                throw ObjectHelper.wrapRuntimeCamelException(e);
            }
        }
    }
    // if the body is a stream cache we must use a copy of the stream in the wire tapped exchange
    Message msg = answer.hasOut() ? answer.getOut() : answer.getIn();
    if (msg.getBody() instanceof StreamCache) {
        // in parallel processing case, the stream must be copied, therefore get the stream
        StreamCache cache = (StreamCache) msg.getBody();
        StreamCache copied = cache.copy(answer);
        if (copied != null) {
            msg.setBody(copied);
        }
    }
    // invoke on prepare on the exchange if specified
    if (onPrepare != null) {
        try {
            onPrepare.process(answer);
        } catch (Exception e) {
            throw ObjectHelper.wrapRuntimeCamelException(e);
        }
    }
    return answer;
}
Also used : Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Processor(org.apache.camel.Processor) AsyncProcessor(org.apache.camel.AsyncProcessor) Message(org.apache.camel.Message) StreamCache(org.apache.camel.StreamCache) IOException(java.io.IOException)

Example 50 with IOException

use of java.io.IOException in project camel by apache.

the class ExceptionBuilderWithHandledExceptionTest method testUnhandledException.

public void testUnhandledException() throws Exception {
    MockEndpoint result = getMockEndpoint(RESULT_QUEUE);
    result.expectedMessageCount(0);
    MockEndpoint mock = getMockEndpoint(ERROR_QUEUE);
    mock.expectedMessageCount(1);
    mock.expectedHeaderReceived(MESSAGE_INFO, "Handled exchange with IOException");
    try {
        template.sendBodyAndHeader("direct:a", "Hello IOE", "foo", "something that does not match");
        fail("Should have thrown a IOException");
    } catch (RuntimeCamelException e) {
        assertTrue(e.getCause() instanceof IOException);
    // expected, failure is not handled because predicate doesn't match
    }
    MockEndpoint.assertIsSatisfied(result, mock);
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)41104 File (java.io.File)7663 InputStream (java.io.InputStream)4105 Test (org.junit.Test)3557 ArrayList (java.util.ArrayList)3023 FileInputStream (java.io.FileInputStream)2674 FileOutputStream (java.io.FileOutputStream)2358 FileNotFoundException (java.io.FileNotFoundException)2146 BufferedReader (java.io.BufferedReader)2028 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1761 InputStreamReader (java.io.InputStreamReader)1677 URL (java.net.URL)1608 HashMap (java.util.HashMap)1552 ByteArrayInputStream (java.io.ByteArrayInputStream)1534 OutputStream (java.io.OutputStream)1349 Path (org.apache.hadoop.fs.Path)1316 Map (java.util.Map)1212 List (java.util.List)1042 Properties (java.util.Properties)994 ServletException (javax.servlet.ServletException)916