From 1ae8a96e062a20d93cc6d066309bae1d4b275cd1 Mon Sep 17 00:00:00 2001 From: Sweidan Omar Date: Mon, 25 Apr 2022 08:18:14 +0000 Subject: [PATCH] git-tfs-id: [http://tfs.userrendszerhaz.hu:8080/tfs/DefaultCollection]$/MediaCube;C33195 --- .../server/JobEngineConfiguration.java | 117 +++++++++++++++++- 1 file changed, 115 insertions(+), 2 deletions(-) diff --git a/server/user.jobengine.osgi.server/src/user/jobengine/server/JobEngineConfiguration.java b/server/user.jobengine.osgi.server/src/user/jobengine/server/JobEngineConfiguration.java index 1bd5c162..c13f8b6c 100644 --- a/server/user.jobengine.osgi.server/src/user/jobengine/server/JobEngineConfiguration.java +++ b/server/user.jobengine.osgi.server/src/user/jobengine/server/JobEngineConfiguration.java @@ -1,11 +1,14 @@ package user.jobengine.server; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; +import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -20,9 +23,18 @@ import java.util.function.Consumer; import java.util.function.Predicate; import java.util.stream.Stream; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + import org.apache.commons.lang.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.w3c.dom.Document; import com.ibm.nosql.json.JSONUtil; import com.ibm.nosql.json.api.BasicDBList; @@ -43,6 +55,7 @@ public class JobEngineConfiguration implements IJobEngineConfiguration { public static final String CONF_MAESTRO = "settings/maestro.yaml"; public static final String CONF_EXECUTORS = "jobs/executors.xml"; public static final String CONF_SCHEDULES = "jobs/schedules.json"; + public static final String CONF_SCHEDULES2 = "jobs/schedules2.json"; public static final String DIR_STEPS = "jobs/steps"; public static final String DIR_CLASSES = "jobs/classes"; public static final String DIR_TEMPLATES = "jobs/templates"; @@ -289,9 +302,9 @@ public class JobEngineConfiguration implements IJobEngineConfiguration { logger.info("Loading scheduler configuration file {}", configFilePath); String jsonConfig = new String(Files.readAllBytes(Paths.get(configFilePath))); BasicDBObject dbo = (BasicDBObject) JSONUtil.jsonToDbObject(jsonConfig); - BasicDBList scheduleJobs = NoSQLUtils.asDBList(dbo, "joblist"); + BasicDBList scheduledJobs = NoSQLUtils.asDBList(dbo, "joblist"); - scheduleJobs.forEach(o -> { + scheduledJobs.forEach(o -> { try { BasicDBObject j = (BasicDBObject) o; String template = j.getString("template"); @@ -363,8 +376,108 @@ public class JobEngineConfiguration implements IJobEngineConfiguration { return result; } + @Override + public void saveSchedule(BasicDBObject processToSave) throws Exception { + logger.info("saveSchedule()"); + schedules.add(new SimpleEntry("", processToSave)); + + Path schedulesPath = null; + try { + schedulesPath = Paths.get(systemConfig.getConfig(CONF_SCHEDULES2)); + if (!Files.exists(schedulesPath)) { + Files.createFile(schedulesPath); + } + if (Files.isDirectory(schedulesPath)) + throw new FileNotFoundException(schedulesPath + " is a directory!"); + logger.info("Saving schedules.json to {}", schedulesPath); + Files.write(schedulesPath, processToSave.toPrettyString("").getBytes()); + } catch (FileNotFoundException e) { + logger.error("File not found: {}", CONF_SCHEDULES2); + } catch (IOException e) { + logger.error("File is not writeable/not created: {}", schedulesPath.toString()); + } + } + + @Override + public void saveTemplateXml(String oldSelectedJob, JobTemplate templateToSave) { + logger.info("saveTemplateXml()"); + if (templateToSave == null) { + throw new NullPointerException("templateToSave == null"); + } + for (int i = 0; i < templates.size(); i++) { + if (templates.get(i).getName().equals(oldSelectedJob)) { + templates.get(i).setName(templateToSave.getName()); + Path templatesPath = null; + try { + templatesPath = Paths.get(systemConfig.getConfig(DIR_TEMPLATES)); + if (!Files.isDirectory(templatesPath)) + throw new FileNotFoundException(templatesPath + " is not a directory!"); + String filePath = templatesPath.toString() + FileSystems.getDefault().getSeparator() + + templates.get(i).getFileName(); + FileOutputStream fos = new FileOutputStream(filePath); + logger.info("Saving job template to {}", filePath); +// if (templateToSave.toXmlDocument() != null) { + if (templates.get(i).toXmlDocument() != null) { + writeXml(templates.get(i).toXmlDocument(), fos); + } + } catch (FileNotFoundException e) { + logger.error("Folder not found: {}", DIR_TEMPLATES); + } + } + } + } + + private void writeXml(Document document, OutputStream os) { + document.setXmlVersion("1.1"); + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + Transformer transformer; + try { + transformer = transformerFactory.newTransformer(); + transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); + transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); + transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "xml"); + DOMSource source = new DOMSource(document); + StreamResult result = new StreamResult(os); + transformer.transform(source, result); + } catch (TransformerConfigurationException e) { + e.printStackTrace(); + } catch (TransformerException e) { + e.printStackTrace(); + } + } + + @Override + public void duplicateProcess(JobTemplate templateToDuplicate) { + logger.info("duplicateProcess()"); + JobTemplate templateWithNewName = new JobTemplate(templateToDuplicate); + templateWithNewName.setName(templateToDuplicate.getName().concat("-copy")); + templates.add(templateWithNewName); + saveTemplateXml("", templateWithNewName); + } + + @Override + public void copyProcessEntry(BasicDBObject scheduleToCopy) { + logger.info("copyProcessEntry()"); + BasicDBObject dbo = new BasicDBObject(); + dbo.append("active", scheduleToCopy.getBoolean("active")); + dbo.append("executeimmediate", scheduleToCopy.getBoolean("executeimmediate")); + dbo.append("name", scheduleToCopy.get("name").toString().concat("-copy")); + dbo.append("template", scheduleToCopy.get("template").toString()); + if (scheduleToCopy.get("cronexpression") != null) { + dbo.append("cronexpression", scheduleToCopy.get("cronexpression").toString()); + } + dbo.append("parameters", scheduleToCopy.get("parameters")); + + SimpleEntry newScheduledJob = new SimpleEntry("", dbo); + schedules.add(newScheduledJob); + + } + @Override public void resetStepClassLoader() { resetStepClassLoader = true; } + } -- 2.54.0