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

Commit

Permalink
cleanup for scripting rule support (#3337)
Browse files Browse the repository at this point in the history
- changed project JRE to use default (1.8)
- removed project specific compiler settings
- updated RuleRegistry implementation to latest interface in order to resolve compilation errors
- changed folder watchers to use configuration dir as a root
- addressed exceptions in the log if folders do not exist
- added the bundle to p2 automation feature
- added new Karaf feature

Signed-off-by: Kai Kreuzer <[email protected]>
  • Loading branch information
kaikreuzer authored and maggu2810 committed Apr 29, 2017
1 parent 7911cb2 commit d8d4340
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +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="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.eclipse.smarthome.automation.module.script.ScriptEngineContainer;
import org.eclipse.smarthome.automation.module.script.ScriptEngineManager;
import org.eclipse.smarthome.config.core.ConfigConstants;
import org.eclipse.smarthome.core.common.ThreadPoolManager;
import org.eclipse.smarthome.core.service.AbstractWatchService;

Expand All @@ -34,7 +35,7 @@
*
*/
public class ScriptFileWatcher extends AbstractWatchService {
private static final String FILE_DIRECTORY = "automation/jsr223";
private static final String FILE_DIRECTORY = "automation" + File.separator + "jsr223";
private static final String SCRIPT_FILE_WATCH_POOL = "SCRIPT_FILE_WATCH_POOL";
private static final long INITIAL_DELAY = 25;
private static final long RECHECK_INTERVAL = 20;
Expand All @@ -47,7 +48,7 @@ public class ScriptFileWatcher extends AbstractWatchService {
private Set<URL> loaded = new HashSet<>();

public ScriptFileWatcher() {
super(FILE_DIRECTORY);
super(ConfigConstants.getConfigFolder() + File.separator + FILE_DIRECTORY);
}

public void setScriptEngineManager(ScriptEngineManager manager) {
Expand All @@ -57,7 +58,7 @@ public void setScriptEngineManager(ScriptEngineManager manager) {
@Override
public void activate() {
super.activate();
importResources(new File(FILE_DIRECTORY));
importResources(new File(pathToWatch));
startScheduler();
}

Expand Down Expand Up @@ -134,19 +135,19 @@ private synchronized void importFile(URL url) {
} else {
if (manager.isSupported(scriptType)) {
try (InputStreamReader reader = new InputStreamReader(new BufferedInputStream(url.openStream()))) {
logger.info("script loading: {}", url.toString());
logger.info("loading script '{}'", url);

ScriptEngineContainer container = manager.createScriptEngine(scriptType, url.toString());

if (container != null) {
manager.loadScript(container.getIdentifier(), reader);
loaded.add(url);
logger.debug("script successfully loaded: {}", url.toString());
logger.debug("script successfully loaded: {}", url);
} else {
logger.error("script ERROR, ignore file: {}", url);
}
} catch (IOException e) {
logger.error("url=" + url, e);
logger.error("url={}", url, e);
}
} else {
enqueueUrl(url, scriptType);
Expand Down Expand Up @@ -207,7 +208,7 @@ private String getScriptIdentifier(URL url) {

private void startScheduler() {
ScheduledExecutorService scheduler = ThreadPoolManager.getScheduledPool(SCRIPT_FILE_WATCH_POOL);
scheduler.scheduleAtFixedRate(this::checkFiles, INITIAL_DELAY, RECHECK_INTERVAL, TimeUnit.SECONDS);
scheduler.scheduleWithFixedDelay(this::checkFiles, INITIAL_DELAY, RECHECK_INTERVAL, TimeUnit.SECONDS);
}

private void checkFiles() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;

import org.eclipse.smarthome.automation.Rule;
import org.eclipse.smarthome.automation.RuleRegistry;
Expand Down Expand Up @@ -137,8 +138,8 @@ public void runNow(String ruleUID) {
}

@Override
public void runNow(String ruleUID, boolean considerConditions) {
ruleRegistry.runNow(ruleUID, considerConditions);
public void runNow(String ruleUID, boolean considerConditions, Map<String, Object> context) {
ruleRegistry.runNow(ruleUID, considerConditions, context);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.smarthome.automation.template.TemplateProvider;
import org.eclipse.smarthome.automation.type.ModuleType;
import org.eclipse.smarthome.automation.type.ModuleTypeProvider;
import org.eclipse.smarthome.config.core.ConfigConstants;
import org.eclipse.smarthome.core.common.registry.Provider;
import org.eclipse.smarthome.core.common.registry.ProviderChangeListener;
import org.slf4j.Logger;
Expand Down Expand Up @@ -79,7 +80,7 @@ public abstract class AbstractFileProvider<E> implements Provider<E> {

public AbstractFileProvider(String root) {
this.rootSubdirectory = root;
configurationRoots = new String[] { "automation" };
configurationRoots = new String[] { ConfigConstants.getConfigFolder() + File.separator + "automation" };
}

public void activate(Map<String, Object> config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.WatchEvent;
Expand Down Expand Up @@ -97,6 +98,8 @@ protected void customizeWatchQueueReader(AbstractWatchService watchService, Path
} else {
registerDirectoryInternal(watchService, watchService.getWatchEventKinds(toWatch), toWatch);
}
} catch (NoSuchFileException e) {
logger.debug("Not watching folder '{}' as it does not exist.", toWatch);
} catch (IOException e) {
logger.warn("Cannot customize folder watcher for folder '{}'", toWatch, e);
}
Expand Down Expand Up @@ -132,8 +135,7 @@ private synchronized void registerDirectoryInternal(AbstractWatchService service
try {
registrationKey = directory.register(this.watchService, kinds);
} catch (IOException e) {
e.printStackTrace();
logger.debug("The directory '{}' was not registered in the watch service", directory, e);
logger.debug("The directory '{}' was not registered in the watch service: {}", directory, e.getMessage());
}
if (registrationKey != null) {
registeredKeys.put(registrationKey, directory);
Expand Down
7 changes: 7 additions & 0 deletions features/karaf/esh-core/src/main/feature/feature.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@
<bundle>mvn:org.eclipse.smarthome.automation/org.eclipse.smarthome.automation.module.script.defaultscope/${project.version}</bundle>
</feature>

<feature name="esh-automation-module-script-rulesupport" version="${project.version}">
<feature>esh-base</feature>
<feature dependency="true">esh-automation-api</feature>
<feature dependency="true">esh-automation-module-script</feature>
<bundle>mvn:org.eclipse.smarthome.automation/org.eclipse.smarthome.automation.module.script.rulesupport/${project.version}</bundle>
</feature>

<feature name="esh-automation-module-media" version="${project.version}">
<feature>esh-base</feature>
<feature dependency="true">esh-automation-api</feature>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Automation support
</description>

<copyright>
Copyright (c) 2014-2016 by the respective copyright holders.
Copyright (c) 2014-2017 by the respective copyright holders.
</copyright>

<license url="%licenseURL">
Expand Down Expand Up @@ -80,6 +80,13 @@ Automation support
version="0.0.0"
unpack="false"/>

<plugin
id="org.eclipse.smarthome.automation.module.script.rulesupport"
download-size="0"
install-size="0"
version="0.0.0"
unpack="false"/>

<plugin
id="org.eclipse.smarthome.automation.parser.gson"
download-size="0"
Expand Down

0 comments on commit d8d4340

Please sign in to comment.