package org.cboard.services;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.cboard.services.persist.PersistContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Created by yfyuan on 2017/2/10.
 */
@Service
public class PersistService {

    @Value("${phantomjs_path}")
    private String phantomjsPath;
    private String scriptPath = new File(this.getClass().getResource("/phantom.js").getFile()).getPath();

    @Value("${web_port}")
    private String webPort;
    @Value("${web_context}")
    private String webContext;

    private static final ConcurrentMap<String, PersistContext> TASK_MAP = new ConcurrentHashMap<>();

    public PersistContext persist(Long dashboardId, String userId) {
        String persistId = UUID.randomUUID().toString().replaceAll("-", "");
        try {
            String web = webPort + "/";
            if (StringUtils.isNotBlank(webContext)) {
                web += webContext + "/";
            }
            Process process = Runtime.getRuntime().exec(String.format("%s %s %s %s %s %s", phantomjsPath, scriptPath, dashboardId, persistId, userId, web));
            PersistContext context = new PersistContext(dashboardId);
            TASK_MAP.put(persistId, context);
            synchronized (context) {
                context.wait();
            }
            process.destroy();
            TASK_MAP.remove(persistId);
            return context;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String persistCallback(String persistId, JSONObject data) {
        PersistContext context = TASK_MAP.get(persistId);
        synchronized (context) {
            context.setData(data);
            context.notify();
        }
        return "1";
    }
}

@@ -93,76 +93,279 @@
 ons.
-lang3.StringUtils;%0Aimport org.cboard.services.persist.PersistContext
+collections.map.HashedMap;%0Aimport org.apache.commons.lang.text.StrSubstitutor;%0Aimport org.apache.commons.lang3.StringUtils;%0Aimport org.cboard.jdbc.JdbcDataProvider;%0Aimport org.cboard.services.persist.PersistContext;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
 ;%0Aim
@@ -490,16 +490,38 @@
 o.File;%0A
+import java.util.Map;%0A
 import j
@@ -709,16 +709,102 @@
 vice %7B%0A%0A
+    private static final Logger LOG = LoggerFactory.getLogger(PersistService.class);%0A%0A
     @Val
@@ -1331,16 +1331,48 @@
 %22, %22%22);%0A
+        Process process = null;%0A
         
@@ -1542,52 +1542,21 @@
     
-Process process = Runtime.getRuntime().exec(
+String cmd = 
 Stri
@@ -1649,16 +1649,125 @@
 Id, web)
+;%0A            LOG.info(%22Run phantomjs command: %7B%7D%22, cmd);%0A            process = Runtime.getRuntime().exec(cmd
 );%0A     
@@ -2087,24 +2087,108 @@
 eption e) %7B%0A
+            if (process != null) %7B%0A                process.destroy();%0A            %7D%0A
             

fix style of line designer