顯示具有 Java 標籤的文章。 顯示所有文章
顯示具有 Java 標籤的文章。 顯示所有文章

2015年11月5日 星期四

可設定 Timeout 之 Java Command Line Executor



import java.util.concurrent.TimeoutException;

public class TimeoutCommandLine {
    public static int executeCommandLine(String commandLine, long timeout)
            throws
Exception {
        Process process = Runtime.getRuntime().exec(commandLine);
        Worker worker = new Worker(process);
        worker.start();
        try {
            worker.join(timeout);
            if (worker.exit != null) {
                return worker.exit;
            } else {
                throw new TimeoutException();
            }
        } catch (InterruptedException ex) {
            worker.interrupt();
            Thread.currentThread().interrupt();
            throw ex;
        } finally {
            process.destroy();
        }
    }

    private static class Worker extends Thread {
        private final Process process;
        private Integer exit;

        private Worker(Process process) {
            this.process = process;
        }

        public void run() {
            try {
                exit = process.waitFor();
            } catch (InterruptedException ignore) {
                return;
            }
        }
    }
}
使用方式:
TimeoutCommandLine.executeCommandLine("rsh xxxHost hostname", 2000);
設定執行 command rsh xxxHost hostname , 超過 2秒 沒回應即回傳 TimeoutException。

2015年9月14日 星期一

Java Method for Regular Expression


以下 Method 為判斷字串是否符合所設定之 Regular Expression

    /**
     * Get the match result between string and regular expression pattern string
     *
     * @param patternStr - Regular expression pattern string.
     * @param testString - String for testing with match pattern.
     * @return true/false: String is match (true), or is not match (false).
     */
    public static boolean isRegexMatch(String patternStr, String testString) {
        Pattern pattern = Pattern.compile(patternStr);
        Matcher matcher = pattern.matcher(testString);
       
        return matcher.matches();
    }

2015年1月18日 星期日

JSON 與 Map 互轉 (使用 GSON)


import java.lang.reflect.Type;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

public class JsonUtils {
  
    public static Map<String, Object> fromJson(String json) {
        return fromJson(json, Map.class);
    }

    public static String toJson(Object src) {

        Gson gson = new Gson();
        return gson.toJson(src);
    }

    public static <T> T fromJson(String json, Type typeOfT) {
        Gson gson = new Gson();
        return (T) gson.fromJson(json, typeOfT);
    }
   
    public static boolean isValidJson(String json) {
        try {
            fromJson(json);
            return true;
        } catch(JsonSyntaxException ex) {
            return false;
        }
    }
   
    public static boolean isValidJson(String json, Type typeOfT) {
        try {
            fromJson(json, Map.class);
            return true;
        } catch(JsonSyntaxException ex) {
            return false;
        }
    }
}

使用方式:
[JSON Map]
Map<String, Object> retMap = JsonUtils.fromJson(jsonData);
傳入 JSON 字串 jsonData ==> JsonUtils.fromJson(jsonData)
回傳型態為 Map<String, Object> map: retMap

[Map JSON]
String jsonString = JsonUtils.toJson(mapData);
傳入 型態為 Map<String, Object> map mapData ==> JsonUtils.toJson(mapData);
回傳 JSON 字串 jsonString