You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by lp...@apache.org on 2017/07/05 12:15:19 UTC

ambari git commit: AMBARI-21307 using Gson for transformation to/from json

Repository: ambari
Updated Branches:
  refs/heads/feature-branch-AMBARI-21307 797240aca -> 21bb9d9a3


AMBARI-21307 using Gson for transformation to/from json


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

Branch: refs/heads/feature-branch-AMBARI-21307
Commit: 21bb9d9a36a0dc510a7bb924f1fc45fe44f9ee17
Parents: 797240a
Author: lpuskas <lp...@apache.org>
Authored: Wed Jul 5 14:14:48 2017 +0200
Committer: lpuskas <lp...@apache.org>
Committed: Wed Jul 5 14:14:48 2017 +0200

----------------------------------------------------------------------
 .../AmbariConfigurationResourceProvider.java    | 42 ++++----------------
 1 file changed, 8 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/21bb9d9a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AmbariConfigurationResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AmbariConfigurationResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AmbariConfigurationResourceProvider.java
index 6d5fe5b..5e5af9e 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AmbariConfigurationResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AmbariConfigurationResourceProvider.java
@@ -14,7 +14,6 @@
 
 package org.apache.ambari.server.controller.internal;
 
-import java.io.IOException;
 import java.util.Calendar;
 import java.util.Collections;
 import java.util.EnumSet;
@@ -41,11 +40,12 @@ import org.apache.ambari.server.orm.dao.AmbariConfigurationDAO;
 import org.apache.ambari.server.orm.entities.AmbariConfigurationEntity;
 import org.apache.ambari.server.orm.entities.ConfigurationBaseEntity;
 import org.apache.ambari.server.security.authorization.RoleAuthorization;
-import org.codehaus.jackson.map.ObjectMapper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.collect.Sets;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
 
 /**
  * Resource provider for AmbariConfiguration resources.
@@ -54,11 +54,9 @@ import com.google.common.collect.Sets;
 public class AmbariConfigurationResourceProvider extends AbstractAuthorizedResourceProvider {
 
   private static final Logger LOGGER = LoggerFactory.getLogger(AmbariConfigurationResourceProvider.class);
-
   private static final String DEFAULT_VERSION_TAG = "Default version";
   private static final Integer DEFAULT_VERSION = 1;
 
-
   /**
    * Resource property id constants.
    */
@@ -116,10 +114,14 @@ public class AmbariConfigurationResourceProvider extends AbstractAuthorizedResou
   @Inject
   private static AmbariConfigurationDAO ambariConfigurationDAO;
 
+  private Gson gson;
+
   protected AmbariConfigurationResourceProvider() {
     super(properties, pkPropertyMap);
     setRequiredCreateAuthorizations(EnumSet.of(RoleAuthorization.AMBARI_MANAGE_CONFIGURATION));
     setRequiredDeleteAuthorizations(EnumSet.of(RoleAuthorization.AMBARI_MANAGE_CONFIGURATION));
+
+    gson = new GsonBuilder().create();
   }
 
   @Override
@@ -181,7 +183,7 @@ public class AmbariConfigurationResourceProvider extends AbstractAuthorizedResou
 
   private Resource toResource(AmbariConfigurationEntity entity, Set<String> requestedIds) throws AmbariException {
     Resource resource = new ResourceImpl(Resource.Type.AmbariConfiguration);
-    Set<Map<String, String>> configurationSet = ConfigurationDataConverter.fromJson(entity.getConfigurationBaseEntity().getConfigurationData());
+    Set<Map<String, String>> configurationSet = gson.fromJson(entity.getConfigurationBaseEntity().getConfigurationData(), Set.class);
 
     setResourceProperty(resource, ResourcePropertyId.ID.getPropertyId(), entity.getId(), requestedIds);
     setResourceProperty(resource, ResourcePropertyId.TYPE.getPropertyId(), entity.getConfigurationBaseEntity().getType(), requestedIds);
@@ -205,7 +207,7 @@ public class AmbariConfigurationResourceProvider extends AbstractAuthorizedResou
             throw new IllegalArgumentException("No configuration data is provided in the request");
           }
 
-          ambariConfigurationEntity.getConfigurationBaseEntity().setConfigurationData(ConfigurationDataConverter.toJson(requestValue));
+          ambariConfigurationEntity.getConfigurationBaseEntity().setConfigurationData(gson.toJson(requestValue));
           break;
         case TYPE:
           ambariConfigurationEntity.getConfigurationBaseEntity().setType((String) requestValue);
@@ -242,32 +244,4 @@ public class AmbariConfigurationResourceProvider extends AbstractAuthorizedResou
     return requestValue;
   }
 
-  private static final class ConfigurationDataConverter {
-    private static final ObjectMapper objectMapper = new ObjectMapper();
-
-    public static String toJson(Object configData) {
-      String json = null;
-      try {
-        json = objectMapper.writeValueAsString(configData);
-      } catch (IOException e) {
-        LOGGER.error("Could not transform configuration data to json: {}", configData);
-        throw new IllegalArgumentException("Could not transform configuration data to json");
-      }
-      return json;
-    }
-
-    public static Set fromJson(String configAsJson) {
-      Set configSet = Sets.newHashSet();
-      try {
-        configSet = objectMapper.readValue(configAsJson, configSet.getClass());
-      } catch (IOException e) {
-        LOGGER.error("Could not transform configuration data from json: {}", configAsJson);
-        throw new IllegalArgumentException("Could not transform configuration data from json");
-      }
-
-      return configSet;
-    }
-  }
-
-
 }