| /** |
| * |
| * Copyright (c) 2011, 2018 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| * |
| */ |
| package org.eclipse.osbp.xtext.signal.common; |
| |
| import java.io.File; |
| import java.io.FileInputStream; |
| import java.io.FileOutputStream; |
| import java.io.IOException; |
| import java.io.PrintWriter; |
| import java.io.StringWriter; |
| import java.net.MalformedURLException; |
| import java.net.URL; |
| import java.nio.file.Files; |
| import java.nio.file.Path; |
| import java.nio.file.Paths; |
| import java.util.ArrayList; |
| import java.util.HashMap; |
| import java.util.Map; |
| import java.util.Properties; |
| import java.util.concurrent.FutureTask; |
| |
| import org.eclipse.osbp.preferences.ProductConfiguration; |
| import org.eclipse.osbp.xtext.datainterchange.common.WorkerThreadRunnable; |
| import org.quartz.CronExpression; |
| import org.quartz.DateBuilder; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| /** |
| * This class contains all common data to be used either from |
| * {@link OSBPSignalWatcher} or {@link OSBPSignalScheduler}. |
| */ |
| public abstract class OSBPSignalCommonData { |
| /** the log */ |
| private static Logger log = LoggerFactory.getLogger("signal"); |
| /** the list of all tasks, data interchange unit to be executed */ |
| private static Map<String, ArrayList<WorkerThreadRunnable>> listOfTasks; |
| /** the list of all watcher configurations */ |
| private static HashMap<String, String> watcherconfigurations; |
| /** if the config file is ready to use **/ |
| private static HashMap<String, Boolean> configurationsready; |
| |
| /** |
| * Returns the {@link #listOfTasks}. |
| * |
| * @return {@link Map<String, ArrayList<WorkerThreadRunnable>>} the list of |
| * all tasks. |
| */ |
| public Map<String, ArrayList<WorkerThreadRunnable>> getListOfTasks() { |
| return listOfTasks; |
| } |
| |
| /** |
| * Initializes the list of tasks. |
| * |
| * @param listOfTasks |
| * the list of tasks. |
| */ |
| public void initListOfTasks() { |
| if (listOfTasks == null) { |
| listOfTasks = new HashMap<>(); |
| } |
| } |
| |
| /** |
| * Checks if all tasks are done. |
| * |
| * @param tasks |
| * list of tasks to be checked |
| * {@link ArrayList<FutureTask<WorkerThreadRunnable>>} |
| * @return true if yes, false if not |
| */ |
| public boolean allTasksDone(ArrayList<FutureTask<WorkerThreadRunnable>> tasks) { |
| for (FutureTask<WorkerThreadRunnable> task : tasks) { |
| if (task != null && !task.isDone()) { |
| return false; |
| } |
| } |
| return true; |
| } |
| |
| /** |
| * Make sure that the given file is deleted. |
| * |
| * @param file |
| * the file to be deleted |
| */ |
| public void deleteFile(final Path file) { |
| try { |
| if (Files.deleteIfExists(file)) { |
| if (log.isDebugEnabled()) { |
| log.debug("File: " + file + " successfuly removed."); |
| } |
| } else { |
| if (log.isDebugEnabled()) { |
| log.debug("File: " + file + " doesn't exists and could not be removed."); |
| } |
| } |
| } catch (IOException e) { |
| log.error("File: " + file + " could not be deleted.\n" + e.getMessage()); |
| } |
| } |
| |
| /** |
| * Checks if the given file mask is empty. Further validation can be |
| * implemented here. |
| * |
| * @param filemask |
| * the file mask |
| * @return the file mask value, or null |
| */ |
| public String getFilemaskValue(String filemask) { |
| if (filemask != null && !filemask.isEmpty()) { |
| return filemask; |
| } |
| return null; |
| } |
| |
| /** |
| * Checks if the given cron expression is valid and gives it back |
| * eventually. |
| * |
| * @param expression |
| * the cron expression |
| * @return a valid cron expression or null |
| */ |
| public String getCronExpressionValue(String expression) { |
| if (expression != null && CronExpression.isValidExpression(expression)) { |
| return expression; |
| } |
| return null; |
| } |
| |
| /** |
| * Checks if the given minutes value is between 0 and 59 and gives the |
| * appropriate integer value back. |
| * |
| * @param minutes |
| * the minutes |
| * @return a value between 0 and 59, or -1 |
| */ |
| public int getMinutesValue(String minutes) { |
| try { |
| int value = Integer.valueOf(minutes); |
| if (value >= 0 && value <= 59) { |
| return value; |
| } |
| } catch (NumberFormatException e) { |
| log.error("getMinutesValue - given minute value [" + minutes + "] is not valid."); |
| } |
| return -1; |
| } |
| |
| /** |
| * Checks if the given hour value is between 0 and 23 and gives the |
| * appropriate integer value back. |
| * |
| * @param hour |
| * the hour |
| * @return a value between 0 and 23, or -1 |
| */ |
| public int getHourValue(String hour) { |
| try { |
| int value = Integer.valueOf(hour); |
| if (value >= 0 && value <= 23) { |
| return value; |
| } |
| } catch (NumberFormatException e) { |
| log.error("getHourValue - given hour value [" + hour + "] is not valid."); |
| } |
| return -1; |
| } |
| |
| /** |
| * Checks if the given day of the week value is valid and give the |
| * appropriate integer value back. Valid value here are: sunday | monday | |
| * tuesday | wednesday | thursday | friday | saturday |
| * |
| * @param dayofweek |
| * the day of the week |
| * @return a value between 1 and 7, or -1 |
| */ |
| public int getDayOfWeekValue(String dayofweek) { |
| switch (dayofweek.toLowerCase()) { |
| case "sunday": |
| return DateBuilder.SUNDAY; |
| case "monday": |
| return DateBuilder.MONDAY; |
| case "tuesday": |
| return DateBuilder.TUESDAY; |
| case "wednesday": |
| return DateBuilder.WEDNESDAY; |
| case "thursday": |
| return DateBuilder.THURSDAY; |
| case "friday": |
| return DateBuilder.FRIDAY; |
| case "saturday": |
| return DateBuilder.SATURDAY; |
| default: |
| log.error("getDayOfWeekValue - given dayofweek value [" + dayofweek + "] is not valid."); |
| return -1; |
| } |
| } |
| |
| /** |
| * Checks if the given day of the month value is between 1 and 31 and gives |
| * the appropriate integer value back. |
| * |
| * @param dayofmonth |
| * the day of month |
| * @return a value between 1 and 31, or -1 |
| */ |
| public int getDayOfMonthValue(String dayofmonth) { |
| try { |
| int value = Integer.valueOf(dayofmonth); |
| if (value >= 1 && value <= 31) { |
| return value; |
| } |
| } catch (NumberFormatException e) { |
| log.error("getDayofMonthValue - given dayofmonth value [" + dayofmonth + "] is not valid."); |
| } |
| return -1; |
| } |
| |
| /** |
| * Gives the {@link #watcherconfigurations} field back. |
| * |
| * @return {@link HashMap<String, String>} the list of watcher |
| * configurations. |
| */ |
| public HashMap<String, String> getWatcherconfigurations() { |
| return watcherconfigurations; |
| } |
| |
| /** |
| * Sets the list {@link #watcherconfigurations} field. |
| * |
| * @param watcherconfigurations |
| * the list of watcher configurations. |
| */ |
| public static void initWatcherconfigurations() { |
| if (watcherconfigurations == null) { |
| watcherconfigurations = new HashMap<>(); |
| } |
| if (configurationsready == null) { |
| watcherconfigurations = new HashMap<>(); |
| } |
| } |
| |
| /** |
| * Add a configuration entry. |
| * |
| * @param key |
| * the configuration key |
| * @param value |
| * the value |
| */ |
| public static void addWatcherConfig(String key, String value) { |
| String val = watcherconfigurations.get(key); |
| if (val == null) { |
| watcherconfigurations.put(key, value); |
| } |
| } |
| |
| /** |
| * Returns a value depending on the given key. |
| * |
| * @param key |
| * the configuration key |
| * @return the value or null |
| */ |
| public String getWatcherConfig(String key) { |
| return watcherconfigurations.get(key); |
| } |
| |
| /** |
| * Creates the signal dsl configuration file at the default location, if it |
| * doesn't exist, and sets its path in the preferences. Updates the |
| * configuration file to the missing entries with default value coming |
| * originally from the model. |
| */ |
| public static boolean createOrUpdateConfigurationFile() { |
| String url = ProductConfiguration.getSignalConfiguration(); |
| if (url == null || url.isEmpty()) { |
| url = System.getProperty("user.home") + "/.osbee/" + "SignalConfig.xml"; |
| } |
| File file = new File(url); |
| if (file.exists()) { |
| try { |
| FileInputStream fileInput = new FileInputStream(file); |
| Properties properties = new Properties(); |
| properties.loadFromXML(fileInput); |
| fileInput.close(); |
| for (String key : watcherconfigurations.keySet()) { |
| if (!properties.containsKey(key)) { |
| properties.put(key, watcherconfigurations.get(key)); |
| } |
| } |
| FileOutputStream fileOutput = new FileOutputStream(file); |
| properties.storeToXML(fileOutput, "Signal DSL Configuration"); |
| fileOutput.close(); |
| log.info("Signal configuration file " + file + " successfuly updated."); |
| return true; |
| } catch (IOException e) { |
| StringWriter sw = new StringWriter(); |
| e.printStackTrace(new PrintWriter(sw)); |
| log.error("createOrUpdateConfigurationFile: Error during update of file: " + file + " ... {}", |
| sw.toString()); |
| } |
| } else { |
| try { |
| FileOutputStream fileOutput = new FileOutputStream(file); |
| // create a new file and set the preference entry |
| file.createNewFile(); |
| Properties properties = new Properties(); |
| for (String key : watcherconfigurations.keySet()) { |
| properties.put(key, watcherconfigurations.get(key)); |
| } |
| properties.storeToXML(fileOutput, "Signal DSL Configuration"); |
| fileOutput.close(); |
| if (log.isDebugEnabled()) { |
| log.debug("Signal configuration file " + file + " successfuly created."); |
| } |
| String path = file.getAbsolutePath(); |
| ProductConfiguration.setSignalConfiguration(path); |
| log.info("Actual signal configuration file: " + file); |
| return true; |
| } catch (IOException e) { |
| StringWriter sw = new StringWriter(); |
| e.printStackTrace(new PrintWriter(sw)); |
| log.error("createOrUpdateConfigurationFile: Error during creation of file: [" + file + "] ... {}", |
| sw.toString()); |
| } |
| } |
| return false; |
| } |
| |
| /** |
| * Gives the property value of the given property name if existing. |
| * |
| * @param propertyname |
| * the property name |
| * @return the property value if existing, or null if not |
| */ |
| public String getHandlerPropertyFromConfigurationFile(String propertyname) { |
| if (propertyname != null) { |
| String url = ProductConfiguration.getSignalConfiguration(); |
| if (url == null || url.isEmpty()) { |
| url = System.getProperty("user.home") + "/.osbee/" + "SignalConfig.xml"; |
| } |
| File file = new File(url); |
| if (file.exists()) { |
| try { |
| FileInputStream fileInput = new FileInputStream(file); |
| Properties properties = new Properties(); |
| properties.loadFromXML(fileInput); |
| fileInput.close(); |
| String value = properties.getProperty(propertyname); |
| if (value != null) { |
| if (log.isDebugEnabled()) { |
| log.debug("getHandlerPropertyFromConfigurationFile propertyname[" + propertyname |
| + "] -> value [" + value + "]"); |
| } |
| return value; |
| } |
| } catch (IOException e) { |
| StringWriter sw = new StringWriter(); |
| e.printStackTrace(new PrintWriter(sw)); |
| log.error("Error during getHandlerPropertyFromConfigurationFile of propertyname: [" + propertyname |
| + "] ... {}", sw.toString()); |
| } |
| } |
| } |
| if (log.isDebugEnabled()) { |
| log.debug("getHandlerPropertyFromConfigurationFile propertyname[" + propertyname + "] -> value [null]"); |
| } |
| return null; |
| } |
| |
| /** |
| * Checks if the given path points to an existing directory. |
| * |
| * @param path |
| * the path |
| * @return true if yes, false if not |
| */ |
| public boolean existsDirectory(String path) { |
| try { |
| return Files.isDirectory(Paths.get(path)); |
| } catch (Exception e) { |
| log.error("existsDirectory - error ", e); |
| } |
| return false; |
| } |
| |
| /** |
| * Checks if the given string a valid url. |
| * |
| * @param url |
| * @return true if yes, false if not. |
| */ |
| public URL isDirectoryValidURL(String url) { |
| try { |
| return new URL(url); |
| } catch (MalformedURLException e1) { |
| if (e1.getMessage().startsWith("unknown protocol") || e1.getMessage().startsWith("no protocol")) { |
| try { |
| return Paths.get(url).toUri().toURL(); |
| } catch (MalformedURLException e2) { |
| StringWriter sw = new StringWriter(); |
| e2.printStackTrace(new PrintWriter(sw)); |
| log.error("{}", sw.toString()); |
| } |
| } |
| } |
| return null; |
| } |
| |
| public static void setConfigReady(String path, boolean ready) { |
| configurationsready.put(path, ready); |
| } |
| |
| public static boolean isConfigReady(String path) { |
| if (configurationsready == null) { |
| configurationsready = new HashMap<>(); |
| } |
| if (configurationsready.containsKey(path)) { |
| return configurationsready.get(path).booleanValue(); |
| } |
| return false; |
| } |
| |
| } |