diff --git a/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scope/SharedCache.java b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scope/SharedCache.java new file mode 100644 index 00000000000..225e5fddf20 --- /dev/null +++ b/bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scope/SharedCache.java @@ -0,0 +1,101 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.automation.jsscripting.internal.scope; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.function.Supplier; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.automation.module.script.ScriptExtensionProvider; +import org.osgi.service.component.annotations.Component; + +/** + * Shared Cache implementation for JS scripting. + * + * @author Jonathan Gilbert - Initial contribution + */ +@Component(immediate = true) +@NonNullByDefault +public class SharedCache implements ScriptExtensionProvider { + + private static final String PRESET_NAME = "cache"; + private static final String OBJECT_NAME = "sharedcache"; + + private JSCache cache = new JSCache(); + + @Override + public Collection getDefaultPresets() { + return Set.of(PRESET_NAME); + } + + @Override + public Collection getPresets() { + return Set.of(PRESET_NAME); + } + + @Override + public Collection getTypes() { + return Set.of(OBJECT_NAME); + } + + @Override + public @Nullable Object get(String scriptIdentifier, String type) throws IllegalArgumentException { + if (OBJECT_NAME.equals(type)) { + return cache; + } + + return null; + } + + @Override + public Map importPreset(String scriptIdentifier, String preset) { + if (PRESET_NAME.equals(preset)) { + final Object requestedType = get(scriptIdentifier, OBJECT_NAME); + if (requestedType != null) { + return Map.of(OBJECT_NAME, requestedType); + } + } + + return Collections.emptyMap(); + } + + @Override + public void unload(String scriptIdentifier) { + // ignore for now + } + + public static class JSCache { + private Map backingMap = new HashMap<>(); + + public void put(String k, Object v) { + backingMap.put(k, v); + } + + public @Nullable Object remove(String k) { + return backingMap.remove(k); + } + + public @Nullable Object get(String k) { + return backingMap.get(k); + } + + public @Nullable Object get(String k, Supplier supplier) { + return backingMap.computeIfAbsent(k, (unused_key) -> supplier.get()); + } + } +}