Package org.persvr.javascript

Examples of org.persvr.javascript.PersevereNativeFunction


      Map<String,String> requestHeaders = new HashMap();
      Map responseHeaders = new HashMap();
    @Override
    public Object get(String name, Scriptable start) {
      if("open".equals(name)){
        return new PersevereNativeFunction(){
          @Override
          public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
            methodName = (String) args[0];
            targetUrl = (String) args[1];
            if(!targetUrl.matches("\\w+tps?:/.*"))
              targetUrl = localURI + (targetUrl.startsWith("/") ? targetUrl.substring(1) : targetUrl);
            requestHeaders.put("Accept", "*/*");
            responseText = null;
            return null;
          }
         
        };
      }
      if("send".equals(name)){
        return new PersevereNativeFunction(){
          @Override
          public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
            responseText = null;
              HttpMethod method = null;
              if("GET".equals(methodName)){
                method = new GetMethod(targetUrl);
              }
              else if("PUT".equals(methodName)){
                method = new PutMethod(targetUrl);
                ((PutMethod)method).setRequestBody((String) args[0]);
              }
              else if("POST".equals(methodName)){
                method = new PostMethod(targetUrl);
                ((PostMethod)method).setRequestBody((String) args[0]);
              }
              else if("DELETE".equals(methodName)){
                method = new DeleteMethod(targetUrl);
              }
              else
                throw new RuntimeException("Unknown method for XMLHttpRequest");
              try {
                for(Map.Entry<String,String> header : requestHeaders.entrySet())
                  method.setRequestHeader(header.getKey(), header.getValue());
              status = httpClient.executeMethod(method);
              for(Header header : method.getResponseHeaders()){
                responseHeaders.put(header.getName(), header.getValue());
              }
             
              responseText = slurp(method.getResponseBodyAsStream());
            } catch (HttpException e) {
              throw ScriptRuntime.constructError("Error", e.getMessage());
            } catch (IOException e) {
              throw ScriptRuntime.constructError("Error", e.getMessage());
            }
            finally {
              method.releaseConnection();
            }
              readyState = 4;
              Object readyStateChangeHandler = XMLHttpRequest.this.get("onreadystatechange",XMLHttpRequest.this);
              if(readyStateChangeHandler instanceof Function){
                ((Function)readyStateChangeHandler).call(cx, scope, thisObj, new Object[0]);
              }
              return null;
          }
         
        };
      }
      if("readyState".equals(name))
        return readyState;
      if("status".equals(name))
        return status;
      if("getResponseHeader".equals(name)){
        return new PersevereNativeFunction(){
          @Override
          public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
            return responseHeaders.get(args[0]);
          }
         
        };
      }
      if("setRequestHeader".equals(name)){
        return new PersevereNativeFunction(){
          @Override
          public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
            return requestHeaders.put(args[0].toString(),ScriptRuntime.toString(args[1]));
          }
         
        };
      }
      if("abort".equals(name)){
        // it's always synchronous, not sure we need to do anything here
        return new PersevereNativeFunction(){
          @Override
          public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
            return null;
          }
         
View Full Code Here


        public String format(LogRecord record){
          return record.getLevel() + ": " + record.getMessage() + '\n';
        }
      });
    }
    set("log", new PersevereNativeFunction() {
      @Override
      public Object call(Context cx, Scriptable scope,
          Scriptable thisObj, Object[] args) {
        String output = "";
        for(Object arg : args){
          output += arg + " ";         
        }
        log.info(output);
        return null;
      }
     
    });
    set("warn", new PersevereNativeFunction() {
      @Override
      public Object call(Context cx, Scriptable scope,
          Scriptable thisObj, Object[] args) {
        String output = "";
        for(Object arg : args){
          output += arg + " ";
        }
        log.warn(output);
        return null;
      }
     
    });
    set("debug", new PersevereNativeFunction() {
      @Override
      public Object call(Context cx, Scriptable scope,
          Scriptable thisObj, Object[] args) {
        String output = "";
        for(Object arg : args){
          output += arg + " ";
        }
        log.debug(output);
        return null;
      }
     
    });
    set("error", new PersevereNativeFunction() {
      @Override
      public Object call(Context cx, Scriptable scope,
          Scriptable thisObj, Object[] args) {
        String output = "";
        for(Object arg : args){
          output += arg + " ";
        }
        log.error(output);
        return null;
      }
     
    });
    set("info", new PersevereNativeFunction() {
      @Override
      public Object call(Context cx, Scriptable scope,
          Scriptable thisObj, Object[] args) {
        String output = "";
        for(Object arg : args){
          output += arg + " ";
        }
        log.info(output);
        return null;
      }
     
    });
    set("fatal", new PersevereNativeFunction() {
      @Override
      public Object call(Context cx, Scriptable scope,
          Scriptable thisObj, Object[] args) {
        String output = "";
        for(Object arg : args){
View Full Code Here

      public Object runMethod(Context cx, Scriptable thisObj, Object newObject) {
        return newObject;
      }
    },"message"));
    objectProto.setAttributes("message",ScriptableObject.DONTENUM);
    objectProto.setGetterOrSetter("parent", 0, new PersevereNativeFunction(){

      @Override
        public Object call(Context cx, Scriptable scope, Scriptable thisObj,
                    Object[] args) {
        return thisObj instanceof Persistable ? ((Persistable)thisObj).getParent() : null;
      }
    },false);
    objectProto.setAttributes("parent",ScriptableObject.DONTENUM);
   
    Scriptable applicationJavascript = new NativeObject();
    objectProto.put("representation:application/javascript", objectProto, applicationJavascript);
    objectProtoMirror.put("representation:application/javascript", objectProtoMirror, applicationJavascript);
    objectProto.setAttributes("representation:application/javascript",ScriptableObject.DONTENUM);
    applicationJavascript.put("quality", applicationJavascript, 0.9);
    applicationJavascript.put("output", applicationJavascript, new PersevereNativeFunction(){
      @Override
      public Object call(Context cx, Scriptable scope,
          Scriptable thisObj, Object[] args) {
        try {
          HttpServletResponse response = Client.getCurrentObjectResponse().getHttpResponse();
          DirtyOutputStreamWriter writer = new DirtyOutputStreamWriter(response.getOutputStream(),"UTF8");
          try{
            new JavaScriptSerializer().serialize(args[0], Client.getCurrentObjectResponse(), writer);
          }
          catch(RuntimeException e){
            if(writer.isDirty)
              writer.write("&&");
            throw e;
          }
          finally{
            writer.flushIfDirty();
          }
          return null;
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }

    });
   
    Scriptable applicationJson = new NativeObject();
    objectProto.put("representation:application/json", objectProto, applicationJson);
    objectProtoMirror.put("representation:application/json", objectProtoMirror, applicationJson);
    objectProto.setAttributes("representation:application/json",ScriptableObject.DONTENUM);
    applicationJson.put("quality", applicationJson, 0.8);
    applicationJson.put("output", applicationJson, new PersevereNativeFunction(){
      @Override
      public Object call(Context cx, Scriptable scope,
          Scriptable thisObj, Object[] args) {
        try {
          HttpServletResponse response = Client.getCurrentObjectResponse().getHttpResponse();
View Full Code Here

      globalScope.put("pjs", globalScope, pjsLibrary);
      Scriptable consoleLibrary = new ConsoleLibrary();
      consoleLibrary.setPrototype(objectPrototype);
      globalScope.put("console", globalScope, consoleLibrary);
      // provide access to the request
      ((ScriptableObject)globalScope).setGetterOrSetter("request", 0, new PersevereNativeFunction() {
        @Override
        public Object call(Context cx, Scriptable scope,
            Scriptable thisObj, Object[] args) {
          return Client.getCurrentObjectResponse().getHttpRequest();
        }
       
      }, false);
      ((ScriptableObject)globalScope).setGetterOrSetter("response", 0, new PersevereNativeFunction() {
        @Override
        public Object call(Context cx, Scriptable scope,
            Scriptable thisObj, Object[] args) {
          return Client.getCurrentObjectResponse().getHttpResponse();
        }
       
      }, false);
     
      globalScope.put("Thread", globalScope, new ThreadConstructor());
      globalScope.put("global", globalScope, globalScope);
      globalScope.put("setTimeout", globalScope, new TimerFunction(false));
      globalScope.put("setInterval", globalScope, new TimerFunction(true));
      Function clearTimer = new PersevereNativeFunction() {
        @Override
        public Object call(Context cx, final Scriptable scope,
            Scriptable thisObj, Object[] args) {
          if(args.length > 0 && args[0] instanceof Number)
            currentQueuedTasks.remove(((Number)args[0]).intValue()).cancel();
          return Undefined.instance;
        }
      };
      globalScope.put("clearTimeout", globalScope, clearTimer);
      globalScope.put("clearInterval", globalScope, clearTimer);
      globalScope.put("tests", globalScope, new TestRunner());

      ((NativeObject)globalScope).setGetterOrSetter("profiling",0, new PersevereNativeFunction(){
        @Override
        public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
          if(Boolean.TRUE.equals(args[0]))
            Method.startProfiling();
          else
            Method.profiling = false;
          return null;
        }
      },true);
      ((NativeObject)globalScope).setGetterOrSetter("profiling",0, new PersevereNativeFunction(){
        @Override
        public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
          return Method.profiling;
        }
      },false);
      globalScope.put("XMLHttpRequest",globalScope, new PersevereNativeFunction(){

        @Override
        public Scriptable construct(Context cx, Scriptable scope, Object[] args) {
          XMLHttpRequest xhr = new XMLHttpRequest();
          xhr.setPrototype(objectPrototype);
View Full Code Here

    @Override
    public Scriptable construct(Context cx, Scriptable scope, Object[] args) {
      final Thread thread = new Thread(runnableInNewThread(scope, args, "Thread"));
      Persistable jsThread = Persevere.newObject();
      jsThread.put("join", jsThread, new PersevereNativeFunction(){

        @Override
        public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
          try {
            thread.join();
View Full Code Here

TOP

Related Classes of org.persvr.javascript.PersevereNativeFunction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.