All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fasterxml.mama.util.ZKDeserializers Maven / Gradle / Ivy

The newest version!
package com.fasterxml.mama.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Function;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.mama.NodeInfo;
import com.fasterxml.mama.NodeState;

/**
 * Helper classes needed to work with "ZooKeeperMap"
 */
public class ZKDeserializers
{
   /**
    * Utility method for converting an array of bytes to a NodeInfo object.
    */
   public static class NodeInfoDeserializer implements Function
   {
       final protected Logger LOG = LoggerFactory.getLogger(getClass());

       @Override
       public NodeInfo apply(byte[] bytes) {
           try {
               return JsonUtil.fromJSON(bytes, NodeInfo.class);
           } catch (Exception e) {
               String str = Strings.stringFromUtf8(bytes);
               NodeInfo info = new NodeInfo(NodeState.Shutdown.toString(), 0);
               LOG.warn("Saw node data in non-JSON format. Have to default to {}. Input = {}", info, str);
               return info;
           }
       }
   }

   /**
    * Utility method for converting an array of bytes to a String.
    */
   public static class StringDeserializer implements Function {
       @Override
       public String apply(byte[] a) {
           try {
               return Strings.stringFromUtf8(a);
           } catch (Exception e) { // can this ever occur?
               return "";
           }
     }
   }

   public static class ObjectNodeDeserializer implements Function {
       final protected Logger LOG = LoggerFactory.getLogger(getClass());

       @Override
       public ObjectNode  apply(byte[] input)
       {
           if (input != null && input.length > 0) {
               try {
                   return JsonUtil.fromJSON(input);
               } catch (Exception e) {
                   LOG.error("Failed to de-serialize ZNode", e);
               }
           }
           return JsonUtil.objectNode();
     }
   }

   /**
    * Utility method for converting an array of bytes to a Double.
    */
   public static class DoubleDeserializer implements Function
   {
       @Override
       public Double apply(byte[] a) {
           try {
               return new Double(Strings.stringFromUtf8(a));
           } catch (Exception e) {
               return 0d;
           }
       }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy