Lukas Cerny пре 2 година
родитељ
комит
cbf4db1cec
18 измењених фајлова са 82 додато и 54 уклоњено
  1. 11 0
      connector-app/src/main/java/cz/senslog/connector/app/Application.java
  2. 1 1
      connector-app/src/main/java/cz/senslog/connector/app/config/AppConfig.java
  3. 2 2
      connector-app/src/main/java/cz/senslog/connector/app/config/ConfigurationServiceImpl.java
  4. 11 12
      connector-app/src/main/java/cz/senslog/connector/app/config/Connector.java
  5. 5 5
      connector-app/src/main/java/cz/senslog/connector/app/config/ConnectorBuilder.java
  6. 0 10
      connector-app/src/main/java/cz/senslog/connector/app/config/FileConfigurationServiceImpl.java
  7. 0 1
      connector-app/src/main/java/cz/senslog/connector/app/config/api/ConfigurationService.java
  8. 1 1
      connector-fetch-alapans/src/main/java/cz/senslog/connector/fetch/alapans/AlapansProxySession.java
  9. 13 1
      connector-fetch-api/src/main/java/cz/senslog/connector/fetch/api/ExecutableFetcher.java
  10. 25 2
      connector-fetch-api/src/main/java/cz/senslog/connector/fetch/api/FetchProxySession.java
  11. 3 0
      connector-model/src/main/java/cz/senslog/connector/model/api/JsonSerializable.java
  12. 3 0
      connector-model/src/main/java/cz/senslog/connector/model/api/VoidSession.java
  13. 0 10
      connector-model/src/main/java/cz/senslog/connector/model/config/UnitSensorChecker.java
  14. 0 1
      connector-model/src/main/java/cz/senslog/connector/model/converter/ModelConverterProvider.java
  15. 1 1
      connector-push-senslog-v1/src/main/java/cz/senslog/connector/push/rest/senslog/v1/SenslogV1Pusher.java
  16. 0 5
      connector-tools/src/main/java/cz/senslog/connector/tools/http/HttpClient.java
  17. 4 0
      connector-tools/src/main/java/cz/senslog/connector/tools/http/HttpCookie.java
  18. 2 2
      connector-tools/src/main/java/cz/senslog/connector/tools/util/ClassUtils.java

+ 11 - 0
connector-app/src/main/java/cz/senslog/connector/app/Application.java

@@ -71,6 +71,9 @@ class Application extends Thread {
         this.params = parameters;
     }
 
+    /**
+     * Override method from {@link Thread} to clean up all scheduled processes after the application is being determined.
+     */
     @Override
     public void interrupt() {
         logger.info("Stopping the application {} version {}", appConfig.getName(), appConfig.getVersion());
@@ -90,6 +93,14 @@ class Application extends Thread {
         super.interrupt();
     }
 
+    /**
+     * The main method executing the application.
+     * 1. Contains configuration services, which provide configuration for modules.
+     *      Only configuration by file is available and required passing the filename as an execution parameter.
+     * 2. All configured modules as loaded and initialized by its configuration.
+     * 3. The class {@link ConnectorBuilder} creates connectors that then are scheduled by the scheduler.
+     *      Each connector is running within a thread separately.
+     */
     @Override
     public void run() {
         logger.info("Starting the application {} version {}", appConfig.getName(), appConfig.getVersion());

+ 1 - 1
connector-app/src/main/java/cz/senslog/connector/app/config/AppConfig.java

@@ -24,7 +24,7 @@ public class AppConfig {
     private static final Logger logger = LogManager.getLogger(AppConfig.class);
 
 
-    /** Name of the properties configuration file. */
+    /** Name of the properties configuration file located in the 'resources' folder. */
     private static final String PROPERTIES_FILE_NAME = "project.properties";
 
     /** Attribute of loaded properties. */

+ 2 - 2
connector-app/src/main/java/cz/senslog/connector/app/config/ConfigurationServiceImpl.java

@@ -22,8 +22,8 @@ import java.util.Set;
  */
 public abstract class ConfigurationServiceImpl implements ConfigurationService {
 
-    private Set<ConnectorDescriptor> connectorDescriptors;
-    private Map<String, DefaultConfig> configurations;
+    private final Set<ConnectorDescriptor> connectorDescriptors;
+    private final Map<String, DefaultConfig> configurations;
 
     ConfigurationServiceImpl() {
         this.connectorDescriptors = new HashSet<>();

+ 11 - 12
connector-app/src/main/java/cz/senslog/connector/app/config/Connector.java

@@ -27,7 +27,6 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS;
 /**
  * The class {@code Connector} represents a created connector
  * which allows to be scheduled by defined period.
- *
  * The idea is to wrap functionality of a connector. The flow is
  * defined as 'fetcher' -> 'converter' -> 'pusher'.
  *
@@ -52,18 +51,18 @@ public final class Connector {
     private final ExecutableFetcher<? super AbstractModel> fetcherExecutor;
 
     /** Instance of a pusher that receives data. */
-    private final ConnectorPusher<? super AbstractModel> pusher; // TODO refactor to a ExecutableFetcher
+    private final ConnectorPusher<? super AbstractModel> pusher;
 
     /** Converter between fetch and push. */
     private final Converter<? super AbstractModel, ? super AbstractModel> converter;
 
     /** Period for scheduler. */
-    private final Optional<Integer> period;
+    private final Integer period;
 
     /** Initialization delay for scheduler. */
-    private final Optional<Integer> initDelay;
+    private final Integer initDelay;
 
-    private final Optional<LocalTime> startAt;
+    private final LocalTime startAt;
 
     /**
      * Constructor allows to set all attributes.
@@ -86,9 +85,9 @@ public final class Connector {
         this.fetcherExecutor = fetcherExecutor;
         this.pusher = pusher;
         this.converter = converter;
-        this.period = ofNullable(period != null ? period * 1_000 : null); // to millis
-        this.initDelay = ofNullable(initDelay != null ? initDelay * 1_000 : null); // to millis
-        this.startAt = ofNullable(startAt);
+        this.period = period != null ? period * 1_000 : null; // to millis
+        this.initDelay = initDelay != null ? initDelay * 1_000 : null; // to millis
+        this.startAt = startAt;
     }
 
     /**
@@ -104,7 +103,7 @@ public final class Connector {
      * @return period for scheduler.
      */
     public Optional<Integer> getPeriod() {
-        return period;
+        return ofNullable(period);
     }
 
     /**
@@ -112,7 +111,7 @@ public final class Connector {
      * @return delay for scheduler.
      */
     public Optional<Integer> getInitDelay() {
-        return initDelay;
+        return ofNullable(initDelay);
     }
 
     /**
@@ -120,7 +119,7 @@ public final class Connector {
      * @return time to execute the connector
      */
     public Optional<LocalTime> getStartAt() {
-        return startAt;
+        return ofNullable(startAt);
     }
 
     /**
@@ -132,7 +131,7 @@ public final class Connector {
     }
 
     /**
-     * Schedules connector according by settings. Input parameters are scheduled service
+     * Schedules connector according to settings. Input parameters are scheduled service
      * {@link ScheduledExecutorService} and {@link CountDownLatch} uses as a thread barrier.
      * @param scheduledService - scheduled service
      * @param latch - thread counter

+ 5 - 5
connector-app/src/main/java/cz/senslog/connector/app/config/ConnectorBuilder.java

@@ -72,9 +72,9 @@ public final class ConnectorBuilder {
      * Creates and returns new instance of fetcher.
      * @param fetchProviderId - id of fetcher provider.
      * @return new instance of fetcher {@code ExecutableFetcher}.
-     * @throws Exception throws if the fetch provider does not exist or any configuration does not exists.
+     * @throws Exception throws if the fetch provider does not exist or any configuration does not exist.
      */
-    private ExecutableFetcher createFetcherExecutor(String fetchProviderId) throws Exception {
+    private ExecutableFetcher<?> createFetcherExecutor(String fetchProviderId) throws Exception {
         logger.debug("Creating a new instance of fetcher for {}.", fetchProviderId);
 
         DefaultConfig config = configService.getConfigForProviderId(fetchProviderId);
@@ -98,9 +98,9 @@ public final class ConnectorBuilder {
      * Creates and returns new instance of pusher.
      * @param pushProviderId - class of push provider.
      * @return new instance of pusher {@code ConnectorPusher}.
-     * @throws Exception throws if the push provider does not exist or any configuration does not exists.
+     * @throws Exception throws if the push provider does not exist or any configuration does not exist.
      */
-    private ConnectorPusher getPusherInstance(String pushProviderId) throws Exception {
+    private ConnectorPusher<?> getPusherInstance(String pushProviderId) throws Exception {
         logger.debug("Creating a new instance of pusher for {}.", pushProviderId);
 
         DefaultConfig config = configService.getConfigForProviderId(pushProviderId);
@@ -184,7 +184,7 @@ public final class ConnectorBuilder {
      * Input class could be type of {@code ConnectorFetcher} or {@code ConnectorPusher}.
      * @param aClass - class contain generic parameters.
      * @return generic parameter extended from {@link AbstractModel}.
-     * @throws Exception throws if can not be get generic parameter from the input class.
+     * @throws Exception throws if the model can not got as a generic parameter from the input class.
      */
     @SuppressWarnings("unchecked")
     private Class<? extends AbstractModel> getAbstractModelFromGeneric(Class aClass) throws Exception {

+ 0 - 10
connector-app/src/main/java/cz/senslog/connector/app/config/FileConfigurationServiceImpl.java

@@ -18,43 +18,33 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.time.LocalTime;
-import java.time.format.DateTimeFormatter;
 import java.util.*;
 
 import static java.time.format.DateTimeFormatter.ofPattern;
 
 /**
  * The class {@code FileConfigurationServiceImpl} represents an implementation of {@link FileConfigurationService}.
- *
  * Configuration file is in YAML format and contains two major groups: 'settings' and 'connectors'.
- *
  * <h2>connectors</h2>
  * Each connector must contain following attributes:
  *  - name of connector
  *  - ID of fetch provider (ID is mentioned in 'settings' group)
  *  - ID of push provider (ID is mentioned in 'settings' group)
  *  - period in second when the connector will be scheduled
- *
  *  Example:
- *
  *      connectors:
  *          - ConnectorName:
  *              fetcher: "<id>"
  *              pusher: "<id>"
  *              period: 60
- *
- *
  * <h2>settings</h2>
  * Each provider must contain basic attributes:
  *  - identifier of provider
  *  - name of provider
  *  - provider class
- *
  *  Other attributes are dynamically loaded when they are needed
  *  but must keep the key and value syntax.
- *
  *  Example:
- *
  *      settings:
  *          - ProviderID:
  *              name: "<name>"

+ 0 - 1
connector-app/src/main/java/cz/senslog/connector/app/config/api/ConfigurationService.java

@@ -13,7 +13,6 @@ import java.util.Set;
 /**
  * The interface {@code ConfigurationService} provides a generic service for configuration.
  * Configuration can be gotten from a file, database or anything else.
- *
  * Provides two crucial functionalities:
  *  - returns set of connector descriptors
  *  - returns configuration for a class

+ 1 - 1
connector-fetch-alapans/src/main/java/cz/senslog/connector/fetch/alapans/AlapansProxySession.java

@@ -56,7 +56,7 @@ public class AlapansProxySession extends FetchProxySession<AlapansSession, Alapa
                     config.getSenslogAuth().getPassword()
             );
 
-            if (!authCookie.isSecure()) {
+            if (authCookie.isNotSecure()) {
                 logger.warn("Auth cookie is not valid to be used.");
                 return session;
             }

+ 13 - 1
connector-fetch-api/src/main/java/cz/senslog/connector/fetch/api/ExecutableFetcher.java

@@ -5,11 +5,18 @@ package cz.senslog.connector.fetch.api;
 
 import cz.senslog.connector.model.api.AbstractModel;
 import cz.senslog.connector.model.api.ProxySessionModel;
+import cz.senslog.connector.tools.interceptor.ProxyUtils;
 
 import java.util.Optional;
 
 import static cz.senslog.connector.tools.interceptor.ProxyUtils.createProxy;
 
+/**
+ * The class contains methods to create executable fetcher (i.e., {@link ConnectorFetcher})
+ * either by direct access to the instance or through a proxy (i.e., {@link FetchProxySession}).
+ *
+ * @param <T> model representing the fetched data as an inherited class of the {@link AbstractModel}
+ */
 public class ExecutableFetcher<T extends AbstractModel> {
 
     private final ConnectorFetcher<? extends ProxySessionModel, T> rawFetcher;
@@ -19,7 +26,7 @@ public class ExecutableFetcher<T extends AbstractModel> {
     public static <S extends ProxySessionModel, M extends AbstractModel> ExecutableFetcher<M> create(
             ConnectorFetcher<S, M> rawFetcher
     ) {
-        return new ExecutableFetcher<M>(rawFetcher, rawFetcher);
+        return new ExecutableFetcher<>(rawFetcher, rawFetcher);
     }
 
     public static <S extends ProxySessionModel, M extends AbstractModel> ExecutableFetcher<M> createWithProxySession(
@@ -29,6 +36,11 @@ public class ExecutableFetcher<T extends AbstractModel> {
         return new ExecutableFetcher<M>(object, createProxy(object.getClass(), proxySession));
     }
 
+    /**
+     * Private constructor sets fetchers. If the session is disabled, then 'rawFetcher' and 'fetcher' are the same instances.
+     * @param rawFetcher fetcher that retrieves data
+     * @param fetcher fetcher that is wrap by session if enabled.
+     */
     private ExecutableFetcher(
             ConnectorFetcher<? extends ProxySessionModel, T> rawFetcher,
             ConnectorFetcher<? extends ProxySessionModel, T> fetcher

+ 25 - 2
connector-fetch-api/src/main/java/cz/senslog/connector/fetch/api/FetchProxySession.java

@@ -12,6 +12,19 @@ import java.util.Optional;
 import static cz.senslog.connector.tools.util.MethodExplorer.loadMethod;
 import static java.util.Optional.*;
 
+/**
+ * The {@link ExecutableFetcher} may require additional preprocessing for its fetch task,
+ *      e.g., get an initial timestamp as a latest on of persisted data
+ * For such task is responsible the method {@link #preProcessing(Object[])}, which is executed before {@link ConnectorFetcher#fetch(Optional)}.
+ * The class includes session as an inherited instance of {@link ProxySessionModel}.
+ * One the session is created, it lives through the entire time and can be used to store data.
+ * The same logic is applied to the method {@link #postProcessing(Object)}, which is executed after {@link  ConnectorFetcher#fetch(Optional)}.
+ *
+ * @param <S> data model for the session as an inherited class of the {@link ProxySessionModel}
+ * @param <T> model representing the fetched data as an inherited class of the {@link AbstractModel}
+ *
+ * @author Lukas Cerny
+ */
 public abstract class FetchProxySession<S extends ProxySessionModel, T extends AbstractModel> extends AbstractMethodInterceptor {
 
     private Optional<S> optionalSession;
@@ -21,8 +34,18 @@ public abstract class FetchProxySession<S extends ProxySessionModel, T extends A
         this.optionalSession = Optional.empty();
     }
 
+    /**
+     * Method to override that may include logic before the fetch is executed.
+     * @param previousSession session of the previous run
+     * @return session
+     */
     protected abstract S preProcessing(Optional<S> previousSession);
 
+    /**
+     * Method to override that may include logic to transform fetched data.
+     * @param model retrieved data after fetch
+     * @param session current session of the ongoing run
+     */
     protected abstract void postProcessing(Optional<T> model, Optional<S> session);
 
     protected ConnectorFetcher<S, T> getObject() {
@@ -30,7 +53,7 @@ public abstract class FetchProxySession<S extends ProxySessionModel, T extends A
     }
 
     @Override
-    public Object[] preProcessing(Object[] methodParams) {
+    public final Object[] preProcessing(Object[] methodParams) {
         S s = preProcessing(optionalSession);
         if (s.isActive()) {
             optionalSession = of(s);
@@ -42,7 +65,7 @@ public abstract class FetchProxySession<S extends ProxySessionModel, T extends A
     }
 
     @Override
-    public Object postProcessing(Object returnObject) {
+    public final Object postProcessing(Object returnObject) {
         Optional<T> returnOpt = ofNullable((T)returnObject);
         postProcessing(returnOpt, optionalSession);
         return returnObject;

+ 3 - 0
connector-model/src/main/java/cz/senslog/connector/model/api/JsonSerializable.java

@@ -3,6 +3,9 @@
 
 package cz.senslog.connector.model.api;
 
+/**
+ * Interface to mark classes that must implement the method {@link #toJson()}.
+ */
 public interface JsonSerializable {
     String toJson();
 }

+ 3 - 0
connector-model/src/main/java/cz/senslog/connector/model/api/VoidSession.java

@@ -1,5 +1,8 @@
 package cz.senslog.connector.model.api;
 
+/**
+ * The class signalizes that the session is not active and no data is expecting.
+ */
 public class VoidSession extends ProxySessionModel {
 
     public VoidSession() {

+ 0 - 10
connector-model/src/main/java/cz/senslog/connector/model/config/UnitSensorChecker.java

@@ -1,10 +0,0 @@
-// Copyright (c) 2020 UWB & LESP.
-// The UWB & LESP license this file to you under the MIT license.
-
-package cz.senslog.connector.model.config;
-
-public interface UnitSensorChecker<U, S> {
-
-    boolean isValid(U unit);
-    boolean isValid(U unit, S sensor);
-}

+ 0 - 1
connector-model/src/main/java/cz/senslog/connector/model/converter/ModelConverterProvider.java

@@ -8,7 +8,6 @@ import cz.senslog.connector.model.api.ConverterProvider;
 /**
  * The class {@see ModelConverterProvider} represents a configuration class
  * for all converters which can be used for models between fetchers and pushers.
- *
  * The method {@see ModelConverterProvider#config} provides the registration
  * for a new converter.
  *

+ 1 - 1
connector-push-senslog-v1/src/main/java/cz/senslog/connector/push/rest/senslog/v1/SenslogV1Pusher.java

@@ -189,7 +189,7 @@ class  SenslogV1Pusher implements ConnectorPusher<SenslogV1Model> {
         HttpRequest request;
         if (authCookie == null) {
             request = reqBuilder.build();
-        } else if (!authCookie.isSecure()) {
+        } else if (authCookie.isNotSecure()) {
             logger.error("Auth cookie is not valid to be used."); return "false";
         } else {
             request = reqBuilder.addCookie(authCookie).build();

+ 0 - 5
connector-tools/src/main/java/cz/senslog/connector/tools/http/HttpClient.java

@@ -63,11 +63,6 @@ public class HttpClient {
     private final org.apache.hc.client5.http.classic.HttpClient client;
     private final CookieStore cookieStore;
 
-    static {
-//        System.setProperty("javax.net.ssl.trustStore", "/home/vrenclouff/Downloads/temp/clienttruststore.jks");
-//        System.setProperty("javax.net.ssl.keyStorePassword", "password");
-    }
-
     private static HttpClient newClient() {
 
         PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create()

+ 4 - 0
connector-tools/src/main/java/cz/senslog/connector/tools/http/HttpCookie.java

@@ -40,6 +40,10 @@ public class HttpCookie {
         return this.cookie.isSecure();
     }
 
+    public boolean isNotSecure() {
+        return !this.cookie.isSecure();
+    }
+
     public BasicClientCookie get() {
         return this.cookie;
     }

+ 2 - 2
connector-tools/src/main/java/cz/senslog/connector/tools/util/ClassUtils.java

@@ -13,9 +13,9 @@ public class ClassUtils {
 
     /**
      * Provides type safe functionality of casting. Can be used only in case,
-     * if is casted a value which is direct type of the class (inheritance is not supported).
+     * if is cast a value which is direct type of the class (inheritance is not supported).
      *
-     * @param value - value to be casted.
+     * @param value - value to be cast.
      * @param castClass - class for casting.
      * @param <T> - generic type of casting.
      * @return casted value.