com.skynetcomputing.skynetserver.persistence.FileManager.java Source code

Java tutorial

Introduction

Here is the source code for com.skynetcomputing.skynetserver.persistence.FileManager.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.skynetcomputing.skynetserver.persistence;

import com.google.gson.Gson;
import com.skynetcomputing.database.Database;
import com.skynetcomputing.serializable.SrzData;
import com.skynetcomputing.serializable.SrzJob;
import com.skynetcomputing.serializable.SrzTask;
import com.skynetcomputing.skynetserver.job.Data;
import com.skynetcomputing.skynetserver.job.Jar;
import com.skynetcomputing.skynetserver.job.Job;
import com.skynetcomputing.skynetserver.job.Task;
import com.skynetcomputing.utils.FileSystemWatcher;
import org.apache.commons.io.FileUtils;

import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author jordy
 */
public class FileManager implements AutoCloseable {

    /// Constants.
    public static final String JOB_FOLDER_PATH = "..\\Skynet\\jobs";
    public static final String JAR_FOLDER_PATH = "..\\Skynet\\jars";
    public static final String TEMP_FOLDER_PATH = "..\\Skynet\\temp";

    public static final String TASK_FOLDER_NAME = "tasks";
    public static final String DATA_FOLDER_NAME = "data";
    public static final String OUTPUT_FOLDER_NAME = "output";

    private final FileSystemWatcher watcher;

    protected FileManager(boolean resetFolders) throws IOException, InterruptedException {
        File jobsDir = new File(JOB_FOLDER_PATH);
        File jarsDir = new File(JAR_FOLDER_PATH);
        File tempDir = new File(TEMP_FOLDER_PATH);

        if (resetFolders) {
            FileUtils.deleteDirectory(jobsDir);
            FileUtils.deleteDirectory(jarsDir);
            FileUtils.deleteDirectory(tempDir);
        }

        jobsDir.mkdirs();
        jarsDir.mkdirs();
        tempDir.mkdirs();

        this.watcher = FileSystemWatcher.watch(Paths.get(TEMP_FOLDER_PATH));
    }

    protected void addListener(WatchEvent.Kind<Path> watchKind, Consumer<Path> consumer) {
        watcher.addListener(watchKind, consumer);
    }

    protected Job readJob(String filePath) throws IOException {

        SrzJob srzJob = null;
        Gson g = new Gson();

        File file = new File(filePath);

        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            srzJob = g.fromJson(br, SrzJob.class);

        }
        return new Job(srzJob);
    }

    protected boolean saveJob(Job job) throws IOException {
        Gson g = new Gson();

        String filePath = JOB_FOLDER_PATH + File.separator + job.getID() + SrzJob.EXT;
        File file = new File(filePath);

        if (!file.exists()) {
            try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
                g.toJson(job.getSrzJob(), bw);
            }
            createJobFolders(job.getID());
            job.setTasksJarInfo();
            List<Task> tasks = new ArrayList<>(job.getTasks().values());
            saveTasks(tasks);
            saveDataFiles(job);
            return true;
        }
        return false;
    }

    protected File getTaskFile(Task task) throws FileNotFoundException {
        File tasksFolder = getTasksFolder(task.getJobID());

        for (File file : tasksFolder.listFiles()) {
            if (file.getName().equalsIgnoreCase(Integer.toString(task.getID()) + SrzTask.EXT)) {
                return file;
            }
        }
        return null;
    }

    public File getTaskFile(int jobID, int taskID) throws FileNotFoundException {
        File tasksFolder = getTasksFolder(jobID);

        for (File file : tasksFolder.listFiles()) {
            if (file.getName().equalsIgnoreCase(Integer.toString(taskID) + SrzTask.EXT)) {
                return file;
            }
        }
        return null;
    }

    protected File getJobFolder(int jobID) throws FileNotFoundException {
        File file = new File(JOB_FOLDER_PATH + File.separator + jobID);

        if (!file.exists()) {
            throw new FileNotFoundException(file.getAbsolutePath());
        }
        return file;
    }

    public boolean getJobFolderExists(int jobId) {
        File file = new File(JOB_FOLDER_PATH + File.separator + jobId);
        return (file.exists() && file.isDirectory());
    }

    protected File getTasksFolder(int jobID) throws FileNotFoundException {
        File file = new File(getJobFolder(jobID) + File.separator + TASK_FOLDER_NAME);

        if (!file.exists()) {
            throw new FileNotFoundException(file.getAbsolutePath());
        }
        return file;
    }

    public SrzTask getTask(int jobID, int taskID) {
        return null;
    }

    protected File getJarFile(Jar jar) {
        return findJarFile(jar.getName()).orElse(null);
    }

    protected File getJobDataFile(int jobID, int dataID) throws FileNotFoundException {
        // return null if not yet filled 
        File dataFolder = new File(getJobFolder(jobID).getPath() + File.separator + DATA_FOLDER_NAME);

        List<File> files = null;
        if (dataFolder.exists()) {
            files = Arrays.asList(dataFolder.listFiles());
            for (File file : files) {
                if (file.getName().equalsIgnoreCase(String.valueOf(dataID) + SrzData.EXT)) {
                    return file;
                }
            }
        }
        return null;
    }

    protected boolean saveTasks(List<Task> tasks) {

        Gson g = new Gson();

        for (Task task : tasks) {
            String jobFolderPath = JOB_FOLDER_PATH + File.separator + task.getJobID() + File.separator;
            File file = new File(jobFolderPath + File.separator + TASK_FOLDER_NAME + File.separator + task.getID()
                    + SrzTask.EXT);
            if (!file.exists()) {
                try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
                    g.toJson(task.getSrzTask(), bw);
                } catch (IOException ex) {
                    Logger.getLogger(FileManager.class.getName()).log(Level.SEVERE, null, ex);
                    return false;
                }
            }
        }
        return true;
    }

    protected boolean saveOutput(int jobID, Data data) {
        Gson g = new Gson();
        File jobFolder;
        boolean result = false;
        try {
            jobFolder = getJobFolder(jobID);
            File file = new File(jobFolder.getPath() + File.separator + OUTPUT_FOLDER_NAME + File.separator
                    + data.getID() + SrzData.EXT);
            try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
                g.toJson(data.getSrzData(), bw);
                result = true;
            } catch (IOException ex) {
                Logger.getLogger(FileManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FileManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    protected boolean saveData(Data data) {
        File file = new File(JOB_FOLDER_PATH + File.separator + data.getJobID() + File.separator + DATA_FOLDER_NAME
                + File.separator + data.getID() + SrzData.EXT);

        Gson g = new Gson();

        try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
            g.toJson(data.getSrzData(), bw);
            data.setDataPath(file.getPath());

            try {
                Database.getInstance().procedure("p_UPDATE_DATA", PreparedStatement::execute, data.getID(),
                        data.getJobID(), data.getDataPath(), data.hasContent());
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (IOException ex) {
            Logger.getLogger(FileManager.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return true;
    }

    protected void saveDataFiles(Job job) {
        for (Data data : job.getData().values()) {
            saveData(data);
        }
    }

    protected Data readData(File file) {
        //TODO
        SrzData srzData = null;

        Gson g = new Gson();

        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            srzData = g.fromJson(br, SrzData.class);
            return new Data(srzData);
        } catch (IOException ex) {
            Logger.getLogger(FileManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    protected List<Data> readDataFiles(int jobID) {
        File file = new File(JOB_FOLDER_PATH + File.separator + jobID + File.separator + DATA_FOLDER_NAME);
        List<Data> dataList = new ArrayList<>();

        if (file.exists()) {
            List<File> files = Arrays.asList(file.listFiles());
            for (File f : files) {
                Data data = readData(f);
                if (data != null) {
                    dataList.add(data);
                }
            }
        }
        return dataList;
    }

    protected void createJobFolders(int jobID) {
        String jobFolderName = String.valueOf(jobID);

        File jobFolder = createDirectory(JOB_FOLDER_PATH, jobFolderName);
        createDirectory(jobFolder.getPath(), TASK_FOLDER_NAME);
        createDirectory(jobFolder.getPath(), DATA_FOLDER_NAME);
        createDirectory(jobFolder.getPath(), OUTPUT_FOLDER_NAME);
    }

    protected File createDirectory(String path, String directoryName) {
        String dirPath = path + File.separator + directoryName;
        File file = new File(dirPath);
        if (!file.exists()) {
            if (file.mkdir()) {
                return file;
            }
        }
        return null;
    }

    protected boolean isJarPresent(String jarName) throws IOException {
        return findJarFile(jarName) != null;
    }

    protected Optional<File> findJarFile(String jarName) {
        File folder = new File(JAR_FOLDER_PATH);
        if (folder == null)
            return Optional.empty();
        List<File> listOfFiles = Arrays.asList(folder.listFiles());
        String jobVersion = jarName;

        for (File file : listOfFiles) {
            String fileName = file.getName();
            if (fileName.contains(jobVersion)) {
                return Optional.of(file);
            }
        }

        return Optional.empty();
    }

    @Override
    public void close() throws Exception {
        watcher.close();
    }

    public File getOutputFile(int jobID, int dataID) {
        File outputFile = null;
        try {
            File jobFolder = getJobFolder(jobID);
            outputFile = new File(jobFolder.getPath() + File.separator + OUTPUT_FOLDER_NAME + File.separator
                    + dataID + SrzData.EXT);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FileManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (outputFile.exists()) {
            return outputFile;
        } else {
            return null;
        }
    }
}