eclipse-archived / smarthome

Eclipse SmartHome™ project
https://www.eclipse.org/smarthome/
Eclipse Public License 2.0
862 stars 783 forks source link

Kotlin Support in JSR223 #4377

Open smerschjohann opened 7 years ago

smerschjohann commented 7 years ago

I would like to use Kotlin Scripts for the JSR223 scripting functionality.

As it is not possible to just through a jar file into the lib directory, I would like to build a module for this which enables this capability.

Before implementing such module, I would like to discuss how to do it:

  1. Should this be a module for smarthome or openhab2?
  2. If I have done this skeleton, should I also create modules for jython, groovy, etc.?
lewie commented 7 years ago

@smerschjohann, in JDBC binding we load JDBC driver jars over maven dependencies in feature.xml dynamically. This could be done here in a similar way?

Looks like:

  <feature name="openhab-persistence-jdbc-postgresql" description="JDBC Persistence PostgreSQL" version="${project.version}">
    <configfile finalname="${openhab.conf}/services/jdbc.cfg" override="false">mvn:${project.groupId}/openhab-addons-external/${project.version}/cfg/jdbc</configfile>
    <feature prerequisite="false" dependency="false">openhab-runtime-base</feature>
    <feature prerequisite="false" dependency="false">openhab-runtime-compat1x</feature>
    <bundle start-level="80">mvn:org.postgresql/postgresql/${postgresql.version}</bundle>
    <bundle start-level="80">mvn:org.openhab.persistence/org.openhab.persistence.jdbc/${project.version}</bundle>
  </feature>
smerschjohann commented 7 years ago

I don't quite get this syntax. Is this about the karaf integration (feature selection in the openhab market)?

For this module, I would just need to add the following dependencies to have kotlin support:

<dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib</artifactId>
        <version>1.1.51</version>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-compiler</artifactId>
        <version>1.1.51</version>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-script-util</artifactId>
        <version>1.1.51</version>
    </dependency>

I still have to add them to the pom.xml, haven't I? Where do I find the feature.xml of the jdbc feature?

lewie commented 6 years ago

@smerschjohann

I still have to add them to the pom.xml, haven't I?

Not shure, in JDBC I had to do it like in openhab\bundles\persistence\org.openhab.persistence.jdbc\pom.xml. Without, the binding would not compile.

As I have understood, you does not need to load a kotlin*.jars directly or in a pom for any binding, in this case. This should be part of "feature.xml" and "dependencies.xml" (is generated?), which do it for the hole distribution. They contain the presets for a bunch of jars which represents several binding configurations and its different dependencies.

First in openhab-core\poms\pom.xml only set versions for kotlin, jython, groovy, etc. jars:

<kotlin.version>1.1.51</kotlin.version>
<jython.version>2.7.1</jython.version>
<groovy.version>2.4.12</groovy.version>

Second in openhab-core\features\openhab-core\src\main\feature\feature.xml like:

The JavaScript only ruleengine:

    <feature name="openhab-misc-ruleengine-javascript" description="Rule Engine JavaScript only" version="${project.version}">
        <feature>openhab-runtime-base</feature>
        <feature>esh-automation-api</feature>
        <feature>esh-automation-commands</feature>
        <feature>esh-automation-core</feature>
        <feature>esh-automation-module-core</feature>
        <feature>esh-automation-module-media</feature>
        <feature>esh-automation-module-script</feature>
        <feature>esh-automation-module-script-defaultscope</feature>
        <feature>esh-automation-module-script-rulesupport</feature>
        <feature>esh-automation-module-timer</feature>
        <feature>esh-automation-parser-gson</feature>
        <feature>esh-automation-providers</feature>
        <feature>esh-automation-rest</feature>
    </feature>

and for kotlin:

    <feature name="openhab-misc-ruleengine-kotlin" description="Rule Engine Kotlin" version="${project.version}">
        <bundle start-level="80">mvn:org.jetbrains.kotlin/kotlin-stdlib/${kotlin.version}</bundle>
        <bundle start-level="80">mvn:org.jetbrains.kotlin/kotlin-compiler/${kotlin.version}</bundle>
        <bundle start-level="80">mvn:org.jetbrains.kotlin/kotlin-script-util/${kotlin.version}</bundle>
        <feature>openhab-misc-ruleengine-javascript</feature>
    </feature>

for jython:

    <feature name="openhab-misc-ruleengine-jython" description="Rule Engine Jython" version="${project.version}">
        <bundle start-level="80">mvn:org.python/jython-standalone/${jython.version}</bundle>
        <feature>openhab-misc-ruleengine-javascript</feature>
    </feature>

for groovy:

    <feature name="openhab-misc-ruleengine-groovy" description="Rule Engine Groovy" version="${project.version}">
        <bundle start-level="80">mvn:org.codehaus.groovy/groovy-jsr223/${groovy.version}</bundle>
        <feature>openhab-misc-ruleengine-javascript</feature>
    </feature>

... add some more :-)

Where do I find the feature.xml of the jdbc feature?

openhab-distro\distributions\openhab-addons\src\main\history\dependencies.xml
openhab-distro\features\addons\src\main\history\dependencies.xml
openhab\features\openhab-addons\src\main\feature\feature.xml

Be aware, JDBC is a openHAB1 Binding. Therefore the features are defined at other places as for smarthome.

I am sure not to have gathered all the connections, so @KaiKreuzer should verify this please. One open question for me: How does we get the install-/uninstall-link in Paper-UI.

kaikreuzer commented 6 years ago

@smerschjohann I am not sure I fully understand what is needed. For Jython & Groovy, all that was required is to have the jars on the classpath on the runtime during execution and there was no need to have them during compile time. Is this the same situation for Kotlin and is all you ask for a simple mechanism to put those jars on the classpath (without having to ask users to put it in ext in their JVM)?

If so, I think @lewie's suggestion is the best way to solve it - we would simply define Karaf features for openHAB, which make those Jars available easily. There would be no need to deal with licensing checks & approvals here at ESH at all.

smerschjohann commented 6 years ago

@kaikreuzer Yes, for most scripting languages, it is enough to add them to the classpath. But this is not the case if using kotlin. For kotlin to work, you also need to add to /resources/META-INF/services/javax.script.ScriptEngineFactory: org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory. This collides with OSGI as it does not play well with the java ServiceLoader interface. There is an kotlin-osgi bundle, but as the documentation lacks any details, I'm currently not sure what is needed to get kotlin as script engine to work in openhab.

Still, even if we have to provide some additional files, I think it is still a nice idea to define them as features for openhab. I tried to get kotlin to work in the eclipse environment, but I'm missing some information: Can I define dependencies in the pom and run some eclipse task to have it resolve the maven dependencies? This would be nice to figure out what is really required for kotlin support. If not, do we have to add libs statically? Is this feature concept something completely unrelated to the eclipse dev environment?

It is quiet hard for me to integrate such feature to openhab as I am not exactly sure how it all fits together.

smerschjohann commented 6 years ago

@lewie thanks for this writeup. But I still find it hard to get it to run with this information. Some of the file locations you mention are not available in my dev environment.

lewie commented 6 years ago

@smerschjohann, import the related Projects into your dev environment. Or for a short test, manipulate feature.xml and openhab-core\poms\pom.xml file without import in filesystem directly.

Did you test to copy the kotlin-osgi bundle to addons folder? Then you can see if it starts. After, then hopefully you can test if kotlin scripts are started in conf/automation/jsr223.