Unverified Commit 09a8a4fb authored by 张亮's avatar 张亮 Committed by GitHub
Browse files

Merge pull request #949 from tristaZero/dev

Remove json files.
parents 46e8d31b 75ac2ee9
Loading
Loading
Loading
Loading
+0 −137
Original line number Diff line number Diff line
/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed 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.
 * </p>
 */

package io.shardingsphere.jdbc.orchestration.internal.json;

import com.google.common.collect.Sets;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import io.shardingsphere.core.exception.ShardingException;
import io.shardingsphere.core.jdbc.core.datasource.NamedDataSource;
import io.shardingsphere.core.util.DataSourceUtil;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

/**
 * Data source gson type adapter.
 *
 * @author zhangliang
 */
public final class DataSourceGsonTypeAdapter extends TypeAdapter<NamedDataSource> {
    
    private static Collection<Class<?>> generalClassType;
    
    static {
        generalClassType = Sets.<Class<?>>newHashSet(boolean.class, Boolean.class, int.class, Integer.class, long.class, Long.class, String.class);
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public NamedDataSource read(final JsonReader in) throws IOException {
        String name = "";
        String clazz = "";
        Map<String, Object> properties = new TreeMap<>();
        in.beginObject();
        while (in.hasNext()) {
            String jsonName = in.nextName();
            switch (jsonName) {
                case DataSourceGsonTypeConstants.DATASOURCE_NAME:
                    name = in.nextString();
                    break;
                case DataSourceGsonTypeConstants.CLAZZ_NAME:
                    clazz = in.nextString();
                    break;
                default:
                    properties.put(jsonName, in.nextString());
                    break;
            }
        }
        in.endObject();
        try {
            return new NamedDataSource(name, DataSourceUtil.getDataSource(clazz, properties));
        } catch (final ReflectiveOperationException ex) {
            throw new ShardingException(ex);
        }
    }
    
    @Override
    public void write(final JsonWriter out, final NamedDataSource value) throws IOException {
        out.beginObject();
        out.name(DataSourceGsonTypeConstants.DATASOURCE_NAME).value(value.getName());
        out.name(DataSourceGsonTypeConstants.CLAZZ_NAME).value(value.getDataSource().getClass().getName());
        Method[] methods = value.getDataSource().getClass().getMethods();
        Map<String, Method> getterMethods = new TreeMap<>();
        Map<String, Method> setterMethods = new TreeMap<>();
        for (Method each : methods) {
            if (isGetterMethod(each)) {
                getterMethods.put(getPropertyName(each), each);
            } else if (isSetterMethod(each)) {
                setterMethods.put(getPropertyName(each), each);
            }
        }
        Map<String, Method> getterPairedGetterMethods = getPairedGetterMethods(getterMethods, setterMethods);
        for (Entry<String, Method> entry : getterPairedGetterMethods.entrySet()) {
            Object getterResult = null;
            try {
                getterResult = entry.getValue().invoke(value.getDataSource());
                // CHECKSTYLE:OFF
            } catch (final Exception ignore) {
                // CHECKSTYLE:ON
            }
            if (null != getterResult) {
                out.name(entry.getKey()).value(getterResult.toString());
            }
        }
        out.endObject();
    }
    
    private boolean isGetterMethod(final Method method) {
        return method.getName().startsWith("get") && 0 == method.getParameterTypes().length && isGeneralClassType(method.getReturnType());
    }
    
    private boolean isSetterMethod(final Method method) {
        return method.getName().startsWith("set") && 1 == method.getParameterTypes().length && isGeneralClassType(method.getParameterTypes()[0]) && isVoid(method.getReturnType());
    }
    
    private boolean isGeneralClassType(final Class<?> clazz) {
        return generalClassType.contains(clazz);
    }
    
    private boolean isVoid(final Class<?> clazz) {
        return void.class == clazz || Void.class == clazz;
    }
    
    private String getPropertyName(final Method method) {
        return String.valueOf(method.getName().charAt(3)).toLowerCase() + method.getName().substring(4, method.getName().length());
    }
    
    private Map<String, Method> getPairedGetterMethods(final Map<String, Method> getterMethods, final Map<String, Method> setterMethods) {
        Map<String, Method> result = new TreeMap<>();
        for (Entry<String, Method> entry : getterMethods.entrySet()) {
            if (setterMethods.containsKey(entry.getKey())) {
                result.put(entry.getKey(), entry.getValue());
            }
        }
        return result;
    }
}
+0 −34
Original line number Diff line number Diff line
/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed 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.
 * </p>
 */

package io.shardingsphere.jdbc.orchestration.internal.json;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * Data source constants for json.
 * 
 * @author zhangliang 
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class DataSourceGsonTypeConstants {
    
    public static final String DATASOURCE_NAME = "shardingJdbcDataSourceName";
    
    public static final String CLAZZ_NAME = "shardingJdbcDataSourceClazz";
}
+0 −76
Original line number Diff line number Diff line
/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed 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.
 * </p>
 */

package io.shardingsphere.jdbc.orchestration.internal.json;

import io.shardingsphere.core.jdbc.core.datasource.NamedDataSource;
import com.google.gson.reflect.TypeToken;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import javax.sql.DataSource;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Data source map json converter.
 *
 * @author zhangliang
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class DataSourceJsonConverter {
    
    static {
        GsonFactory.registerTypeAdapter(NamedDataSource.class, new DataSourceGsonTypeAdapter());
    }
    
    /**
     * Convert data source map to json.
     * 
     * @param dataSources data source map
     * @return data source map json string
     */
    public static String toJson(final Map<String, DataSource> dataSources) {
        Collection<NamedDataSource> result = new LinkedList<>();
        for (Entry<String, DataSource> entry : dataSources.entrySet()) {
            result.add(new NamedDataSource(entry.getKey(), entry.getValue()));
        }
        return GsonFactory.getGson().toJson(result);
    }
    
    /**
     * Convert data source map from json.
     *
     * @param json data source map json string
     * @return data source map
     */
    @SuppressWarnings("unchecked")
    public static Map<String, DataSource> fromJson(final String json) {
        Map<String, DataSource> result = new LinkedHashMap<>();
        List<NamedDataSource> namedDataSources = GsonFactory.getGson().fromJson(json, new TypeToken<List<NamedDataSource>>() {
            
        }.getType());
        for (NamedDataSource each : namedDataSources) {
            result.put(each.getName(), each.getDataSource());
        }
        return result;
    }
}
+0 −58
Original line number Diff line number Diff line
/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed 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.
 * </p>
 */

package io.shardingsphere.jdbc.orchestration.internal.json;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.lang.reflect.Type;

/**
 * Gson Factor.
 *
 * @author zhangliang
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class GsonFactory {
    
    private static final GsonBuilder GSON_BUILDER = new GsonBuilder();
    
    private static volatile Gson gson = GSON_BUILDER.create();
    
    /**
     * Register gson type adapter.
     * @param type gson type
     * @param typeAdapter gson type adapter
     */
    public static synchronized void registerTypeAdapter(final Type type, final TypeAdapter typeAdapter) {
        GSON_BUILDER.registerTypeAdapter(type, typeAdapter);
        gson = GSON_BUILDER.create();
    }
    
    /**
     * Get gson instance.
     * 
     * @return gson instance
     */
    public static Gson getGson() {
        return gson;
    }
}
+0 −57
Original line number Diff line number Diff line
/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed 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.
 * </p>
 */

package io.shardingsphere.jdbc.orchestration.internal.json;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import io.shardingsphere.core.keygen.KeyGenerator;
import io.shardingsphere.core.keygen.KeyGeneratorFactory;

import java.io.IOException;

/**
 * Key generator gson type adapter.
 *
 * @author zhangliang
 */
public final class KeyGeneratorGsonTypeAdapter extends TypeAdapter<KeyGenerator> {
    
    @Override
    public KeyGenerator read(final JsonReader in) throws IOException {
        String keyGeneratorClassName = null;
        in.beginObject();
        while (in.hasNext()) {
            Preconditions.checkArgument("keyGenerator".equals(in.nextName()));
            keyGeneratorClassName = in.nextString();
        }
        in.endObject();
        return Strings.isNullOrEmpty(keyGeneratorClassName) ? null : KeyGeneratorFactory.newInstance(keyGeneratorClassName);
    }
    
    @Override
    public void write(final JsonWriter out, final KeyGenerator value) throws IOException {
        out.beginObject();
        if (null != value) {
            out.name("keyGenerator").value(value.getClass().getName());
        }
        out.endObject();
    }
}
Loading