package org.projectodd.nodej;

import java.io.FileNotFoundException;

import org.dynjs.runtime.DynJS;
import org.dynjs.runtime.GlobalObject;
import org.dynjs.vertx.DynJSVerticleFactory;
import org.vertx.java.core.Vertx;
import org.vertx.java.platform.Container;

public class NodeJSVerticleFactory extends DynJSVerticleFactory {
    
    private Process process;

    @Override
    public void init(Vertx vertx, Container container, ClassLoader classloader) {
        super.init(vertx, container, classloader);
        DynJS runtime = this.getRuntime();
        GlobalObject global = runtime.getExecutionContext().getGlobalObject();
        process = new Process(global, null);
        runtime.getConfig().setGlobalObjectFactory(new NodeJSGlobalObjectFactory());
        try {
            loadScript(getRuntime().getExecutionContext(), "node.js");
        } catch (FileNotFoundException e) {
            System.err.println("Cannot initialize NodeJ");
            e.printStackTrace();
        }
    }

    private class NodeJSGlobalObjectFactory extends DynJSGlobalObjectFactory {
        @Override
        public GlobalObject newGlobalObject(final DynJS runtime) {
            GlobalObject global = super.newGlobalObject(runtime);
            global.defineReadOnlyGlobalProperty("process", process);
            return global;
        }
    }
    
}

@@ -95,16 +95,59 @@
 .DynJS;%0A
+import org.dynjs.runtime.ExecutionContext;%0A
 import o
@@ -374,38 +374,8 @@
    %0A
-    private Process process;%0A%0A
     
@@ -568,35 +568,40 @@
         
-GlobalObject global
+ExecutionContext context
  = runti
@@ -628,71 +628,8 @@
 xt()
-.getGlobalObject();%0A        process = new Process(global, null)
 ;%0A  
@@ -707,16 +707,23 @@
 Factory(
+context
 ));%0A    
@@ -759,42 +759,15 @@
 ipt(
-getRuntime().getExecutionC
+c
 ontext
-()
 , %22n
@@ -1004,24 +1004,207 @@
 ctFactory %7B%0A
+        private Process process;%0A%0A        NodeJSGlobalObjectFactory(ExecutionContext context) %7B%0A            process = new Process(context.getGlobalObject(), null);%0A        %7D%0A        %0A
         @Ove

Make the global object factory responsible for creating the Process object.