2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.automation.jrubyscripting.internal.watch;
16 import java.nio.file.Path;
17 import java.nio.file.WatchEvent;
18 import java.util.Objects;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.automation.jrubyscripting.internal.JRubyScriptEngineFactory;
22 import org.openhab.core.automation.module.script.ScriptDependencyTracker;
23 import org.openhab.core.automation.module.script.ScriptEngineFactory;
24 import org.openhab.core.automation.module.script.ScriptEngineManager;
25 import org.openhab.core.automation.module.script.rulesupport.loader.AbstractScriptFileWatcher;
26 import org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileReference;
27 import org.openhab.core.service.ReadyService;
28 import org.osgi.framework.Constants;
29 import org.osgi.service.component.annotations.Activate;
30 import org.osgi.service.component.annotations.Component;
31 import org.osgi.service.component.annotations.Reference;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Monitors <openHAB-conf>/automation/ruby for Ruby files, but not libraries in lib or gems
38 * @author Cody Cutrer - Initial contribution
40 @Component(immediate = true, service = ScriptDependencyTracker.Listener.class)
41 public class JRubyScriptFileWatcher extends AbstractScriptFileWatcher {
42 private final Logger logger = LoggerFactory.getLogger(JRubyScriptFileWatcher.class);
44 private static final String FILE_DIRECTORY = "automation" + File.separator + "ruby";
46 private final JRubyScriptEngineFactory scriptEngineFactory;
49 public JRubyScriptFileWatcher(final @Reference ScriptEngineManager manager,
50 final @Reference ReadyService readyService, final @Reference(target = "(" + Constants.SERVICE_PID
51 + "=org.openhab.automation.jrubyscripting)") ScriptEngineFactory scriptEngineFactory) {
52 super(manager, readyService, FILE_DIRECTORY);
54 this.scriptEngineFactory = (JRubyScriptEngineFactory) scriptEngineFactory;
58 protected void importFile(ScriptFileReference ref) {
59 if (isIgnored(ref.getScriptFileURL().getFile())) {
62 super.importFile(ref);
66 protected void processWatchEvent(@Nullable WatchEvent<?> event, WatchEvent.@Nullable Kind<?> kind,
67 @Nullable Path path) {
68 if (Objects.nonNull(path)) {
69 logger.trace("looking at {}", path);
70 if (!isIgnored(path.toString())) {
71 logger.trace("and propagating it");
72 super.processWatchEvent(event, kind, path);
77 private boolean isIgnored(String path) {
78 return scriptEngineFactory.isFileInGemHome(path) || scriptEngineFactory.isFileInLoadPath(path);