io.fabric8.jube.model.EntityModelSupport Maven / Gradle / Ivy
/**
* Copyright 2005-2014 Red Hat, Inc.
*
* Red Hat licenses this file to you 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.fabric8.jube.model;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.fabric8.kubernetes.api.KubernetesFactory;
/**
* A base implementation of {@link EntityModel}
*/
public abstract class EntityModelSupport implements EntityModel {
private final ConcurrentHashMap map = new ConcurrentHashMap<>();
private final Class clazz;
private final ObjectMapper objectMapper;
protected EntityModelSupport(Class clazz) {
this(clazz, KubernetesFactory.createObjectMapper());
}
protected EntityModelSupport(Class clazz, ObjectMapper objectMapper) {
this.clazz = clazz;
this.objectMapper = objectMapper;
}
@Override
public T deleteEntity(String id) {
return map.remove(id);
}
public T getEntity(String id) {
return map.get(id);
}
public Map getMap() {
return new HashMap<>(map);
}
@Override
public T updateEntity(String id, byte[] data) throws IOException {
T entity = unmarshal(data);
map.put(id, entity);
return entity;
}
@Override
public String marshal(T entity) throws IOException {
return objectMapper.writerWithType(clazz).writeValueAsString(entity);
}
@Override
public T unmarshal(byte[] data) throws IOException {
return objectMapper.reader(clazz).readValue(data);
}
}