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

fr.ms.log4jdbc.utils.ServicesJDBC Maven / Gradle / Ivy

/*
 * This file is part of Log4Jdbc.
 *
 * Log4Jdbc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Log4Jdbc 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Log4Jdbc.  If not, see .
 *
 */
package fr.ms.log4jdbc.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import fr.ms.log4jdbc.SqlOperationLogger;
import fr.ms.log4jdbc.rdbms.RdbmsSpecifics;
import fr.ms.util.Service;

/**
 *
 * @see Marco4J
 *
 *
 * @author Marco Semiao
 *
 */
public final class ServicesJDBC {

    public static SqlOperationLogger[] getMessageLogger(final String typeLogger) {
	final List results = new ArrayList();

	for (int i = 0; i < MessageLoggerHolder.messageLogger.length; i++) {
	    final SqlOperationLogger m = MessageLoggerHolder.messageLogger[i];
	    final boolean type = m.isLogger(typeLogger);
	    if (type) {
		results.add(m);
	    }
	}

	return (SqlOperationLogger[]) results.toArray(new SqlOperationLogger[results.size()]);
    }

    public static RdbmsSpecifics getRdbmsSpecifics(final String classType) {

	for (int i = 0; i < RdbmsSpecificsHolder.rdbmsSpecifics.length; i++) {
	    final RdbmsSpecifics r = RdbmsSpecificsHolder.rdbmsSpecifics[i];
	    final boolean rdbms = r.isRdbms(classType);
	    if (rdbms) {
		return r;
	    }
	}
	return null;
    }

    private static class MessageLoggerHolder {
	private final static SqlOperationLogger[] messageLogger = getMessageLogger();

	private final static SqlOperationLogger[] getMessageLogger() {
	    SqlOperationLogger[] messageLogger;

	    final Iterator providers = Service.providers(SqlOperationLogger.class);

	    final List list = new ArrayList();
	    while (providers.hasNext()) {
		try {
		    final SqlOperationLogger m = (SqlOperationLogger) providers.next();
		    list.add(m);
		} catch (final Throwable t) {
		    t.printStackTrace();
		}
	    }

	    messageLogger = (SqlOperationLogger[]) list.toArray(new SqlOperationLogger[list.size()]);

	    return messageLogger;
	}
    }

    private static class RdbmsSpecificsHolder {
	private final static RdbmsSpecifics[] rdbmsSpecifics = getRdbmsSpecifics();

	private final static RdbmsSpecifics[] getRdbmsSpecifics() {
	    RdbmsSpecifics[] rdbmsSpecifics;

	    final Iterator providers = Service.providers(RdbmsSpecifics.class);

	    final List list = new ArrayList();
	    while (providers.hasNext()) {
		try {
		    final RdbmsSpecifics r = (RdbmsSpecifics) providers.next();
		    list.add(r);
		} catch (final Throwable t) {
		    t.printStackTrace();
		}
	    }

	    rdbmsSpecifics = (RdbmsSpecifics[]) list.toArray(new RdbmsSpecifics[list.size()]);

	    return rdbmsSpecifics;
	}
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy