Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
- Ability to load scripts from bundles and local directory
Browse files Browse the repository at this point in the history
- allow other bundles to use ScriptEngine, allow providing of ScriptExtensions that can be loaded dynamically and are able to have a ScriptEngine state, improvement of GLOBAL_SCOPE nashorn mechanism
- scriptable rules by the "RuleSupport" extension

Signed-off-by: Simon Merschjohann <[email protected]>
  • Loading branch information
smerschjohann committed Jun 30, 2016
1 parent 8870c70 commit 158d61c
Show file tree
Hide file tree
Showing 50 changed files with 2,872 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.eclipse.smarthome.automation.module.script.extensions.rulesupport</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ds.core.builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
eclipse.preferences.version=1
pluginProject.equinox=false
pluginProject.extensions=false
resolve.requirebundle=false
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"conditions":[
{
"uid":"ScriptedCondition",
"label":"Scripted condition",
"description":"allows the definition of a condition by a script",
"configDescriptions":[
{
"name":"privId",
"type":"TEXT",
"description":"the identifier of the private method",
"required":true
}
]
}
],
"actions":[
{
"uid":"ScriptedAction",
"label":"Scripted action",
"description":"allows the execution of a method defined by a script",
"configDescriptions":[
{
"name":"privId",
"type":"TEXT",
"description":"the identifier of the private method",
"required":true
}
],
"outputs":[
{
"name":"result",
"type":"java.lang.Object",
"label":"result",
"description":"the script result.",
"reference":""
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Eclipse SmartHome Automation Script Extension RuleSupport
Bundle-SymbolicName: org.eclipse.smarthome.automation.module.script.extensions.rulesupport
Bundle-Version: 0.8.0.qualifier
Bundle-Vendor: Eclipse.org/SmartHome
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-Activator: org.eclipse.smarthome.automation.module.script.extensions.rulesupport.internal.RuleSupportActivator
Import-Package: org.eclipse.smarthome.automation,
org.eclipse.smarthome.automation.handler,
org.eclipse.smarthome.automation.module.script,
org.eclipse.smarthome.automation.parser,
org.eclipse.smarthome.automation.template,
org.eclipse.smarthome.automation.type,
org.eclipse.smarthome.config.core,
org.eclipse.smarthome.core.common.registry,
org.eclipse.smarthome.core.events,
org.eclipse.smarthome.core.items,
org.eclipse.smarthome.core.items.events,
org.eclipse.smarthome.core.library.types,
org.eclipse.smarthome.core.thing,
org.eclipse.smarthome.core.types,
org.osgi.framework,
org.osgi.service.cm;version="1.5.0",
org.osgi.util.tracker;version="1.5.1",
org.slf4j
Automation-ResourceType: json
Service-Component: OSGI-INF/*.xml
Require-Bundle: org.eclipse.smarthome.automation.core;bundle-version="0.0.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2015-2016 by Simon Merschjohann and others.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/epl-v10.html
-->
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" immediate="true" name="org.eclipse.smarthome.automation.module.script.loader.globals">
<implementation class="org.eclipse.smarthome.automation.module.script.extensions.rulesupport.internal.LoaderScriptExtension"/>
<service>
<provide interface="org.eclipse.smarthome.automation.module.script.ScriptExtensionProvider"/>
</service>
<reference bind="setRuleRegistry" cardinality="1..1" interface="org.eclipse.smarthome.automation.RuleRegistry" name="RuleRegistry" policy="static" unbind="unsetRuleRegistry"/>
</scr:component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>About</title>
</head>
<body lang="EN-US">
<h2>About This Content</h2>

<p>June 5, 2006</p>
<h3>License</h3>

<p>The Eclipse Foundation makes available all content in this plug-in (&quot;Content&quot;). Unless otherwise
indicated below, the Content is provided to you under the terms and conditions of the
Eclipse Public License Version 1.0 (&quot;EPL&quot;). A copy of the EPL is available
at <a href="http://www.eclipse.org/legal/epl-v10.html">http://www.eclipse.org/legal/epl-v10.html</a>.
For purposes of the EPL, &quot;Program&quot; will mean the Content.</p>

<p>If you did not receive this Content directly from the Eclipse Foundation, the Content is
being redistributed by another party (&quot;Redistributor&quot;) and different terms and conditions may
apply to your use of any object code in the Content. Check the Redistributor's license that was
provided with the Content. If no such license exists, contact the Redistributor. Unless otherwise
indicated below, the terms and conditions of the EPL still apply to any source code in the Content
and such source code may be obtained at <a href="http://www.eclipse.org/">http://www.eclipse.org</a>.</p>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Bundle-Activator: org.eclipse.smarthome.automation.module.script.extensions.RuleSupportActivator

output.. = target/classes/
bin.includes = META-INF/,\
.,\
OSGI-INF/,\
ESH-INF/,\
about.html
source.. = src/main/java/
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<parent>
<groupId>org.eclipse.smarthome.bundles</groupId>
<artifactId>automation</artifactId>
<version>0.8.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<groupId>org.eclipse.smarthome.automation</groupId>
<artifactId>org.eclipse.smarthome.automation.module.script.extensions.rulesupport</artifactId>

<name>Eclipse SmartHome Automation Extension RuleSupport</name>

<packaging>eclipse-plugin</packaging>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/**
* Copyright (c) 2015-2016 Simon Merschjohann and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.smarthome.automation.module.script.extensions.rulesupport.internal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.smarthome.automation.Action;
import org.eclipse.smarthome.automation.Condition;
import org.eclipse.smarthome.automation.Rule;
import org.eclipse.smarthome.automation.RuleRegistry;
import org.eclipse.smarthome.automation.Trigger;
import org.eclipse.smarthome.automation.module.script.ScriptExtensionProvider;
import org.eclipse.smarthome.automation.module.script.extensions.rulesupport.internal.shared.ScriptedRule;
import org.eclipse.smarthome.automation.module.script.extensions.rulesupport.internal.shared.modulehandler.ScriptedActionHandlerFactory;
import org.eclipse.smarthome.automation.module.script.extensions.rulesupport.internal.shared.modulehandler.ScriptedConditionHandlerFactory;
import org.eclipse.smarthome.automation.module.script.extensions.rulesupport.internal.shared.modulehandler.ScriptedTriggerHandlerFactory;
import org.eclipse.smarthome.automation.module.script.extensions.rulesupport.internal.shared.simple.SimpleActionHandler;
import org.eclipse.smarthome.automation.module.script.extensions.rulesupport.internal.shared.simple.SimpleConditionHandler;
import org.eclipse.smarthome.automation.module.script.extensions.rulesupport.internal.shared.simple.SimpleRule;
import org.eclipse.smarthome.automation.type.ActionType;
import org.eclipse.smarthome.automation.type.ModuleType;
import org.eclipse.smarthome.config.core.ConfigDescriptionParameter;
import org.eclipse.smarthome.config.core.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoaderScriptExtension implements ScriptExtensionProvider {
private final static Logger logger = LoggerFactory.getLogger(LoaderScriptExtension.class);

private static RuleRegistry ruleRegistry;

private static HashMap<String, Collection<String>> presets = new HashMap<>();

private static HashMap<String, Object> staticTypes = new HashMap<>();
private static HashSet<String> types = new HashSet<String>();

private ConcurrentHashMap<Integer, HashMap<String, Object>> objectCache = new ConcurrentHashMap<>();

static {
staticTypes.put("ScriptedRule", ScriptedRule.class);
staticTypes.put("SimpleRule", SimpleRule.class);
staticTypes.put("SimpleActionHandler", SimpleActionHandler.class);
staticTypes.put("SimpleConditionHandler", SimpleConditionHandler.class);

staticTypes.put("ActionHandlerFactory", ScriptedActionHandlerFactory.class);
staticTypes.put("ConditionHandlerFactory", ScriptedConditionHandlerFactory.class);
staticTypes.put("TriggerHandlerFactory", ScriptedTriggerHandlerFactory.class);

staticTypes.put("Configuration", Configuration.class);
staticTypes.put("Action", Action.class);
staticTypes.put("Condition", Condition.class);
staticTypes.put("Trigger", Trigger.class);
staticTypes.put("Rule", Rule.class);
staticTypes.put("ModuleType", ModuleType.class);
staticTypes.put("ActionType", ActionType.class);
staticTypes.put("ConfigDescriptionParameter", ConfigDescriptionParameter.class);

types.addAll(staticTypes.keySet());

types.add("HandlerRegistry");
types.add("RuleRegistry");
types.add("rules");

presets.put("RuleSupport",
Arrays.asList("Configuration", "Action", "Condition", "Trigger", "Rule", "ModuleType", "ActionType"));
presets.put("RuleSimple", Arrays.asList("ScriptedRule", "SimpleRule"));
presets.put("RuleFactories",
Arrays.asList("ActionHandlerFactory", "ConditionHandlerFactory", "TriggerHandlerFactory"));
}

public static RuleRegistry getRuleRegistry() {
return ruleRegistry;
}

public void setRuleRegistry(RuleRegistry ruleRegistry) {
logger.info("rule registry registered");
LoaderScriptExtension.ruleRegistry = ruleRegistry;
}

@Override
public Collection<String> getDefaultPresets() {
return new ArrayList<String>(0);
}

@Override
public Collection<String> getPresets() {
return presets.keySet();
}

@Override
public Collection<String> getTypes() {
return types;
}

@Override
public Object get(int scriptEngineId, String type) {
Object obj = staticTypes.get(type);
if (obj != null) {
return obj;
}

HashMap<String, Object> objects = objectCache.get(scriptEngineId);

if (objects == null) {
objects = new HashMap<>();
objectCache.put(scriptEngineId, objects);
}

obj = objects.get(type);
if (obj != null) {
return obj;
}

if (type.equals("HandlerRegistry") || type.equals("RuleRegistry")) {
ScriptedHandlerRegistryImpl handlerRegistry = new ScriptedHandlerRegistryImpl(
LoaderScriptExtension.getRuleRegistry(), RuleSupportActivator.getModuleHandlerFactory());
objects.put("HandlerRegistry", handlerRegistry);
RuleRegistry ruleRegistry = handlerRegistry.getRuleRegistry();
objects.put("RuleRegistry", ruleRegistry);

obj = objects.get(type);
}

return obj;
}

@Override
public Map<String, Object> importPreset(int scriptEngineId, String preset) {
Map<String, Object> scopeValues = new HashMap<>();

Collection<String> values = presets.get(preset);

for (String value : values) {
scopeValues.put(value, staticTypes.get(value));
}

if (preset.equals("RuleSupport")) {
scopeValues.put("HandlerRegistry", get(scriptEngineId, "HandlerRegistry"));

Object ruleRegistry = get(scriptEngineId, "RuleRegistry");
scopeValues.put("RuleRegistry", ruleRegistry);
scopeValues.put("rules", ruleRegistry);
}

return scopeValues;
}

@Override
public void unLoad(int scriptEngineId) {
HashMap<String, Object> objects = objectCache.remove(scriptEngineId);

if (objects != null) {
Object hr = objects.get("HandlerRegistry");
if (hr != null) {
ScriptedHandlerRegistryImpl handlerRegistry = (ScriptedHandlerRegistryImpl) hr;

handlerRegistry.removeAll();
}
}
}

}
Loading

0 comments on commit 158d61c

Please sign in to comment.