Search in sources :

Example 11 with FunctionDefinition

use of com.dexels.navajo.expression.api.FunctionDefinition in project navajo by Dexels.

the class JarFunctionFactory method init.

@Override
public void init() {
    Map<String, FunctionDefinition> fuds = getDefaultConfig();
    if (fuds == null) {
        fuds = new HashMap<>();
        setDefaultConfig(fuds);
    }
    ClassLoader myClassLoader = null;
    if (DispatcherFactory.getInstance() != null) {
        myClassLoader = DispatcherFactory.getInstance().getNavajoConfig().getClassloader();
    } else {
        myClassLoader = getClass().getClassLoader();
    }
    try {
        Iterator<?> iter = java.util.ServiceLoader.load(Class.forName("navajo.ExtensionDefinition", true, myClassLoader), myClassLoader).iterator();
        while (iter.hasNext()) {
            ExtensionDefinition ed = (ExtensionDefinition) iter.next();
            readDefinitionFile(fuds, ed);
        }
    } catch (Throwable e) {
        logger.debug("ServiceLookup failed. Normal in OSGi environment", e);
        if (!Version.osgiActive()) {
            logger.error("But OSGi isn't active, so something is definitely wrong.", e);
        }
    }
}
Also used : ExtensionDefinition(navajo.ExtensionDefinition) FunctionDefinition(com.dexels.navajo.expression.api.FunctionDefinition)

Example 12 with FunctionDefinition

use of com.dexels.navajo.expression.api.FunctionDefinition in project navajo by Dexels.

the class JarFunctionFactory method parseAdapters.

public void parseAdapters(Map<String, FunctionDefinition> fuds, ExtensionDefinition fd, XMLElement element) {
    String name = element.getElementByTagName("tagname").getContent();
    String className = element.getElementByTagName("object").getContent();
    if (fd != null) {
        FunctionDefinition functionDefinition = new FunctionDefinition(className, null, null, null);
        getAdapterConfig(fd).put(name, functionDefinition);
        try {
            MapMetaData.getInstance().addMapDefinition(element);
        } catch (Exception e) {
            logger.error("Error: ", e);
        }
    } else {
        throw new UnsupportedOperationException("Can not register adapter (pre-OSGi) without a ExtensionDefinition.");
    }
}
Also used : FunctionDefinition(com.dexels.navajo.expression.api.FunctionDefinition)

Example 13 with FunctionDefinition

use of com.dexels.navajo.expression.api.FunctionDefinition in project navajo by Dexels.

the class FunctionFactoryInterface method getInstance.

// public void clearAdapterNames() {
// adapterConfig.clear();
// }
@SuppressWarnings("unchecked")
public FunctionInterface getInstance(final ClassLoader cl, final String functionName) {
    try {
        FunctionDefinition fd = getDef(functionName);
        if (fd == null) {
            logger.error("Missing function definition: {}", functionName);
            return null;
        }
        Class<FunctionInterface> myClass = (Class<FunctionInterface>) Class.forName(fd.getObject(), true, cl);
        FunctionInterface fi = myClass.getDeclaredConstructor().newInstance();
        fi.setDefinition(fd);
        if (!fi.isInitialized()) {
            fi.setTypes(fd.getInputParams(), fd.getResultParam());
        }
        return fi;
    } catch (Exception e) {
        logger.error("Function: " + functionName + " not found!", e);
        return null;
    }
}
Also used : FunctionInterface(com.dexels.navajo.expression.api.FunctionInterface) FunctionDefinition(com.dexels.navajo.expression.api.FunctionDefinition) InvocationTargetException(java.lang.reflect.InvocationTargetException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) UserException(com.dexels.navajo.script.api.UserException)

Example 14 with FunctionDefinition

use of com.dexels.navajo.expression.api.FunctionDefinition in project navajo by Dexels.

the class JarFunctionFactory method parseFunction.

public FunctionDefinition parseFunction(Map<String, FunctionDefinition> fuds, XMLElement element) {
    Vector<XMLElement> def = element.getChildren();
    String name = (String) element.getAttribute("name");
    String object = (String) element.getAttribute("class");
    String description = null;
    String inputParams = null;
    String resultParam = null;
    for (int j = 0; j < def.size(); j++) {
        if (def.get(j).getName().equals("description")) {
            description = def.get(j).getContent();
        }
        if (def.get(j).getName().equals("input")) {
            inputParams = def.get(j).getContent();
        }
        if (def.get(j).getName().equals("result")) {
            resultParam = def.get(j).getContent();
        }
    }
    if (name != null) {
        FunctionDefinition functionDefinition = new FunctionDefinition(object, description, inputParams, resultParam);
        functionDefinition.setXmlElement(element);
        fuds.put(name, functionDefinition);
        return functionDefinition;
    }
    return null;
}
Also used : FunctionDefinition(com.dexels.navajo.expression.api.FunctionDefinition) CaseSensitiveXMLElement(com.dexels.navajo.document.nanoimpl.CaseSensitiveXMLElement) XMLElement(com.dexels.navajo.document.nanoimpl.XMLElement)

Example 15 with FunctionDefinition

use of com.dexels.navajo.expression.api.FunctionDefinition in project navajo by Dexels.

the class OSGiFunctionFactoryFactory method getFunctionInterface.

public static FunctionInterface getFunctionInterface(final String functionName) {
    if (cache.containsKey(functionName)) {
        return cache.get(functionName).getFunctionInstance();
    }
    FunctionDefinition fd = (FunctionDefinition) getComponent(functionName, "functionName", FunctionDefinition.class);
    if (fd == null) {
        throw NavajoFactory.getInstance().createNavajoException("No such function: " + functionName);
    }
    cache.put(functionName, fd);
    FunctionInterface instance = fd.getFunctionInstance();
    instance.setDefinition(fd);
    return instance;
}
Also used : FunctionInterface(com.dexels.navajo.expression.api.FunctionInterface) FunctionDefinition(com.dexels.navajo.expression.api.FunctionDefinition)

Aggregations

FunctionDefinition (com.dexels.navajo.expression.api.FunctionDefinition)26 FunctionInterface (com.dexels.navajo.expression.api.FunctionInterface)20 Test (org.junit.Test)13 GiveLongTestFunction (com.dexels.navajo.expression.compiled.GiveLongTestFunction)10 Operand (com.dexels.navajo.document.Operand)8 ArrayList (java.util.ArrayList)7 StringReader (java.io.StringReader)5 AddTestFunction (com.dexels.navajo.expression.compiled.AddTestFunction)4 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)4 ServiceReference (org.osgi.framework.ServiceReference)4 XMLElement (com.dexels.navajo.document.nanoimpl.XMLElement)3 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)3 Before (org.junit.Before)2 BundleContext (org.osgi.framework.BundleContext)2 CaseSensitiveXMLElement (com.dexels.navajo.document.nanoimpl.CaseSensitiveXMLElement)1 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)1 GiveMoneyTestFunction (com.dexels.navajo.expression.compiled.GiveMoneyTestFunction)1 GivePercentageTestFunction (com.dexels.navajo.expression.compiled.GivePercentageTestFunction)1 ParameterNamesFunction (com.dexels.navajo.expression.compiled.ParameterNamesFunction)1 FunctionFactoryInterface (com.dexels.navajo.functions.util.FunctionFactoryInterface)1