Search in sources :

Example 1 with Script

use of org.cristalise.kernel.scripting.Script in project kernel by cristal-ise.

the class DependencyMember method evaluateScript.

/**
 * @return either PropertyArrayList or CastorHashMap
 *
 * @throws InvalidDataException
 * @throws ObjectNotFoundException
 */
protected Object evaluateScript() throws InvalidDataException, ObjectNotFoundException {
    Logger.msg(5, "DependencyMember.evaluateScript() - memberUUID:" + getChildUUID());
    Script script = LocalObjectLoader.getScript(getProperties());
    try {
        script.setInputParamValue("dependencyMember", this);
        script.setInputParamValue("storage", Gateway.getStorage());
        script.setInputParamValue("proxy", Gateway.getProxyManager());
        script.setInputParamValue("lookup", Gateway.getLookup());
        return script.evaluate(getItemPath(), getProperties(), null, null);
    } catch (ScriptingEngineException e) {
        Logger.error(e);
        throw new InvalidDataException(e.getMessage());
    }
}
Also used : Script(org.cristalise.kernel.scripting.Script) ScriptingEngineException(org.cristalise.kernel.scripting.ScriptingEngineException) InvalidDataException(org.cristalise.kernel.common.InvalidDataException)

Example 2 with Script

use of org.cristalise.kernel.scripting.Script in project kernel by cristal-ise.

the class MainTest method testScriptParsing.

@Test
public void testScriptParsing() throws Exception {
    OutcomeValidator valid = new OutcomeValidator(getSchema("Script", 0, "boot/OD/Script.xsd"));
    String testScriptString = FileStringUtility.url2String(MainTest.class.getResource("/TestScript.xml"));
    String errors = valid.validate(testScriptString);
    assert errors.length() == 0 : "Test script not valid to schema: " + errors;
    Script testScript = new Script("TestScript", 0, null, testScriptString);
    assert testScript.getInputParams().size() == 1 : "Script input param count wrong";
    assert testScript.getInputParams().get("test") != null : "Could not retrieve script input param value";
    testScript.setInputParamValue("test", "Test");
    assert testScript.getInputParams().get("test").getInitialised() : "Script is not initialized when it should be";
    Object result = testScript.execute();
    assert result != null : "Script returned null result";
    assert result instanceof String : "Script failed to return a String";
    assert ((String) result).equals("TestTest") : "Script failed to produce correct result: " + result;
}
Also used : Script(org.cristalise.kernel.scripting.Script) OutcomeValidator(org.cristalise.kernel.persistency.outcome.OutcomeValidator) Test(org.junit.Test)

Example 3 with Script

use of org.cristalise.kernel.scripting.Script in project kernel by cristal-ise.

the class DefaultResourceImportHandler method getCollections.

private CollectionArrayList getCollections(String name, Integer version, String xml) throws Exception {
    if (type == SCHEMA_RESOURCE) {
        return new Schema(name, version, null, xml).makeDescCollections();
    } else if (type == SCRIPT_RESOURCE) {
        return new Script(name, version, null, xml).makeDescCollections();
    } else if (type == QUERY_RESOURCE) {
        return new Query(name, version, null, xml).makeDescCollections();
    } else {
        DescriptionObject descObject = (DescriptionObject) Gateway.getMarshaller().unmarshall(xml);
        descObject.setVersion(version);
        return descObject.makeDescCollections();
    }
}
Also used : Script(org.cristalise.kernel.scripting.Script) Query(org.cristalise.kernel.querying.Query) Schema(org.cristalise.kernel.persistency.outcome.Schema) DescriptionObject(org.cristalise.kernel.utils.DescriptionObject)

Example 4 with Script

use of org.cristalise.kernel.scripting.Script in project kernel by cristal-ise.

the class AgentProxy method callScript.

@SuppressWarnings("rawtypes")
private ErrorInfo callScript(ItemProxy item, Job job) throws ScriptingEngineException, InvalidDataException, ObjectNotFoundException {
    Script script = job.getScript();
    if (script.getOutputParams().size() == 1) {
        Parameter p = script.getOutputParams().values().iterator().next();
        if (p.getType() == ErrorInfo.class) {
            script.setActExecEnvironment(item, this, job);
            Object returnVal = script.execute();
            if (returnVal instanceof Map)
                return (ErrorInfo) ((Map) returnVal).get(p.getName());
            else
                return (ErrorInfo) returnVal;
        }
    }
    throw new InvalidDataException("Script " + script.getName() + " must define single output of type org.cristalise.kernel.scripting.ErrorInfo");
}
Also used : Script(org.cristalise.kernel.scripting.Script) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) Parameter(org.cristalise.kernel.scripting.Parameter) C2KLocalObject(org.cristalise.kernel.entity.C2KLocalObject) Map(java.util.Map)

Example 5 with Script

use of org.cristalise.kernel.scripting.Script in project kernel by cristal-ise.

the class MainTest method testBootItems.

@Test
public void testBootItems() throws Exception {
    HashMap<String, OutcomeValidator> validators = new HashMap<String, OutcomeValidator>();
    validators.put("CA", new OutcomeValidator(getSchema("CompositeActivityDef", 0, "boot/OD/CompositeActivityDef.xsd")));
    validators.put("EA", new OutcomeValidator(getSchema("ElementaryActivityDef", 0, "boot/OD/ElementaryActivityDef.xsd")));
    validators.put("SC", new OutcomeValidator(getSchema("Script", 0, "boot/OD/Script.xsd")));
    validators.put("SM", new OutcomeValidator(getSchema("StateMachine", 0, "boot/OD/StateMachine.xsd")));
    validators.put("OD", new SchemaValidator());
    String bootItems = FileStringUtility.url2String(Gateway.getResource().getKernelResourceURL("boot/allbootitems.txt"));
    StringTokenizer str = new StringTokenizer(bootItems, "\n\r");
    while (str.hasMoreTokens()) {
        String thisItem = str.nextToken();
        StringTokenizer str2 = new StringTokenizer(thisItem, "/,");
        String id = str2.nextToken();
        String itemType = str2.nextToken(), resName = str2.nextToken();
        Logger.msg(1, "Validating " + itemType + " " + resName);
        OutcomeValidator validator = validators.get(itemType);
        String data = Gateway.getResource().getTextResource(null, "boot/" + itemType + "/" + resName + (itemType.equals("OD") ? ".xsd" : ".xml"));
        assert data != null : "Boot " + itemType + " data item " + thisItem + " not found";
        String errors = validator.validate(data);
        assert errors.length() == 0 : "Kernel resource " + itemType + " " + resName + " has errors :" + errors;
        if (itemType.equals("CA") || itemType.equals("EA") || itemType.equals("SM")) {
            Logger.msg(1, "Remarshalling " + itemType + " " + resName);
            long then = System.currentTimeMillis();
            Object unmarshalled = Gateway.getMarshaller().unmarshall(data);
            assert unmarshalled != null;
            String remarshalled = Gateway.getMarshaller().marshall(unmarshalled);
            long now = System.currentTimeMillis();
            Logger.msg("Marshall/remarshall of " + itemType + " " + resName + " took " + (now - then) + "ms");
            errors = validator.validate(remarshalled);
            assert errors.length() == 0 : "Remarshalled resource " + itemType + " " + resName + " has errors :" + errors + "\nRemarshalled form:\n" + remarshalled;
        // Diff xmlDiff = new Diff(data, remarshalled);
        // if (!xmlDiff.identical()) {
        // Logger.msg("Difference found in remarshalled "+thisItem+": "+xmlDiff.toString());
        // Logger.msg("Original: "+data);
        // Logger.msg("Remarshalled: "+remarshalled);
        // }
        // assert xmlDiff.identical();
        }
        if (itemType.equals("SC")) {
            Logger.msg(1, "Parsing script " + resName);
            new Script(resName, 0, null, data);
        }
    }
}
Also used : Script(org.cristalise.kernel.scripting.Script) OutcomeValidator(org.cristalise.kernel.persistency.outcome.OutcomeValidator) StringTokenizer(java.util.StringTokenizer) HashMap(java.util.HashMap) SchemaValidator(org.cristalise.kernel.persistency.outcome.SchemaValidator) Test(org.junit.Test)

Aggregations

Script (org.cristalise.kernel.scripting.Script)5 InvalidDataException (org.cristalise.kernel.common.InvalidDataException)2 OutcomeValidator (org.cristalise.kernel.persistency.outcome.OutcomeValidator)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 StringTokenizer (java.util.StringTokenizer)1 C2KLocalObject (org.cristalise.kernel.entity.C2KLocalObject)1 Schema (org.cristalise.kernel.persistency.outcome.Schema)1 SchemaValidator (org.cristalise.kernel.persistency.outcome.SchemaValidator)1 Query (org.cristalise.kernel.querying.Query)1 Parameter (org.cristalise.kernel.scripting.Parameter)1 ScriptingEngineException (org.cristalise.kernel.scripting.ScriptingEngineException)1 DescriptionObject (org.cristalise.kernel.utils.DescriptionObject)1