001/*
002 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 */
017package org.nuxeo.ecm.automation.io.services.codec;
018
019import java.io.IOException;
020import java.lang.reflect.ParameterizedType;
021import java.lang.reflect.Type;
022
023import org.codehaus.jackson.JsonGenerator;
024import org.codehaus.jackson.JsonParser;
025import org.codehaus.jackson.map.ObjectMapper;
026import org.nuxeo.ecm.core.api.CoreSession;
027
028/**
029 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
030 */
031public abstract class ObjectCodec<T> {
032
033    public static Class<?> findParametrizedType(Class<?> clazz) {
034        Type superclass = clazz.getGenericSuperclass();
035        while (superclass instanceof Class<?>) {
036            superclass = ((Class<?>) superclass).getGenericSuperclass();
037        }
038        if (superclass == null) {
039            throw new RuntimeException("Missing type parameter.");
040        }
041        Type type = ((ParameterizedType) superclass).getActualTypeArguments()[0];
042        if (!(type instanceof Class<?>)) {
043            throw new RuntimeException("Invalid class parameter type. " + type);
044        }
045        return (Class<?>) type;
046    }
047
048    protected Class<T> type;
049
050    @SuppressWarnings("unchecked")
051    public ObjectCodec() {
052        this.type = (Class<T>) findParametrizedType(getClass());
053    }
054
055    public ObjectCodec(Class<T> type) {
056        this.type = type;
057    }
058
059    /**
060     * Get this codec type. Implementors can override to return a short name. The default name is the object type name.
061     *
062     * @return
063     */
064    public String getType() {
065        return type.getName();
066    }
067
068    /**
069     * Whether this codec is a builtin codec
070     *
071     * @return
072     */
073    public boolean isBuiltin() {
074        return false;
075    }
076
077    public Class<T> getJavaType() {
078        return type;
079    }
080
081    public void write(JsonGenerator jg, T value) throws IOException {
082        if (jg.getCodec() == null) {
083            jg.setCodec(new ObjectMapper());
084        }
085        jg.writeObject(value);
086    }
087
088    /**
089     * When the object codec is called the stream is positioned on the first value. For inlined objects this is the
090     * first value after the "entity-type" property. For non inlined objects this will be the object itself (i.e. '{' or
091     * '[')
092     *
093     * @param jp
094     * @return
095     * @throws IOException
096     */
097    public T read(JsonParser jp, CoreSession session) throws IOException {
098        if (jp.getCodec() == null) {
099            jp.setCodec(new ObjectMapper());
100        }
101        return jp.readValueAs(type);
102    }
103
104}