2014-08-01 19:50:58 +02:00
|
|
|
package org.gcube.portlets.user.td.monitorwidget.client;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import org.gcube.portlets.user.td.gwtservice.client.rpc.TDGWTServiceAsync;
|
2014-08-06 20:11:44 +02:00
|
|
|
import org.gcube.portlets.user.td.gwtservice.shared.monitor.OperationMonitor;
|
|
|
|
import org.gcube.portlets.user.td.gwtservice.shared.monitor.OperationMonitorSession;
|
2014-08-01 19:50:58 +02:00
|
|
|
import org.gcube.portlets.user.td.widgetcommonevent.shared.TRId;
|
|
|
|
|
|
|
|
import com.allen_sauer.gwt.log.client.Log;
|
|
|
|
import com.google.gwt.user.client.Timer;
|
|
|
|
import com.google.gwt.user.client.rpc.AsyncCallback;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author "Giancarlo Panichi"
|
|
|
|
* <a href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
|
|
|
|
*
|
|
|
|
*/
|
2014-08-08 17:42:20 +02:00
|
|
|
public class MonitorUpdater extends Timer implements MonitorDialogEventUIListener {
|
2014-08-06 20:11:44 +02:00
|
|
|
|
2014-08-08 17:42:20 +02:00
|
|
|
protected ArrayList<MonitorUpdaterListener> listeners = new ArrayList<MonitorUpdaterListener>();
|
2014-08-06 20:11:44 +02:00
|
|
|
protected OperationMonitorSession operationMonitorSession;
|
|
|
|
|
2014-08-08 16:39:25 +02:00
|
|
|
public MonitorUpdater(OperationMonitorSession operationMonitorSession){
|
2014-08-06 20:11:44 +02:00
|
|
|
this.operationMonitorSession=operationMonitorSession;
|
2014-08-08 18:35:01 +02:00
|
|
|
|
2014-08-06 20:11:44 +02:00
|
|
|
}
|
2014-08-08 16:49:42 +02:00
|
|
|
|
2014-08-01 19:50:58 +02:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
Log.debug("requesting operation progress");
|
|
|
|
TDGWTServiceAsync.INSTANCE
|
2014-08-06 20:11:44 +02:00
|
|
|
.getOperationMonitor(operationMonitorSession,new AsyncCallback<OperationMonitor>() {
|
2014-08-01 19:50:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
public void onFailure(Throwable caught) {
|
|
|
|
cancel();
|
2014-08-08 18:35:01 +02:00
|
|
|
|
2014-08-01 19:50:58 +02:00
|
|
|
Log.error("Error retrieving the operation state",
|
|
|
|
caught);
|
|
|
|
String message = getStack(caught);
|
2014-08-08 18:35:01 +02:00
|
|
|
fireMonitorFailed(caught,
|
2014-08-28 13:18:30 +02:00
|
|
|
"Failed getting operation updates", message, null);
|
2014-08-01 19:50:58 +02:00
|
|
|
}
|
|
|
|
|
2014-08-06 20:11:44 +02:00
|
|
|
public void onSuccess(OperationMonitor result) {
|
2014-08-08 18:50:46 +02:00
|
|
|
Log.debug("retrieved OperationMonitor: "
|
|
|
|
+ result);
|
2014-08-08 18:35:01 +02:00
|
|
|
|
2014-08-08 18:50:46 +02:00
|
|
|
if(result.isInBackground()){
|
2014-08-08 18:35:01 +02:00
|
|
|
cancel();
|
2014-08-08 18:50:46 +02:00
|
|
|
fireMonitorPutInBackground();
|
|
|
|
return;
|
2014-08-08 18:35:01 +02:00
|
|
|
}
|
|
|
|
|
2014-08-06 20:11:44 +02:00
|
|
|
switch (result.getTask().getState()) {
|
2014-08-01 19:50:58 +02:00
|
|
|
case INITIALIZING:
|
|
|
|
Log.info("Initializing...");
|
2014-08-26 16:20:34 +02:00
|
|
|
fireMonitorInitializing(result);
|
2014-08-01 19:50:58 +02:00
|
|
|
break;
|
|
|
|
case ABORTED:
|
|
|
|
cancel();
|
2014-08-08 18:50:46 +02:00
|
|
|
fireMonitorAborted();
|
2014-08-01 19:50:58 +02:00
|
|
|
Log.info("Aborted");
|
|
|
|
break;
|
|
|
|
case IN_PROGRESS:
|
2014-08-26 16:20:34 +02:00
|
|
|
fireMonitorUpdate(result);
|
|
|
|
|
2014-08-01 19:50:58 +02:00
|
|
|
break;
|
|
|
|
case VALIDATING_RULES:
|
2014-08-26 16:20:34 +02:00
|
|
|
fireMonitorValidate(result);
|
2014-08-01 19:50:58 +02:00
|
|
|
break;
|
|
|
|
case GENERATING_VIEW:
|
|
|
|
Log.info("Generating View...");
|
2014-08-26 16:20:34 +02:00
|
|
|
fireMonitorGeneratingView(result);
|
2014-08-01 19:50:58 +02:00
|
|
|
break;
|
|
|
|
case STOPPED:
|
|
|
|
cancel();
|
|
|
|
stopMessage(result);
|
|
|
|
break;
|
|
|
|
case FAILED:
|
|
|
|
cancel();
|
|
|
|
errorMessage(result);
|
|
|
|
break;
|
|
|
|
case SUCCEDED:
|
|
|
|
cancel();
|
|
|
|
Log.info("Fisnish :"
|
|
|
|
+ result.getTrId());
|
2014-08-28 13:18:30 +02:00
|
|
|
fireMonitorComplete(result);
|
2014-08-01 19:50:58 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Log.info("Unknow State");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-08-26 16:20:34 +02:00
|
|
|
|
2014-08-06 20:11:44 +02:00
|
|
|
protected void errorMessage(OperationMonitor result) {
|
2014-08-08 18:53:43 +02:00
|
|
|
Log.info("Operation Failed");
|
2014-08-06 20:11:44 +02:00
|
|
|
Throwable th;
|
2014-08-08 18:35:01 +02:00
|
|
|
String reason = null;
|
2014-08-01 19:50:58 +02:00
|
|
|
String details = null;
|
2014-08-06 20:11:44 +02:00
|
|
|
if (result.getTask().getErrorCause() != null) {
|
|
|
|
th = result.getTask().getErrorCause();
|
2014-08-08 18:35:01 +02:00
|
|
|
reason = "Failed Client Library";
|
2014-08-06 20:11:44 +02:00
|
|
|
details = result.getTask().getErrorCause().getLocalizedMessage();
|
2014-08-01 19:50:58 +02:00
|
|
|
} else {
|
2014-08-08 18:35:01 +02:00
|
|
|
th = new Throwable("Operation failed");
|
|
|
|
reason = "Failed Client Library";
|
2014-08-06 20:11:44 +02:00
|
|
|
details = "Operation failed";
|
2014-08-01 19:50:58 +02:00
|
|
|
}
|
|
|
|
|
2014-08-28 13:18:30 +02:00
|
|
|
fireMonitorFailed(th, reason, details,result);
|
2014-08-01 19:50:58 +02:00
|
|
|
}
|
|
|
|
|
2014-08-06 20:11:44 +02:00
|
|
|
protected void stopMessage(OperationMonitor result) {
|
|
|
|
Log.info("Operation Stopped");
|
2014-08-08 18:35:01 +02:00
|
|
|
String reason = null;
|
2014-08-01 19:50:58 +02:00
|
|
|
String details = null;
|
2014-08-06 20:11:44 +02:00
|
|
|
if (result.getTask().getErrorCause() != null) {
|
2014-08-08 18:53:43 +02:00
|
|
|
reason = "Validations failed";
|
2014-08-06 20:11:44 +02:00
|
|
|
details = result.getTask().getErrorCause().getLocalizedMessage();
|
2014-08-01 19:50:58 +02:00
|
|
|
} else {
|
2014-08-08 18:53:43 +02:00
|
|
|
reason = "Validations failed";
|
2014-08-06 20:11:44 +02:00
|
|
|
details = "Operation stopped";
|
2014-08-01 19:50:58 +02:00
|
|
|
}
|
|
|
|
|
2014-08-28 13:18:30 +02:00
|
|
|
fireMonitorStopped(result.getTrId(),reason, details,result);
|
2014-08-01 19:50:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected String getStack(Throwable e) {
|
|
|
|
String message = e.getLocalizedMessage() + " -> <br>";
|
|
|
|
Throwable c = e.getCause();
|
|
|
|
if (c != null)
|
|
|
|
message += getStack(c);
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2014-08-26 16:20:34 +02:00
|
|
|
protected void fireMonitorInitializing(OperationMonitor result) {
|
2014-08-08 17:42:20 +02:00
|
|
|
for (MonitorUpdaterListener listener : listeners)
|
2014-08-26 16:20:34 +02:00
|
|
|
listener.monitorInitializing(result);
|
2014-08-01 19:50:58 +02:00
|
|
|
}
|
|
|
|
|
2014-08-26 16:20:34 +02:00
|
|
|
protected void fireMonitorGeneratingView(OperationMonitor result) {
|
2014-08-08 17:42:20 +02:00
|
|
|
for (MonitorUpdaterListener listener : listeners)
|
2014-08-26 16:20:34 +02:00
|
|
|
listener.monitorGeneratingView(result);
|
2014-08-01 19:50:58 +02:00
|
|
|
}
|
|
|
|
|
2014-08-26 16:20:34 +02:00
|
|
|
protected void fireMonitorUpdate(OperationMonitor result) {
|
2014-08-08 17:42:20 +02:00
|
|
|
for (MonitorUpdaterListener listener : listeners)
|
2014-08-26 16:20:34 +02:00
|
|
|
listener.monitorUpdate(result);
|
2014-08-01 19:50:58 +02:00
|
|
|
}
|
|
|
|
|
2014-08-26 16:20:34 +02:00
|
|
|
protected void fireMonitorValidate(OperationMonitor result) {
|
2014-08-08 17:42:20 +02:00
|
|
|
for (MonitorUpdaterListener listener : listeners)
|
2014-08-26 16:20:34 +02:00
|
|
|
listener.monitorValidate(result);
|
2014-08-01 19:50:58 +02:00
|
|
|
}
|
|
|
|
|
2014-08-28 13:18:30 +02:00
|
|
|
protected void fireMonitorComplete(OperationMonitor result) {
|
2014-08-08 17:42:20 +02:00
|
|
|
for (MonitorUpdaterListener listener : listeners)
|
2014-08-28 13:18:30 +02:00
|
|
|
listener.monitorComplete(result.getTrId(), result);
|
2014-08-01 19:50:58 +02:00
|
|
|
}
|
|
|
|
|
2014-08-08 18:35:01 +02:00
|
|
|
protected void fireMonitorFailed(Throwable caught, String failure,
|
2014-08-28 13:18:30 +02:00
|
|
|
String failureDetails, OperationMonitor operationMonitor) {
|
2014-08-08 17:42:20 +02:00
|
|
|
for (MonitorUpdaterListener listener : listeners)
|
2014-08-28 13:18:30 +02:00
|
|
|
listener.monitorFailed(caught, failure, failureDetails, operationMonitor);
|
2014-08-01 19:50:58 +02:00
|
|
|
}
|
|
|
|
|
2014-08-28 13:18:30 +02:00
|
|
|
protected void fireMonitorStopped(TRId trId, String reason, String details, OperationMonitor operationMonitor) {
|
2014-08-08 17:42:20 +02:00
|
|
|
for (MonitorUpdaterListener listener : listeners)
|
2014-08-28 13:18:30 +02:00
|
|
|
listener.monitorStopped(trId,reason, details, operationMonitor);
|
2014-08-01 19:50:58 +02:00
|
|
|
}
|
|
|
|
|
2014-08-08 18:50:46 +02:00
|
|
|
protected void fireMonitorAborted() {
|
|
|
|
for (MonitorUpdaterListener listener : listeners)
|
|
|
|
listener.monitorAborted();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void fireMonitorPutInBackground() {
|
|
|
|
for (MonitorUpdaterListener listener : listeners)
|
|
|
|
listener.monitorPutInBackground();
|
|
|
|
}
|
|
|
|
|
2014-08-01 19:50:58 +02:00
|
|
|
|
|
|
|
/**
|
2014-08-08 17:42:20 +02:00
|
|
|
* Add a new {@link MonitorUpdaterListener} to this
|
2014-08-01 19:50:58 +02:00
|
|
|
* {@link AddColumnProgressUpdater}.
|
|
|
|
*
|
|
|
|
* @param listener
|
|
|
|
* the listener to add.
|
|
|
|
*/
|
2014-08-08 17:42:20 +02:00
|
|
|
public void addListener(MonitorUpdaterListener listener) {
|
2014-08-01 19:50:58 +02:00
|
|
|
listeners.add(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-08 17:42:20 +02:00
|
|
|
* Removes the specified {@link MonitorUpdaterListener} from this
|
2014-08-01 19:50:58 +02:00
|
|
|
* {@link AddColumnProgressUpdater}.
|
|
|
|
*
|
|
|
|
* @param listener
|
|
|
|
* the listener to remove.
|
|
|
|
*/
|
2014-08-08 17:42:20 +02:00
|
|
|
public void removeListener(MonitorUpdaterListener listener) {
|
2014-08-01 19:50:58 +02:00
|
|
|
listeners.remove(listener);
|
|
|
|
}
|
2014-08-08 16:39:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-08-08 17:42:20 +02:00
|
|
|
public void requestAborted() {
|
2014-09-03 17:43:18 +02:00
|
|
|
operationMonitorSession.setAbort(true);
|
2014-08-08 16:39:25 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-08-08 17:42:20 +02:00
|
|
|
public void requestPutInBackground() {
|
2014-08-08 16:39:25 +02:00
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
|
|
|
}
|
2014-08-01 19:50:58 +02:00
|
|
|
}
|