최초 커밋

master
mjkhan21 2 years ago
commit 0ca8e2fb20

@ -0,0 +1,114 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cokr.xit.interfaces</groupId>
<artifactId>xit-filejob</artifactId>
<version>23.04.01-SNAPSHOT</version>
<packaging>jar</packaging>
<name>xit-filejob</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>mvn2s</id>
<url>https://repo1.maven.org/maven2/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>egovframe</id>
<url>http://maven.egovframe.kr:8080/maven/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>egovframe2</id>
<url>http://www.egovframe.go.kr/maven/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>maven-public</id>
<url>http://xit.xit-nexus.com:8081/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>cokr.xit.base</groupId>
<artifactId>xit-file</artifactId>
<version>23.04.01-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.egovframe.rte</groupId>
<artifactId>org.egovframe.rte.bat.core</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.7.2</version>
</dependency>
</dependencies>
<build>
<defaultGoal>install</defaultGoal>
<directory>${basedir}/target</directory>
<finalName>${artifactId}-${version}</finalName>
<resources>
<resource><directory>${basedir}/src/main/resources</directory></resource>
</resources>
<testResources>
<testResource><directory>${basedir}/src/test/resources</directory></testResource>
<testResource><directory>${basedir}/src/main/resources</directory></testResource>
</testResources>
</build>
<!-- Nexus deploy -->
<distributionManagement>
<snapshotRepository>
<id>maven-snapshot</id>
<url>http://xit.xit-nexus.com:8081/repository/maven-snapshots/</url>
</snapshotRepository>
<repository>
<id>maven-release</id>
<url>http://xit.xit-nexus.com:8081/repository/maven-releases/</url>
</repository>
</distributionManagement>
<!-- Nexus deploy -->
</project>

@ -0,0 +1,147 @@
package cokr.xit.interfaces.filejob;
import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.core.io.ClassPathResource;
import cokr.xit.foundation.AbstractComponent;
import cokr.xit.foundation.Assert;
import cokr.xit.foundation.data.JSON;
/** .
* <p> conf/file-job.conf .
*
* @author mjkhan
*/
public class JobConf extends AbstractComponent {
private static JobConf conf;
public static JobConf get() {
if (conf == null)
try {
ClassPathResource res = new ClassPathResource("conf/file-job.conf");
conf = new JSON().parse(res.getInputStream(), JobConf.class);
} catch (Exception e) {
throw runtimeException(e);
}
return conf;
}
private Map<String, String>
dirs,
defaults,
dirCodes;
private List<Map<String, String>> jobs;
private Map<String, Map<String, String>> jobMap;
private JobConf() {}
/** type .
* @param type
* <ul><li>"receive": </li>
* <li>"send": </li>
* <li>"working": </li>
* <li>"success": </li>
* <li>"fail": </li>
* </ul>
* @return type
*/
public String getDir(String type) {
return ifEmpty(dirs != null ? dirs.get(type) : null, "");
}
/** .
* @param dirs "dirs"
*/
public void setDirs(Map<String, String> dirs) {
this.dirs = dirs;
}
/** key .
* @param key
* @return key
*/
public String getDefault(String key) {
return ifEmpty(defaults != null ? defaults.get(key) : null, "");
}
/** .<br />
* @param defaults "defaults"
*/
public void setDefaults(Map<String, String> defaults) {
this.defaults = defaults;
}
/** key .
* @param key
* @return
*/
public String getDirCode(String key) {
return ifEmpty(dirCodes != null ? dirCodes.get(key) : null, "");
}
public void setDirCodes(Map<String, String> dirCodes) {
this.dirCodes = dirCodes;
}
public Map<String, String> getJob(String key) {
if (jobMap == null) {
if (jobs == null)
jobMap = Collections.emptyMap();
else
jobMap = jobs.stream()
.collect(Collectors.toMap(job -> job.get("name"), job -> job));
}
return Assert.ifEmpty(jobMap.get(key), Collections::emptyMap);
}
public void setJobs(List<Map<String, String>> jobs) {
this.jobs = jobs;
}
public String getJobConf(String jobName, String key) {
Map<String, String> job = getJob(jobName);
return Assert.ifEmpty(job.get(key), () -> getDefault(key));
}
String path(String... str) {
return String.join(File.separator, str);
}
public String getDir(String jobName, String dir) {
String org = getJob(jobName).get("dirCode");
String code = getDirCode(org);
return path(getDir(dir), code);
}
public String getWorkingDir(String jobName, String dir) {
String org = getJob(jobName).get("dirCode");
String code = getDirCode(org);
return path(getDir("working"), dir, code);
/*
return path(getDir(jobName, "working"), dir);
*/
}
public String getSuccessDir(String jobName) {
String org = getJob(jobName).get("dirCode");
String code = getDirCode(org);
return path(getDir("success"), "receive", code);
/*
return path(getDir(jobName, "success"), "receive");
*/
}
public String getFailDir(String jobName) {
String org = getJob(jobName).get("dirCode");
String code = getDirCode(org);
return path(getDir("fail"), "receive", code);
/*
return path(getDir(jobName, "fail"), "receive");
*/
}
}

@ -0,0 +1,3 @@
/** .
*/
package cokr.xit.interfaces.filejob;

@ -0,0 +1,251 @@
package cokr.xit.interfaces.filejob.service.bean;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import cokr.xit.foundation.AbstractComponent;
import cokr.xit.interfaces.filejob.JobConf;
/**
* @author mjkhan
*/
public abstract class FileJobBean extends AbstractComponent {
private boolean busy;
/** Bean .<br />
* {@link JobConf} Bean .
* @return
*/
public abstract String jobName();
/** .
* @return
*/
protected JobConf config() {
return JobConf.get();
}
/** Bean .<br />
* {@link FileJobServiceBean} .
* @return
* <ul><li> true</li>
* <li> false</li>
* </ul>
*/
public boolean isBusy() {
return busy;
}
/** Bean .<br />
* {@link FileJobServiceBean} .
* @param busy
* <ul><li> true</li>
* <li> false</li>
* </ul>
*/
public void setBusy(boolean busy) {
this.busy = busy;
}
/**
* @author mjkhan
*/
public static class FileStatus {
private boolean success = true;
private Path path;
private HashMap<String, Object> info;
private Throwable cause;
/** .
* @return
* <ul><li> true()</li>
* <li> false</li>
* </ul>
*/
public boolean isSuccess() {
return success;
}
/** .
* @param success
* <ul><li> true()</li>
* <li> false</li>
* </ul>
* @return FileStatus
*/
public FileStatus setSuccess(boolean success) {
this.success = success;
return this;
}
/** .
* @return
*/
public Path getPath() {
return path;
}
/** .
* @param path
* @return FileStatus
*/
public FileStatus setPath(Path path) {
this.path = path;
return this;
}
/** .
* @param <T>
* @param key
* @return
*/
@SuppressWarnings("unchecked")
public <T> T get(String key) {
if (isEmpty(info)) return null;
Object obj = info.get(key);
return obj != null ? (T)obj : null;
}
/** .
* @param key
* @param obj
* @return FileStatus
*/
public FileStatus set(String key, Object obj) {
if (info == null)
info = new HashMap<>();
info.put(key, obj);
return this;
}
/** .
* @return
*/
public Throwable getCause() {
return cause;
}
/** .<br />
* {@link #setSuccess(boolean) }.
* @param cause
* @return FileStatus
*/
public FileStatus setCause(Throwable cause) {
this.cause = rootCause(cause);
return setSuccess(this.cause == null);
}
}
/** .
* @return
*/
protected String receiveDir() {
return config().getDir(jobName(), "receive");
}
/** .
* @return
*/
protected String receiveWorkingDir() {
return config().getWorkingDir(jobName(), "receive");
}
/** .
* @return
*/
protected String sendDir() {
return config().getDir(jobName(), "send");
}
/** .
* @return
*/
protected String sendWorkingDir() {
return config().getWorkingDir(jobName(), "send");
}
/** .
* @return
*/
protected String successDir() {
return config().getSuccessDir(jobName());
}
/** .
* @return
*/
protected String failDir() {
return config().getFailDir(jobName());
}
/** .
* @param key
* @return
*/
protected String config(String key) {
return config().getJobConf(jobName(), key);
}
/** .
* @param dir
* @param filter .
* @return
*/
protected List<Path> getFilePaths(String dir, Predicate<Path> filter) {
try (Stream<Path> walk = Files.list(Paths.get(dir))) {
return walk.filter(ifEmpty(filter, path -> true)).collect(Collectors.toList());
} catch (Exception e) {
throw runtimeException(e);
}
}
/** .
* @param paths
* @param destDir
*/
protected void move(List<Path> paths, String destDir) {
if (isEmpty(paths)) return;
mkdirs(destDir);
paths.forEach(src -> {
try {
Path dest = Paths.get(destDir + File.separator + src.getFileName());
Files.move(src, dest);
log().debug(src.toAbsolutePath() + " -> " + dest.toAbsolutePath());
} catch (Exception e) {
throw runtimeException(e);
}
});
}
/** .
* @param dirPath
*/
protected void mkdirs(String dirPath) {
File dir = new File(dirPath);
if (!dir.exists())
dir.mkdirs();
}
/** , .
* @return
*/
protected List<Path> getReceivedFilePaths() {
String receiveDir = receiveDir(),
workingDir = receiveWorkingDir();
List<Path> paths = getFilePaths(receiveDir, Files::isRegularFile);
move(paths, workingDir);
return getFilePaths(workingDir, null);
}
}

@ -0,0 +1,41 @@
package cokr.xit.interfaces.filejob.service.bean;
import java.util.function.Supplier;
import cokr.xit.foundation.component.AbstractServiceBean;
/** , /
* @author mjkhan
*/
public class FileJobServiceBean extends AbstractServiceBean {
/** . fileJob .
* @param fileJob Bean
* @param task Bean
*/
protected void execute(FileJobBean fileJob, Runnable task) {
if (fileJob.isBusy()) return;
fileJob.setBusy(true);
try {
task.run();
} finally {
fileJob.setBusy(false);
}
}
/** . fileJob null .
* @param fileJob Bean
* @param task Bean
* @return
*/
protected <T> T execute(FileJobBean fileJob, Supplier<T> task) {
if (fileJob.isBusy()) return null;
fileJob.setBusy(true);
try {
return task.get();
} finally {
fileJob.setBusy(false);
}
}
}

@ -0,0 +1,3 @@
/** .
*/
package cokr.xit.interfaces.filejob.service.bean;
Loading…
Cancel
Save