Skip to content

Commit 94331f9

Browse files
authored
Add SocketFactory constructor with Properties argument for Postgres. (#99)
* Add new Postgres SocketFactory with Properties arguement. * Update README. * Fix style violations. * Update example urls.
1 parent f5d060b commit 94331f9

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ When specifying the JDBC connection URL, add the additional parameters:
8484
| Property | Value |
8585
| ---------------- | ------------- |
8686
| socketFactory | com.google.cloud.sql.mysql.SocketFactory |
87-
| cloudSqlInstance | The instance connection name (found on the instance details) |
87+
| cloudSqlInstance | The instance connection name (found on the instance details page) |
8888
| useSSL | False |
8989
| user | MySQL username |
9090
| password | MySQL user's password |
@@ -103,13 +103,13 @@ When specifying the JDBC connection URL, add the additional parameters:
103103
| Property | Value |
104104
| ---------------- | ------------- |
105105
| socketFactory | com.google.cloud.sql.postgres.SocketFactory |
106-
| socketFactoryArg | The instance connection name (which is found on the instance details page in Google Developers Console) |
106+
| cloudSqlInstance | The instance connection name (found on the instance details page) |
107107
| user | Postgres username |
108108
| password | Postgres user's password |
109109

110110
The full JDBC url should look like this:
111111
```
112-
jdbc:postgresql://google/<DATABASE_NAME>?useSSL=false&socketFactoryArg=<INSTANCE_CONNECTION_NAME>&socketFactory=com.google.cloud.sql.postgres.SocketFactory&user=<POSTGRESQL_USER_NAME>&password=<POSTGRESQL_USER_PASSWORD>
112+
jdbc:postgresql://google/<DATABASE_NAME>?cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&socketFactory=com.google.cloud.sql.postgres.SocketFactory&user=<POSTGRESQL_USER_NAME>&password=<POSTGRESQL_USER_PASSWORD>
113113
```
114114

115115
## Additional Information

examples/postgres/appengine-standard-java8/src/main/webapp/WEB-INF/appengine-web.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<runtime>java8</runtime>
1818

1919
<system-properties>
20-
<property name="postgresql" value="jdbc:postgresql://google/${database}?useSSL=false&amp;socketFactoryArg=${INSTANCE_CONNECTION_NAME}&amp;socketFactory=com.google.cloud.sql.postgres.SocketFactory&amp;user=${user}&amp;password=${password}" />
20+
<property name="postgresql" value="jdbc:postgresql://google/${database}?useSSL=false&amp;cloudSqlInstance=${INSTANCE_CONNECTION_NAME}&amp;socketFactory=com.google.cloud.sql.postgres.SocketFactory&amp;user=${user}&amp;password=${password}" />
2121
</system-properties>
2222
</appengine-web-app>
2323
<!-- [END config] -->

examples/postgres/compute-engine/src/main/java/com/google/cloud/sql/postgres/example/ListTables.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static void main(String[] args) throws IOException, SQLException {
4141
//[START doc-example]
4242
String jdbcUrl = String.format(
4343
"jdbc:postgresql://google/%s?socketFactory=com.google.cloud.sql.postgres.SocketFactory"
44-
+ "&socketFactoryArg=%s",
44+
+ "&cloudSqlInstance=%s",
4545
databaseName,
4646
instanceConnectionName);
4747

postgres/src/main/java/com/google/cloud/sql/postgres/SocketFactory.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
import java.io.IOException;
2424
import java.net.InetAddress;
2525
import java.net.Socket;
26+
import java.util.Properties;
2627
import java.util.logging.Logger;
27-
import jnr.unixsocket.UnixSocket;
2828
import jnr.unixsocket.UnixSocketAddress;
2929
import jnr.unixsocket.UnixSocketChannel;
3030

@@ -36,18 +36,34 @@
3636
*/
3737
public class SocketFactory extends javax.net.SocketFactory {
3838
private static final Logger logger = Logger.getLogger(SocketFactory.class.getName());
39-
private static final String CloudSqlPrefix = "/cloudsql/";
40-
private static final String PostgreSqlSufix = "/.s.PGSQL.5432";
39+
40+
private static final String CLOUD_SQL_PREFIX = "/cloudsql/";
41+
private static final String POSTGRES_SUFFIX = "/.s.PGSQL.5432";
42+
43+
private static final String INSTANCE_PROPERTY_KEY = "cloudSqlInstance";
4144

4245
private final String instanceName;
4346

44-
public SocketFactory(String instanceName) {
47+
48+
public SocketFactory(Properties info) {
49+
this.instanceName = info.getProperty(INSTANCE_PROPERTY_KEY);
4550
checkArgument(
46-
instanceName != null,
47-
"socketFactoryArg property not set. Please specify this property in the JDBC "
48-
+ "URL or the connection Properties with the instance connection name in "
49-
+ "form \"project:region:instance\"");
50-
this.instanceName = instanceName;
51+
this.instanceName != null,
52+
"cloudSqlInstance property not set. Please specify this property in the JDBC URL or "
53+
+ "the connection Properties with value in form \"project:region:instance\"");
54+
}
55+
56+
@Deprecated
57+
public SocketFactory(String instanceName) {
58+
// Deprecated constructor for converting 'SocketFactoryArg' to ' 'CloudSqlInstance'
59+
this(createDefaultProperties(instanceName));
60+
}
61+
62+
63+
private static Properties createDefaultProperties(String instanceName) {
64+
Properties info = new Properties();
65+
info.setProperty(INSTANCE_PROPERTY_KEY, instanceName);
66+
return info;
5167
}
5268

5369
@Override
@@ -64,7 +80,7 @@ public Socket createSocket() throws IOException {
6480
logger.info(String.format(
6581
"Connecting to Cloud SQL instance [%s] via unix socket.", instanceName));
6682
UnixSocketAddress socketAddress = new UnixSocketAddress(
67-
new File(CloudSqlPrefix + instanceName + PostgreSqlSufix));
83+
new File(CLOUD_SQL_PREFIX + instanceName + POSTGRES_SUFFIX));
6884
return UnixSocketChannel.open(socketAddress).socket();
6985
}
7086
// Default to SSL Socket

0 commit comments

Comments
 (0)