Skip to content

Commit 8c9d747

Browse files
authored
Updated GAE check for Java11 compatibility. (#116)
* Updated GAE check for Java11 compatbility. * Update GAE check for postgres connector. * Add TODO for consolidating GAE check. * Reformat code. * Fix comment to javadoc. * Fixed formatting. * Fixed javadoc reference to true.
1 parent f6c3215 commit 8c9d747

File tree

4 files changed

+108
-62
lines changed

4 files changed

+108
-62
lines changed

connector-j-5/src/main/java/com/google/cloud/sql/mysql/SocketFactory.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* <p>The heavy lifting is done by the singleton {@link SslSocketFactory} class.
3636
*/
3737
public class SocketFactory implements com.mysql.jdbc.SocketFactory {
38+
3839
private static final Logger logger = Logger.getLogger(SocketFactory.class.getName());
3940
private static final String CloudSqlPrefix = "/cloudsql/";
4041

@@ -48,24 +49,20 @@ public Socket connect(String hostname, int portNumber, Properties props) throws
4849
"cloudSqlInstance property not set. Please specify this property in the JDBC URL or "
4950
+ "the connection Properties with value in form \"project:region:instance\"");
5051

51-
// gaeEnv="standard" indicates standard instances
52-
// runEnv="Production" indicates production instances
53-
String gaeEnv = System.getenv("GAE_ENV");
54-
String runEnv = System.getProperty("com.google.appengine.runtime.environment");
5552
// Custom env variable for forcing unix socket
5653
Boolean forceUnixSocket = System.getenv("CLOUD_SQL_FORCE_UNIX_SOCKET") != null;
5754

5855
// If running on GAE Standard, connect with unix socket
59-
if (forceUnixSocket || "standard".equals(gaeEnv) && "Production".equals(runEnv)) {
60-
logger.info(String.format(
61-
"Connecting to Cloud SQL instance [%s] via unix socket.", instanceName));
62-
UnixSocketAddress socketAddress = new UnixSocketAddress(
63-
new File(CloudSqlPrefix + instanceName));
56+
if (forceUnixSocket || runningOnGaeStandard()) {
57+
logger.info(
58+
String.format("Connecting to Cloud SQL instance [%s] via unix socket.", instanceName));
59+
UnixSocketAddress socketAddress =
60+
new UnixSocketAddress(new File(CloudSqlPrefix + instanceName));
6461
this.socket = UnixSocketChannel.open(socketAddress).socket();
6562
} else {
6663
// Default to SSL Socket
67-
logger.info(String.format(
68-
"Connecting to Cloud SQL instance [%s] via ssl socket.", instanceName));
64+
logger.info(
65+
String.format("Connecting to Cloud SQL instance [%s] via ssl socket.", instanceName));
6966
List<String> ipTypes =
7067
SslSocketFactory.listIpTypes(
7168
props.getProperty("ipTypes", SslSocketFactory.DEFAULT_IP_TYPES));
@@ -74,9 +71,9 @@ public Socket connect(String hostname, int portNumber, Properties props) throws
7471
return this.socket;
7572
}
7673

77-
// Cloud SQL sockets always use TLS and the socket returned by connect above is already TLS-ready. It is fine
74+
// Cloud SQL sockets always use TLS and the socket returned by connect above is already TLS-ready.
75+
// It is fine
7876
// to implement these as no-ops.
79-
8077
@Override
8178
public Socket beforeHandshake() {
8279
return socket;
@@ -86,4 +83,18 @@ public Socket beforeHandshake() {
8683
public Socket afterHandshake() {
8784
return socket;
8885
}
86+
87+
/** Returns {@code true} if running in a Google App Engine Standard runtime. */
88+
// TODO(kurtisvg) move this check into a shared class
89+
private boolean runningOnGaeStandard() {
90+
// gaeEnv="standard" indicates standard instances
91+
String gaeEnv = System.getenv("GAE_ENV");
92+
// runEnv="Production" requires to rule out Java 8 emulated environments
93+
String runEnv = System.getProperty("com.google.appengine.runtime.environment");
94+
// gaeRuntime="java11" in Java 11 environments (no emulated environments)
95+
String gaeRuntime = System.getenv("GAE_RUNTIME");
96+
97+
return "standard".equals(gaeEnv)
98+
&& ("Production".equals(runEnv) || "java11".equals(gaeRuntime));
99+
}
89100
}

connector-j-6/src/main/java/com/google/cloud/sql/mysql/SocketFactory.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,35 @@
3535
* <p>The heavy lifting is done by the singleton {@link SslSocketFactory} class.
3636
*/
3737
public class SocketFactory implements com.mysql.cj.api.io.SocketFactory {
38+
3839
private static final Logger logger = Logger.getLogger(SocketFactory.class.getName());
3940
private static final String CloudSqlPrefix = "/cloudsql/";
4041

4142
private Socket socket;
4243

4344
@Override
44-
public Socket connect(String host, int portNumber, Properties props, int loginTimeout) throws IOException {
45+
public Socket connect(String host, int portNumber, Properties props, int loginTimeout)
46+
throws IOException {
4547
String instanceName = props.getProperty("cloudSqlInstance");
4648
checkArgument(
47-
instanceName != null,
48-
"cloudSqlInstance property not set. Please specify this property in the JDBC URL or "
49-
+ "the connection Properties with value in form \"project:region:instance\"");
49+
instanceName != null,
50+
"cloudSqlInstance property not set. Please specify this property in the JDBC URL or "
51+
+ "the connection Properties with value in form \"project:region:instance\"");
5052

51-
// gaeEnv="standard" indicates standard instances
52-
// runEnv="Production" indicates production instances
53-
String gaeEnv = System.getenv("GAE_ENV");
54-
String runEnv = System.getProperty("com.google.appengine.runtime.environment");
5553
// Custom env variable for forcing unix socket
56-
Boolean forceUnixSocket = System.getenv("CLOUD_SQL_FORCE_UNIX_SOCKET") != null;
54+
boolean forceUnixSocket = System.getenv("CLOUD_SQL_FORCE_UNIX_SOCKET") != null;
5755

5856
// If running on GAE Standard, connect with unix socket
59-
if (forceUnixSocket || "standard".equals(gaeEnv) && "Production".equals(runEnv)) {
60-
logger.info(String.format(
61-
"Connecting to Cloud SQL instance [%s] via unix socket.", instanceName));
62-
UnixSocketAddress socketAddress = new UnixSocketAddress(
63-
new File(CloudSqlPrefix + instanceName));
57+
if (forceUnixSocket || runningOnGaeStandard()) {
58+
logger.info(
59+
String.format("Connecting to Cloud SQL instance [%s] via unix socket.", instanceName));
60+
UnixSocketAddress socketAddress =
61+
new UnixSocketAddress(new File(CloudSqlPrefix + instanceName));
6462
this.socket = UnixSocketChannel.open(socketAddress).socket();
6563
} else {
6664
// Default to SSL Socket
67-
logger.info(String.format(
68-
"Connecting to Cloud SQL instance [%s] via ssl socket.", instanceName));
65+
logger.info(
66+
String.format("Connecting to Cloud SQL instance [%s] via ssl socket.", instanceName));
6967
List<String> ipTypes =
7068
SslSocketFactory.listIpTypes(
7169
props.getProperty("ipTypes", SslSocketFactory.DEFAULT_IP_TYPES));
@@ -74,7 +72,8 @@ public Socket connect(String host, int portNumber, Properties props, int loginTi
7472
return this.socket;
7573
}
7674

77-
// Cloud SQL sockets always use TLS and the socket returned by connect above is already TLS-ready. It is fine
75+
// Cloud SQL sockets always use TLS and the socket returned by connect above is already TLS-ready.
76+
// It is fine
7877
// to implement these as no-ops.
7978
@Override
8079
public Socket beforeHandshake() {
@@ -85,4 +84,18 @@ public Socket beforeHandshake() {
8584
public Socket afterHandshake() {
8685
return socket;
8786
}
87+
88+
/** Returns {@code true} if running in a Google App Engine Standard runtime. */
89+
// TODO(kurtisvg) move this check into a shared class
90+
private boolean runningOnGaeStandard() {
91+
// gaeEnv="standard" indicates standard instances
92+
String gaeEnv = System.getenv("GAE_ENV");
93+
// runEnv="Production" requires to rule out Java 8 emulated environments
94+
String runEnv = System.getProperty("com.google.appengine.runtime.environment");
95+
// gaeRuntime="java11" in Java 11 environments (no emulated environments)
96+
String gaeRuntime = System.getenv("GAE_RUNTIME");
97+
98+
return "standard".equals(gaeEnv)
99+
&& ("Production".equals(runEnv) || "java11".equals(gaeRuntime));
100+
}
88101
}

connector-j-8/src/main/java/com/google/cloud/sql/mysql/SocketFactory.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.mysql.cj.conf.PropertySet;
2323
import com.mysql.cj.protocol.ServerSession;
2424
import com.mysql.cj.protocol.SocketConnection;
25-
2625
import java.io.Closeable;
2726
import java.io.File;
2827
import java.io.IOException;
@@ -40,35 +39,34 @@
4039
* <p>The heavy lifting is done by the singleton {@link SslSocketFactory} class.
4140
*/
4241
public class SocketFactory implements com.mysql.cj.protocol.SocketFactory {
42+
4343
private static final Logger logger = Logger.getLogger(SocketFactory.class.getName());
4444
private static final String CloudSqlPrefix = "/cloudsql/";
4545

4646
private Socket socket;
4747

4848
@Override
49-
public <T extends Closeable> T connect(String host, int portNumber, PropertySet props, int loginTimeout)
49+
public <T extends Closeable> T connect(String host, int portNumber, PropertySet props,
50+
int loginTimeout)
5051
throws IOException {
5152
return connect(host, portNumber, props.exposeAsProperties(), loginTimeout);
5253
}
5354

5455
// This is the API before mysql-connector-java 8.0.13
55-
public <T extends Closeable> T connect(String host, int portNumber, Properties props, int loginTimeout)
56+
public <T extends Closeable> T connect(String host, int portNumber, Properties props,
57+
int loginTimeout)
5658
throws IOException {
5759
String instanceName = props.getProperty("cloudSqlInstance");
5860
checkArgument(
59-
instanceName != null,
60-
"cloudSqlInstance property not set. Please specify this property in the JDBC URL or "
61-
+ "the connection Properties with value in form \"project:region:instance\"");
61+
instanceName != null,
62+
"cloudSqlInstance property not set. Please specify this property in the JDBC URL or "
63+
+ "the connection Properties with value in form \"project:region:instance\"");
6264

63-
// gaeEnv="standard" indicates standard instances
64-
// runEnv="Production" indicates production instances
65-
String gaeEnv = System.getenv("GAE_ENV");
66-
String runEnv = System.getProperty("com.google.appengine.runtime.environment");
6765
// Custom env variable for forcing unix socket
68-
Boolean forceUnixSocket = System.getenv("CLOUD_SQL_FORCE_UNIX_SOCKET") != null;
66+
boolean forceUnixSocket = System.getenv("CLOUD_SQL_FORCE_UNIX_SOCKET") != null;
6967

7068
// If running on GAE Standard, connect with unix socket
71-
if (forceUnixSocket || "standard".equals(gaeEnv) && "Production".equals(runEnv)) {
69+
if (forceUnixSocket || runningOnGaeStandard()) {
7270
logger.info(String.format(
7371
"Connecting to Cloud SQL instance [%s] via unix socket.", instanceName));
7472
UnixSocketAddress socketAddress = new UnixSocketAddress(
@@ -88,18 +86,33 @@ public <T extends Closeable> T connect(String host, int portNumber, Properties p
8886
return castSocket;
8987
}
9088

91-
9289
// Cloud SQL sockets always use TLS and the socket returned by connect above is already TLS-ready. It is fine
9390
// to implement these as no-ops.
94-
9591
@Override
96-
public void beforeHandshake() {}
92+
public void beforeHandshake() {
93+
}
9794

9895
@Override
99-
public Socket performTlsHandshake(SocketConnection socketConnection, ServerSession serverSession) {
96+
public Socket performTlsHandshake(SocketConnection socketConnection,
97+
ServerSession serverSession) {
10098
return socket;
10199
}
102100

103101
@Override
104-
public void afterHandshake() {}
102+
public void afterHandshake() {
103+
}
104+
105+
/** Returns {@code true} if running in a Google App Engine Standard runtime. */
106+
// TODO(kurtisvg) move this check into a shared class
107+
private boolean runningOnGaeStandard() {
108+
// gaeEnv="standard" indicates standard instances
109+
String gaeEnv = System.getenv("GAE_ENV");
110+
// runEnv="Production" requires to rule out Java 8 emulated environments
111+
String runEnv = System.getProperty("com.google.appengine.runtime.environment");
112+
// gaeRuntime="java11" in Java 11 environments (no emulated environments)
113+
String gaeRuntime = System.getenv("GAE_RUNTIME");
114+
115+
return "standard".equals(gaeEnv)
116+
&& ("Production".equals(runEnv) || "java11".equals(gaeRuntime));
117+
}
105118
}

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

Lines changed: 24 additions & 15 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;
2726
import java.util.List;
27+
import java.util.Properties;
2828
import java.util.logging.Logger;
2929
import jnr.unixsocket.UnixSocketAddress;
3030
import jnr.unixsocket.UnixSocketChannel;
@@ -36,6 +36,7 @@
3636
* <p>The heavy lifting is done by the singleton {@link SslSocketFactory} class.
3737
*/
3838
public class SocketFactory extends javax.net.SocketFactory {
39+
3940
private static final Logger logger = Logger.getLogger(SocketFactory.class.getName());
4041

4142
private static final String CLOUD_SQL_PREFIX = "/cloudsql/";
@@ -47,7 +48,6 @@ public class SocketFactory extends javax.net.SocketFactory {
4748
private final String instanceName;
4849
private final List<String> ipTypes;
4950

50-
5151
public SocketFactory(Properties info) {
5252
this.instanceName = info.getProperty(INSTANCE_PROPERTY_KEY);
5353
checkArgument(
@@ -66,7 +66,6 @@ public SocketFactory(String instanceName) {
6666
this(createDefaultProperties(instanceName));
6767
}
6868

69-
7069
private static Properties createDefaultProperties(String instanceName) {
7170
Properties info = new Properties();
7271
info.setProperty(INSTANCE_PROPERTY_KEY, instanceName);
@@ -75,24 +74,20 @@ private static Properties createDefaultProperties(String instanceName) {
7574

7675
@Override
7776
public Socket createSocket() throws IOException {
78-
// gaeEnv="standard" indicates standard instances
79-
// runEnv="Production" indicates production instances
80-
String gaeEnv = System.getenv("GAE_ENV");
81-
String runEnv = System.getProperty("com.google.appengine.runtime.environment");
8277
// Custom env variable for forcing unix socket
83-
Boolean forceUnixSocket = System.getenv("CLOUD_SQL_FORCE_UNIX_SOCKET") != null;
78+
boolean forceUnixSocket = System.getenv("CLOUD_SQL_FORCE_UNIX_SOCKET") != null;
8479

8580
// If running on GAE Standard, connect with unix socket
86-
if (forceUnixSocket || "standard".equals(gaeEnv) && "Production".equals(runEnv)) {
87-
logger.info(String.format(
88-
"Connecting to Cloud SQL instance [%s] via unix socket.", instanceName));
89-
UnixSocketAddress socketAddress = new UnixSocketAddress(
90-
new File(CLOUD_SQL_PREFIX + instanceName + POSTGRES_SUFFIX));
81+
if (forceUnixSocket || runningOnGaeStandard()) {
82+
logger.info(
83+
String.format("Connecting to Cloud SQL instance [%s] via unix socket.", instanceName));
84+
UnixSocketAddress socketAddress =
85+
new UnixSocketAddress(new File(CLOUD_SQL_PREFIX + instanceName + POSTGRES_SUFFIX));
9186
return UnixSocketChannel.open(socketAddress).socket();
9287
}
9388
// Default to SSL Socket
94-
logger.info(String.format(
95-
"Connecting to Cloud SQL instance [%s] via ssl socket.", instanceName));
89+
logger.info(
90+
String.format("Connecting to Cloud SQL instance [%s] via ssl socket.", instanceName));
9691
return SslSocketFactory.getInstance().create(instanceName, ipTypes);
9792
}
9893

@@ -117,4 +112,18 @@ public Socket createSocket(InetAddress address, int port, InetAddress localAddre
117112
throws IOException {
118113
throw new UnsupportedOperationException();
119114
}
115+
116+
/** Returns {@code true} if running in a Google App Engine Standard runtime. */
117+
// TODO(kurtisvg) move this check into a shared class
118+
private boolean runningOnGaeStandard() {
119+
// gaeEnv="standard" indicates standard instances
120+
String gaeEnv = System.getenv("GAE_ENV");
121+
// runEnv="Production" requires to rule out Java 8 emulated environments
122+
String runEnv = System.getProperty("com.google.appengine.runtime.environment");
123+
// gaeRuntime="java11" in Java 11 environments (no emulated environments)
124+
String gaeRuntime = System.getenv("GAE_RUNTIME");
125+
126+
return "standard".equals(gaeEnv)
127+
&& ("Production".equals(runEnv) || "java11".equals(gaeRuntime));
128+
}
120129
}

0 commit comments

Comments
 (0)