ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ivasilin...@apache.org
Subject incubator-ignite git commit: #ignite-964: add JSONCacheObject.
Date Sun, 05 Jul 2015 19:16:10 GMT
Repository: incubator-ignite
Updated Branches:
  refs/heads/ignite-964-1 832bf7797 -> 77e6c5ca8


#ignite-964: add JSONCacheObject.


Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/77e6c5ca
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/77e6c5ca
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/77e6c5ca

Branch: refs/heads/ignite-964-1
Commit: 77e6c5ca8c2faccc1ef1ae329a99a5da06951c2a
Parents: 832bf77
Author: ivasilinets <ivasilinets@gridgain.com>
Authored: Sun Jul 5 22:16:02 2015 +0300
Committer: ivasilinets <ivasilinets@gridgain.com>
Committed: Sun Jul 5 22:16:02 2015 +0300

----------------------------------------------------------------------
 .../handlers/scripting/JSONCacheObject.java     | 247 -------------------
 .../rest/handlers/scripting/NodeJsCache.java    |   9 +-
 .../rest/handlers/scripting/RestEntry.java      |   8 +-
 .../handlers/scripting/RestJSONCacheObject.java | 166 +++++++++++++
 .../processors/scripting/JSONCacheObject.java   | 105 ++++++++
 .../http/jetty/GridJettyRestHandler.java        |   5 +-
 .../jetty/IgniteScriptingCommandHandler.java    |   6 +-
 7 files changed, 288 insertions(+), 258 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/77e6c5ca/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/JSONCacheObject.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/JSONCacheObject.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/JSONCacheObject.java
deleted file mode 100644
index 00cb28a..0000000
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/JSONCacheObject.java
+++ /dev/null
@@ -1,247 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 org.apache.ignite.internal.processors.rest.handlers.scripting;
-
-import jdk.nashorn.api.scripting.*;
-import org.apache.ignite.internal.util.typedef.internal.U;
-
-import java.util.*;
-
-/**
- * Json cache object.
- */
-public class JSONCacheObject implements JSObject {
-    Map<Object, Object> fields = new HashMap();
-    /**
-     * Empty constructor.
-     */
-    private JSONCacheObject() {
-    }
-
-    /**
-     * @param o JSON object.
-     */
-    public JSONCacheObject(Map o) {
-        for (Object key : o.keySet())
-            addField(toSimpleObject(key), toSimpleObject(o.get(key)));
-    }
-
-    public Map<Object, Object> getFields() {
-        return fields;
-    }
-
-    @Override public int hashCode() {
-        return fields.hashCode();
-    }
-
-    /** {@inheritDoc} */
-    @Override public boolean equals(Object obj) {
-        if (obj == null || !(obj instanceof JSONCacheObject))
-            return false;
-
-        JSONCacheObject obj0 = (JSONCacheObject) obj;
-
-        if (fields.size() != obj0.fields.size())
-            return false;
-
-        for (Object key : obj0.fields.keySet()) {
-            if (!fields.containsKey(key))
-                return false;
-
-            if (!obj0.getField(key).equals(getField(key)))
-                return false;
-        }
-
-        return true;
-    }
-
-    /**
-     * @param key Field name.
-     * @param val Field value.
-     */
-    public void addField(Object key, Object val) {
-        fields.put(key, val);
-    }
-
-    /**
-     * @param key Field name.
-     * @return Field value.
-     */
-    public Object getField(Object key) {
-        return fields.get(key);
-    }
-
-    /**
-     * Convert JSON object to JSONCacheObject
-     *
-     * @param o Object to convert.
-     * @return Converted object.
-     */
-    public static Object toSimpleObject(Object o) {
-        o = tryConvert(o, Object[].class);
-
-        if (o instanceof Map) {
-            Map o1 = (Map)o;
-
-            JSONCacheObject res = new JSONCacheObject();
-
-            for (Object key : o1.keySet())
-                res.addField(toSimpleObject(key), toSimpleObject(o1.get(key)));
-
-            return res;
-        }
-        else if (o instanceof List) {
-            List o1 = (List) o;
-
-            List<Object> val = new ArrayList<>();
-
-            for (Object v : o1)
-                val.add(toSimpleObject(v));
-
-            return val;
-        }
-        else if (o.getClass().isArray()) {
-            Object[] o1 = (Object[]) o;
-
-            List<Object> val = new ArrayList<>();
-
-            for (Object v : o1)
-                val.add(toSimpleObject(v));
-
-            return val;
-        }
-
-        return o;
-    }
-
-    /**
-     * @param o Object.
-     * @param cl Class.
-     */
-    private static Object tryConvert(Object o, Class cl) {
-        try {
-            return ScriptUtils.convert(o, cl);
-
-        } catch (Exception e) {
-            //skip.
-        }
-
-        return o;
-    }
-
-    @Override public Object call(Object o, Object... objects) {
-        System.out.println("!!!!CALL");
-        return null;
-    }
-
-    @Override public Object newObject(Object... objects) {
-        System.out.println("!!!!newObject");
-        return null;
-    }
-
-    @Override public Object eval(String s) {
-        System.out.println("!!!!eval");
-        return null;
-    }
-
-    @Override public Object getMember(String s) {
-        System.out.println("!!!!getMember + " + s);
-        return fields.get(s);
-    }
-
-    @Override public Object getSlot(int i) {
-        System.out.println("!!!!getSlot");
-        return null;
-    }
-
-    @Override public boolean hasMember(String s) {
-        System.out.println("!!!!hasMember");
-        return fields.containsKey(s);
-    }
-
-    @Override public boolean hasSlot(int i) {
-        System.out.println("!!!!hasSlot");
-        return false;
-    }
-
-    @Override public void removeMember(String s) {
-        System.out.println("!!!!removeMember");
-        fields.remove(s);
-    }
-
-    @Override public void setMember(String s, Object o) {
-        System.out.println("!!!!setMember");
-        fields.put(s, o);
-    }
-
-    @Override public void setSlot(int i, Object o) {
-        System.out.println("!!!!setSlot");
-
-    }
-
-    @Override public Set<String> keySet() {
-        System.out.println("!!!!keySet");
-        Set<String> keys = new HashSet<>();
-
-        for (Object o : keys) {
-            if (!(o instanceof JSONCacheObject))
-                keys.add(o.toString());
-        }
-
-        return keys;
-    }
-
-    @Override public Collection<Object> values() {
-        System.out.println("!!!!values");
-        return fields.values();
-    }
-
-    @Override public boolean isInstance(Object o) {
-        System.out.println("!!!!isInstance");
-        return false;
-    }
-
-    @Override public boolean isInstanceOf(Object o) {
-        System.out.println("!!!!isInstanceOf");
-        return false;
-    }
-
-    @Override public String getClassName() {
-        System.out.println("!!!!getClassName");
-        return U.getSimpleName(JSONCacheObject.class);
-    }
-
-    @Override public boolean isFunction() {
-        System.out.println("!!!!isFunction");
-        return false;
-    }
-
-    @Override public boolean isStrictFunction() {
-        System.out.println("!!!!isStrictFunction");
-        return false;
-    }
-
-    @Override public boolean isArray() {
-        System.out.println("!!!!isArray");
-        return false;
-    }
-
-    @Override public double toNumber() {
-        return 0;
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/77e6c5ca/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/NodeJsCache.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/NodeJsCache.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/NodeJsCache.java
index 0bb56d7..40473fd 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/NodeJsCache.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/NodeJsCache.java
@@ -18,6 +18,7 @@
 package org.apache.ignite.internal.processors.rest.handlers.scripting;
 
 import org.apache.ignite.*;
+import org.apache.ignite.internal.processors.scripting.*;
 import org.apache.ignite.internal.util.typedef.internal.*;
 
 import java.util.*;
@@ -53,7 +54,7 @@ public class NodeJsCache {
     public Object get(Object key) {
         Object cacheKey = JSONCacheObject.toSimpleObject(key);
 
-        return cache.get(cacheKey);
+        return RestJSONCacheObject.convertToRestObject(cache.get(cacheKey));
     }
 
     /**
@@ -71,7 +72,7 @@ public class NodeJsCache {
      * @return True if cache contains key.
      */
     public boolean containsKeys(List keys) {
-        List cacheKeys = (List)JSONCacheObject.toSimpleObject(keys);
+        List<Object> cacheKeys = (List<Object>)JSONCacheObject.toSimpleObject(keys);
 
         return cache.containsKeys(new HashSet<>(cacheKeys));
     }
@@ -88,7 +89,9 @@ public class NodeJsCache {
         List<RestEntry> res = new ArrayList<>();
 
         for (Map.Entry<Object, Object> e : entries.entrySet())
-            res.add(new RestEntry(e.getKey(), e.getValue()));
+            res.add(new RestEntry(
+                RestJSONCacheObject.convertToRestObject(e.getKey()),
+                RestJSONCacheObject.convertToRestObject(e.getValue())));
 
         return res;
     }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/77e6c5ca/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/RestEntry.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/RestEntry.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/RestEntry.java
index 77af22f..c4f9fd1 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/RestEntry.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/RestEntry.java
@@ -16,13 +16,13 @@ public class RestEntry {
      * @param val Value.
      */
     public RestEntry(Object key, Object val) {
-        if (key instanceof JSONCacheObject)
-            this.key = ((JSONCacheObject)key).getFields();
+        if (key instanceof RestJSONCacheObject)
+            this.key = ((RestJSONCacheObject)key).getFields();
         else
             this.key = key;
 
-        if (val instanceof JSONCacheObject)
-            this.val = ((JSONCacheObject)val).getFields();
+        if (val instanceof RestJSONCacheObject)
+            this.val = ((RestJSONCacheObject)val).getFields();
         else
             this.val = val;
     }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/77e6c5ca/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/RestJSONCacheObject.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/RestJSONCacheObject.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/RestJSONCacheObject.java
new file mode 100644
index 0000000..3bb615c
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/scripting/RestJSONCacheObject.java
@@ -0,0 +1,166 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.ignite.internal.processors.rest.handlers.scripting;
+
+import jdk.nashorn.api.scripting.*;
+import org.apache.ignite.internal.processors.scripting.*;
+import org.apache.ignite.internal.util.typedef.internal.U;
+
+import java.util.*;
+
+/**
+ * Json cache object.
+ */
+public class RestJSONCacheObject implements JSObject {
+    /** Fields. */
+    private final JSONCacheObject fields;
+
+    /**
+     * @param o JSON object.
+     */
+    private RestJSONCacheObject(JSONCacheObject o) {
+        fields = o;
+    }
+
+    /**
+     * @param o Object.
+     * @return Rest JSON cache object.
+     */
+    public static Object convertToRestObject(Object o) {
+        if (o instanceof JSONCacheObject)
+            return new RestJSONCacheObject((JSONCacheObject)o);
+
+        return o;
+    }
+
+    /**
+     * @return Fields.
+     */
+    public Map<Object, Object> getFields() {
+        return fields;
+    }
+
+    /**
+     * @param key Field name.
+     * @return Field value.
+     */
+    public Object getField(Object key) {
+        return fields.get(key);
+    }
+
+    @Override public Object call(Object o, Object... objects) {
+        System.out.println("!!!!CALL");
+        return null;
+    }
+
+    @Override public Object newObject(Object... objects) {
+        System.out.println("!!!!newObject");
+        return null;
+    }
+
+    @Override public Object eval(String s) {
+        System.out.println("!!!!eval");
+        return null;
+    }
+
+    @Override public Object getMember(String s) {
+        System.out.println("!!!!getMember + " + s);
+        return fields.get(s);
+    }
+
+    @Override public Object getSlot(int i) {
+        System.out.println("!!!!getSlot");
+        return null;
+    }
+
+    @Override public boolean hasMember(String s) {
+        System.out.println("!!!!hasMember");
+        return fields.containsKey(s);
+    }
+
+    @Override public boolean hasSlot(int i) {
+        System.out.println("!!!!hasSlot");
+        return false;
+    }
+
+    @Override public void removeMember(String s) {
+        System.out.println("!!!!removeMember");
+        fields.remove(s);
+    }
+
+    @Override public void setMember(String s, Object o) {
+        System.out.println("!!!!setMember");
+        fields.put(s, o);
+    }
+
+    @Override public void setSlot(int i, Object o) {
+        System.out.println("!!!!setSlot");
+
+    }
+
+    @Override public Set<String> keySet() {
+        System.out.println("!!!!keySet");
+        Set<String> keys = new HashSet<>();
+
+        for (Object o : fields.keySet()) {
+            if (!(o instanceof RestJSONCacheObject))
+                keys.add(o.toString());
+        }
+
+        return keys;
+    }
+
+    @Override public Collection<Object> values() {
+        System.out.println("!!!!values");
+        return fields.values();
+    }
+
+    @Override public boolean isInstance(Object o) {
+        System.out.println("!!!!isInstance");
+        return false;
+    }
+
+    @Override public boolean isInstanceOf(Object o) {
+        System.out.println("!!!!isInstanceOf");
+        return false;
+    }
+
+    @Override public String getClassName() {
+        System.out.println("!!!!getClassName");
+        return U.getSimpleName(RestJSONCacheObject.class);
+    }
+
+    @Override public boolean isFunction() {
+        System.out.println("!!!!isFunction");
+        return false;
+    }
+
+    @Override public boolean isStrictFunction() {
+        System.out.println("!!!!isStrictFunction");
+        return false;
+    }
+
+    @Override public boolean isArray() {
+        System.out.println("!!!!isArray");
+        return false;
+    }
+
+    @Override public double toNumber() {
+        return 0;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/77e6c5ca/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/JSONCacheObject.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/JSONCacheObject.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/JSONCacheObject.java
new file mode 100644
index 0000000..5d3b424
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/JSONCacheObject.java
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.ignite.internal.processors.scripting;
+
+import java.util.*;
+
+/**
+ * JSON cache object.
+ */
+public class JSONCacheObject extends HashMap<Object, Object> {
+    /**
+     * Empty constructor.
+     */
+    public JSONCacheObject() {
+        // No-op.
+    }
+
+    /**
+     * @param o Map.
+     */
+    public JSONCacheObject(Map o) {
+        for (Object key : o.keySet())
+            addField(JSONCacheObject.toSimpleObject(key), JSONCacheObject.toSimpleObject(o.get(key)));
+    }
+
+    /**
+     * @param key Field name.
+     * @param val Field value.
+     */
+    public void addField(Object key, Object val) {
+        put(key, val);
+    }
+
+    /**
+     * @param key Field name.
+     * @return Field value.
+     */
+    public Object getField(Object key) {
+        return get(key);
+    }
+
+    /**
+     * Convert JSON object to RestJSONCacheObject
+     *
+     * @param o Object to convert.
+     * @return Converted object.
+     */
+    public static Object toSimpleObject(Object o) {
+        if (o instanceof Map) {
+            Map o1 = (Map)o;
+
+            JSONCacheObject res = new JSONCacheObject();
+
+            for (Object key : o1.keySet())
+                res.addField(toSimpleObject(key), toSimpleObject(o1.get(key)));
+
+            return res;
+        }
+        else if (o instanceof List) {
+            List o1 = (List) o;
+
+            List<Object> val = new ArrayList<>();
+
+            for (Object v : o1)
+                val.add(toSimpleObject(v));
+
+            return val;
+        }
+        else if (o.getClass().isArray()) {
+            Object[] o1 = (Object[]) o;
+
+            List<Object> val = new ArrayList<>();
+
+            for (Object v : o1)
+                val.add(toSimpleObject(v));
+
+            return val;
+        }
+
+        return o;
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean equals(Object obj) {
+        if (obj == null || !(obj instanceof JSONCacheObject))
+            return false;
+
+        return super.equals(obj);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/77e6c5ca/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java
----------------------------------------------------------------------
diff --git a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java
b/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java
index aecb79f..dd57c93 100644
--- a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java
+++ b/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java
@@ -24,6 +24,7 @@ import org.apache.ignite.internal.processors.rest.*;
 import org.apache.ignite.internal.processors.rest.client.message.*;
 import org.apache.ignite.internal.processors.rest.handlers.scripting.*;
 import org.apache.ignite.internal.processors.rest.request.*;
+import org.apache.ignite.internal.processors.scripting.*;
 import org.apache.ignite.internal.util.typedef.*;
 import org.apache.ignite.internal.util.typedef.internal.*;
 import org.apache.ignite.lang.*;
@@ -340,8 +341,8 @@ public class GridJettyRestHandler extends AbstractHandler {
         else {
             Object o = cmdRes.getResponse();
 
-            if (o instanceof JSONCacheObject)
-                cmdRes.setResponse(((JSONCacheObject)o).getFields());
+            if (o instanceof RestJSONCacheObject)
+                cmdRes.setResponse(((RestJSONCacheObject)o).getFields());
         }
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/77e6c5ca/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/IgniteScriptingCommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/IgniteScriptingCommandHandler.java
b/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/IgniteScriptingCommandHandler.java
index 7b7273f..558e232 100644
--- a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/IgniteScriptingCommandHandler.java
+++ b/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/IgniteScriptingCommandHandler.java
@@ -211,14 +211,16 @@ public class IgniteScriptingCommandHandler extends GridRestCommandHandlerAdapter
         public JsCallFunctionJob(String func, Object argv) {
             this.func = func;
 
-            this.argv = JSONCacheObject.toSimpleObject(argv);
+            this.argv = RestJSONCacheObject.convertToRestObject(
+                JSONCacheObject.toSimpleObject(argv));
         }
 
         /** {@inheritDoc} */
         @Override public Object execute() throws IgniteException {
             try {
                 return ((IgniteKernal)ignite).context().scripting().invokeFunction(func,
-                    JSONCacheObject.toSimpleObject(argv), null);
+                    RestJSONCacheObject.convertToRestObject(JSONCacheObject.toSimpleObject(argv)),
+                    null);
             }
             catch (IgniteCheckedException e) {
                 throw U.convertException(e);


Mime
View raw message