/*
 * Copyright 2015.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tascape.qa.th.ios.comm;

import com.martiansoftware.nailgun.NGContext;
import com.tascape.qa.th.SystemConfiguration;
import net.sf.lipermi.handler.CallHandler;
import net.sf.lipermi.net.Client;
import org.slf4j.LoggerFactory;

/**
 *
 * @author linsong wang
 */
public class JavaScriptNail {
    private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(JavaScriptNail.class);

    public static final String SYSPROP_NG_CLIENT = "qa.th.comm.NG_CLIENT";

    public static final String NG_CLIENT = SystemConfiguration.getInstance().getProperty(SYSPROP_NG_CLIENT,
        "/usr/local/bin/ng");

    public static void nailMain(NGContext context) throws Exception {
        int port = Integer.parseInt(context.getArgs()[0]);
        CallHandler callHandler = new CallHandler();
        Client client = new Client("localhost", port, callHandler);
        JavaScriptServer jss = JavaScriptServer.class.cast(client.getGlobal(JavaScriptServer.class));
        String js = jss.retrieveJavaScript();
        System.out.println(js);
    }
}

@@ -672,45 +672,137 @@
 ort 
-com.tascape.qa.th.SystemConfiguration
+java.io.File;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.nio.file.Paths
 ;%0Aim
@@ -876,16 +876,54 @@
 Client;%0A
+import org.apache.commons.io.IOUtils;%0A
 import o
@@ -1139,24 +1139,16 @@
  String 
-SYSPROP_
 NG_CLIEN
@@ -1152,173 +1152,904 @@
 IENT
- = %22qa.th.comm.NG_CLIENT%22;%0A%0A    public static final String NG_CLIENT = SystemConfiguration.getInstance().getProperty(SYSPROP_NG_CLIENT,%0A        %22/usr/local/bin/
+;%0A%0A    static %7B%0A        InputStream in = JavaScriptNail.class.getResourceAsStream(%22/ng%22);%0A        File ng = Paths.get(System.getProperty(%22user.home%22)).resolve(System.currentTimeMillis() + %22%22)%0A            .resolve(%22ng%22).toFile();%0A        try %7B%0A            if (!(ng.getParentFile().mkdirs() && ng.createNewFile())) %7B%0A                throw new RuntimeException(%22Cannot create ng client file %22 + ng);%0A            %7D%0A        %7D catch (IOException ex) %7B%0A            throw new RuntimeException(%22Cannot create ng client file%22, ex);%0A        %7D%0A        try %7B%0A            IOUtils.copy(in, new FileOutputStream(ng));%0A        %7D catch (IOException ex) %7B%0A            throw new RuntimeException(ex);%0A        %7D%0A        if (!ng.setExecutable(true)) %7B%0A            throw new RuntimeException(%22Cannot mark ng client executable %22 + 
 ng
-%22
 );
+%0A        %7D%0A        ng.deleteOnExit();%0A        NG_CLIENT = ng.getAbsolutePath();%0A    %7D
 %0A%0A  

use packaged ng client