use of org.apache.derby.iapi.services.loader.ClassFactory in project derby by apache.
the class Java5SystemProcedures method SYSCS_REGISTER_TOOL.
// /////////////////////////////////////////////////////////////////////////////////
//
// PUBLIC BEHAVIOR
//
// /////////////////////////////////////////////////////////////////////////////////
/**
* <p>
* Load or unload an optional tool package. If the tool name is the special
* CUSTOM_TOOL_CLASS_NAME tool, then the first optionalArg is the name
* of a user-supplied class which implements OptionalTool.
* </p>
*
* @param toolName Name of the tool package.
* @param register True if the package should be loaded, false otherwise.
* @param optionalArgs Tool-specific configuration parameters.
*/
public static void SYSCS_REGISTER_TOOL(String toolName, boolean register, String... optionalArgs) throws SQLException {
try {
ClassFactoryContext cfc = (ClassFactoryContext) getContext(ClassFactoryContext.CONTEXT_ID);
ClassFactory classFactory = cfc.getClassFactory();
String toolClassName = findToolClassName(toolName, optionalArgs);
OptionalTool tool = null;
Class<?> toolClass;
try {
toolClass = classFactory.loadApplicationClass(toolClassName);
} catch (ClassNotFoundException cnfe) {
throw wrap(cnfe);
}
if (!OptionalTool.class.isAssignableFrom(toolClass)) {
throw badCustomTool(toolClassName);
}
try {
tool = (OptionalTool) toolClass.getConstructor().newInstance();
} catch (InstantiationException ie) {
throw wrap(ie);
} catch (IllegalAccessException iae) {
throw wrap(iae);
} catch (NoSuchMethodException ie) {
throw wrap(ie);
} catch (java.lang.reflect.InvocationTargetException iae) {
throw wrap(iae);
}
// Strip the custom tool class name from the optional args as necessary
if (CUSTOM_TOOL_CLASS_NAME.equals(toolName)) {
optionalArgs = stripCustomClassName(optionalArgs);
}
if (register) {
tool.loadTool(optionalArgs);
} else {
tool.unloadTool(optionalArgs);
}
} catch (StandardException se) {
throw PublicAPI.wrapStandardException(se);
}
}
use of org.apache.derby.iapi.services.loader.ClassFactory in project derby by apache.
the class GenericStorablePreparedStatement method loadGeneratedClass.
// ///////////////////////////////////////////////////////////
//
// STORABLEPREPAREDSTATEMENT INTERFACE
//
// ///////////////////////////////////////////////////////////
/**
* Load up the class from the saved bytes.
*
* @exception StandardException on error
*/
public void loadGeneratedClass() throws StandardException {
LanguageConnectionContext lcc = (LanguageConnectionContext) getContext(LanguageConnectionContext.CONTEXT_ID);
ClassFactory classFactory = lcc.getLanguageConnectionFactory().getClassFactory();
GeneratedClass gc = classFactory.loadGeneratedClass(className, byteCode);
/*
** No special try catch logic to write out bad classes
** here. We don't expect any problems, and in any
** event, we don't have the class builder available
** here.
*/
setActivationClass(gc);
}
use of org.apache.derby.iapi.services.loader.ClassFactory in project derby by apache.
the class AggregateNode method bindExpression.
/**
* Bind this operator. Determine the type of the subexpression,
* and pass that into the UserAggregate.
*
* @param fromList The query's FROM list
* @param subqueryList The subquery list being built as we find SubqueryNodes
* @param aggregates The aggregate list being built as we find AggregateNodes
*
* @return The new top of the expression tree.
*
* @exception StandardException Thrown on error
*/
@Override
ValueNode bindExpression(FromList fromList, SubqueryList subqueryList, List<AggregateNode> aggregates) throws StandardException {
DataDictionary dd = getDataDictionary();
DataTypeDescriptor dts = null;
ClassFactory cf;
cf = getClassFactory();
classInspector = cf.getClassInspector();
boolean noSchema = true;
if (userAggregateName != null) {
noSchema = (userAggregateName.getSchemaName() == null);
userAggregateName.bind();
}
// bind it now.
if (userAggregateName != null && uad == null) {
String schemaName = userAggregateName.getSchemaName();
AliasDescriptor ad = resolveAggregate(dd, getSchemaDescriptor(schemaName, true), userAggregateName.getTableName(), noSchema);
if (ad == null) {
throw StandardException.newException(SQLState.LANG_OBJECT_NOT_FOUND, AliasDescriptor.getAliasType(AliasInfo.ALIAS_TYPE_AGGREGATE_AS_CHAR), userAggregateName.getTableName());
}
setUserDefinedAggregate(new UserAggregateDefinition(ad));
aggregateName = ad.getJavaClassName();
}
instantiateAggDef();
// if this is a user-defined aggregate
if (isUserDefinedAggregate()) {
AliasDescriptor ad = ((UserAggregateDefinition) uad).getAliasDescriptor();
boolean isModernBuiltinAggregate = SchemaDescriptor.STD_SYSTEM_SCHEMA_NAME.equals(ad.getSchemaName());
if (distinct && isModernBuiltinAggregate) {
throw StandardException.newException(SQLState.LANG_BAD_DISTINCT_AGG);
}
// set up dependency on the user-defined aggregate and compile a check for USAGE
// priv if needed
getCompilerContext().createDependency(ad);
if (isPrivilegeCollectionRequired()) {
//
if (!isModernBuiltinAggregate) {
getCompilerContext().addRequiredUsagePriv(ad);
}
}
}
// Add ourselves to the list of aggregates before we do anything else.
aggregates.add(this);
CompilerContext cc = getCompilerContext();
// operand being null means a count(*)
if (operand != null) {
int previousReliability = orReliability(CompilerContext.AGGREGATE_RESTRICTION);
bindOperand(fromList, subqueryList, aggregates);
cc.setReliability(previousReliability);
/*
** Make sure that we don't have an aggregate
** IMMEDIATELY below us. Don't search below
** any ResultSetNodes.
*/
HasNodeVisitor visitor = new HasNodeVisitor(this.getClass(), ResultSetNode.class);
operand.accept(visitor);
if (visitor.hasNode()) {
throw StandardException.newException(SQLState.LANG_USER_AGGREGATE_CONTAINS_AGGREGATE, getSQLName());
}
// Also forbid any window function inside an aggregate unless in
// subquery, cf. SQL 2003, section 10.9, SR 7 a).
SelectNode.checkNoWindowFunctions(operand, aggregateName);
/*
** Check the type of the operand. Make sure that the user
** defined aggregate can handle the operand datatype.
*/
dts = operand.getTypeServices();
/* Convert count(nonNullableColumn) to count(*) */
if (uad instanceof CountAggregateDefinition && !dts.isNullable()) {
setOperator(aggregateName);
setMethodName(aggregateName);
}
/*
** If we have a distinct, then the value expression
** MUST implement Orderable because we are going
** to process it using it as part of a sort.
*/
if (distinct) {
/*
** For now, we check to see if orderable() returns
** true for this type. In the future we may need
** to check to see if the type implements Orderable
**
*/
if (!operand.getTypeId().orderable(cf)) {
throw StandardException.newException(SQLState.LANG_COLUMN_NOT_ORDERABLE_DURING_EXECUTION, dts.getTypeId().getSQLTypeName());
}
}
/*
** Don't allow an untyped null
*/
if (operand instanceof UntypedNullConstantNode) {
throw StandardException.newException(SQLState.LANG_USER_AGGREGATE_BAD_TYPE_NULL, getSQLName());
}
}
/*
** Ask the aggregate definition whether it can handle
** the input datatype.
*/
aggregatorClassName = new StringBuffer();
DataTypeDescriptor resultType = uad.getAggregator(dts, aggregatorClassName);
if (resultType == null) {
throw StandardException.newException(SQLState.LANG_USER_AGGREGATE_BAD_TYPE, getSQLName(), operand.getTypeId().getSQLTypeName());
}
// coerced to the expected input type of the aggregator.
if (isUserDefinedAggregate()) {
ValueNode castNode = ((UserAggregateDefinition) uad).castInputValue(operand, getContextManager());
if (castNode != null) {
operand = castNode.bindExpression(fromList, subqueryList, aggregates);
}
}
checkAggregatorClassName(aggregatorClassName.toString());
setType(resultType);
return this;
}
use of org.apache.derby.iapi.services.loader.ClassFactory in project derby by apache.
the class InListOperatorNode method getDominantType.
/**
* Get the dominant type of all the operands in this IN list.
* @return the type descriptor for the dominant type
* @see DataTypeDescriptor#getDominantType(DataTypeDescriptor, ClassFactory)
*/
private DataTypeDescriptor getDominantType() {
DataTypeDescriptor targetType = leftOperand.getTypeServices();
TypeId judgeTypeId = targetType.getTypeId();
if (!rightOperandList.allSamePrecendence(judgeTypeId.typePrecedence())) {
// Iterate through the entire list of values to find out
// what the dominant type is.
ClassFactory cf = getClassFactory();
for (ValueNode vn : rightOperandList) {
targetType = targetType.getDominantType(vn.getTypeServices(), cf);
}
}
return targetType;
}
use of org.apache.derby.iapi.services.loader.ClassFactory 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);
}
Aggregations