-
-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dynamic scripting language transformation service
This replaced SCRIPT transformation with one specific to each language e.g. JS, RB, GROOVY, etc. Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
- Loading branch information
Showing
18 changed files
with
324 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...in/java/org/openhab/core/automation/module/script/ScriptTransformationServiceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
* Copyright (c) 2010-2023 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.core.automation.module.script; | ||
|
||
import java.util.Dictionary; | ||
import java.util.Hashtable; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.core.automation.module.script.internal.ScriptEngineFactoryHelper; | ||
import org.openhab.core.transform.TransformationRegistry; | ||
import org.openhab.core.transform.TransformationService; | ||
import org.osgi.framework.BundleContext; | ||
import org.osgi.framework.ServiceRegistration; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Deactivate; | ||
import org.osgi.service.component.annotations.Reference; | ||
import org.osgi.service.component.annotations.ReferenceCardinality; | ||
import org.osgi.service.component.annotations.ReferencePolicy; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* The {@link ScriptTransformationServiceFactory} registers a {@link ScriptTransformationService} | ||
* for each newly added script engine. | ||
* | ||
* @author Jimmy Tanagra - Initial contribution | ||
*/ | ||
@Component(immediate = true, service = ScriptTransformationServiceFactory.class) | ||
@NonNullByDefault | ||
public class ScriptTransformationServiceFactory { | ||
private final Logger logger = LoggerFactory.getLogger(ScriptTransformationServiceFactory.class); | ||
|
||
private final TransformationRegistry transformationRegistry; | ||
private final ScriptEngineManager scriptEngineManager; | ||
private final BundleContext bundleContext; | ||
|
||
private final Map<String, ServiceRegistration> scriptTransformations = new ConcurrentHashMap<>(); | ||
|
||
@Activate | ||
public ScriptTransformationServiceFactory(@Reference TransformationRegistry transformationRegistry, | ||
@Reference ScriptEngineManager scriptEngineManager, BundleContext bundleContext) { | ||
this.bundleContext = bundleContext; | ||
this.transformationRegistry = transformationRegistry; | ||
this.scriptEngineManager = scriptEngineManager; | ||
} | ||
|
||
@Deactivate | ||
public void deactivate() { | ||
scriptTransformations.values().forEach(this::unregisterService); | ||
} | ||
|
||
/** | ||
* As {@link ScriptEngineFactory}s are added/removed, this method will cache all available script types | ||
* and registers a transformation service for the script engine. | ||
*/ | ||
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC) | ||
public void setScriptEngineFactory(ScriptEngineFactory engineFactory) { | ||
Optional<String> scriptType = ScriptEngineFactoryHelper.getPreferredExtension(engineFactory); | ||
if (!scriptType.isPresent()) { | ||
return; | ||
} | ||
|
||
scriptTransformations.computeIfAbsent(scriptType.get(), type -> { | ||
ScriptTransformationService scriptTransformation = new ScriptTransformationService(type, | ||
transformationRegistry, scriptEngineManager); | ||
|
||
Dictionary<String, Object> properties = new Hashtable<>(); | ||
properties.put(TransformationService.SERVICE_PROPERTY_NAME, type.toUpperCase()); | ||
return bundleContext.registerService(TransformationService.class, scriptTransformation, properties); | ||
}); | ||
} | ||
|
||
public void unsetScriptEngineFactory(ScriptEngineFactory engineFactory) { | ||
Optional<String> scriptType = ScriptEngineFactoryHelper.getPreferredExtension(engineFactory); | ||
if (scriptType.isPresent()) { | ||
Optional.ofNullable(scriptTransformations.remove(scriptType.get())).ifPresent(this::unregisterService); | ||
} | ||
} | ||
|
||
public Set<String> getScriptTypes() { | ||
return scriptTransformations.keySet(); | ||
} | ||
|
||
@Nullable | ||
public ScriptTransformationService getTransformationService(final String scriptType) { | ||
ServiceRegistration reg = scriptTransformations.get(scriptType); | ||
if (reg != null) { | ||
return (ScriptTransformationService) bundleContext.getService(reg.getReference()); | ||
} | ||
return null; | ||
} | ||
|
||
private void unregisterService(ServiceRegistration reg) { | ||
ScriptTransformationService scriptTransformation = (ScriptTransformationService) bundleContext | ||
.getService(reg.getReference()); | ||
scriptTransformation.deactivate(); | ||
reg.unregister(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.