ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anovi...@apache.org
Subject [2/3] ignite git commit: # GG-1223 Fix serialization GridCacheSqlMetadata, GridCacheSqlIndexMetadata for metadata.
Date Wed, 26 Aug 2015 10:32:30 GMT
# GG-1223 Fix serialization GridCacheSqlMetadata, GridCacheSqlIndexMetadata for metadata.


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

Branch: refs/heads/ignite-843
Commit: bd06777237a50f0d41758cfd8a129f9b10727841
Parents: 3cbe58c
Author: Andrey <anovikov@gridgain.com>
Authored: Wed Aug 26 17:24:01 2015 +0700
Committer: Andrey <anovikov@gridgain.com>
Committed: Wed Aug 26 17:24:01 2015 +0700

----------------------------------------------------------------------
 .../http/jetty/GridJettyJsonConfig.java         | 142 +++++++++++++++----
 1 file changed, 118 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/bd067772/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyJsonConfig.java
----------------------------------------------------------------------
diff --git a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyJsonConfig.java
b/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyJsonConfig.java
index 8d00249..8cd9cb4 100644
--- a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyJsonConfig.java
+++ b/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyJsonConfig.java
@@ -19,7 +19,9 @@ package org.apache.ignite.internal.processors.rest.protocols.http.jetty;
 
 import net.sf.json.*;
 import net.sf.json.processors.*;
+import org.apache.ignite.internal.processors.cache.query.*;
 
+import java.text.*;
 import java.util.*;
 
 /**
@@ -33,63 +35,155 @@ public class GridJettyJsonConfig extends JsonConfig {
         registerJsonValueProcessor(UUID.class, new UUIDToStringJsonProcessor());
         registerJsonValueProcessor(Date.class, new DateToStringJsonProcessor());
         registerJsonValueProcessor(java.sql.Date.class, new DateToStringJsonProcessor());
+
+        registerJsonBeanProcessor(GridCacheSqlMetadata.class, new GridCacheSqlMetadataBeanProcessor());
+        registerJsonValueProcessor(GridCacheSqlIndexMetadata.class, new GridCacheSqlIndexMetadataToJson());
+
+        setJsonBeanProcessorMatcher(new GridJettyJsonBeanProcessorMatcher());
+        setJsonValueProcessorMatcher(new GridJettyJsonValueProcessorMatcher());
     }
 
     /**
-     * Helper class for simple to-string conversion for the beans.
+     * Helper class for simple to-string conversion for {@link UUID}.
      */
     private static class UUIDToStringJsonProcessor implements JsonValueProcessor {
         /** {@inheritDoc} */
         @Override public Object processArrayValue(Object val, JsonConfig jsonCfg) {
+            if (val == null)
+                return new JSONObject(true);
+
             if (val instanceof UUID)
                 return val.toString();
 
-            if (val instanceof UUID[]) {
-                UUID[] uuids = (UUID[])val;
+            throw new UnsupportedOperationException("Serialize value to json is not supported:
" + val);
+        }
 
-                String[] result = new String[uuids.length];
+        /** {@inheritDoc} */
+        @Override public Object processObjectValue(String key, Object val, JsonConfig jsonCfg)
{
+            return processArrayValue(val, jsonCfg);
+        }
+    }
 
-                for (int i = 0; i < uuids.length; i++)
-                    result[i] = uuids[i] == null ? null : uuids[i].toString();
+    /**
+     * Helper class for simple to-string conversion for {@link Date}.
+     */
+    private static class DateToStringJsonProcessor implements JsonValueProcessor {
+        private final DateFormat enUsFormat
+            =  DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.US);
 
-                return result;
-            }
+        /** {@inheritDoc} */
+        @Override public synchronized Object processArrayValue(Object val, JsonConfig jsonCfg)
{
+            if (val == null)
+                return new JSONObject(true);
+            
+            if (val instanceof Date)
+                return enUsFormat.format(val);
 
-            throw new UnsupportedOperationException("Serialize array to string is not supported:
" + val);
+            throw new UnsupportedOperationException("Serialize value to json is not supported:
" + val);
         }
 
         /** {@inheritDoc} */
-        @Override public Object processObjectValue(String key, Object val, JsonConfig jsonCfg)
{
-            return val == null ? null : val.toString();
+        @Override public synchronized Object processObjectValue(String key, Object val, JsonConfig
jsonCfg) {
+            return processArrayValue(val, jsonCfg);
         }
     }
 
     /**
-     * Helper class for simple to-string conversion for the beans.
+     * Helper class for simple to-json conversion for {@link GridCacheSqlMetadata}.
      */
-    private static class DateToStringJsonProcessor implements JsonValueProcessor {
+    private static class GridCacheSqlMetadataBeanProcessor implements JsonBeanProcessor {
         /** {@inheritDoc} */
-        @Override public Object processArrayValue(Object val, JsonConfig jsonCfg) {
-            if (val instanceof Date)
-                return val.toString();
+        @Override public JSONObject processBean(Object bean, JsonConfig jsonCfg) {
+            if (bean == null)
+                return new JSONObject(true);
+
+            if (bean instanceof GridCacheSqlMetadata) {
+                GridCacheSqlMetadata r = (GridCacheSqlMetadata) bean;
+
+                return new JSONObject()
+                    .element("cacheName", r.cacheName(), jsonCfg)
+                    .element("types", r.types(), jsonCfg)
+                    .element("keyClasses", r.keyClasses(), jsonCfg)
+                    .element("valClasses", r.valClasses(), jsonCfg)
+                    .element("fields", r.fields(), jsonCfg)
+                    .element("indexes", r.indexes(), jsonCfg);
+            }
 
-            if (val instanceof Date[]) {
-                Date[] dates = (Date[])val;
+            throw new UnsupportedOperationException("Serialize bean to json is not supported:
" + bean);
+        }
+    }
 
-                String[] result = new String[dates.length];
+    /**
+     * Helper class for simple to-json conversion for {@link GridCacheSqlIndexMetadata}.
+     */
+    private static class GridCacheSqlIndexMetadataToJson implements JsonValueProcessor {
+        /** {@inheritDoc} */
+        @Override public Object processArrayValue(Object val, JsonConfig jsonCfg) {
+            if (val == null)
+                return new JSONObject(true);
 
-                for (int i = 0; i < dates.length; i++)
-                    result[i] = dates[i] == null ? null : dates[i].toString();
+            if (val instanceof GridCacheSqlIndexMetadata) {
+                GridCacheSqlIndexMetadata r = (GridCacheSqlIndexMetadata) val;
 
-                return result;
+                return new JSONObject()
+                    .element("name", r.name())
+                    .element("fields", r.fields())
+                    .element("descendings", r.descendings())
+                    .element("unique", r.unique());
             }
 
             throw new UnsupportedOperationException("Serialize array to string is not supported:
" + val);
         }
 
         /** {@inheritDoc} */
-        @Override public Object processObjectValue(String key, Object val, JsonConfig jsonConfig)
{
-            return val == null ? null : val.toString();
+        @Override public Object processObjectValue(String key, Object value, JsonConfig jsonCfg)
{
+            return processArrayValue(value, jsonCfg);
+        }
+    }
+
+    /**
+     * Class for finding a matching JsonBeanProcessor. Matches the target class with instanceOf.
+     */
+    private static final class GridJettyJsonBeanProcessorMatcher extends JsonBeanProcessorMatcher
{
+        /** {@inheritDoc} */
+        @Override public Object getMatch(Class target, Set keys) {
+            if (target == null || keys == null)
+                return null;
+
+            if (keys.contains(target))
+                return target;
+
+            for (Object key : keys) {
+                Class<?> clazz = (Class<?>) key;
+
+                if (clazz.isAssignableFrom(target))
+                    return key;
+            }
+
+            return null;
+        }
+    }
+
+    /**
+     * Class for finding a matching JsonValueProcessor. Matches the target class with instanceOf.
+     */
+    private static final class GridJettyJsonValueProcessorMatcher extends JsonValueProcessorMatcher
{
+        /** {@inheritDoc} */
+        @Override public Object getMatch(Class target, Set keys) {
+            if (target == null || keys == null)
+                return null;
+
+            if (keys.contains(target))
+                return target;
+
+            for (Object key : keys) {
+                Class<?> clazz = (Class<?>) key;
+
+                if (clazz.isAssignableFrom(target))
+                    return key;
+            }
+
+            return null;
         }
     }
 }


Mime
View raw message