Skip to content

Commit

Permalink
Merge branch 'issues/27'
Browse files Browse the repository at this point in the history
  • Loading branch information
dotasek committed Jul 11, 2017
2 parents 0495da5 + 6321661 commit 5625fd0
Show file tree
Hide file tree
Showing 4 changed files with 338 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
@Api(tags = {CyRESTSwagger.CyRESTSwaggerConfig.COMMANDS_TAG})
@Singleton
@Path("/v1/commands")
public class CommandResource implements PaxAppender, TaskObserver
public class CommandResource
{

@Inject
@NotNull
private AvailableCommands available;
Expand All @@ -62,9 +62,7 @@ public class CommandResource implements PaxAppender, TaskObserver
private SynchronousTaskManager<?> taskManager;


private CustomFailureException taskException;
private MessageHandler messageHandler;
private boolean processingCommand = false;


@GET
@Path("/")
Expand Down Expand Up @@ -266,65 +264,31 @@ private final String executeCommand(
}
}

processingCommand = true;
messageHandler = handler;
taskException = null;


CommandResourceTaskObserver taskObserver = new CommandResourceTaskObserver(handler);

taskManager.execute(ceTaskFactory.createTaskIterator(namespace,
command, modifiedSettings, this), this);

String messages = messageHandler.getMessages();
processingCommand = false;
if (taskException != null)
throw taskException;
return messages;
}

public void doAppend(PaxLoggingEvent event) {
// System.out.println(event.getLevel().toInt() + ": " + event.getMessage());
// Get prefix
// Handle levels
if (!processingCommand) {
return;
command, modifiedSettings, taskObserver), taskObserver);

synchronized (taskObserver) {
try{
while (!taskObserver.isFinished()) {
System.out.println("Waiting for all tasks to finish at "+System.currentTimeMillis());
taskObserver.wait();
}
}catch(InterruptedException e){
e.printStackTrace();
}
}

PaxLevel level = event.getLevel();
if (level.toInt() == 40000)
messageHandler.appendError(event.getMessage());
else if (level.toInt() == 30000)
messageHandler.appendWarning(event.getMessage());
else
messageHandler.appendMessage(event.getMessage());
}

////////////////// For Observable Task //////////////////////////
String messages = taskObserver.getMessageHandler().getMessages();

@Override
public void taskFinished(ObservableTask t) {
final Object res = t.getResults(String.class);
if (res != null)
messageHandler.appendResult(res);
if (taskObserver.getTaskException() != null)
throw taskObserver.getTaskException();
return messages;
}


@Override
public void allFinished(FinishStatus status) {
if (status.getType().equals(FinishStatus.Type.SUCCEEDED))
messageHandler.appendMessage("Finished");
else if (status.getType().equals(FinishStatus.Type.CANCELLED))
messageHandler.appendWarning("Cancelled by user");
else if (status.getType().equals(FinishStatus.Type.FAILED)) {
if (status.getException() != null) {
messageHandler.appendError("Failed: "
+ status.getException().getMessage());
taskException = new CustomFailureException("Failed: "
+ status.getException().getMessage());
} else {
messageHandler.appendError("Failed");
taskException = new CustomFailureException();
}
}
}



private final String stripQuotes(final String quotedString) {
String tqString = quotedString.trim();
Expand All @@ -344,14 +308,5 @@ public CustomNotFoundException(String message) {
}
}

public class CustomFailureException extends WebApplicationException {
public CustomFailureException() {
super(500);
}

public CustomFailureException(String message) {
super(Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(message).type("text/plain").build());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.cytoscape.rest.internal.commands.resources;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

import org.cytoscape.rest.internal.commands.handlers.MessageHandler;
import org.cytoscape.work.FinishStatus;
import org.cytoscape.work.ObservableTask;
import org.cytoscape.work.TaskObserver;
import org.ops4j.pax.logging.spi.PaxAppender;
import org.ops4j.pax.logging.spi.PaxLevel;
import org.ops4j.pax.logging.spi.PaxLoggingEvent;

public class CommandResourceTaskObserver implements PaxAppender, TaskObserver {

private CustomFailureException taskException;
public CustomFailureException getTaskException() {
return taskException;
}



public MessageHandler getMessageHandler() {
return messageHandler;
}

private MessageHandler messageHandler;

private boolean finished;

public CommandResourceTaskObserver (MessageHandler messageHandler) {
this.messageHandler = messageHandler;
finished = false;
}

public boolean isFinished() {
return finished;
}



@Override
public void taskFinished(ObservableTask t) {
final Object res = t.getResults(String.class);
if (res != null)
messageHandler.appendResult(res);
}


@Override
public void allFinished(FinishStatus status) {
if (status.getType().equals(FinishStatus.Type.SUCCEEDED))
messageHandler.appendMessage("Finished");
else if (status.getType().equals(FinishStatus.Type.CANCELLED))
messageHandler.appendWarning("Cancelled by user");
else if (status.getType().equals(FinishStatus.Type.FAILED)) {
if (status.getException() != null) {
messageHandler.appendError("Failed: "
+ status.getException().getMessage());
taskException = new CustomFailureException("Failed: "
+ status.getException().getMessage());
} else {
messageHandler.appendError("Failed");
taskException = new CustomFailureException();
}
}
synchronized(this) {
finished = true;
notify();
}
}

public class CustomFailureException extends WebApplicationException {
public CustomFailureException() {
super(500);
}

public CustomFailureException(String message) {
super(Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(message).type("text/plain").build());
}
}

public void doAppend(PaxLoggingEvent event) {
// System.out.println(event.getLevel().toInt() + ": " + event.getMessage());
// Get prefix
// Handle levels


PaxLevel level = event.getLevel();
if (level.toInt() == 40000)
messageHandler.appendError(event.getMessage());
else if (level.toInt() == 30000)
messageHandler.appendWarning(event.getMessage());
else
messageHandler.appendMessage(event.getMessage());
}
}
Loading

0 comments on commit 5625fd0

Please sign in to comment.