Search in sources :

Example 1 with SQLMethod

use of org.datanucleus.store.rdbms.sql.method.SQLMethod in project datanucleus-rdbms by datanucleus.

the class SQLExpressionFactory method getMethod.

/**
 * Accessor for the method defined by the class/method names and supplied args.
 * Throws a NucleusException is the method is not supported.
 * Note that if the class name passed in is not for a listed class with that method defined then will check all remaining defined methods for a superclass.
 * @param className Class we are invoking the method on
 * @param methodName Name of the method
 * @param args Any arguments to the method call (ignored currently) TODO Check the arguments
 * @return The method
 */
protected SQLMethod getMethod(String className, String methodName, List args) {
    String datastoreId = storeMgr.getDatastoreAdapter().getVendorID();
    // Try to find datastore-dependent evaluator for class+method
    MethodKey methodKey1 = getSQLMethodKey(datastoreId, className, methodName);
    MethodKey methodKey2 = null;
    SQLMethod method = sqlMethodsByKey.get(methodKey1);
    if (method == null) {
        // Try to find datastore-independent evaluator for class+method
        methodKey2 = getSQLMethodKey(null, className, methodName);
        method = sqlMethodsByKey.get(methodKey2);
    }
    if (method != null) {
        return method;
    }
    // No existing instance, so check the built-in SQLMethods from DatastoreAdapter
    Class sqlMethodCls = storeMgr.getDatastoreAdapter().getSQLMethodClass(className, methodName, clr);
    if (sqlMethodCls != null) {
        // Built-in SQLMethod found, so instantiate it, cache it and return it
        try {
            method = (SQLMethod) sqlMethodCls.getDeclaredConstructor().newInstance();
            MethodKey key = getSQLMethodKey(datastoreId, className, methodName);
            sqlMethodsByKey.put(key, method);
            return method;
        } catch (Exception e) {
            throw new NucleusException("Error creating SQLMethod of type " + sqlMethodCls.getName() + " for class=" + className + " method=" + methodName);
        }
    }
    // Check the plugin mechanism
    // 1). Try datastore-dependent key
    boolean datastoreDependent = true;
    if (!pluginSqlMethodsKeysSupported.contains(methodKey1)) {
        // 2). No datastore-dependent method, so try a datastore-independent key
        datastoreDependent = false;
        if (!pluginSqlMethodsKeysSupported.contains(methodKey2)) {
            // Not listed as supported for this particular class+method, so maybe is for a superclass
            boolean unsupported = true;
            if (!StringUtils.isWhitespace(className)) {
                Class cls = clr.classForName(className);
                // Try datastore-dependent
                for (MethodKey methodKey : pluginSqlMethodsKeysSupported) {
                    if (methodKey.methodName.equals(methodName) && methodKey.datastoreName.equals(datastoreId)) {
                        Class methodCls = null;
                        try {
                            methodCls = clr.classForName(methodKey.clsName);
                        } catch (ClassNotResolvedException cnre) {
                        // Maybe generic array support?
                        }
                        if (methodCls != null && methodCls.isAssignableFrom(cls)) {
                            // This one is usable here, for superclass
                            method = sqlMethodsByKey.get(methodKey);
                            if (method != null) {
                                MethodKey superMethodKey = new MethodKey();
                                superMethodKey.clsName = className;
                                superMethodKey.methodName = methodKey.methodName;
                                superMethodKey.datastoreName = methodKey.datastoreName;
                                // Cache the same method under this class also
                                sqlMethodsByKey.put(superMethodKey, method);
                                return method;
                            }
                            className = methodKey.clsName;
                            datastoreId = methodKey.datastoreName;
                            datastoreDependent = true;
                            unsupported = false;
                            break;
                        }
                    }
                }
                if (unsupported) {
                    // Try datastore-independent
                    for (MethodKey methodKey : pluginSqlMethodsKeysSupported) {
                        if (methodKey.methodName.equals(methodName) && methodKey.datastoreName.equals("ALL")) {
                            Class methodCls = null;
                            try {
                                methodCls = clr.classForName(methodKey.clsName);
                            } catch (ClassNotResolvedException cnre) {
                            // Maybe generic array support?
                            }
                            if (methodCls != null && methodCls.isAssignableFrom(cls)) {
                                // This one is usable here, for superclass
                                method = sqlMethodsByKey.get(methodKey);
                                if (method != null) {
                                    MethodKey superMethodKey = new MethodKey();
                                    superMethodKey.clsName = className;
                                    superMethodKey.methodName = methodKey.methodName;
                                    superMethodKey.datastoreName = methodKey.datastoreName;
                                    // Cache the same method under this class also
                                    sqlMethodsByKey.put(superMethodKey, method);
                                    return method;
                                }
                                className = methodKey.clsName;
                                datastoreId = methodKey.datastoreName;
                                datastoreDependent = false;
                                unsupported = false;
                                break;
                            }
                        }
                    }
                }
            }
            if (unsupported) {
                if (className != null) {
                    throw new NucleusUserException(Localiser.msg("060008", methodName, className));
                }
                throw new NucleusUserException(Localiser.msg("060009", methodName));
            }
        }
    }
    // Fallback to plugin lookup of class+method[+datastore]
    PluginManager pluginMgr = storeMgr.getNucleusContext().getPluginManager();
    String[] attrNames = (datastoreDependent ? new String[] { "class", "method", "datastore" } : new String[] { "class", "method" });
    String[] attrValues = (datastoreDependent ? new String[] { className, methodName, datastoreId } : new String[] { className, methodName });
    try {
        method = (SQLMethod) pluginMgr.createExecutableExtension("org.datanucleus.store.rdbms.sql_method", attrNames, attrValues, "evaluator", new Class[] {}, new Object[] {});
        // Register the method
        sqlMethodsByKey.put(getSQLMethodKey(datastoreDependent ? datastoreId : null, className, methodName), method);
        return method;
    } catch (Exception e) {
        throw new NucleusUserException(Localiser.msg("060011", "class=" + className + " method=" + methodName), e);
    }
}
Also used : PluginManager(org.datanucleus.plugin.PluginManager) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) SQLMethod(org.datanucleus.store.rdbms.sql.method.SQLMethod) NucleusException(org.datanucleus.exceptions.NucleusException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) NucleusException(org.datanucleus.exceptions.NucleusException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException)

Aggregations

ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 PluginManager (org.datanucleus.plugin.PluginManager)1 SQLMethod (org.datanucleus.store.rdbms.sql.method.SQLMethod)1