Roman - 2014-02-03

From my squirrel hack... Prerequisites:
- project to be debugged needs to have the hacked squirrel-sql in the classpath (possible improvement: somehow inject the hacked squirrel at runtime into the classpath)
- user right clicks on a stack element which has a jdbc session somewhere referenced which can be resolved through reflection

import org.eclipse.core.internal.jobs.JobStatus;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.debug.core.model.IDebugElement;
import org.eclipse.debug.core.model.IWatchExpression;
import org.eclipse.debug.internal.core.WatchExpression;

class SquirrelStarterJob extends Job {

private static final String SQUIRREL_COMMAND = "my.SquirrelStarter.startApp(this)";
private IDebugElement context;

public SquirrelStarterJob() {
    super("Squirrel Starter Job");
}

/**
 * 
 * 
 * @param context
 */
public SquirrelStarterJob(IDebugElement context) {
    this();
    this.context = context;
}

/**
 * {@inheritDoc}
 */
@Override
protected IStatus run(IProgressMonitor monitor) {
    try {
        IWatchExpression watchExpression = new WatchExpression(
                SQUIRREL_COMMAND);
        watchExpression.setExpressionContext(context);
        watchExpression.evaluate();
        long timeout = 30000;
        long start = System.currentTimeMillis();
        while (watchExpression.isPending()
                && (timeout > (System.currentTimeMillis() - start))) {
            Thread.sleep(200);
        }
        if (watchExpression.getErrorMessages() != null
                && watchExpression.getErrorMessages().length > 0) {
            String errorMsg = "squirrel command returned an error: "
                    + toString(watchExpression.getErrorMessages());
            ConsoleUtil.printlnConsoleMsg(errorMsg);
            return new JobStatus(JobStatus.ERROR, this, errorMsg);
        } else {
            String successMsg = "squirrel start command queued";
            ConsoleUtil.printlnConsoleMsg(successMsg);
            return new JobStatus(JobStatus.OK, this, successMsg);
        }
    } catch (Exception e) {
        ConsoleUtil.printlnConsoleMsg("failed with " + e.getMessage());
        e.printStackTrace();
        return new JobStatus(JobStatus.CANCEL, this, "failed with "
                + e.getMessage());
    }
}

}