ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ivasilin...@apache.org
Subject [36/50] [abbrv] incubator-ignite git commit: #ignite-964: wip.
Date Mon, 13 Jul 2015 06:35:37 GMT
#ignite-964: wip.


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

Branch: refs/heads/ignite-961
Commit: b19536b7397a25a42d38e3a6029c793c070d8592
Parents: 5390b67
Author: ivasilinets <ivasilinets@gridgain.com>
Authored: Fri Jul 10 16:50:20 2015 +0300
Committer: ivasilinets <ivasilinets@gridgain.com>
Committed: Fri Jul 10 16:50:20 2015 +0300

----------------------------------------------------------------------
 .../scripting/IgniteScriptingProcessor.java     | 37 ++++++++++-
 .../scripting/ScriptingCacheEntry.java          |  8 +--
 .../processors/scripting/ScriptingJsCache.java  | 70 ++++++++++----------
 .../scripting/ScriptingObjectConverter.java     | 19 ++++++
 4 files changed, 93 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b19536b7/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptingProcessor.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptingProcessor.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptingProcessor.java
index cf380c0..9ff89e2 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptingProcessor.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptingProcessor.java
@@ -23,6 +23,8 @@ import org.apache.ignite.internal.processors.*;
 
 import javax.script.*;
 
+import java.lang.reflect.*;
+
 import static javax.script.ScriptContext.*;
 
 /**
@@ -30,7 +32,14 @@ import static javax.script.ScriptContext.*;
  */
 public class IgniteScriptingProcessor extends GridProcessorAdapter {
     /** Javascript engine name. */
-    public static final String JAVA_SCRIPT_ENGINE_NAME = "nashorn";
+    public static final String JAVA_SCRIPT_ENGINE_NAME = "javascript";
+
+    /** Java8 scripting converter class. */
+    private static final String CONV_CLS_JAVA8 =
+        "org.apache.ignite.internal.processors.scripting.ScriptingObjectConverter8";
+
+    /** Script object converter. */
+    private ScriptingObjectConverter converter;
 
     /** Javascript engine. */
     private ScriptEngine jsEngine;
@@ -44,6 +53,20 @@ public class IgniteScriptingProcessor extends GridProcessorAdapter {
 
     /** {@inheritDoc} */
     @Override public void start() throws IgniteCheckedException {
+        try {
+            Class<?> cls = Class.forName(CONV_CLS_JAVA8);
+
+            Constructor<?> ctor = cls.getConstructor(GridKernalContext.class);
+
+            converter = (ScriptingObjectConverter)ctor.newInstance(ctx);
+        }
+        catch (ClassNotFoundException ignored) {
+            converter = new ScriptingObjectConverter();
+        }
+        catch (NoSuchMethodException | InvocationTargetException | InstantiationException
| IllegalAccessException e) {
+            throw new IgniteCheckedException("Failed to initialize HTTP REST protocol.",
e);
+        }
+
         ScriptEngineManager factory = new ScriptEngineManager();
 
         jsEngine = factory.getEngineByName(JAVA_SCRIPT_ENGINE_NAME);
@@ -145,11 +168,19 @@ public class IgniteScriptingProcessor extends GridProcessorAdapter {
         }
     }
 
+    /**
+     * @param o Object.
+     * @return Object for script.
+     */
     public Object toScriptingObject(Object o) {
-
+        return converter.toScriptingObject(o);
     }
 
+    /**
+     * @param o Object.
+     * @return  Object for Ignite cache.
+     */
     public Object toJavaObject(Object o) {
-
+        return converter.toJavaObject(o);
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b19536b7/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/ScriptingCacheEntry.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/ScriptingCacheEntry.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/ScriptingCacheEntry.java
index bcb2458..adb43d1 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/ScriptingCacheEntry.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/ScriptingCacheEntry.java
@@ -16,13 +16,13 @@ public class ScriptingCacheEntry {
      * @param val Value.
      */
     public ScriptingCacheEntry(Object key, Object val) {
-        if (key instanceof ScriptingObjectConverter8)
-            this.key = ((ScriptingObjectConverter8)key).getFields();
+        if (key instanceof ScriptingObjectConverter)
+            this.key = ((ScriptingObjectConverter)key).getFields();
         else
             this.key = key;
 
-        if (val instanceof ScriptingObjectConverter8)
-            this.val = ((ScriptingObjectConverter8)val).getFields();
+        if (val instanceof ScriptingObjectConverter)
+            this.val = ((ScriptingObjectConverter)val).getFields();
         else
             this.val = val;
     }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b19536b7/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/ScriptingJsCache.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/ScriptingJsCache.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/ScriptingJsCache.java
index 9ab5e21..ce3975f 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/ScriptingJsCache.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/ScriptingJsCache.java
@@ -19,7 +19,6 @@ package org.apache.ignite.internal.processors.scripting;
 
 import org.apache.ignite.*;
 import org.apache.ignite.internal.util.typedef.internal.*;
-import org.apache.ignite.json.*;
 
 import java.util.*;
 
@@ -30,10 +29,13 @@ public class ScriptingJsCache {
     /** Ignite cache. */
     private IgniteCache<Object, Object> cache;
 
+    /** Scripting processor. */
+    private IgniteScriptingProcessor proc;
+
     /**
      * @param cache Ignite cache.
      */
-    public ScriptingJsCache(IgniteCache cache) {
+    public ScriptingJsCache(IgniteCache cache, IgniteScriptingProcessor proc) {
         this.cache = cache;
     }
 
@@ -42,8 +44,8 @@ public class ScriptingJsCache {
      * @param val Value.
      */
     public void put(Object key, Object val) {
-        Object cacheKey = JSONCacheObject.toSimpleObject(key);
-        Object cacheVal = JSONCacheObject.toSimpleObject(val);
+        Object cacheKey = proc.toJavaObject(key);
+        Object cacheVal = proc.toJavaObject(val);
 
         cache.put(cacheKey, cacheVal);
     }
@@ -52,9 +54,9 @@ public class ScriptingJsCache {
      * @param key Key.
      */
     public Object get(Object key) {
-        Object cacheKey = JSONCacheObject.toSimpleObject(key);
+        Object cacheKey = proc.toJavaObject(key);
 
-        return ScriptingObjectConverter8.convertToRestObject(cache.get(cacheKey));
+        return proc.toScriptingObject(cache.get(cacheKey));
     }
 
     /**
@@ -62,7 +64,7 @@ public class ScriptingJsCache {
      * @return True if cache contains key.
      */
     public boolean containsKey(Object key) {
-        Object cacheKey = JSONCacheObject.toSimpleObject(key);
+        Object cacheKey = proc.toJavaObject(key);
 
         return cache.containsKey(cacheKey);
     }
@@ -72,7 +74,7 @@ public class ScriptingJsCache {
      * @return True if cache contains key.
      */
     public boolean containsKeys(List keys) {
-        List<Object> cacheKeys = (List<Object>)JSONCacheObject.toSimpleObject(keys);
+        List<Object> cacheKeys = (List<Object>)proc.toJavaObject(keys);
 
         return cache.containsKeys(new HashSet<>(cacheKeys));
     }
@@ -82,7 +84,7 @@ public class ScriptingJsCache {
      * @return Cache entries.
      */
     public List<ScriptingCacheEntry> getAll(List keys) {
-        List cacheKeys = (List)JSONCacheObject.toSimpleObject(keys);
+        List cacheKeys = (List)proc.toJavaObject(keys);
 
         Map<Object, Object> entries = cache.getAll(new HashSet<>(cacheKeys));
 
@@ -90,8 +92,8 @@ public class ScriptingJsCache {
 
         for (Map.Entry<Object, Object> e : entries.entrySet())
             res.add(new ScriptingCacheEntry(
-                ScriptingObjectConverter8.convertToRestObject(e.getKey()),
-                ScriptingObjectConverter8.convertToRestObject(e.getValue())));
+                proc.toScriptingObject(e.getKey()),
+                proc.toScriptingObject(e.getValue())));
 
         return res;
     }
@@ -100,7 +102,7 @@ public class ScriptingJsCache {
      * @param keys Keys.
      */
     public void removeAll(List keys) {
-        List cacheKeys = (List)JSONCacheObject.toSimpleObject(keys);
+        List cacheKeys = (List)proc.toJavaObject(keys);
 
         cache.removeAll(new HashSet<>(cacheKeys));
     }
@@ -109,7 +111,7 @@ public class ScriptingJsCache {
      * @param entries Entries.
      */
     public void putAll(List entries) {
-        List cacheKeys = (List)JSONCacheObject.toSimpleObject(entries);
+        List cacheKeys = (List)proc.toJavaObject(entries);
 
         Map<Object, Object> cacheEntries = U.newHashMap(entries.size());
 
@@ -127,10 +129,10 @@ public class ScriptingJsCache {
      * @return Previous value.
      */
     public Object getAndPut(Object key, Object val) {
-        Object cacheKey = JSONCacheObject.toSimpleObject(key);
-        Object cacheVal = JSONCacheObject.toSimpleObject(val);
+        Object cacheKey = proc.toJavaObject(key);
+        Object cacheVal = proc.toJavaObject(val);
 
-        return ScriptingObjectConverter8.convertToRestObject(cache.getAndPut(cacheKey, cacheVal));
+        return proc.toScriptingObject(cache.getAndPut(cacheKey, cacheVal));
     }
 
     /**
@@ -139,10 +141,10 @@ public class ScriptingJsCache {
      * @return Previous value.
      */
     public Object getAndReplace(Object key, Object val) {
-        Object cacheKey = JSONCacheObject.toSimpleObject(key);
-        Object cacheVal = JSONCacheObject.toSimpleObject(val);
+        Object cacheKey = proc.toJavaObject(key);
+        Object cacheVal = proc.toJavaObject(val);
 
-        Object o = ScriptingObjectConverter8.convertToRestObject(cache.getAndReplace(cacheKey,
cacheVal));
+        Object o = proc.toScriptingObject(cache.getAndReplace(cacheKey, cacheVal));
 
         return o;
     }
@@ -153,10 +155,10 @@ public class ScriptingJsCache {
      * @return Previous value.
      */
     public Object getAndPutIfAbsent(Object key, Object val) {
-        Object cacheKey = JSONCacheObject.toSimpleObject(key);
-        Object cacheVal = JSONCacheObject.toSimpleObject(val);
+        Object cacheKey = proc.toJavaObject(key);
+        Object cacheVal = proc.toJavaObject(val);
 
-        return ScriptingObjectConverter8.convertToRestObject(cache.getAndPutIfAbsent(cacheKey,
cacheVal));
+        return proc.toScriptingObject(cache.getAndPutIfAbsent(cacheKey, cacheVal));
     }
 
     /**
@@ -164,9 +166,9 @@ public class ScriptingJsCache {
      * @return Previous value.
      */
     public Object getAndRemove(Object key) {
-        Object cacheKey = JSONCacheObject.toSimpleObject(key);
+        Object cacheKey = proc.toJavaObject(key);
 
-        return ScriptingObjectConverter8.convertToRestObject(cache.getAndRemove(cacheKey));
+        return proc.toScriptingObject(cache.getAndRemove(cacheKey));
     }
 
     /**
@@ -174,7 +176,7 @@ public class ScriptingJsCache {
      * @return If operation success.
      */
     public boolean remove(Object key) {
-        Object cacheKey = JSONCacheObject.toSimpleObject(key);
+        Object cacheKey = proc.toJavaObject(key);
 
         return cache.remove(cacheKey);
     }
@@ -185,8 +187,8 @@ public class ScriptingJsCache {
      * @return If operation success.
      */
     public boolean removeValue(Object key, Object val) {
-        Object cacheKey = JSONCacheObject.toSimpleObject(key);
-        Object cacheVal = JSONCacheObject.toSimpleObject(val);
+        Object cacheKey = proc.toJavaObject(key);
+        Object cacheVal = proc.toJavaObject(val);
 
         return cache.remove(cacheKey, cacheVal);
     }
@@ -197,8 +199,8 @@ public class ScriptingJsCache {
      * @return If operation success.
      */
     public boolean replace(Object key, Object val) {
-        Object cacheKey = JSONCacheObject.toSimpleObject(key);
-        Object cacheVal = JSONCacheObject.toSimpleObject(val);
+        Object cacheKey = proc.toJavaObject(key);
+        Object cacheVal = proc.toJavaObject(val);
 
         return cache.replace(cacheKey, cacheVal);
     }
@@ -210,9 +212,9 @@ public class ScriptingJsCache {
      * @return If operation success.
      */
     public boolean replaceValue(Object key, Object val, Object oldVal) {
-        Object cacheKey = JSONCacheObject.toSimpleObject(key);
-        Object cacheVal = JSONCacheObject.toSimpleObject(val);
-        Object oldCacheVal = JSONCacheObject.toSimpleObject(oldVal);
+        Object cacheKey = proc.toJavaObject(key);
+        Object cacheVal = proc.toJavaObject(val);
+        Object oldCacheVal = proc.toJavaObject(oldVal);
 
         return cache.replace(cacheKey, oldCacheVal, cacheVal);
     }
@@ -230,8 +232,8 @@ public class ScriptingJsCache {
      * @return Previous value.
      */
     public Object putIfAbsent(Object key, Object val) {
-        Object cacheKey = JSONCacheObject.toSimpleObject(key);
-        Object cacheVal = JSONCacheObject.toSimpleObject(val);
+        Object cacheKey = proc.toJavaObject(key);
+        Object cacheVal = proc.toJavaObject(val);
 
         return cache.putIfAbsent(cacheKey, cacheVal);
     }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/b19536b7/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/ScriptingObjectConverter.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/ScriptingObjectConverter.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/ScriptingObjectConverter.java
index a02a762..7e2758b 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/ScriptingObjectConverter.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/ScriptingObjectConverter.java
@@ -37,4 +37,23 @@ public class ScriptingObjectConverter {
     public Object toJavaObject(Object o) {
         return JSONCacheObject.toSimpleObject(o);
     }
+
+    /**
+     * @param o Object from script.
+     * @return Object to store in cache.
+     */
+    public Object getField(String key, Object o) {
+        if (o instanceof JSONCacheObject)
+            return ((JSONCacheObject)o).getField(key);
+
+        return null;
+    }
+
+    /**
+     * @param o Object from script.
+     * @return Object to store in cache.
+     */
+    public Object getFields(Object o) {
+        return JSONCacheObject.toSimpleObject(o);
+    }
 }


Mime
View raw message