提交 95526c82 编写于 作者: A Alexander Fedorov

#2556 add DBGEvent

上级 c66c46a6
......@@ -23,6 +23,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.osgi.util.NLS;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
......@@ -41,6 +42,8 @@ public abstract class DBGBaseController implements DBGController {
private final Map<String, Object> configuration = new HashMap<String, Object>();
private final Map<Object, DBGSession> sessions = new HashMap<Object, DBGSession>(1);
private ListenerList<DBGEventHandler> eventHandlers = new ListenerList<>();
private DBCExecutionContext executionContext;
public DBGBaseController(DBPDataSourceContainer dataSourceContainer) {
......@@ -112,6 +115,10 @@ public abstract class DBGBaseController implements DBGController {
for (DBGSession session : values) {
session.close();
}
Object[] listeners = eventHandlers.getListeners();
for (Object listener : listeners) {
unregisterEventHandler((DBGEventHandler) listener);
}
}
@Override
......@@ -180,4 +187,20 @@ public abstract class DBGBaseController implements DBGController {
//throw DBGException?
}
@Override
public void registerEventHandler(DBGEventHandler eventHandler) {
eventHandlers.add(eventHandler);
}
@Override
public void unregisterEventHandler(DBGEventHandler eventHandler) {
eventHandlers.remove(eventHandler);
}
public void fireEvent(DBGEvent event) {
for (DBGEventHandler eventHandler : eventHandlers) {
eventHandler.handleDebugEvent(event);
}
}
}
package org.jkiss.dbeaver.debug;
public abstract class DBGBaseSession implements DBGSession {
private final DBGBaseController controller;
public DBGBaseSession(DBGBaseController controller) {
this.controller = controller;
}
public DBGBaseController getController() {
return controller;
}
}
......@@ -86,5 +86,12 @@ public interface DBGController {
void stepInto(Object sessionKey) throws DBGException;
void stepOver(Object sessionKey) throws DBGException;
void stepReturn(Object sessionKey) throws DBGException;
/*
* Events
*/
void registerEventHandler(DBGEventHandler eventHandler);
void unregisterEventHandler(DBGEventHandler eventHandler);
}
......@@ -6,9 +6,37 @@ public class DBGEvent extends EventObject {
private static final long serialVersionUID = 1L;
public DBGEvent(Object source) {
super(source);
public static final int ATTACH = 0x0001;
public static final int SUSPEND = 0x0002;
public static final int RESUME = 0x0004;
public static final int DETACH = 0x0008;
public static final int UNSPECIFIED = 0;
public static final int STEP_INTO = 0x0001;
public static final int STEP_OVER = 0x0002;
public static final int STEP_RETURN = 0x0004;
public static final int STEP_END = 0x0008;
private int kind;
private int details;
public DBGEvent(Object source, int kind) {
this(source, kind, UNSPECIFIED);
}
public DBGEvent(Object source, int kind, int details) {
super(source);
this.kind = kind;
this.details = details;
}
public int getKind() {
return kind;
}
public int getDetails() {
return details;
}
}
package org.jkiss.dbeaver.debug;
public interface DBGEventHandler {
void handleDebugEvent(DBGEvent event);
}
......@@ -36,12 +36,14 @@ import org.eclipse.debug.core.model.IThread;
import org.eclipse.osgi.util.NLS;
import org.jkiss.dbeaver.debug.DBGController;
import org.jkiss.dbeaver.debug.DBGEvent;
import org.jkiss.dbeaver.debug.DBGEventHandler;
import org.jkiss.dbeaver.debug.DBGException;
import org.jkiss.dbeaver.debug.core.DebugCore;
import org.jkiss.dbeaver.debug.core.DebugEvents;
import org.jkiss.dbeaver.model.runtime.DefaultProgressMonitor;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
public abstract class DatabaseDebugTarget extends DatabaseDebugElement implements IDatabaseDebugTarget {
public abstract class DatabaseDebugTarget extends DatabaseDebugElement implements IDatabaseDebugTarget, DBGEventHandler {
private final String modelIdentifier;
......@@ -64,6 +66,7 @@ public abstract class DatabaseDebugTarget extends DatabaseDebugElement implement
this.launch = launch;
this.process = process;
this.controller = controller;
this.controller.registerEventHandler(this);
this.threads = new ArrayList<IThread>();
}
......@@ -174,6 +177,7 @@ public abstract class DatabaseDebugTarget extends DatabaseDebugElement implement
suspended = false;
try {
controller.detach(sessionKey, getProgressMonitor());
controller.unregisterEventHandler(this);
} catch (DBGException e) {
String message = NLS.bind("Error terminating {0}", getName());
IStatus status = DebugCore.newErrorStatus(message, e);
......@@ -285,7 +289,7 @@ public abstract class DatabaseDebugTarget extends DatabaseDebugElement implement
@Override
public DebugEvent toDebugEvent(DBGEvent event) {
return new DebugEvent(event.getSource(), DebugEvent.MODEL_SPECIFIC);
return new DebugEvent(event.getSource(), event.getKind(), event.getDetails());
}
@Override
......@@ -297,5 +301,11 @@ public abstract class DatabaseDebugTarget extends DatabaseDebugElement implement
public IMemoryBlock getMemoryBlock(long startAddress, long length) throws DebugException {
return null;
}
@Override
public void handleDebugEvent(DBGEvent event) {
DebugEvent debugEvent = toDebugEvent(event);
DebugEvents.fireEvent(debugEvent);
}
}
......@@ -155,7 +155,7 @@ public class PostgreDebugController extends DBGBaseController {
@Override
public PostgreDebugSession createSession(DBGSessionInfo targetInfo, DBCExecutionContext sessionContext) throws DBGException {
PostgreDebugSessionInfo sessionInfo = getSessionDescriptor(sessionContext);
PostgreDebugSession debugSession = new PostgreDebugSession(sessionInfo, targetInfo.getID());
PostgreDebugSession debugSession = new PostgreDebugSession(this, sessionInfo, targetInfo.getID());
return debugSession;
......
......@@ -30,8 +30,11 @@ import java.util.concurrent.FutureTask;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.debug.DBGBaseController;
import org.jkiss.dbeaver.debug.DBGBaseSession;
import org.jkiss.dbeaver.debug.DBGBreakpointDescriptor;
import org.jkiss.dbeaver.debug.DBGBreakpointProperties;
import org.jkiss.dbeaver.debug.DBGEvent;
import org.jkiss.dbeaver.debug.DBGException;
import org.jkiss.dbeaver.debug.DBGObjectDescriptor;
import org.jkiss.dbeaver.debug.DBGSession;
......@@ -57,7 +60,7 @@ import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
* procedure will be reached <br/>
*
*/
public class PostgreDebugSession implements DBGSession {
public class PostgreDebugSession extends DBGBaseSession {
private static final Log log = Log.getLog(PostgreDebugSession.class);
......@@ -96,7 +99,7 @@ public class PostgreDebugSession implements DBGSession {
private List<PostgreDebugBreakpointDescriptor> breakpoints = new ArrayList<PostgreDebugBreakpointDescriptor>(1);
private FutureTask<Void> task;
private FutureTask<DBGEvent> task;
private Thread workerThread = null;
......@@ -108,7 +111,8 @@ public class PostgreDebugSession implements DBGSession {
* @param sessionDebugInfo - session (debugger client connection) description
* @throws DBGException
*/
public PostgreDebugSession(PostgreDebugSessionInfo sessionInfo, Object targetId) throws DBGException {
public PostgreDebugSession(DBGBaseController controller, PostgreDebugSessionInfo sessionInfo, Object targetId) throws DBGException {
super(controller);
this.sessionInfo = sessionInfo;
this.targetId = targetId;
}
......@@ -148,8 +152,9 @@ public class PostgreDebugSession implements DBGSession {
PostgreDebugObjectDescriptor obj = new PostgreDebugObjectDescriptor(OID,"ENTRY","SESSION","THIS","PG");
addBreakpoint(obj, properties);
runAsync(SQL_ATTACH.replaceAll("\\?sessionid", String.valueOf(sessionId)),
String.valueOf(sessionId) + " global attached to " + String.valueOf(targetId));
String sessionParam = String.valueOf(getSessionId());
String taskName = sessionParam + " global attached to " + String.valueOf(targetId);
runAsync(SQL_ATTACH.replaceAll("\\?sessionid", sessionParam), taskName, new DBGEvent(this, DBGEvent.ATTACH));
/*if (breakpoint) {
runAsync(SQL_ATTACH_BREAKPOINT.replaceAll("\\?sessionid", String.valueOf(sessionId)),
......@@ -246,19 +251,18 @@ public class PostgreDebugSession implements DBGSession {
@Override
public void execContinue() throws DBGException {
execStep(SQL_CONTINUE, " continue for ");
execStep(SQL_CONTINUE, " continue for ", DBGEvent.RESUME);
}
@Override
public void execStepInto() throws DBGException {
execStep(SQL_STEP_INTO, " step into for ");
execStep(SQL_STEP_INTO, " step into for ", DBGEvent.STEP_INTO);
}
@Override
public void execStepOver() throws DBGException {
execStep(SQL_STEP_OVER, " step over for ");
execStep(SQL_STEP_OVER, " step over for ", DBGEvent.STEP_OVER);
}
......@@ -270,14 +274,15 @@ public class PostgreDebugSession implements DBGSession {
* @param name - session 'name' part
* @throws DBGException
*/
public void execStep(String commandSQL, String name) throws DBGException {
public void execStep(String commandSQL, String name, int eventDetail) throws DBGException {
acquireWriteLock();
try {
DBGEvent event = new DBGEvent(this, DBGEvent.RESUME, eventDetail);
runAsync(commandSQL.replaceAll("\\?sessionid", String.valueOf(sessionId)),
String.valueOf(sessionId) + name + String.valueOf(targetId));
String.valueOf(sessionId) + name + String.valueOf(targetId), event);
} finally {
lock.writeLock().unlock();
......@@ -401,7 +406,7 @@ public class PostgreDebugSession implements DBGSession {
List<DBGStackFrame> stack = new ArrayList<DBGStackFrame>(1);
String sql = SQL_GET_STACK.replaceAll("\\?sessionid", String.valueOf(sessionId));
String sql = SQL_GET_STACK.replaceAll("\\?sessionid", String.valueOf(getSessionId()));
try (Statement stmt = getConnection(connection).createStatement(); ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
int level = rs.getInt("level");
......@@ -467,7 +472,8 @@ public class PostgreDebugSession implements DBGSession {
if (task.isDone()) {
try {
task.get();
DBGEvent dbgEvent = task.get();
getController().fireEvent(dbgEvent);
} catch (InterruptedException e) {
log.error("DEBUG INTERRUPT ERROR ",e);
return false;
......@@ -499,16 +505,16 @@ public class PostgreDebugSession implements DBGSession {
* @param name
* @throws DBGException
*/
private void runAsync(String commandSQL, String name) throws DBGException {
private void runAsync(String commandSQL, String name, DBGEvent event) throws DBGException {
Connection connection = getConnection();
try (Statement stmt = connection.createStatement()) {
connection.setAutoCommit(false);
PostgreDebugSessionWorker worker = new PostgreDebugSessionWorker(connection, commandSQL);
PostgreDebugSessionWorker worker = new PostgreDebugSessionWorker(connection, commandSQL, event);
task = new FutureTask<Void>(worker);
task = new FutureTask<DBGEvent>(worker);
workerThread = new Thread(task);
......
......@@ -23,24 +23,28 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.Callable;
public class PostgreDebugSessionWorker implements Callable<Void> {
import org.jkiss.dbeaver.debug.DBGEvent;
public class PostgreDebugSessionWorker implements Callable<DBGEvent> {
private final Connection conn;
private final String sql;
private final DBGEvent event;
public PostgreDebugSessionWorker(Connection conn, String sqlCommand)
public PostgreDebugSessionWorker(Connection conn, String sqlCommand, DBGEvent event)
{
this.conn = conn;
this.sql = sqlCommand;
this.event = event;
}
@Override
public Void call() throws Exception
public DBGEvent call() throws Exception
{
try (Statement stmt = conn.createStatement()) {
stmt.executeQuery(sql);
return null;
return event;
} catch (SQLException e) {
String message = String.format("Failed to execute %s", sql);
throw new Exception(message, e);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册