Example usage for java.lang InterruptedException initCause

List of usage examples for java.lang InterruptedException initCause

Introduction

In this page you can find the example usage for java.lang InterruptedException 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:com.twitter.common.zookeeper.ZooKeeperClient.java

/**
 * Returns the current active ZK connection or establishes a new one if none has yet been
 * established or a previous connection was disconnected or had its session time out.  This method
 * will attempt to re-use sessions when possible.  Equivalent to:
 * <pre>get(Amount.of(0L, ...)</pre>.
 *
 * @return a connected ZooKeeper client//from  w ww  . j a  v  a 2s .c om
 * @throws ZooKeeperConnectionException if there was a problem connecting to the ZK cluster
 * @throws InterruptedException if interrupted while waiting for a connection to be established
 */
public synchronized ZooKeeper get() throws ZooKeeperConnectionException, InterruptedException {
    try {
        return get(WAIT_FOREVER);
    } catch (TimeoutException e) {
        InterruptedException interruptedException = new InterruptedException(
                "Got an unexpected TimeoutException for 0 wait");
        interruptedException.initCause(e);
        throw interruptedException;
    }
}

From source file:org.marketcetera.util.except.ExceptUtils.java

/**
 * Checks whether the calling thread has been interrupted, and, if
 * so, throws an {@link InterruptedException} with the default
 * interruption message and the given underlying cause. The given
 * underlying cause is set on the thrown exception. The
 * interrupted status of the thread is cleared.
 *
 * @param cause The cause./*from   w w w  .ja v  a  2s  .c  o  m*/
 *
 * @throws InterruptedException Thrown if the calling thread
 * was interrupted.
 */

public static void checkInterruption(Throwable cause) throws InterruptedException {
    if (Thread.currentThread().isInterrupted()) {
        InterruptedException ex = new InterruptedException(Messages.THREAD_INTERRUPTED.getText());
        ex.initCause(cause);
        throw ex;
    }
}

From source file:org.marketcetera.util.except.ExceptUtils.java

/**
 * Checks whether the calling thread has been interrupted, and, if
 * so, throws an {@link InterruptedException} with the given
 * interruption message and the given underlying cause. The given
 * underlying cause is set on the thrown exception. The
 * interrupted status of the thread is cleared.
 *
 * @param cause The cause./*from  w w w  . ja va 2s. com*/
 * @param message The message.
 *
 * @throws InterruptedException Thrown if the calling thread
 * was interrupted.
 */

public static void checkInterruption(Throwable cause, String message) throws InterruptedException {
    if (Thread.currentThread().isInterrupted()) {
        InterruptedException ex = new InterruptedException(message);
        ex.initCause(cause);
        throw ex;
    }
}

From source file:org.openconcerto.sql.replication.MemoryRep.java

protected final void replicateData() throws SQLException, IOException, InterruptedException {
    final SQLSyntax slaveSyntax = SQLSyntax.get(this.slave);
    final File tempDir = FileUtils.createTempDir(getClass().getCanonicalName() + "_StoreData");
    try {// w  ww .  j a va  2s. com
        final List<String> queries = new ArrayList<String>();
        final List<ResultSetHandler> handlers = new ArrayList<ResultSetHandler>();
        final Map<File, SQLTable> files = new HashMap<File, SQLTable>();
        for (final Entry<String, Set<String>> e : this.tables.entrySet()) {
            if (Thread.interrupted())
                throw new InterruptedException("While creating handlers");
            final String rootName = e.getKey();
            final File rootDir = new File(tempDir, rootName);
            FileUtils.mkdir_p(rootDir);
            final DBRoot root = this.master.getRoot(rootName);
            final DBRoot slaveRoot = this.slave.getRoot(rootName);
            for (final String tableName : e.getValue()) {
                final SQLTable masterT = root.getTable(tableName);
                final SQLSelect select = new SQLSelect(true).addSelectStar(masterT);
                queries.add(select.asString());
                // don't use cache to be sure to have up to date data
                handlers.add(new IResultSetHandler(new ResultSetHandler() {

                    private final CSVHandler csvH = new CSVHandler(masterT.getOrderedFields());

                    @Override
                    public Object handle(ResultSet rs) throws SQLException {
                        final File tempFile = new File(rootDir,
                                FileUtils.FILENAME_ESCAPER.escape(tableName) + ".csv");
                        assert !tempFile.exists();
                        try {
                            FileUtils.write(this.csvH.handle(rs), tempFile);
                            files.put(tempFile, slaveRoot.getTable(tableName));
                        } catch (IOException e) {
                            throw new SQLException(e);
                        }
                        return null;
                    }
                }, false));
            }
        }
        try {
            SQLUtils.executeAtomic(this.master.getDataSource(),
                    new ConnectionHandlerNoSetup<Object, SQLException>() {
                        @Override
                        public Object handle(SQLDataSource ds) throws SQLException {
                            SQLUtils.executeMultiple(MemoryRep.this.master, queries, handlers);
                            return null;
                        }
                    });
        } catch (RTInterruptedException e) {
            final InterruptedException exn = new InterruptedException("Interrupted while querying the master");
            exn.initCause(e);
            throw exn;
        }
        SQLUtils.executeAtomic(this.slave.getDataSource(), new ConnectionHandlerNoSetup<Object, IOException>() {
            @Override
            public Object handle(SQLDataSource ds) throws SQLException, IOException {
                for (final Entry<File, SQLTable> e : files.entrySet()) {
                    final SQLTable slaveT = e.getValue();
                    // loadData() fires table modified
                    slaveSyntax.loadData(e.getKey(), slaveT, true);
                }
                return null;
            }
        });
        this.count.incrementAndGet();
    } finally {
        FileUtils.rm_R(tempDir);
    }
}