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

net.openhft.chronicle.map.ChroncileMapJsonSerializer Maven / Gradle / Ivy

/*
 * Copyright 2014 Higher Frequency Trading
 *
 * http://www.higherfrequencytrading.com
 *
 * Licensed 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 net.openhft.chronicle.map;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import net.openhft.xstream.converters.ByteBufferConverter;
import net.openhft.xstream.converters.DataValueConverter;
import net.openhft.xstream.converters.StatelessChronicleMapConverter;
import net.openhft.xstream.converters.VanillaChronicleMapConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * @author Rob Austin.
 */
class JsonSerializer {

    private static final Logger LOG = LoggerFactory.getLogger(StatelessChronicleMap.class);

    static synchronized  void getAll(File toFile, Map map, List jsonConverters) throws IOException {
        final XStream xstream = xStream(map, jsonConverters);
        OutputStream outputStream = new FileOutputStream(toFile);
        if (toFile.getName().toLowerCase().endsWith(".gz"))
            outputStream = new GZIPOutputStream(outputStream);
        try (OutputStream out = outputStream) {
            xstream.toXML(map, out);
        }
    }


    static synchronized  void putAll(File fromFile, Map map, List jsonConverters)
            throws IOException {
        final XStream xstream = xStream(map, jsonConverters);

        InputStream inputStream = new FileInputStream(fromFile);
        if (fromFile.getName().toLowerCase().endsWith(".gz"))
            inputStream = new GZIPInputStream(inputStream);
        try (InputStream out = inputStream) {
            xstream.fromXML(out);
        }
    }

    private static  XStream xStream(Map map, List jsonConverters) {
        try {
            final XStream xstream = new XStream(new JettisonMappedXmlDriver());
            xstream.setMode(XStream.NO_REFERENCES);
            xstream.alias("cmap", map.getClass());

            registerChronicleMapConverter(map, xstream);
            xstream.registerConverter(new ByteBufferConverter());
            xstream.registerConverter(new DataValueConverter());

            for (Object c : jsonConverters) {
                if (c instanceof Converter) {
                    xstream.registerConverter((Converter) c);
                } else {
                    LOG.warn("Skipping Converter of type class=" + c.getClass().getName() + " as " +
                            " expecting an object of type com.thoughtworks.xstream.converters" +
                            ".Converter");
                }

            }

            return xstream;
        } catch (NoClassDefFoundError e) {
            logErrorSuggestXStreem(e);
            throw e;
        }
    }

    static private void logErrorSuggestXStreem(Error e) {
        LOG.error("map.getAll() and map.putAll() methods require the JSON XStream serializer, " +
                "we don't include these artifacts by default as some users don't require this functionality. " +
                "Please add the following artifacts to your project\n" +
                "\n" +
                " xstream\n" +
                " xstream\n" +
                " 1.2.2\n" +
                "\n" +
                "\n" +
                " org.codehaus.jettison\n" +
                " jettison\n" +
                " 1.3.6\n" +
                "\n", e);
    }

    private static  void registerChronicleMapConverter(Map map, XStream xstream) {

        Converter converter = map instanceof StatelessChronicleMap ?
                new StatelessChronicleMapConverter(map) :
                new VanillaChronicleMapConverter(map);

        xstream.registerConverter(converter);
    }
}






© 2015 - 2025 Weber Informatics LLC | Privacy Policy