Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Jython and Groovy ScriptEngineFactories #519

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,64 @@
/**
* Copyright (c) 2019 Contributors to the Eclipse Foundation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if I should use this...

Copyright (c) 2010-${year} Contributors to the openHAB project

Or...

Copyright (c) 2010-2019 Contributors to the openHAB project

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clearly the version with the years, not the placeholders!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought so... but clearly not so clear to me! Thank you! 😄

*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* 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.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import org.openhab.core.automation.module.script.ScriptEngineFactory;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some Javadoc

* @author Scott Rushworth - Initial contribution
*/
@Component(service = ScriptEngineFactory.class)
public class GroovyScriptEngineFactory implements ScriptEngineFactory {
private final Logger logger = LoggerFactory.getLogger(this.getClass());

private ScriptEngineManager engineManager = new ScriptEngineManager();

@Override
public List<String> getLanguages() {
return Arrays.asList("groovy", "application/groovy");
}

@Override
public void scopeValues(ScriptEngine scriptEngine, Map<String, Object> scopeValues) {
for (Entry<String, Object> entry : scopeValues.entrySet()) {
scriptEngine.put(entry.getKey(), entry.getValue());
}
}

@Override
public ScriptEngine createScriptEngine(String fileExtension) {
if (!getLanguages().contains(fileExtension)) {
logger.error("Invalid fileExtension: {}", fileExtension);
return null;
}

return engineManager.getEngineByName("groovy");
}

@Override
public boolean isSupported(String fileExtension) {
return getLanguages().contains(fileExtension);
}
}
@@ -0,0 +1,65 @@
/**
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* 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.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import org.openhab.core.automation.module.script.ScriptEngineFactory;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some Javadoc

* @author Scott Rushworth - Initial contribution
*/
@Component(service = ScriptEngineFactory.class)
public class JythonScriptEngineFactory implements ScriptEngineFactory {
private final Logger logger = LoggerFactory.getLogger(this.getClass());

private ScriptEngineManager engineManager = new ScriptEngineManager();

@Override
public List<String> getLanguages() {
return Arrays.asList("py", "python", "application/jython", "jython");
}

@Override
public void scopeValues(ScriptEngine scriptEngine, Map<String, Object> scopeValues) {
for (Entry<String, Object> entry : scopeValues.entrySet()) {
scriptEngine.put(entry.getKey(), entry.getValue());
}
}

@Override
public ScriptEngine createScriptEngine(String fileExtension) {
if (!getLanguages().contains(fileExtension)) {
logger.error("Invalid fileExtension: {}", fileExtension);
return null;
}
final ScriptEngine pyEngine = engineManager.getEngineByName("jython");
return pyEngine;
}

@Override
public boolean isSupported(String fileExtension) {
return getLanguages().contains(fileExtension);
}

}
Expand Up @@ -11,9 +11,17 @@
"description":"the scripting language used",
"required":true,
"options":[
{
"label": "Groovy",
"value": "application/groovy"
},
{
"label": "Javascript",
"value": "application/javascript"
},
{
"label": "Jython",
"value": "application/jython"
}
]
},
Expand All @@ -39,9 +47,17 @@
"description":"the scripting language used",
"required":true,
"options":[
{
"label": "Groovy",
"value": "application/groovy"
},
{
"label": "Javascript",
"value": "application/javascript"
},
{
"label": "Jython",
"value": "application/jython"
}
],
"defaultValue":"application/javascript"
Expand Down