package foundation.persist.source; import java.sql.SQLException; import java.util.Iterator; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import foundation.server.config.DBaseType; import foundation.util.MapList; public class DataSourceManager implements Iterable { private static Logger logger; private static DataSourceManager instance; private static MapList dataSourceList; private static NamedDataSource main; static { logger = LogManager.getLogger(DataSourceManager.class); dataSourceList = new MapList(); } public static synchronized DataSourceManager getInstance() { if (instance == null) { instance = new DataSourceManager(); } return instance; } public static void appendDataSource(NamedDataSource dataSource) throws SQLException { String name = dataSource.getName(); if (name == null) { return; } dataSourceList.add(name, dataSource); if (main == null) { main = dataSource; DBaseType.setMain(dataSource.getDBaseType()); } } public static ConnectionAgent getConnection() throws SQLException { return new ConnectionAgent(main.getName(), main.getConnection(), main.getDBaseType()); } public static ConnectionAgent getConnection(String name) { try { NamedDataSource dataSource = dataSourceList.get(name); if (dataSource == null) { return null; } return dataSource.getConnectionAgent(); } catch (SQLException e) { logger.error(e); return null; } } public static NamedDataSource getDataSource(String name) { return dataSourceList.get(name); } public static void setMain(NamedDataSource dataSource) { main = dataSource; DBaseType.setMain(dataSource.getDBaseType()); } public static void setMain(String name) { main = dataSourceList.get(name); DBaseType.setMain(main.getDBaseType()); } public static NamedDataSource getMain() { return main; } @Override public Iterator iterator() { return dataSourceList.iterator(); } public static DBaseType getDBaseType() { return main.getDBaseType(); } }