-
-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a profile for the generic SCRIPT transformation (#3292)
* Introduce a generic script profile Signed-off-by: Jan N. Klug <[email protected]>
- Loading branch information
Showing
9 changed files
with
714 additions
and
35 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
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
70 changes: 70 additions & 0 deletions
70
...in/java/org/openhab/core/automation/module/script/internal/ScriptEngineFactoryHelper.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,70 @@ | ||
/** | ||
* 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.internal; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import javax.script.ScriptEngine; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.core.automation.module.script.ScriptEngineFactory; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* The {@link ScriptEngineFactoryHelper} contains helper methods for handling script engines | ||
* | ||
* @author Jan N. Klug - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class ScriptEngineFactoryHelper { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ScriptEngineFactoryHelper.class); | ||
|
||
private ScriptEngineFactoryHelper() { | ||
// prevent instantiation of static utility class | ||
} | ||
|
||
public static Map.@Nullable Entry<String, String> getParameterOption(ScriptEngineFactory engineFactory) { | ||
List<String> scriptTypes = engineFactory.getScriptTypes(); | ||
if (!scriptTypes.isEmpty()) { | ||
ScriptEngine scriptEngine = engineFactory.createScriptEngine(scriptTypes.get(0)); | ||
if (scriptEngine != null) { | ||
Map.Entry<String, String> parameterOption = Map.entry(getPreferredMimeType(engineFactory), | ||
getLanguageName(scriptEngine.getFactory())); | ||
LOGGER.trace("ParameterOptions: {}", parameterOption); | ||
return parameterOption; | ||
} else { | ||
LOGGER.trace("setScriptEngineFactory: engine was null"); | ||
} | ||
} else { | ||
LOGGER.trace("addScriptEngineFactory: scriptTypes was empty"); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static String getPreferredMimeType(ScriptEngineFactory factory) { | ||
List<String> mimeTypes = new ArrayList<>(factory.getScriptTypes()); | ||
mimeTypes.removeIf(mimeType -> !mimeType.contains("application") || "application/python".equals(mimeType)); | ||
return mimeTypes.isEmpty() ? factory.getScriptTypes().get(0) : mimeTypes.get(0); | ||
} | ||
|
||
public static String getLanguageName(javax.script.ScriptEngineFactory factory) { | ||
return String.format("%s (%s)", | ||
factory.getLanguageName().substring(0, 1).toUpperCase() + factory.getLanguageName().substring(1), | ||
factory.getLanguageVersion()); | ||
} | ||
} |
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.