package org.projectodd.nodej;

import java.io.FileNotFoundException;

import org.dynjs.runtime.DynJS;
import org.dynjs.runtime.GlobalObject;
import org.dynjs.runtime.GlobalObjectFactory;
import org.dynjs.vertx.DynJSVerticleFactory;
import org.vertx.java.platform.Verticle;

public class NodeJSVerticleFactory extends DynJSVerticleFactory {
    private GlobalObjectFactory globalObjectFactory;
    
    @Override
    public Verticle createVerticle(String main) throws Exception {
        initializeRootContext();
        try {
            loadScript(getExecutionContext(), "node.js");
            new Node(getExecutionContext());
        } catch (FileNotFoundException e) {
            System.err.println("Cannot initialize NodeJ");
            e.printStackTrace();
        }
        return new DynJSVerticle(main);
    }
    
    @Override
    public GlobalObjectFactory getGlobalObjectFactory() {
        if (this.globalObjectFactory == null) {
            this.globalObjectFactory = new NodeJSGlobalObjectFactory();
        }
        return this.globalObjectFactory;
    }

    private class NodeJSGlobalObjectFactory extends DynJSGlobalObjectFactory {
        @Override
        public GlobalObject newGlobalObject(final DynJS runtime) {
            GlobalObject global = super.newGlobalObject(runtime);
            global.defineGlobalProperty("process", new Process(global, null));
            global.defineGlobalProperty("Buffer", "foo");
            return global;
        }
    }
    
}

@@ -386,21 +386,17 @@
 actory;%0A
-    
 %0A
+
     @Ove
@@ -810,21 +810,17 @@
 ;%0A    %7D%0A
-    
 %0A
+
     @Ove
@@ -879,24 +879,58 @@
 Factory() %7B%0A
+        synchronized (this) %7B%0A    
         if (
@@ -961,24 +961,28 @@
  == null) %7B%0A
+    
             
@@ -1037,24 +1037,38 @@
 tFactory();%0A
+            %7D%0A
         %7D%0A  
@@ -1523,15 +1523,11 @@
 %7D%0A    %7D%0A
-    
 %0A%7D%0A

Make factory threadsafe