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

io.alauda.kubernetes.internal.KubernetesDeserializer Maven / Gradle / Ivy

There is a newer version: 0.2.12
Show newest version
/**
 * Copyright (C) 2018 Alauda, Inc.
 *
 * 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 io.alauda.kubernetes.internal;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.alauda.kubernetes.api.model.KubernetesList;
import io.alauda.kubernetes.api.model.KubernetesResource;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class KubernetesDeserializer extends JsonDeserializer {

    private static final String KIND = "kind";

    private static final String KUBERNETES_PACKAGE_PREFIX = "io.alauda.kubernetes.api.model.";
    private static final String KUBERNETES_EXTENSIONS_PACKAGE_PREFIX = "io.alauda.kubernetes.api.model.extensions.";
    private static final String KUBERNETES_APIEXTENSIONS_PACKAGE_PREFIX = "io.alauda.kubernetes.api.model.apiextensions.";
    private static final String OPENSHIFT_PACKAGE_PREFIX = "io.fabric8.openshift.api.model.";

    private static final Map> MAP = new HashMap<>();


    static {
        // Exceptions (not just package prefix + class name) can be added here.
        MAP.put("List", KubernetesList.class);
    }

    @Override
    public KubernetesResource deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        ObjectNode node = jp.readValueAsTree();
        JsonNode kind = node.get(KIND);
        if (kind != null) {
            String value = kind.textValue();
            Class resourceType = getTypeForName(value);
            if (resourceType == null) {
                throw ctxt.mappingException("No resource type found for kind:" + value);
            } else {
                return jp.getCodec().treeToValue(node, resourceType);
            }
        }
        return null;
    }

    /**
     * Registers a Custom Resource Definition Kind
     */
    public static void registerCustomKind(String kind, Class clazz) {
        MAP.put(kind, clazz);
    }

    private static Class getTypeForName(String name) {
        Class result = MAP.get(name);
        if (result == null) {
            result = loadClassIfExists(KUBERNETES_PACKAGE_PREFIX + name);
            if (result == null) {
                result = loadClassIfExists(KUBERNETES_EXTENSIONS_PACKAGE_PREFIX + name);
                if (result == null) {
                    result = loadClassIfExists(OPENSHIFT_PACKAGE_PREFIX + name);
                    if (result == null) {
                        result = loadClassIfExists(KUBERNETES_APIEXTENSIONS_PACKAGE_PREFIX + name);
                    }
                }
            }
        }

        if (result != null) {
            MAP.put(name, result);
        }
        return result;
    }

    private static Class loadClassIfExists(String className) {
        try {
            return KubernetesDeserializer.class.getClassLoader().loadClass(className);
        } catch (Throwable t) {
            return null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy