Search in sources :

Example 1 with ClassFactory

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);
    }
}
Also used : ClassFactory(org.apache.derby.iapi.services.loader.ClassFactory) OptionalTool(org.apache.derby.iapi.sql.dictionary.OptionalTool) StandardException(org.apache.derby.shared.common.error.StandardException) ClassFactoryContext(org.apache.derby.iapi.services.loader.ClassFactoryContext)

Example 2 with ClassFactory

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);
}
Also used : GeneratedClass(org.apache.derby.iapi.services.loader.GeneratedClass) ClassFactory(org.apache.derby.iapi.services.loader.ClassFactory) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext)

Example 3 with ClassFactory

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;
}
Also used : ClassFactory(org.apache.derby.iapi.services.loader.ClassFactory) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) CompilerContext(org.apache.derby.iapi.sql.compile.CompilerContext) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) AliasDescriptor(org.apache.derby.iapi.sql.dictionary.AliasDescriptor)

Example 4 with ClassFactory

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;
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) ClassFactory(org.apache.derby.iapi.services.loader.ClassFactory) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor)

Example 5 with ClassFactory

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);
}
Also used : ClassFactory(org.apache.derby.iapi.services.loader.ClassFactory) OptTrace(org.apache.derby.iapi.sql.compile.OptTrace) ClassFactoryContext(org.apache.derby.iapi.services.loader.ClassFactoryContext)

Aggregations

ClassFactory (org.apache.derby.iapi.services.loader.ClassFactory)13 DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)5 TypeId (org.apache.derby.iapi.types.TypeId)4 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)3 ClassFactoryContext (org.apache.derby.iapi.services.loader.ClassFactoryContext)2 CompilerContext (org.apache.derby.iapi.sql.compile.CompilerContext)2 DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)2 Connection (java.sql.Connection)1 Properties (java.util.Properties)1 Vector (java.util.Vector)1 UUID (org.apache.derby.catalog.UUID)1 AggregateAliasInfo (org.apache.derby.catalog.types.AggregateAliasInfo)1 StatisticsImpl (org.apache.derby.catalog.types.StatisticsImpl)1 CacheFactory (org.apache.derby.iapi.services.cache.CacheFactory)1 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)1 GeneratedClass (org.apache.derby.iapi.services.loader.GeneratedClass)1 OptTrace (org.apache.derby.iapi.sql.compile.OptTrace)1 TypeCompilerFactory (org.apache.derby.iapi.sql.compile.TypeCompilerFactory)1 DependencyManager (org.apache.derby.iapi.sql.depend.DependencyManager)1 AliasDescriptor (org.apache.derby.iapi.sql.dictionary.AliasDescriptor)1