Search in sources :

Example 1 with ClassFactoryContext

use of org.apache.derby.iapi.services.loader.ClassFactoryContext 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 ClassFactoryContext

use of org.apache.derby.iapi.services.loader.ClassFactoryContext 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)

Example 3 with ClassFactoryContext

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

the class ClassLoaderLock method lockClassLoader.

private boolean lockClassLoader(ShExQual qualifier) throws StandardException {
    if (lf == null)
        return false;
    ClassFactoryContext cfc = (ClassFactoryContext) getContextOrNull(ClassFactoryContext.CONTEXT_ID);
    // This method can be called from outside of the database
    // engine, in which case tc will be null. In that case
    // we lock the class loader only for the duration of
    // the loadClass().
    CompatibilitySpace lockSpace = null;
    if (cfc != null) {
        lockSpace = cfc.getLockSpace();
    }
    if (lockSpace == null)
        lockSpace = compat;
    Object lockGroup = lockSpace.getOwner();
    lf.lockObject(lockSpace, lockGroup, classLoaderLock, qualifier, C_LockFactory.TIMED_WAIT);
    return (lockGroup == this);
}
Also used : ClassFactoryContext(org.apache.derby.iapi.services.loader.ClassFactoryContext) CompatibilitySpace(org.apache.derby.iapi.services.locks.CompatibilitySpace)

Example 4 with ClassFactoryContext

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

the class ClassLoaderLock method getClasspath.

private String getClasspath() throws StandardException {
    ClassFactoryContext cfc = (ClassFactoryContext) getContextOrNull(ClassFactoryContext.CONTEXT_ID);
    PersistentSet ps = cfc.getPersistentSet();
    String classpath = PropertyUtil.getServiceProperty(ps, Property.DATABASE_CLASSPATH);
    // yet have one we make one up.
    if (classpath == null)
        classpath = "";
    return classpath;
}
Also used : ClassFactoryContext(org.apache.derby.iapi.services.loader.ClassFactoryContext) PersistentSet(org.apache.derby.iapi.services.property.PersistentSet)

Example 5 with ClassFactoryContext

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

the class ClassLoaderLock method getJarReader.

JarReader getJarReader() {
    if (jarReader == null) {
        ClassFactoryContext cfc = (ClassFactoryContext) getContextOrNull(ClassFactoryContext.CONTEXT_ID);
        jarReader = cfc.getJarReader();
    }
    return jarReader;
}
Also used : ClassFactoryContext(org.apache.derby.iapi.services.loader.ClassFactoryContext)

Aggregations

ClassFactoryContext (org.apache.derby.iapi.services.loader.ClassFactoryContext)5 ClassFactory (org.apache.derby.iapi.services.loader.ClassFactory)2 CompatibilitySpace (org.apache.derby.iapi.services.locks.CompatibilitySpace)1 PersistentSet (org.apache.derby.iapi.services.property.PersistentSet)1 OptTrace (org.apache.derby.iapi.sql.compile.OptTrace)1 OptionalTool (org.apache.derby.iapi.sql.dictionary.OptionalTool)1 StandardException (org.apache.derby.shared.common.error.StandardException)1