001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation.io.services.codec;
020
021import java.io.IOException;
022import java.lang.reflect.ParameterizedType;
023import java.lang.reflect.Type;
024
025import org.codehaus.jackson.JsonGenerator;
026import org.codehaus.jackson.JsonParser;
027import org.codehaus.jackson.map.ObjectMapper;
028import org.nuxeo.ecm.core.api.CoreSession;
029
030/**
031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
032 */
033public abstract class ObjectCodec<T> {
034
035    public static Class<?> findParametrizedType(Class<?> clazz) {
036        Type superclass = clazz.getGenericSuperclass();
037        while (superclass instanceof Class<?>) {
038            superclass = ((Class<?>) superclass).getGenericSuperclass();
039        }
040        if (superclass == null) {
041            throw new RuntimeException("Missing type parameter.");
042        }
043        Type type = ((ParameterizedType) superclass).getActualTypeArguments()[0];
044        if (!(type instanceof Class<?>)) {
045            throw new RuntimeException("Invalid class parameter type. " + type);
046        }
047        return (Class<?>) type;
048    }
049
050    protected Class<T> type;
051
052    @SuppressWarnings("unchecked")
053    public ObjectCodec() {
054        this.type = (Class<T>) findParametrizedType(getClass());
055    }
056
057    public ObjectCodec(Class<T> type) {
058        this.type = type;
059    }
060
061    /**
062     * Get this codec type. Implementors can override to return a short name. The default name is the object type name.
063     *
064     * @return
065     */
066    public String getType() {
067        return type.getName();
068    }
069
070    /**
071     * Whether this codec is a builtin codec
072     *
073     * @return
074     */
075    public boolean isBuiltin() {
076        return false;
077    }
078
079    public Class<T> getJavaType() {
080        return type;
081    }
082
083    public void write(JsonGenerator jg, T value) throws IOException {
084        if (jg.getCodec() == null) {
085            jg.setCodec(new ObjectMapper());
086        }
087        jg.writeObject(value);
088    }
089
090    /**
091     * When the object codec is called the stream is positioned on the first value. For inlined objects this is the
092     * first value after the "entity-type" property. For non inlined objects this will be the object itself (i.e. '{' or
093     * '[')
094     *
095     * @param jp
096     * @return
097     * @throws IOException
098     */
099    public T read(JsonParser jp, CoreSession session) throws IOException {
100        if (jp.getCodec() == null) {
101            jp.setCodec(new ObjectMapper());
102        }
103        return jp.readValueAs(type);
104    }
105
106}