use of org.apache.derby.iapi.sql.compile.OptTrace in project derby by apache.
the class OptimizerTracer method loadTool.
// /////////////////////////////////////////////////////////////////////////////////
//
// OptionalTool BEHAVIOR
//
// /////////////////////////////////////////////////////////////////////////////////
/**
* <p>
* Turns on optimizer tracing. May take optional parameters:
* </p>
*
* <ul>
* <li>xml - If the first arg is the "xml" literal, then trace output will be
* formatted as xml.</li>
* <li>custom, $class - If the first arg is the "custom" literal, then the next arg must be
* the name of a class which implements org.apache.derby.iapi.sql.compile.OptTrace
* and which has a 0-arg constructor. The 0-arg constructor is called and the resulting
* OptTrace object is plugged in to trace the optimizer.</li>
* </ul>
*/
public void loadTool(String... configurationParameters) throws SQLException {
OptTrace tracer;
if ((configurationParameters == null) || (configurationParameters.length == 0)) {
tracer = new DefaultOptTrace();
} else if ("xml".equals(configurationParameters[0])) {
try {
tracer = new XMLOptTrace();
} catch (Throwable t) {
throw wrap(t);
}
} else if ("custom".equals(configurationParameters[0])) {
if (configurationParameters.length != 2) {
throw wrap(MessageService.getTextMessage(SQLState.LANG_BAD_OPTIONAL_TOOL_ARGS));
}
String customOptTraceName = configurationParameters[1];
try {
ClassFactoryContext cfc = (ClassFactoryContext) getContext(ClassFactoryContext.CONTEXT_ID);
ClassFactory classFactory = cfc.getClassFactory();
Class<?> clazz = classFactory.loadApplicationClass(customOptTraceName);
tracer = (OptTrace) clazz.getConstructor().newInstance();
} catch (InstantiationException cnfe) {
throw cantInstantiate(customOptTraceName);
} catch (ClassNotFoundException cnfe) {
throw cantInstantiate(customOptTraceName);
} catch (IllegalAccessException cnfe) {
throw cantInstantiate(customOptTraceName);
} catch (NoSuchMethodException cnfe) {
throw cantInstantiate(customOptTraceName);
} catch (java.lang.reflect.InvocationTargetException cnfe) {
throw cantInstantiate(customOptTraceName);
} catch (Throwable t) {
throw wrap(t);
}
} else {
throw wrap(MessageService.getTextMessage(SQLState.LANG_BAD_OPTIONAL_TOOL_ARGS));
}
OptimizerTrace.setOptimizerTracer(tracer);
}
use of org.apache.derby.iapi.sql.compile.OptTrace in project derby by apache.
the class OptimizerTracer method unloadTool.
/**
* <p>
* Print the optimizer trace and turn off tracing. Takes optional parameters:
* </p>
*
* <ul>
* <li><b>fileName</b> - Where to write the optimizer trace. If omitted, the trace is written to System.out.</li>
* </ul>
*/
public void unloadTool(final String... configurationParameters) throws SQLException {
try {
final OptTrace tracer = OptimizerTrace.getOptimizerTracer();
boolean needsClosing = false;
PrintWriter pw;
if ((configurationParameters != null) && (configurationParameters.length > 0)) {
pw = AccessController.doPrivileged(new PrivilegedExceptionAction<PrintWriter>() {
public PrintWriter run() throws SQLException {
try {
String fileName = configurationParameters[0];
File outputFile = new File(fileName);
if (outputFile.exists()) {
throw PublicAPI.wrapStandardException(StandardException.newException(SQLState.DATA_FILE_EXISTS, fileName));
}
return new PrintWriter(outputFile);
} catch (IOException ioe) {
throw new IllegalArgumentException(ioe.getMessage(), ioe);
}
}
});
needsClosing = true;
} else {
pw = new PrintWriter(System.out);
}
if (tracer != null) {
tracer.printToWriter(pw);
pw.flush();
}
if (needsClosing) {
pw.close();
}
} catch (Exception e) {
if (e.getMessage() == null) {
Throwable cause = e.getCause();
if ((cause != null) && (cause instanceof SQLException)) {
throw (SQLException) cause;
}
}
throw wrap(e);
} finally {
OptimizerTrace.setOptimizerTracer(null);
}
}
use of org.apache.derby.iapi.sql.compile.OptTrace in project derby by apache.
the class OptimizerTrace method getOptimizerTraceOutput.
/**
* Get the optimizer trace output for the last optimized query as a String.
*
* @return The optimizer trace output for the last optimized query as a String.
* Null will be returned if optimizer trace output is off or not supported
* or no trace output was found or an exception occurred.
*/
public static String getOptimizerTraceOutput() {
String retCode = null;
try {
// Get the current language connection context. This is associated
// with the current database.
LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC();
OptTrace tracer = lcc.getOptimizerTracer();
if (tracer != null) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
tracer.printToWriter(pw);
pw.flush();
sw.flush();
retCode = sw.toString();
}
} catch (Throwable t) {
// eat all exceptions, simply return null
}
return retCode;
}
use of org.apache.derby.iapi.sql.compile.OptTrace in project derby by apache.
the class OptimizerTrace method setOptimizerTrace.
/**
* Turn default optimizer tracing on or off.
*
* @param onOrOff Whether to turn optimizer tracing on (true) or off (false).
*/
public static void setOptimizerTrace(boolean onOrOff) {
OptTrace optimizerTracer = onOrOff ? new DefaultOptTrace() : null;
setOptimizerTracer(optimizerTracer);
}
Aggregations