All Downloads are FREE. Search and download functionalities are using the official Maven repository.

space.lingu.light.Light Maven / Gradle / Ivy

There is a newer version: 0.4.6
Show newest version
/*
 * Copyright (C) 2022 Lingu Light Project
 *
 * 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.
 */

package space.lingu.light;

import space.lingu.light.connect.ConnectionPool;
import space.lingu.light.sql.DialectProvider;

import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * LightDatabase实用类
 * @author RollW
 */
public final class Light {

    public static  LightDatabase.Builder databaseBuilder(
            Class clazz, Class providerClass) {
        return new LightDatabase.Builder<>(clazz, providerClass);
    }

    public static  T createDialectProviderInstance(final Class clazz) {
        T provider;
        try {
            Constructor constructor = clazz.getConstructor();
            constructor.setAccessible(true);
            provider = constructor.newInstance();
            return provider;
        } catch (NoSuchMethodException e) {
            throw new LightRuntimeException("A DialectProvider class must have a parameterless constructor!");
        } catch (InstantiationException e) {
            throw new LightRuntimeException("Failed to create an instance of "
                    + clazz.getCanonicalName());
        } catch (IllegalAccessException e) {
            throw new LightRuntimeException("Cannot access the constructor "
                    + clazz.getCanonicalName());
        } catch (InvocationTargetException e) {
            throw new LightRuntimeException(e);
        }
    }


    public static  T createConnectionPoolInstance(final Class clazz,
                                                                            DatasourceConfig config) {
        T conn = createConnectionPoolInstance(clazz);
        conn.setDataSourceConfig(config);
        return conn;
    }

    public static  T createConnectionPoolInstance(final Class clazz) {
        T pool;
        try {
            final Constructor tConstructor = clazz.getConstructor();
            tConstructor.setAccessible(true);
            pool = tConstructor.newInstance();
            return pool;
        } catch (NoSuchMethodException e) {
            throw new LightRuntimeException("A ConnectionPool class must have a parameterless constructor!");
        } catch (InstantiationException e) {
            throw new LightRuntimeException("Failed to create an instance of "
                    + clazz.getCanonicalName());
        } catch (IllegalAccessException e) {
            throw new LightRuntimeException("Cannot access the constructor "
                    + clazz.getCanonicalName());
        } catch (InvocationTargetException e) {
            throw new LightRuntimeException(e);
        }
    }

    public static  T getGeneratedImplInstance(final Class clazz,
                                                    final String suffix) {
        final String fullPackage = clazz.getPackage().getName();
        String name = clazz.getCanonicalName();
        final String postPackageName = fullPackage.isEmpty()
                ? name : name.substring(fullPackage.length() + 1);
        final String implName = postPackageName.replace('.', '_') + suffix;
        try {
            final String fullClassName = fullPackage.isEmpty()
                    ? implName : fullPackage + "." + implName;
            @SuppressWarnings("unchecked")
            final Class aClass = (Class) Class.forName(
                    fullClassName, true, clazz.getClassLoader());
            Constructor constructor = aClass.getConstructor();
            constructor.setAccessible(true);
            return constructor.newInstance();
        } catch (ClassNotFoundException e) {
            throw new LightRuntimeException("Cannot find implementation for "
                    + fullPackage + "." + implName + ", class does not exist.");
        } catch (IllegalAccessException e) {
            throw new LightRuntimeException("Cannot access the constructor "
                    + clazz.getCanonicalName());
        } catch (InstantiationException e) {
            throw new LightRuntimeException("Failed to create an instance of "
                    + clazz.getCanonicalName());
        } catch (NoSuchMethodException e) {
            throw new LightRuntimeException("Cannot find a parameterless constructor of "
                    + clazz.getCanonicalName());
        } catch (InvocationTargetException e) {
            throw new LightRuntimeException("Invocation target exception.", e.getTargetException());
        }
    }

    public static InputStream loadResource(String path) {
        return Light.class.getResourceAsStream(File.separator + path);
    }

    private Light() {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy