cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mc...@apache.org
Subject git commit: updated refs/heads/object_store to e0ab845
Date Sat, 04 May 2013 00:29:31 GMT
Updated Branches:
  refs/heads/object_store 23d6f12e9 -> e0ab845e9


Add gson adapter to serialize/deserialize DataTO and DataStoreTO.

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

Branch: refs/heads/object_store
Commit: e0ab845e92ceb55aa9b3fddd65114c7f66677941
Parents: 23d6f12
Author: Min Chen <min.chen@citrix.com>
Authored: Fri May 3 17:29:12 2013 -0700
Committer: Min Chen <min.chen@citrix.com>
Committed: Fri May 3 17:29:12 2013 -0700

----------------------------------------------------------------------
 .../agent/transport/InterfaceTypeAdaptor.java      |   66 +++++++++++++++
 core/src/com/cloud/serializer/GsonHelper.java      |    7 ++
 .../storage/template/S3TemplateDownloader.java     |    3 +-
 3 files changed, 75 insertions(+), 1 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e0ab845e/core/src/com/cloud/agent/transport/InterfaceTypeAdaptor.java
----------------------------------------------------------------------
diff --git a/core/src/com/cloud/agent/transport/InterfaceTypeAdaptor.java b/core/src/com/cloud/agent/transport/InterfaceTypeAdaptor.java
new file mode 100644
index 0000000..1ad4afc
--- /dev/null
+++ b/core/src/com/cloud/agent/transport/InterfaceTypeAdaptor.java
@@ -0,0 +1,66 @@
+// 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 com.cloud.agent.transport;
+
+import java.lang.reflect.Type;
+import java.util.Map;
+
+import com.cloud.utils.exception.CloudRuntimeException;
+import com.google.gson.Gson;
+import com.google.gson.JsonDeserializationContext;
+import com.google.gson.JsonDeserializer;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParseException;
+import com.google.gson.JsonSerializationContext;
+import com.google.gson.JsonSerializer;
+
+public class InterfaceTypeAdaptor<T> implements JsonDeserializer<T>, JsonSerializer<T>
{
+
+    protected Gson              _gson = null;
+
+
+    public InterfaceTypeAdaptor() {
+    }
+
+    public void initGson(Gson gson) {
+        _gson = gson;
+    }
+
+    @Override
+    public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context)
{
+        JsonObject obj = new JsonObject();
+        obj.add(src.getClass().getName(), _gson.toJsonTree(src));
+        return obj;
+     }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
+    throws JsonParseException {
+        JsonObject element = (JsonObject) json;
+        Map.Entry<String, JsonElement> entry = element.entrySet().iterator().next();
+        String name = entry.getKey();
+        Class<?> clazz;
+        try {
+            clazz = Class.forName(name);
+        } catch (ClassNotFoundException e) {
+            throw new CloudRuntimeException("can't find " + name);
+        }
+        return (T) _gson.fromJson(entry.getValue(), clazz);
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e0ab845e/core/src/com/cloud/serializer/GsonHelper.java
----------------------------------------------------------------------
diff --git a/core/src/com/cloud/serializer/GsonHelper.java b/core/src/com/cloud/serializer/GsonHelper.java
index 8b2dcb0..47e9922 100644
--- a/core/src/com/cloud/serializer/GsonHelper.java
+++ b/core/src/com/cloud/serializer/GsonHelper.java
@@ -20,12 +20,15 @@ package com.cloud.serializer;
 
 import java.util.List;
 
+import org.apache.cloudstack.engine.subsystem.api.storage.DataTO;
 import org.apache.log4j.Logger;
 
 import com.cloud.agent.api.Answer;
 import com.cloud.agent.api.Command;
 import com.cloud.agent.api.SecStorageFirewallCfgCommand.PortConfig;
+import com.cloud.agent.api.to.DataStoreTO;
 import com.cloud.agent.transport.ArrayTypeAdaptor;
+import com.cloud.agent.transport.InterfaceTypeAdaptor;
 import com.cloud.agent.transport.LoggingExclusionStrategy;
 import com.cloud.agent.transport.Request.NwGroupsCommandTypeAdaptor;
 import com.cloud.agent.transport.Request.PortConfigListTypeAdaptor;
@@ -52,6 +55,10 @@ public class GsonHelper {
 
     static Gson setDefaultGsonConfig(GsonBuilder builder) {
         builder.setVersion(1.5);
+        InterfaceTypeAdaptor<DataStoreTO> dsAdaptor = new InterfaceTypeAdaptor<DataStoreTO>();
+        builder.registerTypeAdapter(DataStoreTO.class, dsAdaptor);
+        InterfaceTypeAdaptor<DataTO> dtAdaptor = new InterfaceTypeAdaptor<DataTO>();
+        builder.registerTypeAdapter(DataTO.class, dtAdaptor);
         ArrayTypeAdaptor<Command> cmdAdaptor = new ArrayTypeAdaptor<Command>();
         builder.registerTypeAdapter(Command[].class, cmdAdaptor);
         ArrayTypeAdaptor<Answer> ansAdaptor = new ArrayTypeAdaptor<Answer>();

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e0ab845e/core/src/com/cloud/storage/template/S3TemplateDownloader.java
----------------------------------------------------------------------
diff --git a/core/src/com/cloud/storage/template/S3TemplateDownloader.java b/core/src/com/cloud/storage/template/S3TemplateDownloader.java
index eac9bbf..bc0b904 100644
--- a/core/src/com/cloud/storage/template/S3TemplateDownloader.java
+++ b/core/src/com/cloud/storage/template/S3TemplateDownloader.java
@@ -308,8 +308,9 @@ public class S3TemplateDownloader implements TemplateDownloader {
             if (totalBytes >= remoteSize) {
             	status = TemplateDownloader.Status.DOWNLOAD_FINISHED;
             	downloaded = "(download complete remote=" + remoteSize + "bytes)";
+            } else {
+                errorString = "Downloaded " + totalBytes + " bytes " + downloaded;
             }
-            errorString = "Downloaded " + totalBytes + " bytes " + downloaded;
             downloadTime += finish.getTime() - start.getTime();
             return totalBytes;
 		}catch (HttpException hte) {


Mime
View raw message