]> git.basschouten.com Git - openhab-addons.git/blob
af550168093110e963889c7b7b1a6744b9e019cb
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.automation.jrubyscripting.internal.watch;
14
15 import java.io.File;
16 import java.nio.file.Path;
17 import java.nio.file.WatchEvent;
18 import java.util.Objects;
19
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;
34
35 /**
36  * Monitors <openHAB-conf>/automation/ruby for Ruby files, but not libraries in lib or gems
37  *
38  * @author Cody Cutrer - Initial contribution
39  */
40 @Component(immediate = true, service = ScriptDependencyTracker.Listener.class)
41 public class JRubyScriptFileWatcher extends AbstractScriptFileWatcher {
42     private final Logger logger = LoggerFactory.getLogger(JRubyScriptFileWatcher.class);
43
44     private static final String FILE_DIRECTORY = "automation" + File.separator + "ruby";
45
46     private final JRubyScriptEngineFactory scriptEngineFactory;
47
48     @Activate
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);
53
54         this.scriptEngineFactory = (JRubyScriptEngineFactory) scriptEngineFactory;
55     }
56
57     @Override
58     protected void importFile(ScriptFileReference ref) {
59         if (isIgnored(ref.getScriptFileURL().getFile())) {
60             return;
61         }
62         super.importFile(ref);
63     }
64
65     @Override
66     protected void processWatchEvent(@Nullable WatchEvent<?> event, WatchEvent.@Nullable Kind<?> kind,
67             @Nullable Path path) {
68         if (Objects.nonNull(path)) {
69             if (!isIgnored(path.toString())) {
70                 super.processWatchEvent(event, kind, path);
71             }
72         }
73     }
74
75     private boolean isIgnored(String path) {
76         return scriptEngineFactory.isFileInGemHome(path) || scriptEngineFactory.isFileInLoadPath(path);
77     }
78 }