Search in sources :

Example 11 with ClassFactory

use of org.apache.derby.iapi.services.loader.ClassFactory in project derby by apache.

the class GenericAggregateResultSet method getSortAggregators.

/**
 * For each AggregatorInfo in the list, generate a
 * GenericAggregator and stick it in an array of
 * GenericAggregators.
 *
 * @param list 	the list of aggregators to set up
 * @param eliminateDistincts	should distincts be ignored.
 *		Used to toss out distinct aggregates for a prelim
 *		sort.
 * @param lcc the lcc
 * @param inputResultSet the incoming result set
 *
 * @return the array of GenericAggregators
 *
 * @exception StandardException on error
 */
@SuppressWarnings("UseOfObsoleteCollectionType")
protected final GenericAggregator[] getSortAggregators(AggregatorInfoList list, boolean eliminateDistincts, LanguageConnectionContext lcc, NoPutResultSet inputResultSet) throws StandardException {
    GenericAggregator[] aggregators;
    Vector<GenericAggregator> tmpAggregators = new Vector<GenericAggregator>();
    ClassFactory cf = lcc.getLanguageConnectionFactory().getClassFactory();
    for (AggregatorInfo aggInfo : list) {
        if (!(eliminateDistincts && aggInfo.isDistinct())) // if (eliminateDistincts == aggInfo.isDistinct())
        {
            tmpAggregators.addElement(new GenericAggregator(aggInfo, cf));
        }
    }
    aggregators = new GenericAggregator[tmpAggregators.size()];
    tmpAggregators.copyInto(aggregators);
    return aggregators;
}
Also used : ClassFactory(org.apache.derby.iapi.services.loader.ClassFactory) Vector(java.util.Vector)

Example 12 with ClassFactory

use of org.apache.derby.iapi.services.loader.ClassFactory in project derby by apache.

the class ClassLoadingTest method test_01_6654.

/**
 * Test that you can't use Derby's custom class loader to load arbitrary
 * class files. See DERBY-6654.
 */
public void test_01_6654() throws Exception {
    ByteArray classBytes = new ByteArray(new byte[] { (byte) 1 });
    Connection conn = getConnection();
    LanguageConnectionContext lcc = ConstraintCharacteristicsTest.getLCC(conn);
    ClassFactory classFactory = lcc.getLanguageConnectionFactory().getClassFactory();
    String className1 = "BadClassName";
    String className2 = "bad.class.Name";
    vet6654(classFactory, className1, classBytes);
    vet6654(classFactory, className2, classBytes);
}
Also used : ClassFactory(org.apache.derby.iapi.services.loader.ClassFactory) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) Connection(java.sql.Connection) EmbedConnection(org.apache.derby.impl.jdbc.EmbedConnection) ByteArray(org.apache.derby.iapi.util.ByteArray)

Example 13 with ClassFactory

use of org.apache.derby.iapi.services.loader.ClassFactory in project derby by apache.

the class UserAggregateDefinition method getAggregator.

/**
 * Determines the result datatype and verifies that the input datatype is correct.
 *
 * @param inputType	the input type
 * @param aggregatorClass (Output arg) the name of the Derby execution-time class which wraps the aggregate logic
 *
 * @return the result type of the user-defined aggregator
 */
public final DataTypeDescriptor getAggregator(DataTypeDescriptor inputType, StringBuffer aggregatorClass) throws StandardException {
    try {
        CompilerContext cc = (CompilerContext) QueryTreeNode.getContext(CompilerContext.CONTEXT_ID);
        ClassFactory classFactory = cc.getClassFactory();
        TypeCompilerFactory tcf = cc.getTypeCompilerFactory();
        Class<?> derbyAggregatorInterface = classFactory.loadApplicationClass("org.apache.derby.agg.Aggregator");
        Class<?> userAggregatorClass = classFactory.loadApplicationClass(_alias.getJavaClassName());
        Class[][] typeBounds = classFactory.getClassInspector().getTypeBounds(derbyAggregatorInterface, userAggregatorClass);
        if ((typeBounds == null) || (typeBounds.length != AGGREGATOR_PARAM_COUNT) || (typeBounds[INPUT_TYPE] == null) || (typeBounds[RETURN_TYPE] == null)) {
            throw StandardException.newException(SQLState.LANG_ILLEGAL_UDA_CLASS, _alias.getSchemaName(), _alias.getName(), userAggregatorClass.getName());
        }
        Class<?>[] genericParameterTypes = classFactory.getClassInspector().getGenericParameterTypes(derbyAggregatorInterface, userAggregatorClass);
        if (genericParameterTypes == null) {
            genericParameterTypes = new Class<?>[AGGREGATOR_PARAM_COUNT];
        }
        AggregateAliasInfo aai = (AggregateAliasInfo) _alias.getAliasInfo();
        DataTypeDescriptor expectedInputType = DataTypeDescriptor.getType(aai.getForType());
        DataTypeDescriptor expectedReturnType = DataTypeDescriptor.getType(aai.getReturnType());
        Class<?> expectedInputClass = getJavaClass(classFactory, expectedInputType);
        Class<?> expectedReturnClass = getJavaClass(classFactory, expectedReturnType);
        // the input operand must be coercible to the expected input type of the aggregate
        if (!tcf.getTypeCompiler(expectedInputType.getTypeId()).storable(inputType.getTypeId(), classFactory)) {
            return null;
        }
        // 
        // Make sure that the declared input type of the UDA actually falls within
        // the type bounds of the Aggregator implementation.
        // 
        Class[] inputBounds = typeBounds[INPUT_TYPE];
        for (int i = 0; i < inputBounds.length; i++) {
            vetCompatibility((Class<?>) inputBounds[i], expectedInputClass, SQLState.LANG_UDA_WRONG_INPUT_TYPE);
        }
        if (genericParameterTypes[INPUT_TYPE] != null) {
            vetCompatibility(genericParameterTypes[INPUT_TYPE], expectedInputClass, SQLState.LANG_UDA_WRONG_INPUT_TYPE);
        }
        // 
        // Make sure that the declared return type of the UDA actually falls within
        // the type bounds of the Aggregator implementation.
        // 
        Class[] returnBounds = typeBounds[RETURN_TYPE];
        for (int i = 0; i < returnBounds.length; i++) {
            vetCompatibility(returnBounds[i], expectedReturnClass, SQLState.LANG_UDA_WRONG_RETURN_TYPE);
        }
        if (genericParameterTypes[RETURN_TYPE] != null) {
            vetCompatibility(genericParameterTypes[RETURN_TYPE], expectedReturnClass, SQLState.LANG_UDA_WRONG_RETURN_TYPE);
        }
        aggregatorClass.append(ClassName.UserDefinedAggregator);
        return expectedReturnType;
    } catch (ClassNotFoundException cnfe) {
        throw aggregatorInstantiation(cnfe);
    }
}
Also used : ClassFactory(org.apache.derby.iapi.services.loader.ClassFactory) TypeCompilerFactory(org.apache.derby.iapi.sql.compile.TypeCompilerFactory) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) CompilerContext(org.apache.derby.iapi.sql.compile.CompilerContext) AggregateAliasInfo(org.apache.derby.catalog.types.AggregateAliasInfo)

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