From: Sweidan Omar Date: Thu, 28 Apr 2022 08:13:25 +0000 (+0000) Subject: git-tfs-id: [http://tfs.userrendszerhaz.hu:8080/tfs/DefaultCollection]$/MediaCube... X-Git-Url: http://git.useribm.hu/?a=commitdiff_plain;h=d6ba7c02e1a85dcee3ed38888883efb874fa49f2;p=mediacube.git git-tfs-id: [tfs.userrendszerhaz.hu:8080/tfs/DefaultCollection]$/MediaCube;C33226 --- 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 cbbb7b09..af69ea3c 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,5 +1,6 @@ package user.jobengine.server; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; @@ -12,6 +13,7 @@ import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.attribute.DosFileAttributeView; import java.util.AbstractMap.SimpleEntry; import java.util.ArrayList; import java.util.Collections; @@ -31,6 +33,7 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; +import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang.RandomStringUtils; import org.apache.commons.lang.StringUtils; import org.apache.logging.log4j.LogManager; @@ -56,7 +59,6 @@ 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"; @@ -378,53 +380,86 @@ public class JobEngineConfiguration implements IJobEngineConfiguration { } @Override - public void saveSchedule(BasicDBObject processToSave) throws Exception { - logger.info("saveSchedule()"); - schedules.add(new SimpleEntry("", processToSave)); + public void saveSchedulesJson() throws Exception { + logger.info("saveSchedulesJson()"); Path schedulesPath = null; try { - schedulesPath = Paths.get(systemConfig.getConfig(CONF_SCHEDULES2)); + schedulesPath = Paths.get(systemConfig.getConfig(CONF_SCHEDULES)); 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()); + BasicDBObject dbo = new BasicDBObject(); + BasicDBList jobList = new BasicDBList(); + for (int i = 0; i < schedules.size(); i++) { + BasicDBObject temp = schedules.get(i).getValue(); + temp.remove("xml"); + jobList.add(temp); + } + dbo.put("joblist", jobList); + if (Files.isWritable(schedulesPath)) { + logger.info("{} is writable"); + } else { + logger.info("{} was not writable"); + DosFileAttributeView dos = Files.getFileAttributeView(schedulesPath, DosFileAttributeView.class); + if (dos != null) { + try { + dos.setReadOnly(false); + logger.info("{} is writable"); + } catch (IOException e) { + logger.info("IOException: {}", e.getCause()); + } + } + } + Files.write(schedulesPath, dbo.toPrettyString("").getBytes()); } catch (FileNotFoundException e) { - logger.error("File not found: {}", CONF_SCHEDULES2); + logger.error("File not found: {}", CONF_SCHEDULES); } catch (IOException e) { logger.error("File is not writeable/not created: {}", schedulesPath.toString()); } } @Override - public void saveTemplateXml(String oldSelectedJob, JobTemplate templateToSave) { + public void saveTemplateXml(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); + + Path templatesPath = null; + String filePath = null; + try { + templatesPath = Paths.get(systemConfig.getConfig(DIR_TEMPLATES)); + if (!Files.isDirectory(templatesPath)) + throw new FileNotFoundException(templatesPath + " is not a directory!"); + if (templateToSave.getFileName() != null) { + filePath = templatesPath.toString() + FileSystems.getDefault().getSeparator() + + templateToSave.getFileName(); + if (Files.isWritable(Paths.get(filePath))) { + logger.info("{} is writable"); + } else { + logger.info("{} is not writable"); + DosFileAttributeView dos = Files.getFileAttributeView(Paths.get(filePath), + DosFileAttributeView.class); + if (dos != null) { + try { + dos.setReadOnly(false); + } catch (IOException e) { + logger.info("IOException: {}", e.getCause()); + } } - } catch (FileNotFoundException e) { - logger.error("Folder not found: {}", DIR_TEMPLATES); + } + FileOutputStream fos = new FileOutputStream(filePath); + logger.info("Saving job template to {}", filePath); + if (templateToSave.toXmlDocument() != null) { + writeXml(templateToSave.toXmlDocument(), fos); } } + } catch (FileNotFoundException e) { + logger.error("File not found: {}", filePath); } } @@ -450,30 +485,45 @@ public class JobEngineConfiguration implements IJobEngineConfiguration { } @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); - } + public void duplicateTemplate(String originalTemplateName) { + logger.info("duplicateTemplate()"); + if (originalTemplateName == null) { + throw new NullPointerException("originalTemplateName == null"); + } - @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()); + Path templatesPath = null; + String filePath = null; + String duplicateFileName = null; + try { + templatesPath = Paths.get(systemConfig.getConfig(DIR_TEMPLATES)); + if (!Files.isDirectory(templatesPath)) + throw new FileNotFoundException(templatesPath + " is not a directory!"); + duplicateFileName = FilenameUtils.getName(originalTemplateName).replace(".xml", "").concat("-copy.xml"); + filePath = templatesPath.toString() + FileSystems.getDefault().getSeparator() + duplicateFileName; + while (Files.exists(Paths.get(filePath))) { + duplicateFileName = FilenameUtils.getName(duplicateFileName).replace(".xml", "").concat("-copy.xml"); + filePath = templatesPath.toString() + FileSystems.getDefault().getSeparator() + duplicateFileName; + } + InputStream is = new FileInputStream( + templatesPath.toString() + FileSystems.getDefault().getSeparator() + originalTemplateName); + Files.copy(is, Paths.get(filePath)); + } catch (FileNotFoundException e) { + logger.error("File not found: {}", filePath); + } catch (IOException e) { + logger.error("IOException: {}", e.getCause()); } - dbo.append("parameters", scheduleToCopy.get("parameters")); - SimpleEntry newScheduledJob = new SimpleEntry("", dbo); - schedules.add(newScheduledJob); + } + @Override + public boolean scheduleExists(String name) { + boolean doesExist = false; + for (int i = 0; i < schedules.size(); i++) { + if (schedules.get(i).getValue().getString("name").equals(name)) { + doesExist = true; + } + } + return doesExist; } @Override