Skip to content

Commit 57612cc

Browse files
author
Igor Polevoy
committed
#220 Direct connection configuration in DbConfig...
1 parent 7abc7bc commit 57612cc

File tree

5 files changed

+67
-6
lines changed

5 files changed

+67
-6
lines changed

activeweb/src/main/java/org/javalite/activeweb/AbstractDBConfig.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,31 @@
7272
* <li>Line 5: This is a configuration of DB connection for "hudson" environment, but for "testing" mode. The "hudson"
7373
* environment is a computer where this project is built by Hudson - the continuous integration server. Since Hudson
7474
* computer is fully automated, and this project is not running there in "standard" mode, there is no standard configuration
75-
* for hudson environment, just one for testing.
75+
* for Jenkins environment, just one for testing.
7676
* <li>Line 6: This is configuration similar to one on line 3, but for "production" environment.
7777
* </ul>
7878
*
7979
* @author Igor Polevoy
8080
*/
8181
public abstract class AbstractDBConfig extends AppConfig {
8282

83+
/**
84+
* @param environment name of environment (corresponds to env var ACTIVE_ENV)
85+
* @return builder instance
86+
*/
8387
public ConnectionBuilder environment(String environment) {
8488
return new ConnectionBuilder(environment);
8589
}
8690

91+
/**
92+
* @param environment name of environment (corresponds to env var ACTIVE_ENV)
93+
* @param override true to override any previous configuration for the same environment.
94+
* @return builder instance
95+
*/
96+
public ConnectionBuilder environment(String environment, boolean override) {
97+
return new ConnectionBuilder(environment, override);
98+
}
99+
87100
/**
88101
* Configures multiple database connections from a single property file. Example content for such file:
89102
*
@@ -155,15 +168,15 @@ private void createJdbcWrapper(String env, String driver, String url, String use
155168
}
156169
ConnectionJdbcSpec connectionSpec = new ConnectionJdbcSpec(driver, url, userName, password);
157170
wrapper.setConnectionSpec(connectionSpec);
158-
Configuration.addConnectionWrapper(wrapper);
171+
Configuration.addConnectionWrapper(wrapper, false);
159172
}
160173

161174
private void createJndiWrapper(String env, String jndiName) {
162175
ConnectionSpecWrapper wrapper = new ConnectionSpecWrapper();
163176
wrapper.setEnvironment(env);
164177
ConnectionJndiSpec connectionSpec = new ConnectionJndiSpec(jndiName);
165178
wrapper.setConnectionSpec(connectionSpec);
166-
Configuration.addConnectionWrapper(wrapper);
179+
Configuration.addConnectionWrapper(wrapper, false);
167180
}
168181

169182

activeweb/src/main/java/org/javalite/activeweb/Configuration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,10 @@ public static boolean rollback() {
222222
return Boolean.parseBoolean(get(Params.rollback.toString().trim()));
223223
}
224224

225-
protected static void addConnectionWrapper(ConnectionSpecWrapper connectionWrapper) {
225+
protected static void addConnectionWrapper(ConnectionSpecWrapper connectionWrapper, boolean override) {
226226
String connectionWrapperEnv = connectionWrapper.getEnvironment();
227227
List<ConnectionSpecWrapper> envConnectionWrappers = connectionWrappers.get(connectionWrapperEnv);
228-
if(envConnectionWrappers == null) {
228+
if(envConnectionWrappers == null || override) {
229229
envConnectionWrappers = new ArrayList<ConnectionSpecWrapper>();
230230
connectionWrappers.put(connectionWrapperEnv, envConnectionWrappers);
231231
}

activeweb/src/main/java/org/javalite/activeweb/ConnectionBuilder.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ public class ConnectionBuilder {
3333
ConnectionBuilder(String environment) {
3434
connectionWrapper = new ConnectionSpecWrapper();
3535
connectionWrapper.setEnvironment(environment);
36-
Configuration.addConnectionWrapper(connectionWrapper);
36+
Configuration.addConnectionWrapper(connectionWrapper, false);
37+
}
38+
39+
ConnectionBuilder(String environment, boolean override) {
40+
connectionWrapper = new ConnectionSpecWrapper();
41+
connectionWrapper.setEnvironment(environment);
42+
Configuration.addConnectionWrapper(connectionWrapper, override);
3743
}
3844

3945
/**

activeweb/src/test/java/org/javalite/activeweb/AbstractConnectionBuilderSpec.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import org.junit.Before;
66
import org.junit.Test;
77

8+
import java.util.List;
9+
810
import static org.javalite.test.jspec.JSpec.a;
11+
import static org.javalite.test.jspec.JSpec.the;
912

1013
/**
1114
* @author Igor Polevoy on 12/1/14.
@@ -124,4 +127,28 @@ public void init(AppContext appContext) {
124127
a(connectionSpec.getUser()).shouldBeEqual("mary");
125128
a(connectionSpec.getPassword()).shouldBeEqual("pwd1");
126129
}
130+
131+
132+
133+
@Test
134+
public void shouldOverrideConnectionSpecForTheSameEnvironment(){
135+
136+
class DBConfig extends AbstractDBConfig{
137+
public void init(AppContext appContext) {
138+
configFile("/database_new.properties");
139+
environment("production", true).jndi("java:comp/env/jdbc/prod_new");
140+
}
141+
}
142+
143+
DBConfig config = new DBConfig();
144+
config.init(null);
145+
146+
List<ConnectionSpecWrapper> wrappers = Configuration.getConnectionSpecWrappers("production");
147+
148+
//we configured two, one in file, one in class. But the class config overrides one in file.
149+
the(wrappers.size()).shouldBeEqual(1);
150+
151+
ConnectionJndiSpec connectionSpec = (ConnectionJndiSpec) wrappers.get(0).getConnectionSpec();
152+
the(connectionSpec.getDataSourceJndiName()).shouldBeEqual("java:comp/env/jdbc/prod_new");
153+
}
127154
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
development.driver=com.mysql.jdbc.Driver
2+
development.username=john
3+
development.password=pwd
4+
development.url=jdbc:mysql://localhost/proj_dev
5+
6+
7+
test.driver=com.mysql.jdbc.Driver
8+
test.username=mary
9+
test.password=pwd1
10+
test.url=jdbc:mysql://localhost/test
11+
12+
production.driver=com.mysql.jdbc.Driver
13+
production.username=mary
14+
production.password=pwd1
15+
production.url=jdbc:mysql://localhost/prod

0 commit comments

Comments
 (0)