private void createJndiWrapper(String env, String jndiName) { ConnectionSpecWrapper wrapper = new ConnectionSpecWrapper(); wrapper.setEnvironment(env); ConnectionJndiSpec connectionSpec = new ConnectionJndiSpec(jndiName); wrapper.setConnectionSpec(connectionSpec); addConnectionWrapper(wrapper, false); }
private void createJdbcWrapper(String env, String driver, String url, String userName, String password) { ConnectionSpecWrapper wrapper = new ConnectionSpecWrapper(); if(env.equals("test")){ wrapper.setEnvironment("development"); wrapper.setTesting(true); } else if(env.endsWith(".test")) { wrapper.setEnvironment(env.split("\\.")[0]); wrapper.setTesting(true); }else{ wrapper.setEnvironment(env); } ConnectionJdbcSpec connectionSpec = new ConnectionJdbcSpec(driver, url, userName, password); wrapper.setConnectionSpec(connectionSpec); addConnectionWrapper(wrapper, false); }
/** * This method is used internally by framework. * * @param spec specification for a JDBC connection. */ private DB openJdbc(ConnectionJdbcSpec spec) { if(spec.getProps()!= null){ return open(spec.getDriver(), spec.getUrl(), spec.getProps()); }else{ return open(spec.getDriver(), spec.getUrl(), spec.getUser(), spec.getPassword()); } }
public List<ConnectionSpecWrapper> getTestConnectionWrappers() { List<ConnectionSpecWrapper> allConnections = getConnectionSpecWrappers(); List<ConnectionSpecWrapper> result = new LinkedList<>(); for (ConnectionSpecWrapper connectionWrapper : allConnections) { if (connectionWrapper.isTesting()) result.add(connectionWrapper); } return result; } }
/** * Overrides current environment's connection spec from system properties. */ private void overrideFromSystemProperties() { String url = System.getProperty("activejdbc.url"); String user = System.getProperty("activejdbc.user"); String password = System.getProperty("activejdbc.password"); String driver = System.getProperty("activejdbc.driver"); if(!blank(url) && !blank(user) && !blank(driver)){ connectionSpecMap.put(getEnvironment(), new ConnectionJdbcSpec(driver, url, user, password)); } String jndi = System.getProperty("activejdbc.jndi"); if(!blank(jndi)){ connectionSpecMap.put(getEnvironment(), new ConnectionJndiSpec(jndi)); } }
ConnectionBuilder(String environment) { connectionWrapper = new ConnectionSpecWrapper(); connectionWrapper.setEnvironment(environment); DbConfiguration.addConnectionWrapper(connectionWrapper, false); }
@Before @BeforeEach public final void openTestConnections() { if(!suppressDb){ loadConfiguration("/database.properties"); List<ConnectionSpecWrapper> connectionWrappers = getTestConnectionWrappers(); if (connectionWrappers.isEmpty()) { LOGGER.warn("no DB connections are configured, none opened"); return; } for (ConnectionSpecWrapper connectionWrapper : connectionWrappers) { DB db = new DB(connectionWrapper.getDbName()); db.open(connectionWrapper.getConnectionSpec()); if (rollback()){ db.openTransaction(); } } } }
/** * Configure expanded JDBC parameters for opening a connection if needed * @param driver class name of driver * @param url JDBC URL * @param props properties with additional parameters a driver can take. */ public void jdbc(String driver, String url, Properties props) { connectionWrapper.setConnectionSpec(new ConnectionJdbcSpec(driver, url, props)); }
/** * Provide a name of a JNDI datasource configured for runtime. * @param jndi name of a JNDI datasource */ public void jndi(String jndi) { connectionWrapper.setConnectionSpec(new ConnectionJndiSpec(jndi)); }
/** * Provides a list of all connection wrappers corresponding to a current environment. * * @return a list of all connection wrappers corresponding to a current environment. */ public static List<ConnectionSpecWrapper> getConnectionSpecWrappers() { return getConnectionSpecWrappers(Configuration.getEnv()); }
String jndiName = env + "." + "jndi"; if (props.containsKey(jndiName)) { createJndiWrapper(env, props.getProperty(jndiName)); } else { String driver = props.getProperty(env + ".driver"); String password = props.getProperty(env + ".password"); String url = props.getProperty(env + ".url"); checkProps(driver, userName, password, url, env); createJdbcWrapper(env, driver, url, userName, password);
/** * Sets a <code>DataSource</code> to be used by this configuration. * * @param dataSource instance of a datadource */ public void dataSource(DataSource dataSource) { connectionWrapper.setConnectionSpec(new ConnectionDataSourceSpec(dataSource)); } }
@After @AfterEach public final void closeTestConnections() { if(!suppressDb){ List<ConnectionSpecWrapper> connectionWrappers = getTestConnectionWrappers(); for (ConnectionSpecWrapper connectionWrapper : connectionWrappers) { String dbName = connectionWrapper.getDbName(); DB db = new DB(dbName); if (rollback()) { db.rollbackTransaction(); } db.close(); } clearConnectionWrappers(); } } }
/** * This method is used internally by framework. * * @param spec specification for a JDBC connection. */ private DB openJndi(ConnectionJndiSpec spec) { if(spec.getContext() != null){ return openContext(spec.getContext(), spec.getDataSourceJndiName()); }else{ return open(spec.getDataSourceJndiName()); } }
public static void clearConnectionWrappers() { clearConnectionWrappers(Configuration.getEnv()); }
private DB openDataSource(ConnectionDataSourceSpec spec) { return open(spec.getDataSource()); }
public static void addConnectionWrapper(ConnectionSpecWrapper connectionWrapper, boolean override) { String connectionWrapperEnv = connectionWrapper.getEnvironment(); List<ConnectionSpecWrapper> envConnectionWrappers = connectionWrappers.get(connectionWrapperEnv); if(envConnectionWrappers == null || override) { envConnectionWrappers = new ArrayList<>(); connectionWrappers.put(connectionWrapperEnv, envConnectionWrappers); } envConnectionWrappers.add(connectionWrapper); }
private void createJndiWrapper(String env, String jndiName) { ConnectionSpecWrapper wrapper = new ConnectionSpecWrapper(); wrapper.setEnvironment(env); ConnectionJndiSpec connectionSpec = new ConnectionJndiSpec(jndiName); wrapper.setConnectionSpec(connectionSpec); addConnectionWrapper(wrapper, false); }
private void loadConnectionsSpecs() { try{ String propertyFileName = properties == null ? "database.properties" : properties.getProperty(PropertyName.EnvConnectionsFile.name, "database.properties"); Properties connectionProps = readPropertyFile(propertyFileName); for (String env : getEnvironments(connectionProps)) { String jndiName = env + "." + "jndi"; if (connectionProps.containsKey(jndiName)) { connectionSpecMap.put(env, new ConnectionJndiSpec(connectionProps.getProperty(jndiName))); } else { String driver = connectionProps.getProperty(env + ".driver"); String userName = connectionProps.getProperty(env + ".username"); String password = connectionProps.getProperty(env + ".password"); String url = connectionProps.getProperty(env + ".url"); if (driver == null || userName == null || password == null || url == null) { throw new InitException("Four JDBC properties are expected: driver, username, password, url for environment: " + env); } connectionSpecMap.put(env, new ConnectionJdbcSpec(driver, url, userName, password)); } } }catch(Exception e){ // in case property file not found, do nothing } overrideFromEnvironmentVariables(); overrideFromSystemProperties(); }
/** * Overrides current environment's connection spec from system properties. */ private void overrideFromEnvironmentVariables() { String url = System.getenv("ACTIVEJDBC.URL"); String user = System.getenv("ACTIVEJDBC.USER"); String password = System.getenv("ACTIVEJDBC.PASSWORD"); String driver = System.getenv("ACTIVEJDBC.DRIVER"); if(!blank(url) && !blank(user) && !blank(password) && !blank(driver)){ connectionSpecMap.put(getEnvironment(), new ConnectionJdbcSpec(driver, url, user, password)); } String jndi = System.getenv("ACTIVEJDBC.JNDI"); if(!blank(jndi)){ connectionSpecMap.put(getEnvironment(), new ConnectionJndiSpec(jndi)); } }