love.sola.netsupport.sql.SQLCore.java Source code

Java tutorial

Introduction

Here is the source code for love.sola.netsupport.sql.SQLCore.java

Source

/*
 * This file is part of WechatTicketSystem.
 *
 * WechatTicketSystem is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WechatTicketSystem is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with WechatTicketSystem.  If not, see <http://www.gnu.org/licenses/>.
 */

package love.sola.netsupport.sql;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializer;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.annotations.Expose;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.mchange.v2.c3p0.ComboPooledDataSource;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.envers.AuditReader;
import org.hibernate.envers.AuditReaderFactory;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.service.ServiceRegistry;

import java.io.IOException;
import java.util.Date;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import love.sola.netsupport.enums.ISP;
import love.sola.netsupport.wechat.Command;

/**
 * @author Sola {@literal <dev@sola.love>}
 */
public class SQLCore {

    public static InitialContext ic;
    public static DataSource ds;
    public static SessionFactory sf;
    public static ServiceRegistry sr;
    public static Gson gson = new GsonBuilder().addSerializationExclusionStrategy(new ExclusionStrategy() {
        @Override
        public boolean shouldSkipField(FieldAttributes fieldAttributes) {
            final Expose expose = fieldAttributes.getAnnotation(Expose.class);
            return expose != null && !expose.serialize();
        }

        @Override
        public boolean shouldSkipClass(Class<?> aClass) {
            return false;
        }
    }).addDeserializationExclusionStrategy(new ExclusionStrategy() {
        @Override
        public boolean shouldSkipField(FieldAttributes fieldAttributes) {
            final Expose expose = fieldAttributes.getAnnotation(Expose.class);
            return expose != null && !expose.deserialize();
        }

        @Override
        public boolean shouldSkipClass(Class<?> aClass) {
            return false;
        }
    }).registerTypeAdapter(Date.class,
            (JsonDeserializer<Date>) (json, typeOfT, context) -> new Date(json.getAsJsonPrimitive().getAsLong()))
            .registerTypeAdapter(Date.class,
                    (JsonSerializer<Date>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getTime()))
            .registerTypeAdapter(ISP.class,
                    (JsonDeserializer<ISP>) (json, typeOfT, context) -> ISP
                            .fromId(json.getAsJsonPrimitive().getAsInt()))
            .registerTypeAdapter(ISP.class,
                    (JsonSerializer<ISP>) (src, typeOfSrc, context) -> new JsonPrimitive(src.id))
            .registerTypeAdapter(Command.class,
                    (JsonDeserializer<Command>) (json, typeOfT, context) -> Command
                            .fromId(json.getAsJsonPrimitive().getAsInt()))
            .registerTypeAdapter(Command.class,
                    (JsonSerializer<Command>) (src, typeOfSrc, context) -> new JsonPrimitive(src.id))
            .registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY).create();

    static {
        try {
            ic = new InitialContext();
            ds = (DataSource) ic.lookup("java:comp/env/jdbc/netsupport");
            ds.setLoginTimeout(3);

            sr = new StandardServiceRegistryBuilder().configure().build();
            sf = new MetadataSources(sr).buildMetadata().buildSessionFactory();

            TableUser.init();
            TableOperator.init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void destroy() {
        try {
            SQLCore.sf.close();
            ((ComboPooledDataSource) SQLCore.ds).close();
            SQLCore.ic.close();
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }

    public static AuditReader getAuditReader(Session session) {
        return AuditReaderFactory.get(session);
    }

    public static class HibernateProxyTypeAdapter extends TypeAdapter<HibernateProxy> {

        public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
            @Override
            @SuppressWarnings("unchecked")
            public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
                return (HibernateProxy.class.isAssignableFrom(type.getRawType())
                        ? (TypeAdapter<T>) new HibernateProxyTypeAdapter(gson)
                        : null);
            }
        };
        private final Gson context;

        private HibernateProxyTypeAdapter(Gson context) {
            this.context = context;
        }

        @Override
        public HibernateProxy read(JsonReader in) throws IOException {
            throw new UnsupportedOperationException("Not supported");
        }

        @SuppressWarnings({ "rawtypes", "unchecked" })
        @Override
        public void write(JsonWriter out, HibernateProxy value) throws IOException {
            if (value == null) {
                out.nullValue();
                return;
            }
            // Retrieve the original (not proxy) class
            Class<?> baseType = Hibernate.getClass(value);
            // Get the TypeAdapter of the original class, to delegate the serialization
            TypeAdapter delegate = context.getAdapter(TypeToken.get(baseType));
            // Get a filled instance of the original class
            Object unproxiedValue = value.getHibernateLazyInitializer().getImplementation();
            // Serialize the value
            delegate.write(out, unproxiedValue);
        }
    }

}