com.sds.acube.ndisc.xmigration.util.XNDiscMigUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.sds.acube.ndisc.xmigration.util.XNDiscMigUtil.java

Source

/*
 * <pre>
 * Copyright (c) 2014 Samsung SDS.
 * All right reserved.
 *
 * This software is the confidential and proprietary information of Samsung
 * SDS. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license agreement
 * you entered into with Samsung SDS.
 *
 * Author             : Takkies
 * Date                : 2014. 04. 01.
 * Description      : 
 * </pre>
 */
package com.sds.acube.ndisc.xmigration.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;

/**
 * 
 * @author Takkies
 *
 */
public class XNDiscMigUtil {

    /* Console    Command */
    private static final String CLEAR_TERMINAL_ANSI_CMD = new String(new byte[] { 27, 91, 50, 74, 27, 91, 72 });

    /* OS  ??  */
    public static String LINE_SEPERATOR = System.getProperty("line.separator");

    /* XMigration ? (version.txt) */
    private static String XMigration_PublishingVersion;

    /* XMigration ? (version.txt) */
    private static String XMigration_PublishingDate;

    /**
     * ? null 
     * 
     * @param val ?
     * @param def null?   
     * @return null ? ?
     */
    public static String getString(String val, String def) {
        if (val == null) {
            return def;
        }
        return val;
    }

    /**
     * ? null 
     * 
     * @param val ?
     * @return null ? ?
     */
    public static String getString(String val) {
        return getString(val, "");
    }

    /**
     * ?? ?  <br>
     * ?? ? ? overwrite   ?? ?  ?<br>
     * 
     * @param fileid  ? ?
     * @param filename  ?
     * @return ? 
     */
    public static String getFilePath(String fileid, String filename) {
        String tmpDir = XNDiscMigConfig.getString("tmp-dir", null);
        tmpDir = (tmpDir.indexOf("/") >= 0) ? tmpDir + "/" + fileid : tmpDir + File.separator + fileid;
        File d = new File(tmpDir);
        if (!d.isDirectory()) {
            d.mkdirs();
        }
        return (tmpDir.indexOf("/") >= 0) ? tmpDir + "/" + filename : tmpDir + File.separator + filename;
    }

    /**
     * ?? ? ?  
     * 
     * @param fileid ? ?
     */
    public static void deleteTmpFiles(String fileid) {
        String tmpDir = XNDiscMigConfig.getString("tmp-dir", null);
        tmpDir = (tmpDir.indexOf("/") >= 0) ? tmpDir + "/" + fileid : tmpDir + File.separator + fileid;
        File d = new File(tmpDir);
        if (!d.exists()) {
            return;
        }
        File files[] = d.listFiles();
        for (int i = 0; i < files.length; i++) {
            files[i].delete();
        }
        d.delete();
    }

    /**
     *  ? <br>
     *  ??      ? .<br>
     * 
     * @param filename ?
     * @return  ?
     */
    public static String getRealFileName(String filename) {
        if (filename.indexOf("/") >= 0) {
            return filename.substring(filename.lastIndexOf("/"));
        }
        if (filename.indexOf(File.separator) >= 0) {
            return filename.substring(filename.lastIndexOf(File.separator));
        }
        return filename;
    }

    /**
     * console  clear 
     */
    public static void clearConsoleOutput() {
        try {
            String os = System.getProperty("os.name").toLowerCase();
            String ostype = (os.contains("windows")) ? "W" : "U";
            if (ostype.equals("W")) {
                CommandLine cmdLine = CommandLine.parse("cls");
                DefaultExecutor executor = new DefaultExecutor();
                executor.execute(cmdLine);
                System.out.printf("%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n", new Object[0]);
            } else {
                CommandLine cmdLine = CommandLine.parse("clear");
                DefaultExecutor executor = new DefaultExecutor();
                executor.execute(cmdLine);
                System.out.print(CLEAR_TERMINAL_ANSI_CMD);
                System.out.flush();
            }
        } catch (IOException e) {
        }
    }

    /**
     * XMigration ?  
     * 
     * @return XMigration 
     */
    public static String getXMigrationVersion() {
        if (XMigration_PublishingVersion == null) {
            readVersionFromFile();
        }
        return XMigration_PublishingVersion;
    }

    /**
     * XMigration ?? 
     * 
     * @return XMigration ??
     */
    public static String getXMigrationPublshingDate() {
        if (XMigration_PublishingDate == null) {
            readVersionFromFile();
        }
        return XMigration_PublishingDate;
    }

    /**
     * XMigration ? ?
     */
    private static void readVersionFromFile() {
        XMigration_PublishingVersion = "<unknown>";
        XMigration_PublishingDate = "<unknown>";
        InputStreamReader isr = null;
        LineNumberReader lnr = null;
        try {
            isr = new InputStreamReader(
                    XNDiscMigUtil.class.getResourceAsStream("/com/sds/acube/ndisc/xmigration/version.txt"));
            if (isr != null) {
                lnr = new LineNumberReader(isr);
                String line = null;
                do {
                    line = lnr.readLine();
                    if (line != null) {
                        if (line.startsWith("Publishing-Version=")) {
                            XMigration_PublishingVersion = line
                                    .substring("Publishing-Version=".length(), line.length()).trim();
                        } else if (line.startsWith("Publishing-Date=")) {
                            XMigration_PublishingDate = line.substring("Publishing-Date=".length(), line.length())
                                    .trim();
                        }
                    }
                } while (line != null);
                lnr.close();
            }
        } catch (IOException ioe) {
            XMigration_PublishingVersion = "<unknown>";
            XMigration_PublishingDate = "<unknown>";
        } finally {
            try {
                if (lnr != null) {
                    lnr.close();
                }
                if (isr != null) {
                    isr.close();
                }
            } catch (IOException ioe) {
            }
        }
    }
}