Example usage for java.io IOException initCause

List of usage examples for java.io IOException initCause

Introduction

In this page you can find the example usage for java.io IOException initCause.

Prototype

public synchronized Throwable initCause(Throwable cause) 

Source Link

Document

Initializes the cause of this throwable to the specified value.

Usage

From source file:de.smartics.maven.plugin.jboss.modules.util.classpath.AbstractProjectClassLoader.java

/**
 * Opens a reader to the resource file within the archive.
 *
 * @param resourceName the name of the resource to load.
 * @param fileName the name of the resource file to load.
 * @param dirName the name of the directory the archive file is located.
 * @return the reader to the source file for the given class in the archive.
 * @throws IOException if the class cannot be found.
 *//*from www . j  a v a 2 s  . c  o  m*/
protected URL loadResourceFromLibrary(final String resourceName, final String fileName, final File dirName)
        throws IOException {
    ensurePackageProvided(resourceName);
    try {
        return new URL("jar:file:" + dirName.getName() + "!/" + fileName);
    } catch (final IOException e) {
        final String message = "Cannot load class '" + resourceName + "' from file '" + dirName + "'.";
        LOG.fine(message);
        final IOException ioe = new IOException(message);
        ioe.initCause(e);
        throw ioe;
    }
}

From source file:org.apache.solr.util.xslt.TransformerProvider.java

/** Return a new Transformer, possibly created from our cached Templates object  
 * @throws TransformerConfigurationException 
 *//*from  w  ww  . j a  v  a2s .  c  om*/
public synchronized Transformer getTransformer(SolrConfig solrConfig, String filename, int cacheLifetimeSeconds)
        throws IOException {
    // For now, the Templates are blindly reloaded once cacheExpires is over.
    // It'd be better to check the file modification time to reload only if needed.
    if (lastTemplates != null && filename.equals(lastFilename) && System.currentTimeMillis() < cacheExpires) {
        if (log.isDebugEnabled()) {
            log.debug("Using cached Templates:" + filename);
        }
    } else {
        lastTemplates = getTemplates(solrConfig.getResourceLoader(), filename, cacheLifetimeSeconds);
    }

    Transformer result = null;

    try {
        result = lastTemplates.newTransformer();
    } catch (TransformerConfigurationException tce) {
        log.error(getClass().getName(), "getTransformer", tce);
        final IOException ioe = new IOException("newTransformer fails ( " + lastFilename + ")");
        ioe.initCause(tce);
        throw ioe;
    }

    return result;
}

From source file:org.restlet.ext.jaxrs.internal.provider.FileUploadProvider.java

/**
 * @see MessageBodyReader#readFrom(Class, Type, Annotation[], MediaType,
 *      MultivaluedMap, InputStream)/*from w  ww.j av a2s.  c  o  m*/
 */
public List<FileItem> readFrom(Class<List<FileItem>> type, Type genericType, Annotation[] annotations,
        MediaType mediaType, final MultivaluedMap<String, String> respHeaders, final InputStream entityStream)
        throws IOException {
    final FileUpload rfu = new FileUpload();
    final RequestContext requCtx = new RequestContext(entityStream, respHeaders);
    try {
        return rfu.parseRequest(requCtx);
    } catch (FileUploadException e) {
        if (e.getCause() instanceof IOException) {
            throw (IOException) e.getCause();
        }
        final IOException ioExc = new IOException("Could not read the multipart/form-data");
        ioExc.initCause(e);
        throw ioExc;
    }
}

From source file:org.ejbca.util.keystore.KeyTools.java

/**
 * /*from   w  w w.  j  av  a2  s .co  m*/
 * @param is for the SUN PKCS#11 provider
 * @param prop for the IAIK PKCS#11 provider
 * @return Java security Provider for a PCKS#11 token
 * @throws IOException if neither the IAIK or the SUN provider can be created
 */
private static Provider getP11Provider(final InputStream is, final Properties prop) throws IOException {

    // We will construct the PKCS11 provider (sun.security..., or iaik...) using reflection, because 
    // the sun class does not exist on all platforms in jdk5, and we want to be able to compile everything.
    // The below code replaces the single line (for the SUN provider):
    //   return new SunPKCS11(new ByteArrayInputStream(baos.toByteArray()));

    // We will first try to construct the more competent IAIK provider, if it exists in the classpath
    // if that does not exist, we will revert back to use the SUN provider
    Provider ret = null;
    if (prop != null) {
        try {
            final Class implClass = Class.forName(IAIKPKCS11CLASS);
            log.info("Using IAIK PKCS11 provider: " + IAIKPKCS11CLASS);
            // iaik PKCS11 has Properties as constructor argument
            ret = (Provider) implClass.getConstructor(Properties.class).newInstance(new Object[] { prop });
            // It's not enough just to add the p11 provider. Depending on algorithms we may have to install the IAIK JCE provider as well in order to support algorithm delegation
            final Class jceImplClass = Class.forName(KeyTools.IAIKJCEPROVIDERCLASS);
            Provider iaikProvider = (Provider) jceImplClass.getConstructor().newInstance();
            if (Security.getProvider(iaikProvider.getName()) == null) {
                log.info("Adding IAIK JCE provider for Delegation: " + KeyTools.IAIKJCEPROVIDERCLASS);
                Security.addProvider(iaikProvider);
            }
        } catch (Exception e) {
            // do nothing here. Sun provider is tested below.
        }
    }
    if (ret == null) {
        try {
            // Sun PKCS11 has InputStream as constructor argument
            final Class implClass = Class.forName(SUNPKCS11CLASS);
            log.info("Using SUN PKCS11 provider: " + SUNPKCS11CLASS);
            ret = (Provider) implClass.getConstructor(InputStream.class).newInstance(new Object[] { is });
        } catch (Exception e) {
            log.error("Error constructing pkcs11 provider: " + e.getMessage());
            final IOException ioe = new IOException("Error constructing pkcs11 provider: " + e.getMessage());
            ioe.initCause(e);
            throw ioe;
        }
    }
    return ret;
}

From source file:org.apache.solr.util.xslt.TransformerProvider.java

/** Return a Templates object for the given filename */
private Templates getTemplates(ResourceLoader loader, String filename, int cacheLifetimeSeconds)
        throws IOException {

    Templates result = null;//from w w  w. ja va 2s  . c o  m
    lastFilename = null;
    try {
        if (log.isDebugEnabled()) {
            log.debug("compiling XSLT templates:" + filename);
        }
        final String fn = "xslt/" + filename;
        final TransformerFactory tFactory = TransformerFactory.newInstance();
        tFactory.setURIResolver(new SystemIdResolver(loader).asURIResolver());
        tFactory.setErrorListener(xmllog);
        final StreamSource src = new StreamSource(loader.openResource(fn),
                SystemIdResolver.createSystemIdFromResourceName(fn));
        try {
            result = tFactory.newTemplates(src);
        } finally {
            // some XML parsers are broken and don't close the byte stream (but they should according to spec)
            IOUtils.closeQuietly(src.getInputStream());
        }
    } catch (Exception e) {
        log.error(getClass().getName(), "newTemplates", e);
        final IOException ioe = new IOException("Unable to initialize Templates '" + filename + "'");
        ioe.initCause(e);
        throw ioe;
    }

    lastFilename = filename;
    lastTemplates = result;
    cacheExpires = System.currentTimeMillis() + (cacheLifetimeSeconds * 1000);

    return result;
}

From source file:org.apache.jackrabbit.core.data.db.DbInputStream.java

/**
 * Open the stream if required.//  ww w .  j av a2s  . c om
 *
 * @throws IOException
 */
protected void openStream() throws IOException {
    if (endOfStream) {
        throw new EOFException();
    }
    if (in == null) {
        try {
            in = store.openStream(this, identifier);
        } catch (DataStoreException e) {
            IOException e2 = new IOException(e.getMessage());
            e2.initCause(e);
            throw e2;
        }
    }
}

From source file:org.jets3t.service.multi.DownloadPackage.java

/**
 * Creates an output stream to receive the object's data. The output stream is either
 * the output stream provided to this package in its constructor, or an
 * automatically-created FileOutputStream if a File object was provided as the target
 * output object. The output stream will also be wrapped in a GZipInflatingOutputStream if
 * isUnzipping is true and/or a decrypting output stream if this package has an associated
 * non-null EncryptionUtil.//w ww.  j  a  v  a  2  s.  c  o m
 *
 * @return
 * an output stream that writes data to the output target managed by this class.
 *
 * @throws IOException
 */
public OutputStream getOutputStream() throws IOException {
    OutputStream outputStream = null;
    if (outputFile != null) {
        // Create parent directories for file, if necessary.
        if (outputFile.getParentFile() != null) {
            outputFile.getParentFile().mkdirs();
        }

        outputStream = new FileOutputStream(outputFile, appendToFile);
    } else {
        outputStream = this.outputStream;
    }

    if (isUnzipping) {
        log.debug("Inflating gzipped data for object: " + object.getKey());
        outputStream = new GZipInflatingOutputStream(outputStream);
    }
    if (encryptionUtil != null) {
        log.debug("Decrypting encrypted data for object: " + object.getKey());
        try {
            outputStream = encryptionUtil.decrypt(outputStream);
        } catch (InvalidKeyException e) {
            final IOException exception = new IOException(e.getMessage());
            exception.initCause(e);
            throw exception;
        } catch (InvalidAlgorithmParameterException e) {
            final IOException exception = new IOException(e.getMessage());
            exception.initCause(e);
            throw exception;
        } catch (NoSuchAlgorithmException e) {
            final IOException exception = new IOException(e.getMessage());
            exception.initCause(e);
            throw exception;
        } catch (NoSuchPaddingException e) {
            final IOException exception = new IOException(e.getMessage());
            exception.initCause(e);
            throw exception;
        }
    }
    return outputStream;
}

From source file:ch.cyberduck.core.dropbox.client.DropboxClient.java

/**
 * Put a file in the user's Dropbox.//from w w w. ja  va 2  s . c o  m
 */
public void put(String to, ContentBody content) throws IOException {
    HttpClient client = getClient();

    HttpPost req = (HttpPost) buildRequest(HttpPost.METHOD_NAME, "/files/" + ROOT + to, true);

    // this has to be done this way because of how oauth signs params
    // first we add a "fake" param of file=path of *uploaded* file, THEN we sign that.
    List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
    nvps.add(new BasicNameValuePair("file", content.getFilename()));
    req.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
    try {
        auth.sign(req);
    } catch (OAuthException e) {
        IOException failure = new IOException(e.getMessage());
        failure.initCause(e);
        throw failure;
    }
    // now we can add the real file multipart and we're good
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    entity.addPart("file", content);

    // this resets it to the new entity with the real file
    req.setEntity(entity);

    this.finish(client.execute(req));
}

From source file:com.cloudera.util.StatusHttpServer.java

/**
 * Start the server. Does not wait for the server to start.
 *//*from w  w  w .j av  a  2  s. c  o  m*/
public void start() throws IOException {
    try {
        while (true) {
            try {
                webServer.start();
                break;
            } catch (BindException ex) {
                // if the multi exception contains ONLY a bind exception,
                // then try the next port number.
                if (!findPort) {
                    throw ex;
                }
                // pick another port
                webServer.stop();
                channelConnector.setPort(channelConnector.getPort() + 1);
            }
        }
    } catch (Exception e) {
        IOException ie = new IOException("Problem starting http server");
        ie.initCause(e);
        throw ie;
    }
}

From source file:org.ops4j.pax.url.maven.internal.Connection.java

/**
 * Creates an IOException with a message and a cause.
 *
 * @param message exception message/*from  w  w w .  ja  v  a  2 s. c  o m*/
 * @param cause   exception cause
 *
 * @return the created IO Exception
 */
private IOException initIOException(final String message, final Exception cause) {
    IOException exception = new IOException(message);
    exception.initCause(cause);
    return exception;
}