use of java.util.concurrent.CancellationException in project android by cSploit.
the class UpdateService method extract.
/**
* extract an archive into a directory
*
* @throws IOException if some I/O error occurs
* @throws java.util.concurrent.CancellationException if task is cancelled by user
* @throws java.lang.InterruptedException when the the running thread get cancelled.
*/
private void extract() throws RuntimeException, IOException, InterruptedException, ChildManager.ChildNotStartedException {
ArchiveInputStream is = null;
ArchiveEntry entry;
CountingInputStream counter;
OutputStream outputStream = null;
File f, inFile;
File[] list;
String name;
String envPath;
final StringBuffer sb = new StringBuffer();
int mode;
int count;
long total;
boolean isTar, r, w, x, isElf, isScript;
short percentage, old_percentage;
Child which;
if (mCurrentTask.path == null || mCurrentTask.outputDir == null)
return;
mBuilder.setContentTitle(getString(R.string.extracting)).setContentText("").setContentInfo("").setSmallIcon(android.R.drawable.ic_popup_sync).setProgress(100, 0, false);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Logger.info(String.format("extracting '%s' to '%s'", mCurrentTask.path, mCurrentTask.outputDir));
envPath = null;
which = null;
try {
if (mCurrentTask.fixShebang) {
which = System.getTools().raw.async("which env", new Raw.RawReceiver() {
@Override
public void onNewLine(String line) {
sb.delete(0, sb.length());
sb.append(line);
}
});
}
inFile = new File(mCurrentTask.path);
total = inFile.length();
counter = new CountingInputStream(new FileInputStream(inFile));
is = openArchiveStream(counter);
isTar = mCurrentTask.archiver.equals(archiveAlgorithm.tar);
old_percentage = -1;
f = new File(mCurrentTask.outputDir);
if (f.exists() && f.isDirectory() && (list = f.listFiles()) != null && list.length > 2)
wipe();
if (mCurrentTask.fixShebang) {
if (execShell(which, "cancelled while retrieving env path") != 0) {
throw new RuntimeException("cannot find 'env' executable");
}
envPath = sb.toString();
}
while (mRunning && (entry = is.getNextEntry()) != null) {
name = entry.getName().replaceFirst("^\\./?", "");
if (mCurrentTask.skipRoot) {
if (name.contains("/"))
name = name.substring(name.indexOf('/') + 1);
else if (entry.isDirectory())
continue;
}
f = new File(mCurrentTask.outputDir, name);
isElf = isScript = false;
if (entry.isDirectory()) {
if (!f.exists()) {
if (!f.mkdirs()) {
throw new IOException(String.format("Couldn't create directory '%s'.", f.getAbsolutePath()));
}
}
} else {
byte[] buffer = null;
byte[] writeMe = null;
outputStream = new FileOutputStream(f);
// check il file is an ELF or a script
if ((!isTar || mCurrentTask.fixShebang) && entry.getSize() > 4) {
writeMe = buffer = new byte[4];
IOUtils.readFully(is, buffer);
if (buffer[0] == 0x7F && buffer[1] == 0x45 && buffer[2] == 0x4C && buffer[3] == 0x46) {
isElf = true;
} else if (buffer[0] == '#' && buffer[1] == '!') {
isScript = true;
ByteArrayOutputStream firstLine = new ByteArrayOutputStream();
int newline = -1;
// assume that '\n' is more far then 4 chars.
firstLine.write(buffer);
buffer = new byte[1024];
count = 0;
while (mRunning && (count = is.read(buffer)) >= 0 && (newline = Arrays.binarySearch(buffer, 0, count, (byte) 0x0A)) < 0) {
firstLine.write(buffer, 0, count);
}
if (!mRunning) {
throw new CancellationException("cancelled while searching for newline.");
} else if (count < 0) {
newline = count = 0;
} else if (newline < 0) {
newline = count;
}
firstLine.write(buffer, 0, newline);
firstLine.close();
byte[] newFirstLine = new String(firstLine.toByteArray()).replace("/usr/bin/env", envPath).getBytes();
writeMe = new byte[newFirstLine.length + (count - newline)];
java.lang.System.arraycopy(newFirstLine, 0, writeMe, 0, newFirstLine.length);
java.lang.System.arraycopy(buffer, newline, writeMe, newFirstLine.length, count - newline);
}
}
if (writeMe != null) {
outputStream.write(writeMe);
}
IOUtils.copy(is, outputStream);
outputStream.close();
outputStream = null;
percentage = (short) (((double) counter.getBytesRead() / total) * 100);
if (percentage != old_percentage) {
mBuilder.setProgress(100, percentage, false).setContentInfo(percentage + "%");
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
old_percentage = percentage;
}
}
// Zip does not store file permissions.
if (isTar) {
mode = ((TarArchiveEntry) entry).getMode();
r = (mode & 0400) > 0;
w = (mode & 0200) > 0;
x = (mode & 0100) > 0;
} else if (isElf || isScript) {
r = w = x = true;
} else {
continue;
}
if (!f.setExecutable(x, true)) {
Logger.warning(String.format("cannot set executable permission of '%s'", name));
}
if (!f.setWritable(w, true)) {
Logger.warning(String.format("cannot set writable permission of '%s'", name));
}
if (!f.setReadable(r, true)) {
Logger.warning(String.format("cannot set readable permission of '%s'", name));
}
}
if (!mRunning)
throw new CancellationException("extraction cancelled.");
Logger.info("extraction completed");
f = new File(mCurrentTask.outputDir, ".nomedia");
if (f.createNewFile())
Logger.info(".nomedia created");
mBuilder.setContentInfo("").setProgress(100, 100, true);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
} finally {
if (is != null)
is.close();
if (outputStream != null)
outputStream.close();
}
}
use of java.util.concurrent.CancellationException in project druid by druid-io.
the class ChainedExecutionQueryRunner method run.
@Override
public Sequence<T> run(final Query<T> query, final Map<String, Object> responseContext) {
final int priority = BaseQuery.getContextPriority(query, 0);
final Ordering ordering = query.getResultOrdering();
return new BaseSequence<T, Iterator<T>>(new BaseSequence.IteratorMaker<T, Iterator<T>>() {
@Override
public Iterator<T> make() {
// Make it a List<> to materialize all of the values (so that it will submit everything to the executor)
ListenableFuture<List<Iterable<T>>> futures = Futures.allAsList(Lists.newArrayList(Iterables.transform(queryables, new Function<QueryRunner<T>, ListenableFuture<Iterable<T>>>() {
@Override
public ListenableFuture<Iterable<T>> apply(final QueryRunner<T> input) {
if (input == null) {
throw new ISE("Null queryRunner! Looks to be some segment unmapping action happening");
}
return exec.submit(new AbstractPrioritizedCallable<Iterable<T>>(priority) {
@Override
public Iterable<T> call() throws Exception {
try {
Sequence<T> result = input.run(query, responseContext);
if (result == null) {
throw new ISE("Got a null result! Segments are missing!");
}
List<T> retVal = Sequences.toList(result, Lists.<T>newArrayList());
if (retVal == null) {
throw new ISE("Got a null list of results! WTF?!");
}
return retVal;
} catch (QueryInterruptedException e) {
throw Throwables.propagate(e);
} catch (Exception e) {
log.error(e, "Exception with one of the sequences!");
throw Throwables.propagate(e);
}
}
});
}
})));
queryWatcher.registerQuery(query, futures);
try {
final Number timeout = query.getContextValue(QueryContextKeys.TIMEOUT, (Number) null);
return new MergeIterable<>(ordering.nullsFirst(), timeout == null ? futures.get() : futures.get(timeout.longValue(), TimeUnit.MILLISECONDS)).iterator();
} catch (InterruptedException e) {
log.warn(e, "Query interrupted, cancelling pending results, query id [%s]", query.getId());
futures.cancel(true);
throw new QueryInterruptedException(e);
} catch (CancellationException e) {
throw new QueryInterruptedException(e);
} catch (TimeoutException e) {
log.info("Query timeout, cancelling pending results for query id [%s]", query.getId());
futures.cancel(true);
throw new QueryInterruptedException(e);
} catch (ExecutionException e) {
throw Throwables.propagate(e.getCause());
}
}
@Override
public void cleanup(Iterator<T> tIterator) {
}
});
}
use of java.util.concurrent.CancellationException in project druid by druid-io.
the class QueryInterruptedExceptionTest method testErrorCode.
@Test
public void testErrorCode() {
Assert.assertEquals("Query cancelled", new QueryInterruptedException(new QueryInterruptedException(new CancellationException())).getErrorCode());
Assert.assertEquals("Query cancelled", new QueryInterruptedException(new CancellationException()).getErrorCode());
Assert.assertEquals("Query interrupted", new QueryInterruptedException(new InterruptedException()).getErrorCode());
Assert.assertEquals("Query timeout", new QueryInterruptedException(new TimeoutException()).getErrorCode());
Assert.assertEquals("Unknown exception", new QueryInterruptedException(null).getErrorCode());
Assert.assertEquals("Unknown exception", new QueryInterruptedException(new ISE("Something bad!")).getErrorCode());
Assert.assertEquals("Resource limit exceeded", new QueryInterruptedException(new ResourceLimitExceededException("too many!")).getErrorCode());
Assert.assertEquals("Unknown exception", new QueryInterruptedException(new QueryInterruptedException(new ISE("Something bad!"))).getErrorCode());
}
use of java.util.concurrent.CancellationException in project druid by druid-io.
the class QueryInterruptedExceptionTest method testErrorClass.
@Test
public void testErrorClass() {
Assert.assertEquals("java.util.concurrent.CancellationException", new QueryInterruptedException(new QueryInterruptedException(new CancellationException())).getErrorClass());
Assert.assertEquals("java.util.concurrent.CancellationException", new QueryInterruptedException(new CancellationException()).getErrorClass());
Assert.assertEquals("java.lang.InterruptedException", new QueryInterruptedException(new InterruptedException()).getErrorClass());
Assert.assertEquals("java.util.concurrent.TimeoutException", new QueryInterruptedException(new TimeoutException()).getErrorClass());
Assert.assertEquals("io.druid.query.ResourceLimitExceededException", new QueryInterruptedException(new ResourceLimitExceededException("too many!")).getErrorClass());
Assert.assertEquals(null, new QueryInterruptedException(null).getErrorClass());
Assert.assertEquals("io.druid.java.util.common.ISE", new QueryInterruptedException(new ISE("Something bad!")).getErrorClass());
Assert.assertEquals("io.druid.java.util.common.ISE", new QueryInterruptedException(new QueryInterruptedException(new ISE("Something bad!"))).getErrorClass());
}
use of java.util.concurrent.CancellationException in project che by eclipse.
the class WorkspaceRuntimesTest method cancellationOfPendingStartTask.
@Test
public void cancellationOfPendingStartTask() throws Throwable {
WorkspaceImpl workspace = newWorkspace("workspace", "env-name");
when(sharedPool.submit(any())).thenReturn(Futures.immediateFuture(null));
CompletableFuture<WorkspaceRuntimeImpl> cmpFuture = runtimes.startAsync(workspace, "env-name", false);
// the real start is not being executed, fake sharedPool suppressed it
// so the situation is the same to the one if the task is cancelled before
// executor service started executing it
runtimes.stop(workspace.getId());
// start awaiting clients MUST receive interruption
try {
cmpFuture.get();
} catch (ExecutionException x) {
verifyCompletionException(cmpFuture, EnvironmentStartInterruptedException.class, "Start of environment 'env-name' in workspace 'workspace' is interrupted");
}
// completed clients receive interrupted exception and cancellation doesn't bother them
try {
captureAsyncTaskAndExecuteSynchronously();
} catch (CancellationException cancelled) {
assertEquals(cancelled.getMessage(), "Start of the workspace 'workspace' was cancelled");
}
verifyEventsSequence(event("workspace", WorkspaceStatus.STOPPED, WorkspaceStatus.STARTING, EventType.STARTING, null), event("workspace", WorkspaceStatus.STARTING, WorkspaceStatus.STOPPING, EventType.STOPPING, null), event("workspace", WorkspaceStatus.STOPPING, WorkspaceStatus.STOPPED, EventType.STOPPED, null));
}
Aggregations