/** * Copyright (C) 2010 Cloud.com, Inc. All rights reserved. * * This software is licensed under the GNU General Public License v3 or later. * * It is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ package com.cloud.serializer; import java.util.List; 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.transport.ArrayTypeAdaptor; import com.cloud.agent.transport.LoggingExclusionStrategy; import com.cloud.agent.transport.Request.NwGroupsCommandTypeAdaptor; import com.cloud.agent.transport.Request.PortConfigListTypeAdaptor; import com.cloud.utils.Pair; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; public class GsonHelper { private static final Logger s_logger = Logger.getLogger(GsonHelper.class); protected static final Gson s_gson; protected static final Gson s_gogger; static { GsonBuilder gsonBuilder = new GsonBuilder(); s_gson = setDefaultGsonConfig(gsonBuilder); GsonBuilder loggerBuilder = new GsonBuilder(); loggerBuilder.disableHtmlEscaping(); loggerBuilder.setExclusionStrategies(new LoggingExclusionStrategy(s_logger)); s_gogger = setDefaultGsonConfig(loggerBuilder); s_logger.info("Default Builder inited."); } static Gson setDefaultGsonConfig(GsonBuilder builder) { builder.setVersion(1.5); ArrayTypeAdaptor cmdAdaptor = new ArrayTypeAdaptor(); builder.registerTypeAdapter(Command[].class, cmdAdaptor); ArrayTypeAdaptor ansAdaptor = new ArrayTypeAdaptor(); builder.registerTypeAdapter(Answer[].class, ansAdaptor); builder.registerTypeAdapter(new TypeToken>() { }.getType(), new PortConfigListTypeAdaptor()); builder.registerTypeAdapter(new TypeToken>() { }.getType(), new NwGroupsCommandTypeAdaptor()); Gson gson = builder.create(); cmdAdaptor.initGson(gson); ansAdaptor.initGson(gson); return gson; } public final static Gson getGson() { return s_gson; } public final static Gson getGsonLogger() { return s_gogger; } public final static Logger getLogger() { return s_logger; } }