[#1472] Add a Settings.executeDebugging property, and move server-side

Console logic to core

 - Extracted ExecutionType into its own file
 - Added some Javadoc
This commit is contained in:
Lukas Eder 2012-09-11 22:40:32 +02:00
parent 7d3b83a97d
commit ba5bdd2c7b
8 changed files with 206 additions and 73 deletions

View File

@ -36,6 +36,11 @@
*/
package org.jooq.debug.console;
import static org.jooq.tools.debug.ExecutionType.FAIL;
import static org.jooq.tools.debug.ExecutionType.RUN;
import static org.jooq.tools.debug.ExecutionType.SKIP;
import static org.jooq.tools.debug.ExecutionType.STEP;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
@ -59,9 +64,9 @@ import javax.swing.JTabbedPane;
import org.jooq.debug.console.DebuggerPane.BreakpointHitNode;
import org.jooq.tools.debug.BreakpointHit;
import org.jooq.tools.debug.Debugger;
import org.jooq.tools.debug.ExecutionType;
import org.jooq.tools.debug.QueryExecutor;
import org.jooq.tools.debug.QueryExecutorCreator;
import org.jooq.tools.debug.BreakpointHit.ExecutionType;
import org.fife.ui.rtextarea.RTextScrollPane;
@ -71,10 +76,10 @@ import org.fife.ui.rtextarea.RTextScrollPane;
@SuppressWarnings("serial")
public class BreakpointHitEditor extends JPanel {
private BreakpointHit breakpointHit;
private JCheckBox replaceStatementCheckBox;
private JScrollPane replacementSQLTextAreaScrollPane;
private SqlTextArea replacementSQLTextArea;
private BreakpointHit hit;
private JCheckBox replaceCheckbox;
private JScrollPane replacePane;
private SqlTextArea replaceTextArea;
public BreakpointHitEditor(final Debugger debugger, final DebuggerPane debuggerPane, final BreakpointHitNode breakpointHitNode) {
super(new BorderLayout());
@ -84,54 +89,59 @@ public class BreakpointHitEditor extends JPanel {
JPanel breakpointHitExecutionPane = new JPanel(new GridBagLayout());
breakpointHitExecutionPane.setBorder(BorderFactory.createEmptyBorder(2, 5, 5, 5));
breakpointHitExecutionPane.setOpaque(false);
breakpointHit = breakpointHitNode.getUserObject();
hit = breakpointHitNode.getUserObject();
int y = 0;
breakpointHitExecutionPane.add(new JLabel("Statement:"), new GridBagConstraints(0, y++, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
breakpointHitExecutionPane.add(new JLabel("Query:"), new GridBagConstraints(0, y++, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
SqlTextArea sqlTextArea = new SqlTextArea();
String sql = breakpointHit.getSql();
String parameterDescription = breakpointHit.getParameterDescription();
String sql = hit.getSQL();
String parameterDescription = hit.getParameterDescription();
if(parameterDescription != null) {
sql += "\n -> " + parameterDescription;
}
sqlTextArea.setText(sql + "\n");
sqlTextArea.setCaretPosition(0);
breakpointHitExecutionPane.add(new RTextScrollPane(sqlTextArea), new GridBagConstraints(0, y++, 1, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
if(breakpointHit.isBeforeExecution()) {
replaceStatementCheckBox = new JCheckBox("Replace with statement");
replaceStatementCheckBox.setOpaque(false);
replaceStatementCheckBox.addItemListener(new ItemListener() {
if(hit.isBeforeExecution()) {
replaceCheckbox = new JCheckBox("Replace with statement");
replaceCheckbox.setOpaque(false);
replaceCheckbox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
adjustStates();
}
});
breakpointHitExecutionPane.add(replaceStatementCheckBox, new GridBagConstraints(0, y++, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 0, 0, 0), 0, 0));
replacementSQLTextArea = new SqlTextArea();
replacementSQLTextAreaScrollPane = new RTextScrollPane(replacementSQLTextArea);
breakpointHitExecutionPane.add(replacementSQLTextAreaScrollPane, new GridBagConstraints(0, y++, 1, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 20, 0, 0), 0, 0));
breakpointHitExecutionPane.add(replaceCheckbox, new GridBagConstraints(0, y++, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 0, 0, 0), 0, 0));
replaceTextArea = new SqlTextArea();
replacePane = new RTextScrollPane(replaceTextArea);
breakpointHitExecutionPane.add(replacePane, new GridBagConstraints(0, y++, 1, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 20, 0, 0), 0, 0));
}
JPanel executionTypePane = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
// For now, this choice is not exposed.
executionTypePane.setVisible(breakpointHit.isBeforeExecution());
executionTypePane.setVisible(hit.isBeforeExecution());
executionTypePane.setOpaque(false);
ButtonGroup executionTypeGroup = new ButtonGroup();
final JRadioButton executeTypeNoneRadioButton = new JRadioButton("Execute");
executeTypeNoneRadioButton.setOpaque(false);
executeTypeNoneRadioButton.setSelected(true);
executionTypeGroup.add(executeTypeNoneRadioButton);
executionTypePane.add(executeTypeNoneRadioButton);
final JRadioButton executeTypeBreakRadioButton = new JRadioButton("Execute and break");
executeTypeBreakRadioButton.setOpaque(false);
executionTypeGroup.add(executeTypeBreakRadioButton);
executionTypePane.add(executeTypeBreakRadioButton);
final JRadioButton executeTypeSkipRadioButton = new JRadioButton("Skip");
executeTypeSkipRadioButton.setOpaque(false);
executionTypeGroup.add(executeTypeSkipRadioButton);
executionTypePane.add(executeTypeSkipRadioButton);
final JRadioButton executeTypeFailRadioButton = new JRadioButton("Throw exception");
executeTypeFailRadioButton.setOpaque(false);
executionTypeGroup.add(executeTypeFailRadioButton);
executionTypePane.add(executeTypeFailRadioButton);
breakpointHitExecutionPane.add(executionTypePane, new GridBagConstraints(0, y++, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 0, 0, 0), 0, 0));
JPanel buttonPane = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
buttonPane.setOpaque(false);
@ -141,23 +151,28 @@ public class BreakpointHitEditor extends JPanel {
applyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(breakpointHit.isBeforeExecution()) {
if (hit.isBeforeExecution()) {
String replacementSQL = null;
ExecutionType executionType = ExecutionType.RUN;
if(executeTypeNoneRadioButton.isSelected()) {
executionType = ExecutionType.RUN;
replacementSQL = replaceStatementCheckBox.isSelected()? replacementSQLTextArea.getText(): null;
} else if(executeTypeBreakRadioButton.isSelected()) {
executionType = ExecutionType.STEP_THROUGH;
replacementSQL = replaceStatementCheckBox.isSelected()? replacementSQLTextArea.getText(): null;
} else if(executeTypeSkipRadioButton.isSelected()) {
executionType = ExecutionType.RUN_OVER;
} else if(executeTypeFailRadioButton.isSelected()) {
executionType = ExecutionType.FAIL;
ExecutionType type = RUN;
if (executeTypeNoneRadioButton.isSelected()) {
type = RUN;
replacementSQL = replaceCheckbox.isSelected() ? replaceTextArea.getText() : null;
}
breakpointHit.setExecutionType(executionType, replacementSQL);
} else {
breakpointHit.setExecutionType(ExecutionType.RUN, null);
else if (executeTypeBreakRadioButton.isSelected()) {
type = STEP;
replacementSQL = replaceCheckbox.isSelected() ? replaceTextArea.getText() : null;
}
else if (executeTypeSkipRadioButton.isSelected()) {
type = SKIP;
}
else if (executeTypeFailRadioButton.isSelected()) {
type = FAIL;
}
hit.setExecutionType(type, replacementSQL);
}
else {
hit.setExecutionType(RUN, null);
}
debuggerPane.proceedBreakpointHit(breakpointHitNode);
}
@ -169,15 +184,15 @@ public class BreakpointHitEditor extends JPanel {
tabbedPane.addTab("Editor", new EditorsPane(new QueryExecutorCreator() {
@Override
public QueryExecutor createQueryExecutor() {
return debugger.createBreakpointHitStatementExecutor(breakpointHit.getThreadID());
return debugger.createBreakpointHitStatementExecutor(hit.getThreadID());
}
}, false));
add(tabbedPane);
}
private void adjustStates() {
if(breakpointHit.isBeforeExecution()) {
replacementSQLTextAreaScrollPane.setVisible(replaceStatementCheckBox.isSelected());
if(hit.isBeforeExecution()) {
replacePane.setVisible(replaceCheckbox.isSelected());
revalidate();
repaint();
}

View File

@ -334,7 +334,7 @@ public class DebuggerPane extends JPanel {
public void actionPerformed(ActionEvent e) {
StringWriter sw = new StringWriter();
Throwable throwable = new Exception("Statement Stack trace");
throwable.setStackTrace(breakpointHit.getCallerStackTraceElements());
throwable.setStackTrace(breakpointHit.getStackTrace());
throwable.printStackTrace(new PrintWriter(sw));
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(new StringSelection(sw.toString()), null);
@ -346,7 +346,7 @@ public class DebuggerPane extends JPanel {
@Override
public void actionPerformed(ActionEvent e) {
Throwable throwable = new Exception("Statement Stack trace");
throwable.setStackTrace(breakpointHit.getCallerStackTraceElements());
throwable.setStackTrace(breakpointHit.getStackTrace());
throwable.printStackTrace();
}
});
@ -418,8 +418,8 @@ public class DebuggerPane extends JPanel {
class BreakpointHitNode extends DefaultMutableTreeNode {
public BreakpointHitNode(BreakpointHit breakpointHit) {
super(breakpointHit);
StackTraceElement[] callerStackTraceElements = breakpointHit.getCallerStackTraceElements();
for(StackTraceElement stackTraceElement: callerStackTraceElements) {
StackTraceElement[] stackTrace = breakpointHit.getStackTrace();
for (StackTraceElement stackTraceElement : stackTrace) {
add(new StackTraceElementNode(stackTraceElement));
}
}

View File

@ -41,6 +41,11 @@ import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
/**
* A breakpoint that can be set onto a {@link Debugger}
* <p>
* Breakpoints can be set onto a {@link Debugger} in order to make the debugger
* break on certain events
*
* @author Christopher Deckers
* @author Lukas Eder
*/
@ -61,10 +66,16 @@ public class Breakpoint implements Serializable {
private transient AtomicInteger currentHitCount;
/**
* Create a new, empty breakpoint
*/
public Breakpoint() {
this(null, null, null, true, null, null, null);
}
/**
* Create an initialised breakpoint
*/
public Breakpoint(UUID id, Integer hitCount, QueryMatcher matcher, boolean isBreaking, QueryProcessor before,
QueryProcessor replace, QueryProcessor after) {
@ -88,10 +99,17 @@ public class Breakpoint implements Serializable {
this.after = after;
}
/**
* The breakpoints unique ID, which is used to identify it across remote
* systems.
*/
public UUID getID() {
return id;
}
/**
* A matcher used to match queries on which to break
*/
public QueryMatcher getMatcher() {
return matcher;
}
@ -140,10 +158,16 @@ public class Breakpoint implements Serializable {
}
}
/**
* The breakpoint's hit count
*/
public Integer getHitCount() {
return hitCount;
}
/**
* Whether the breakpoint is breaking
*/
public boolean isBreaking() {
return isBreaking;
}

View File

@ -39,35 +39,38 @@ package org.jooq.tools.debug;
import java.io.Serializable;
import java.util.UUID;
import org.jooq.Query;
/**
* @author Christopher Deckers
* @author Lukas Eder
*/
@SuppressWarnings("serial")
public class BreakpointHit implements Serializable {
public static enum ExecutionType {
STEP_THROUGH,
RUN_OVER,
RUN,
FAIL,
}
/**
* Generated UID
*/
private static final long serialVersionUID = -9202720101880051991L;
private boolean isBeforeExecution;
private UUID breakpointID;
private String sql;
private String parameterDescription;
private long threadID;
private String threadName;
private StackTraceElement[] callerStackTraceElements;
private final boolean isBeforeExecution;
private UUID breakpointID;
private String sql;
private final String parameterDescription;
private final long threadID;
private final String threadName;
private final StackTraceElement[] stackTrace;
public BreakpointHit(UUID breakpointID, String sql, String parameterDescription, long threadID, String threadName, StackTraceElement[] callerStackTraceElements, boolean isBeforeExecution) {
private ExecutionType executionType = ExecutionType.RUN;
public BreakpointHit(UUID breakpointID, String sql, String parameterDescription, long threadID, String threadName,
StackTraceElement[] callerStackTraceElements, boolean isBeforeExecution) {
this.isBeforeExecution = isBeforeExecution;
this.breakpointID = breakpointID;
this.sql = sql;
this.parameterDescription = parameterDescription;
this.threadID = threadID;
this.threadName = threadName;
this.callerStackTraceElements = callerStackTraceElements;
this.stackTrace = callerStackTraceElements;
}
public boolean isBeforeExecution() {
@ -75,34 +78,53 @@ public class BreakpointHit implements Serializable {
}
/**
* @return null if the breakpoint was processed and contains an execution type.
* @return null if the breakpoint was processed and contains an execution
* type.
*/
public UUID getBreakpointID() {
return breakpointID;
}
public String getSql() {
/**
* The SQL string of the {@link Query} that was suspended by this breakpoint hit
*/
public String getSQL() {
return sql;
}
/**
* The bind values of the {@link Query} that was suspended by this breakpoint hit
*/
public String getParameterDescription() {
return parameterDescription;
}
/**
* The ID of the thread which was suspended by this breakpoint hit
*/
public long getThreadID() {
return threadID;
}
/**
* The name of the thread which was suspended by this breakpoint hit
*/
public String getThreadName() {
return threadName;
}
public StackTraceElement[] getCallerStackTraceElements() {
return callerStackTraceElements;
/**
* The stacktrace of the thread which was suspended by this breakpoint hit
*/
public StackTraceElement[] getStackTrace() {
return stackTrace;
}
private ExecutionType executionType = ExecutionType.RUN;
/**
* @deprecated - {@link BreakpointHit} should be immutable. This subtle
* state-change has too much implicit semantics.
*/
@Deprecated
public void setExecutionType(ExecutionType executionType, String sql) {
this.breakpointID = null;
this.executionType = executionType;

View File

@ -0,0 +1,69 @@
/**
* Copyright (c) 2009-2012, Lukas Eder, lukas.eder@gmail.com
* Christopher Deckers, chrriis@gmail.com
* All rights reserved.
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOQ" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.jooq.tools.debug;
import org.jooq.Query;
/**
* The {@link Query} execution type which is applied to a query that was
* suspended by a {@link Breakpoint} on a {@link BreakpointHit}
*
* @author Christopher Deckers
* @author Lukas Eder
*/
public enum ExecutionType {
/**
* Execute the query
*/
RUN,
/**
* Execute the query and break again, after the query
*/
STEP,
/**
* Skip the query silently
*/
SKIP,
/**
* Fail the query by throwing an exception
*/
FAIL,
}

View File

@ -46,6 +46,7 @@ import org.jooq.tools.debug.Breakpoint;
import org.jooq.tools.debug.BreakpointHit;
import org.jooq.tools.debug.BreakpointHitHandler;
import org.jooq.tools.debug.Debugger;
import org.jooq.tools.debug.ExecutionType;
import org.jooq.tools.debug.LoggingListener;
import org.jooq.tools.debug.QueryExecutor;
import org.jooq.tools.debug.QueryLog;
@ -251,7 +252,7 @@ class ClientDebugger implements Debugger {
breakpointHitHandler.processBreakpointBeforeExecutionHit(hit);
if (hit.getBreakpointID() != null) {
// The breakpoint was not processed, so we process it here.
hit.setExecutionType(BreakpointHit.ExecutionType.RUN, null);
hit.setExecutionType(ExecutionType.RUN, null);
}
return hit;
}

View File

@ -51,8 +51,8 @@ import org.jooq.impl.DefaultExecuteListener;
import org.jooq.impl.Factory;
import org.jooq.tools.debug.Breakpoint;
import org.jooq.tools.debug.BreakpointHit;
import org.jooq.tools.debug.BreakpointHit.ExecutionType;
import org.jooq.tools.debug.Debugger;
import org.jooq.tools.debug.ExecutionType;
import org.jooq.tools.debug.LoggingListener;
import org.jooq.tools.debug.QueryInfo;
import org.jooq.tools.debug.QueryLog;
@ -227,7 +227,7 @@ public class DebugListener extends DefaultExecuteListener {
throw new RuntimeException(e);
}
}
ExecutionType executionType = BreakpointHit.ExecutionType.RUN;
ExecutionType executionType = ExecutionType.RUN;
if(hasBreakpointHitHandler) {
effectiveSQL = mainSQL != null? mainSQL: matchingSQL;
// TODO: find a way for the handler to replace the statement (not just step over).
@ -241,7 +241,7 @@ public class DebugListener extends DefaultExecuteListener {
// Breakpoint has an answer.
if(breakpointHit.getBreakpointID() == null) {
executionType = breakpointHit.getExecutionType();
String sql = breakpointHit.getSql();
String sql = breakpointHit.getSQL();
if(sql != null) {
effectiveSQL = sql;
matchingParameterDescription = null;
@ -256,14 +256,14 @@ public class DebugListener extends DefaultExecuteListener {
}
}
}
if(executionType != ExecutionType.STEP_THROUGH) {
if(executionType != ExecutionType.STEP) {
matchingDebugger = null;
}
switch(executionType) {
case FAIL: {
throw new DataAccessException("Failing SQL statement.");
}
case RUN_OVER: {
case SKIP: {
try {
ctx.statement().close();
// Better return possibility? Based on originating query?

View File

@ -115,21 +115,23 @@ class ServerDebugger extends LocalDebugger {
}
private void setBreakpointHitHandlerActive(boolean isActive) {
if(isActive) {
if (isActive) {
setBreakpointHitHandler(new BreakpointHitHandler() {
@Override
public void processBreakpointBeforeExecutionHit(BreakpointHit hit) {
BreakpointHit modifiedBreakpointHit = comm.syncSend(new CMC_processBreakpointBeforeExecutionHit(hit));
if(modifiedBreakpointHit != null) {
hit.setExecutionType(modifiedBreakpointHit.getExecutionType(), modifiedBreakpointHit.getSql());
BreakpointHit modified = comm.syncSend(new CMC_processBreakpointBeforeExecutionHit(hit));
if (modified != null) {
hit.setExecutionType(modified.getExecutionType(), modified.getSQL());
}
}
@Override
public void processBreakpointAfterExecutionHit(BreakpointHit hit) {
comm.syncSend(new CMC_processBreakpointAfterExecutionHit(hit));
}
});
} else {
}
else {
setBreakpointHitHandler(null);
}
}