package org.jeeventstore.serialization.gson;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.ConcurrencyManagement;
import javax.ejb.ConcurrencyManagementType;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import org.jeeventstore.core.serialization.EventSerializer;

/**
 * An EventSerializer implementation using Google's Gson library.
 * @author Alexander Langer
 */
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
public class EventSerializerGson implements EventSerializer {

    private static final Logger log = Logger.getLogger(EventSerializerGson.class.getName());

    private Gson gson;

    /*
     * This searches the class path and lists all custom converters.
     */
    @Inject
    private Instance<EventSerializerGsonTypeConverter> typeConverters;

    @PostConstruct
    public void init() {
        GsonBuilder builder = new GsonBuilder();
        builder.registerTypeAdapter(EventList.class, new EventListTypeConverter());

        Iterator<EventSerializerGsonTypeConverter> convit = typeConverters.iterator();
        while (convit.hasNext()) {
            EventSerializerGsonTypeConverter conv = convit.next();
            log.log(Level.INFO, "Registering type converter for class {0}",
                    conv.convertedType().getCanonicalName());
            builder.registerTypeAdapter(conv.convertedType(), conv);
        }

        this.gson = builder.create();
    }

    @Override
    @Lock(LockType.READ)
    public String serialize(List<? extends Serializable> events) {
        EventList evlist = new EventList(events);
        return gson.toJson(evlist);
    }

    @Override
    @Lock(LockType.READ)
    public List<? extends Serializable> deserialize(String body) {
        return this.gson.fromJson(body, EventList.class).events();
    }

}

@@ -291,92 +291,8 @@
 ct;%0A
-import javax.ejb.ConcurrencyManagement;%0Aimport javax.ejb.ConcurrencyManagementType;%0A
 impo
@@ -310,16 +310,16 @@
 b.Lock;%0A
+
 import j
@@ -573,63 +573,8 @@
  */%0A
-@ConcurrencyManagement(ConcurrencyManagementType.BEAN)%0A
 publ
@@ -1092,24 +1092,67 @@
 verter());%0A%0A
+        // register client's type adapters%0A
         Iter
@@ -1535,16 +1535,16 @@
  conv);%0A
+
         
@@ -1541,25 +1541,24 @@
 ;%0A        %7D%0A
-%0A
         this

Make EventStoreGson use container managed locking.