Package org.mozilla.javascript

Examples of org.mozilla.javascript.ScriptableObject


        }
        initializer.setLastModified(lastModified);
        initializer.setVersion(new ObjectVersion(objectReference, previousVersionReference, lastTableChange, versionNumber,
            (objectAttributes & OBJECT_ATTRIBUTE_IS_CURRENT) == OBJECT_ATTRIBUTE_IS_CURRENT,
            (objectAttributes & OBJECT_ATTRIBUTE_IS_DELETED) == OBJECT_ATTRIBUTE_IS_DELETED));
        ScriptableObject loadedObject = (ScriptableObject) initializer.getInitializingObject();
        assert ((ObjectVersion) ((Persistable) loadedObject).getVersion()).currentVersionReference == objectReference;
        if (historic) {
          synchronized (referenceToVersions) {
            referenceToVersions.put(objectReference, (Persistable) loadedObject);
          }
          if(!(loadedObject instanceof PersistableArray)){
            //TODO: make the persistable array load everything up front so we don't have to do this
            loadedObject.sealObject();
          }
        } else
          synchronized (referenceToInstances) {
            referenceToInstances.put(objectReference, (Persistable) loadedObject);
          }
View Full Code Here


  public static PersistableClass Object;
  public static PersistableClass Array;
  // create a new object when the constructor is called
  public Scriptable construct(Context cx, Scriptable scope, Object[] args) {
    if(this == Object){
      ScriptableObject newObject = new PersistableObject();
      newObject.setParentScope(globalScope);
      newObject.setPrototype(objectPrototype);
      return newObject;
    }
    if(this == Array){
      ScriptableObject newObject;
      if(args.length == 0)
        newObject = new PersistableArray(0);
      else if(args.length == 1)
        newObject = new PersistableArray(((Number)args[0]).longValue());
      else
        newObject = new PersistableArray(args);
      newObject.setParentScope(globalScope);
      newObject.setPrototype(arrayPrototype);
      return newObject;
    }
    return instantiate(cx, scope, args, true);
  }
View Full Code Here

   */
  public static void serialize(Object value, String acceptTypeHeader){
    serialize(value, acceptTypeHeader, true);
  }
  public static void serialize(Object value, String acceptTypeHeader, boolean setType){
    ScriptableObject object;
    if (value instanceof Persistable){
      object = ((ScriptableObject)value);
    }else{
      object = (ScriptableObject) ScriptableObject.getClassPrototype(GlobalData.getGlobalScope(),"Object");
    }
     
    Scriptable bestRep = null;
    double bestQuality = 0;
    String bestType = null;
    if(acceptTypeHeader==null)
      acceptTypeHeader = "*/*";
    String[] acceptTypes = acceptTypeHeader.split("\\s*,\\s*");
    PersistableObject.enableSecurity(false);
    Client.getCurrentObjectResponse().requestRoot = value;
    for (String acceptType : acceptTypes){
      String[] parts = acceptType.split("\\s*;\\s*");
      String type = parts[0];
      double clientQuality = 1;
      for(String part: parts){
        if(part.startsWith("q=")){
          try {
            clientQuality = Double.parseDouble(part.substring(2));
          } catch (NumberFormatException e) {
            e.printStackTrace();
          }
        }
      }
      if("*/*".equals(type)){
        ScriptableObject proto = object;
        do {
          for (Map.Entry<String,Object> entry : PersistableObject.entrySet(proto, 5)){
            if(entry.getKey().startsWith("representation:")){
              Object rep = entry.getValue();
              if(rep instanceof ObjectId){
                rep = ((ObjectId)rep).getTarget();
              }
              if(rep instanceof Scriptable){
                Number serverQuality = (Number) ScriptableObject.getProperty((Scriptable)rep,"quality");
                double quality = serverQuality.doubleValue() + clientQuality;
                if(quality > bestQuality){
                  bestRep = (Scriptable) rep;
                  bestQuality = quality;
                  bestType = entry.getKey().substring("representation:".length());
                }
              }
             
            }
          }
          proto = (ScriptableObject) proto.getPrototype();
         
        }while(proto != null);
       
      }
      else{
View Full Code Here

      // if we post to a schema, we can just consider this a post to the table
      target = (Persistable) Identification.idForString(((PersistableClass) target).getId().toString() + "/").getTarget();
    }
    if (bodyData instanceof Persistable) {
      Persistable newObject = (Persistable) bodyData;
      ScriptableObject arrayProto = (ScriptableObject) ScriptableObject.getClassPrototype(GlobalData.getGlobalScope(), "Array");
      ScriptableObject objectProto = (ScriptableObject) ScriptableObject.getClassPrototype(GlobalData.getGlobalScope(), "Object");

      if ("".equals(((Persistable) target).getId().subObjectId)
          && (((Persistable) bodyData).getPrototype() == objectProto || ((Persistable) bodyData).getPrototype() == arrayProto)) {
        // this means it is a generic object or list, we need to make it be the right class
        DataSource thisSource = ((Persistable) target).getId().source;
View Full Code Here

  public abstract Object runMethod(Context cx, Scriptable thisObj, Object bodyValue);
  // this is a mirror of the object proto that is enumerable
  public static Persistable objectProtoMirror = new PersistableObject();
  static void setRestMethods(Scriptable scope) {
    final ScriptableObject arrayProto = (ScriptableObject) ScriptableObject.getClassPrototype(scope,"Array");
    final ScriptableObject objectProto = (ScriptableObject) ScriptableObject.getClassPrototype(scope,"Object");
    objectProto.put("get", objectProto, new GetMethod());
    objectProto.setAttributes("get",ScriptableObject.DONTENUM);
    objectProto.put("head", objectProto, new Method(new GetMethod() {
      @Override
      public Object runMethod(Context cx, Scriptable thisObj, Object bodyValue) {
        super.runMethod(cx, thisObj, bodyValue);
        return Undefined.instance;
      }
    },"head"));
    objectProto.setAttributes("head",ScriptableObject.DONTENUM);
    objectProto.put("put", objectProto, new Method(new RestMethod("function(content, target, property){\n\t[native code]\n}") {
      @Override
      public Object runMethod(Context cx, Scriptable thisObj, Object bodyValue) {
        throw new RuntimeException("Should never be called");
      }
      @Override
      public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
/*        Object content = args[0];
        Persistable target = (Persistable) thisObj;
        if (args.length > 1)
          target = (Persistable) args[1];
        if (args.length > 1 && args[2] != null){
          // if the property is defined we will set just that
          String property = args[2].toString();
        }*/
       
        //TODO: Move the Persevere operations into here
        return thisObj;// pretty simple, the Persevere handles the setting the values
      }
    },"put"));
    objectProto.setAttributes("put",ScriptableObject.DONTENUM);
    objectProto.put("delete", objectProto, new Method(new RestMethod("function(from){\n\t[native code]\n}") {
     
      @Override
      public Object runMethod(Context cx, Scriptable thisObj, Object bodyValue) {
        throw new RuntimeException("Should never be called");
      }
      @Override
      public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
       
        /*if (args.length > 0 && args[0] != null) { // This means we know what object to delete the target from
          String from = (String) args[0];
          Identification<? extends Object> fromId = Identification.idForString(from);
          Object fromTarget = fromId.getTarget();
          if (fromTarget instanceof List) // if it is list we can use the efficient remove method
            ((List<Object>) fromTarget).remove(thisObj);
          else if (fromTarget instanceof Persistable) { // find the right value
            for (Map.Entry<String,Object> entry : ((Persistable) fromTarget).entrySet(0))
              if (thisObj== entry.getValue())
                ((Persistable) fromTarget).delete(entry.getKey());
          } else
            throw new RuntimeException("The 'from' parameter does not refer to a valid object");
        }
        else {
          if (thisObj instanceof Persistable) {
            ((Persistable) thisObj).delete();
          } else {
            throw new RuntimeException("This type of object path does not currently support DELETE");
          }
        }*/
        return Undefined.instance;
      }

    },"delete"));
    objectProto.setAttributes("delete",ScriptableObject.DONTENUM);

    objectProto.put("post", objectProto, new Method(new RestMethod("function(content){\n\t[native code]\n}") {

      @Override
      public Object runMethod(Context cx, Scriptable thisObj, Object newObject) {
        if (newObject instanceof Persistable)
          PersistableClass.enforceObjectIsValidBySchema(((Persistable) newObject).getSchema(), (Persistable) newObject);
        return newObject;
      }
    },"post"));
    objectProto.setAttributes("post",ScriptableObject.DONTENUM);

    objectProto.put("onSave", objectProto, new Method(new RestMethod("function(content){\n}") {

      @Override
      public Object runMethod(Context cx, Scriptable thisObj, Object newObject) {
        return newObject;
      }
    },"onSave"));
    objectProto.setAttributes("onSave",ScriptableObject.DONTENUM);
    objectProto.put("message", objectProto, new Method(new RestMethod("function(content){\n}") {

      @Override
      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) {
View Full Code Here

                throw new MojoExecutionException("Cant' load jslint.js", e);
            }
        } else {
            try {
                Context cx = contextFactory.enter();
                ScriptableObject scope = cx.initStandardObjects();
                Reader r = new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(JSLINT_FILE), UTF8);
                cx.evaluateReader(scope, r, "jslint.js", 1, null);
                return new JSLint(contextFactory, scope);
            } catch (IOException ioe) {
                throw new MojoExecutionException("Cant' load jslint.js", ioe);
View Full Code Here

            this.content = content;
            this.windowWrapper = ww;
        }

        public Object[] buildArguments() {
            ScriptableObject so = new NativeObject();
            so.put("success", so,
                   (success) ? Boolean.TRUE : Boolean.FALSE);
            if (mime != null) {
                so.put("contentType", so,
                       Context.toObject(mime, windowWrapper));
            }
            if (content != null) {
                so.put("content", so,
                       Context.toObject(content, windowWrapper));
            }
            return new Object [] { so };
        }
View Full Code Here

                              final Object[] args,
                              Function funObj)
        throws JavaScriptException {
        int len = args.length;
        final Window window = ((RhinoInterpreter.ExtendedContext)cx).getWindow();
        final ScriptableObject go = ((RhinoInterpreter.ExtendedContext)cx).getGlobalObject();
        if (len < 2) {
            throw Context.reportRuntimeError("invalid argument count");
        }
        RhinoInterpreter interp =
            (RhinoInterpreter)window.getInterpreter();
View Full Code Here

                               final Object[] args,
                               Function funObj)
        throws JavaScriptException {
        int len = args.length;
        final Window window = ((RhinoInterpreter.ExtendedContext)cx).getWindow();
        final ScriptableObject go = ((RhinoInterpreter.ExtendedContext)cx).getGlobalObject();
        if (len < 3) {
            throw Context.reportRuntimeError("invalid argument count");
        }
        RhinoInterpreter interp =
            (RhinoInterpreter)window.getInterpreter();
View Full Code Here

            this.content = content;
            this.scope   = scope;
        }

        public Object[] buildArguments() {
            ScriptableObject so = new NativeObject();
            so.put("success", so,
                   (success) ? Boolean.TRUE : Boolean.FALSE);
            if (mime != null) {
                so.put("contentType", so,
                       Context.toObject(mime, scope));
            }
            if (content != null) {
                so.put("content", so,
                       Context.toObject(content, scope));
            }
            return new Object [] { so };
        }
View Full Code Here

TOP

Related Classes of org.mozilla.javascript.ScriptableObject

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.