-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
338 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...main/java/org/cytoscape/rest/internal/commands/resources/CommandResourceTaskObserver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.