/
ConnectionBuilder.java
97 lines (83 loc) · 3.22 KB
/
ConnectionBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Copyright 2009-2014 Igor Polevoy
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 org.javalite.activeweb;
import org.javalite.activejdbc.ConnectionJdbcSpec;
import org.javalite.activejdbc.ConnectionJndiSpec;
import java.util.Properties;
/**
* Supports DSL for specifying connection parameters for various environments and modes.
* This class is not used directly.
*
* @author Igor Polevoy
*/
public class ConnectionBuilder {
private ConnectionSpecWrapper connectionWrapper;
ConnectionBuilder(String environment) {
connectionWrapper = new ConnectionSpecWrapper();
connectionWrapper.setEnvironment(environment);
Configuration.addConnectionWrapper(connectionWrapper, false);
}
ConnectionBuilder(String environment, boolean override) {
connectionWrapper = new ConnectionSpecWrapper();
connectionWrapper.setEnvironment(environment);
Configuration.addConnectionWrapper(connectionWrapper, override);
}
/**
* 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));
}
/**
* Configure standard JDBC parameters for opening a connection.
*
* @param driver class name of driver
* @param url JDBC URL
* @param user user name
* @param password password
*/
public void jdbc(String driver, String url, String user, String password) {
connectionWrapper.setConnectionSpec(new ConnectionJdbcSpec(driver, url, user, password));
}
/**
* 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));
}
/**
* Name of a database. If this method is not called, the name od database is presumed "default".
* @param dbName name od database for ActiveJDBC models.
* @return self
*/
public ConnectionBuilder db(String dbName) {
connectionWrapper.setDbName(dbName);
return this;
}
/**
* Marks this connection to be used for testing. Whe n you use any of the testing classes, such as DBSpec, DBControllerSpec,
* DNIntegrationSpec and AppIntegrationSpec from activeweb-testing package, they all will use a connection that is marked
* by this method.
*
* @return self
*/
public ConnectionBuilder testing() {
connectionWrapper.setTesting(true);
return this;
}
}