You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cg...@apache.org on 2024/09/24 12:33:27 UTC

(plc4x-extras) branch feature/merlot updated: Working on boot sequence.

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

cgarcia pushed a commit to branch feature/merlot
in repository https://gitbox.apache.org/repos/asf/plc4x-extras.git


The following commit(s) were added to refs/heads/feature/merlot by this push:
     new ad29d73  Working on boot sequence.
ad29d73 is described below

commit ad29d735e1ad40c435497999de3fc9607446ff22
Author: César García <ce...@ceos.com.ve>
AuthorDate: Tue Sep 24 08:33:14 2024 -0400

    Working on boot sequence.
---
 .../plc4x/merlot/api/impl/PlcSecureBootImpl.java   | 28 ++++++-
 .../merlot/org.apache.plc4x.merlot.db/pom.xml      |  4 +-
 .../apache/plc4x/merlot/db/impl/DBRecoverImpl.java | 88 ++++++++++++++++++++++
 3 files changed, 115 insertions(+), 5 deletions(-)

diff --git a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcSecureBootImpl.java b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcSecureBootImpl.java
index 641b9de..e8740be 100644
--- a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcSecureBootImpl.java
+++ b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcSecureBootImpl.java
@@ -33,6 +33,10 @@ import org.apache.plc4x.merlot.api.PlcGeneralFunction;
 import org.apache.plc4x.merlot.api.PlcSecureBoot;
 import org.apache.plc4x.merlot.scheduler.api.Job;
 import org.apache.plc4x.merlot.scheduler.api.JobContext;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventAdmin;
 import org.osgi.service.jdbc.DataSourceFactory;
 import org.slf4j.LoggerFactory;
 
@@ -40,6 +44,8 @@ import org.slf4j.LoggerFactory;
 public class PlcSecureBootImpl implements PlcSecureBoot, Job {
     private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(PlcSecureBootImpl.class);
     private static final String DB_URL = "jdbc:sqlite:data/boot.db";
+    private static final String EVENT_PERSIST = "org/apache/plc4x/merlot/PERSIST";   
+    private static final String EVENT_RECOVER = "org/apache/plc4x/merlot/RECOVER";     
         
     private static final String SQL_CREATE_TABLE_DEVICES = 
             "CREATE TABLE IF NOT EXISTS Devices("
@@ -82,12 +88,14 @@ public class PlcSecureBootImpl implements PlcSecureBoot, Job {
     
     private Map<String, PlcDriver> delayedBootPlcDivers = new ConcurrentHashMap<>();
     
+    private final BundleContext ctx;
     private final PlcGeneralFunction plcGeneralFunction;
     
     DataSourceFactory dsFactory = null;
     Connection dbConnection = null;
 
-    public PlcSecureBootImpl(PlcGeneralFunction plcGeneralFunction) {
+    public PlcSecureBootImpl(BundleContext ctx, PlcGeneralFunction plcGeneralFunction) {
+        this.ctx = ctx;
         this.plcGeneralFunction = plcGeneralFunction;
     }
         
@@ -111,7 +119,8 @@ public class PlcSecureBootImpl implements PlcSecureBoot, Job {
                         String remarks = resultSet.getString("REMARKS"); 
                       }
                     }                    
-                    
+                    dbConnection.commit();
+                    dbConnection.close();                    
                 }
             } catch (SQLException ex) {
                 LOGGER.info(ex.getMessage());
@@ -169,8 +178,15 @@ public class PlcSecureBootImpl implements PlcSecureBoot, Job {
     public void persist() {
         var plcDrivers = plcGeneralFunction.getPlcDrivers();
         plcDrivers.forEach( (k, d) -> store(k));
+        ServiceReference ref = ctx.getServiceReference(EventAdmin.class.getName());
+        if (ref != null){
+            EventAdmin eventAdmin = (EventAdmin) ctx.getService(ref);
+            Event eventPersist = new Event(EVENT_PERSIST, (Map) null); 
+            eventAdmin.sendEvent(eventPersist);            
+        }
     }
 
+    
     @Override
     public void store(String plcDriver) {
         var plcDevices = plcGeneralFunction.getPlcDevices(plcDriver);
@@ -194,7 +210,13 @@ public class PlcSecureBootImpl implements PlcSecureBoot, Job {
     
     @Override
     public void restore(String plcDriver) {
-        throw new UnsupportedOperationException("Not supported yet.");
+
+        ServiceReference ref = ctx.getServiceReference(EventAdmin.class.getName());
+        if (ref != null){
+            EventAdmin eventAdmin = (EventAdmin) ctx.getService(ref);
+            Event eventPersist = new Event(EVENT_RECOVER, (Map) null); 
+            eventAdmin.sendEvent(eventPersist);            
+        }        
     }
     
     
diff --git a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/pom.xml b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/pom.xml
index 6ef8333..d6ff50f 100644
--- a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/pom.xml
+++ b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/pom.xml
@@ -57,8 +57,8 @@
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-compiler-plugin</artifactId>
                 <configuration>
-                    <source>1.8</source>
-                    <target>1.8</target>
+                    <source>18</source>
+                    <target>18</target>
                     <maxmem>256M</maxmem>
                 </configuration>
             </plugin>
diff --git a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/impl/DBRecoverImpl.java b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/impl/DBRecoverImpl.java
new file mode 100644
index 0000000..16bf616
--- /dev/null
+++ b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/impl/DBRecoverImpl.java
@@ -0,0 +1,88 @@
+/*
+ * 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
+ *
+ *   https://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.merlot.db.impl;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Properties;
+import javax.sql.DataSource;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventHandler;
+import org.osgi.service.jdbc.DataSourceFactory;
+import org.slf4j.LoggerFactory;
+
+public class DBRecoverImpl implements EventHandler{    
+    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DBRecoverImpl.class);
+    private static final String DB_URL = "jdbc:sqlite:data/boot.db";
+    private static final String EVENT_PERSIST = "org/apache/plc4x/merlot/PERSIST";   
+    private static final String EVENT_RECOVER = "org/apache/plc4x/merlot/RECOVER"; 
+
+    DataSourceFactory dsFactory = null;
+    Connection dbConnection = null;    
+    
+    public void init() {
+        LOGGER.info("INIT");  
+        if (null != dsFactory) {
+            Properties props = new Properties();
+            props.setProperty(DataSourceFactory.JDBC_URL, DB_URL);
+
+            try {
+                DataSource ds = dsFactory.createDataSource(props);
+                dbConnection = ds.getConnection();
+                if (null != dbConnection) {
+                    var databaseMetaData = dbConnection.getMetaData();
+                    LOGGER.info("Boot driver name is {}.", databaseMetaData.getDriverName());
+                    createTables();
+                    //Catalog,Schema, Table pattern,types of tables
+                    try(ResultSet resultSet = databaseMetaData.getTables(null, null, null, new String[]{"TABLE"})){ 
+                      while(resultSet.next()) { 
+                        String tableName = resultSet.getString("TABLE_NAME"); 
+                        String remarks = resultSet.getString("REMARKS"); 
+                      }
+                    }                    
+                    dbConnection.commit();
+                    dbConnection.close();
+                }
+            } catch (SQLException ex) {
+                LOGGER.info(ex.getMessage());
+            }
+        }        
+    }
+    
+    public void destroy() {
+        LOGGER.info("DESTROY");        
+    }    
+    
+    public void bindDataSourceFactory(DataSourceFactory dsFactory) {
+        this.dsFactory = dsFactory;
+        init();
+    }      
+    
+    
+    @Override
+    public void handleEvent(Event event) {
+        LOGGER.info("HANDLE EVENT");
+    }
+    
+    private void createTables() throws SQLException{
+        LOGGER.info("CREATE TABLES");        
+    }    
+    
+}