You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by br...@apache.org on 2017/12/22 09:16:57 UTC

[incubator-plc4x] 03/07: First Scala wrapper implementations

This is an automated email from the ASF dual-hosted git repository.

britter pushed a commit to branch PLC4X-12
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git

commit 87b0e7290b98f23ad5b1fe831837b0bb521039bd
Author: Benedikt Ritter <be...@codecentric.de>
AuthorDate: Sat Nov 18 17:47:38 2017 +0100

    First Scala wrapper implementations
---
 .../org/apache/plc4x/scala/api/PlcDriver.scala     |  2 +-
 plc4s/{ => core}/pom.xml                           | 62 ++++++++++------------
 .../plc4x/scala/core/JPlcConnectionWrapper.scala   | 39 ++++++++++++++
 .../plc4x/scala/core/JPlcDriverWrapper.scala       | 42 +++++++++++++++
 .../apache/plc4x/scala/core/PlcDriverManager.scala | 40 ++++++++++++++
 plc4s/pom.xml                                      |  1 +
 6 files changed, 150 insertions(+), 36 deletions(-)

diff --git a/plc4s/api/src/main/scala/org/apache/plc4x/scala/api/PlcDriver.scala b/plc4s/api/src/main/scala/org/apache/plc4x/scala/api/PlcDriver.scala
index edf93a4..647ef0f 100644
--- a/plc4s/api/src/main/scala/org/apache/plc4x/scala/api/PlcDriver.scala
+++ b/plc4s/api/src/main/scala/org/apache/plc4x/scala/api/PlcDriver.scala
@@ -19,7 +19,7 @@ under the License.
 package org.apache.plc4x.scala.api
 
 import org.apache.plc4x.java.authentication.PlcAuthentication
-import org.apache.plc4x.java.connection.PlcConnection
+import org.apache.plc4x.scala.api.connection.PlcConnection
 
 /**
   * General interface defining the minimal methods required for adding a new type of driver to the PLC4J system.
diff --git a/plc4s/pom.xml b/plc4s/core/pom.xml
similarity index 53%
copy from plc4s/pom.xml
copy to plc4s/core/pom.xml
index bc3bd0c..07d6fb1 100644
--- a/plc4s/pom.xml
+++ b/plc4s/core/pom.xml
@@ -24,48 +24,40 @@
 
   <parent>
     <groupId>org.apache.plx4x</groupId>
-    <artifactId>plc4x-parent</artifactId>
+    <artifactId>plc4s</artifactId>
     <version>0.0.1-SNAPSHOT</version>
   </parent>
 
-  <artifactId>plc4s</artifactId>
+  <artifactId>plc4s-core</artifactId>
   <version>0.0.1-SNAPSHOT</version>
-  <packaging>pom</packaging>
 
-  <name>PLC4S</name>
-  <description>Implementation of the protocol adapters for usage as Scala library.</description>
-
-  <modules>
-    <module>api</module>
-  </modules>
+  <name>PLC4S: Core</name>
+  <description>Implementation of the core PLC4J system containing the DriverManger.</description>
 
   <build>
-    <pluginManagement>
-      <plugins>
-        <plugin>
-          <groupId>net.alchim31.maven</groupId>
-          <artifactId>scala-maven-plugin</artifactId>
-          <version>3.3.1</version>
-          <configuration>
-            <scalaVersion>2.12.4</scalaVersion>
-          </configuration>
-        </plugin>
-      </plugins>
-    </pluginManagement>
+    <sourceDirectory>src/main/scala</sourceDirectory>
+    <plugins>
+      <plugin>
+        <groupId>net.alchim31.maven</groupId>
+        <artifactId>scala-maven-plugin</artifactId>
+      </plugin>
+    </plugins>
   </build>
 
-  <dependencyManagement>
-    <dependencies>
-      <dependency>
-        <groupId>org.apache.plx4x</groupId>
-        <artifactId>plc4j-api</artifactId>
-        <version>0.0.1-SNAPSHOT</version>
-      </dependency>
-      <dependency>
-        <groupId>org.apache.plx4x</groupId>
-        <artifactId>plc4j-core</artifactId>
-        <version>0.0.1-SNAPSHOT</version>
-      </dependency>
-    </dependencies>
-  </dependencyManagement>
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.plx4x</groupId>
+      <artifactId>plc4j-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.plx4x</groupId>
+      <artifactId>plc4j-core</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.plx4x</groupId>
+      <artifactId>plc4s-api</artifactId>
+      <version>0.0.1-SNAPSHOT</version>
+    </dependency>
+  </dependencies>
+
 </project>
\ No newline at end of file
diff --git a/plc4s/core/src/main/scala/org/apache/plc4x/scala/core/JPlcConnectionWrapper.scala b/plc4s/core/src/main/scala/org/apache/plc4x/scala/core/JPlcConnectionWrapper.scala
new file mode 100644
index 0000000..8c41865
--- /dev/null
+++ b/plc4s/core/src/main/scala/org/apache/plc4x/scala/core/JPlcConnectionWrapper.scala
@@ -0,0 +1,39 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+package org.apache.plc4x.scala.core
+
+import org.apache.plc4x.java.connection.{PlcConnection => JPlcConnection}
+import org.apache.plc4x.scala.api.connection.PlcConnection
+
+private[core] class JPlcConnectionWrapper(val jPlcConnection: JPlcConnection) extends PlcConnection  {
+    /**
+      * Establishes the connection to the remote PLC.
+      *
+      * @return Either nothing in case the connection could be established or an PlcError.
+      */
+    override def connect() = ???
+
+    /**
+      * Parses a PLC/protocol dependent address string into an Address object.
+      *
+      * @param addressString String representation of an address for the current type of PLC/protocol.
+      * @return Either the Address object identifying an address for the current type of PLC/protocol or a PlcError.
+      */
+    override def parseAddress(addressString: String) = ???
+}
diff --git a/plc4s/core/src/main/scala/org/apache/plc4x/scala/core/JPlcDriverWrapper.scala b/plc4s/core/src/main/scala/org/apache/plc4x/scala/core/JPlcDriverWrapper.scala
new file mode 100644
index 0000000..49859ec
--- /dev/null
+++ b/plc4s/core/src/main/scala/org/apache/plc4x/scala/core/JPlcDriverWrapper.scala
@@ -0,0 +1,42 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+package org.apache.plc4x.scala.core
+
+import org.apache.plc4x.java.authentication.PlcAuthentication
+import org.apache.plc4x.java.{PlcDriver => JPlcDriver}
+import org.apache.plc4x.scala.api.{PlcConnectionError, PlcDriver}
+
+import scala.util.{Failure, Success, Try}
+
+private[core] class JPlcDriverWrapper(val jPlcDriver: JPlcDriver) extends PlcDriver {
+
+    override def protocolCode = jPlcDriver.getProtocolCode
+
+    override def protocolName = jPlcDriver.getProtocolCode
+
+    override def connect(url: String) = Try(jPlcDriver.connect(url)) match {
+        case Success(jConnection) => Right(new JPlcConnectionWrapper(jConnection))
+        case Failure(ex) => Left(PlcConnectionError(ex.getMessage))
+    }
+
+    override def connect(url: String, authentication: PlcAuthentication) = Try(jPlcDriver.connect(url, authentication)) match {
+        case Success(jConnection) => Right(new JPlcConnectionWrapper(jConnection))
+        case Failure(ex) => Left(PlcConnectionError(ex.getMessage))
+    }
+}
diff --git a/plc4s/core/src/main/scala/org/apache/plc4x/scala/core/PlcDriverManager.scala b/plc4s/core/src/main/scala/org/apache/plc4x/scala/core/PlcDriverManager.scala
new file mode 100644
index 0000000..093033f
--- /dev/null
+++ b/plc4s/core/src/main/scala/org/apache/plc4x/scala/core/PlcDriverManager.scala
@@ -0,0 +1,40 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+package org.apache.plc4x.scala.core
+
+import org.apache.plc4x.java.{PlcDriverManager => JPlcDriverManager}
+import org.apache.plc4x.scala.api.PlcDriver
+import org.apache.plc4x.scala.api.PlcConnectionError
+
+import scala.util.{Failure, Success, Try}
+import scala.util.control.Exception._
+
+class PlcDriverManager private(val jPlcDriverManager: JPlcDriverManager){
+
+    def this(classLoader: ClassLoader) = this(new JPlcDriverManager(classLoader))
+
+    def this() = this(Thread.currentThread.getContextClassLoader)
+
+    def getDriver(url: String): Either[PlcConnectionError, PlcDriver] = {
+        Try(jPlcDriverManager.getDriver(url)) match {
+            case Success(jDriver) => Right(new JPlcDriverWrapper(jDriver))
+            case Failure(ex) => Left(PlcConnectionError(ex.getMessage))
+        }
+    }
+}
diff --git a/plc4s/pom.xml b/plc4s/pom.xml
index bc3bd0c..08235bf 100644
--- a/plc4s/pom.xml
+++ b/plc4s/pom.xml
@@ -37,6 +37,7 @@
 
   <modules>
     <module>api</module>
+    <module>core</module>
   </modules>
 
   <build>

-- 
To stop receiving notification emails like this one, please contact
"commits@plc4x.apache.org" <co...@plc4x.apache.org>.