|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.sql.nativeimage; |
| 18 | + |
| 19 | +import com.google.api.gax.nativeimage.NativeImageUtils; |
| 20 | +import com.oracle.svm.core.annotate.AutomaticFeature; |
| 21 | +import com.oracle.svm.core.configure.ResourcesRegistry; |
| 22 | +import org.graalvm.nativeimage.ImageSingletons; |
| 23 | +import org.graalvm.nativeimage.hosted.Feature; |
| 24 | +import org.graalvm.nativeimage.hosted.RuntimeClassInitialization; |
| 25 | +import org.graalvm.nativeimage.hosted.RuntimeReflection; |
| 26 | +import org.graalvm.nativeimage.impl.ConfigurationCondition; |
| 27 | + |
| 28 | +/** |
| 29 | + * Registers GraalVM configuration for the Cloud SQL libraries. |
| 30 | + * |
| 31 | + * <p>This class is only used when this library is used in <a |
| 32 | + * href="https://www.graalvm.org/22.0/reference-manual/native-image/">GraalVM native image</a> |
| 33 | + * compilation. |
| 34 | + */ |
| 35 | +@AutomaticFeature |
| 36 | +final class CloudSqlFeature implements Feature { |
| 37 | + |
| 38 | + private static final String CLOUD_SQL_SOCKET_CLASS = |
| 39 | + "com.google.cloud.sql.core.CoreSocketFactory"; |
| 40 | + |
| 41 | + private static final String POSTGRES_SOCKET_CLASS = "com.google.cloud.sql.postgres.SocketFactory"; |
| 42 | + |
| 43 | + private static final String MYSQL_SOCKET_CLASS = "com.google.cloud.sql.mysql.SocketFactory"; |
| 44 | + |
| 45 | + @Override |
| 46 | + public void beforeAnalysis(BeforeAnalysisAccess access) { |
| 47 | + if (access.findClassByName(CLOUD_SQL_SOCKET_CLASS) == null) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + // The Core Cloud SQL Socket |
| 52 | + NativeImageUtils.registerClassForReflection(access, CLOUD_SQL_SOCKET_CLASS); |
| 53 | + |
| 54 | + // Resources for Cloud SQL |
| 55 | + ResourcesRegistry resourcesRegistry = ImageSingletons.lookup(ResourcesRegistry.class); |
| 56 | + resourcesRegistry.addResources( |
| 57 | + ConfigurationCondition.alwaysTrue(), "\\Qcom.google.cloud.sql/project.properties\\E"); |
| 58 | + resourcesRegistry.addResources( |
| 59 | + ConfigurationCondition.alwaysTrue(), "\\QMETA-INF/services/java.sql.Driver\\E"); |
| 60 | + |
| 61 | + // Register Hikari configs if used with Cloud SQL. |
| 62 | + if (access.findClassByName("com.zaxxer.hikari.HikariConfig") != null) { |
| 63 | + NativeImageUtils.registerClassForReflection(access, "com.zaxxer.hikari.HikariConfig"); |
| 64 | + |
| 65 | + RuntimeReflection.register( |
| 66 | + access.findClassByName("[Lcom.zaxxer.hikari.util.ConcurrentBag$IConcurrentBagEntry;")); |
| 67 | + |
| 68 | + RuntimeReflection.register(access.findClassByName("[Ljava.sql.Statement;")); |
| 69 | + } |
| 70 | + |
| 71 | + // Register PostgreSQL driver config. |
| 72 | + if (access.findClassByName(POSTGRES_SOCKET_CLASS) != null) { |
| 73 | + NativeImageUtils.registerClassForReflection( |
| 74 | + access, "com.google.cloud.sql.postgres.SocketFactory"); |
| 75 | + NativeImageUtils.registerClassForReflection(access, "org.postgresql.PGProperty"); |
| 76 | + } |
| 77 | + |
| 78 | + // Register MySQL driver config. |
| 79 | + if (access.findClassByName(MYSQL_SOCKET_CLASS) != null) { |
| 80 | + NativeImageUtils.registerClassForReflection(access, MYSQL_SOCKET_CLASS); |
| 81 | + |
| 82 | + NativeImageUtils.registerClassForReflection(access, "com.mysql.jdbc.StandardSocketFactory"); |
| 83 | + |
| 84 | + NativeImageUtils.registerConstructorsForReflection( |
| 85 | + access, "com.mysql.cj.conf.url.SingleConnectionUrl"); |
| 86 | + |
| 87 | + // for mysql-j-5 |
| 88 | + NativeImageUtils.registerConstructorsForReflection( |
| 89 | + access, "com.mysql.jdbc.log.StandardLogger"); |
| 90 | + // for mysql-j-8 |
| 91 | + NativeImageUtils.registerConstructorsForReflection(access, "com.mysql.cj.log.StandardLogger"); |
| 92 | + |
| 93 | + Class<?> cjExceptionClass = access.findClassByName("com.mysql.cj.exceptions.CJException"); |
| 94 | + if (cjExceptionClass != null) { |
| 95 | + // The CJException exists only jdbc/mysql-j-8 module's dependency |
| 96 | + access.registerSubtypeReachabilityHandler( |
| 97 | + (duringAccess, exceptionClass) -> |
| 98 | + NativeImageUtils.registerClassForReflection(duringAccess, exceptionClass.getName()), |
| 99 | + cjExceptionClass); |
| 100 | + } |
| 101 | + |
| 102 | + Class<?> mySqlNonTransientConnectionException = |
| 103 | + access.findClassByName( |
| 104 | + "com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException"); |
| 105 | + if (mySqlNonTransientConnectionException != null) { |
| 106 | + NativeImageUtils.registerConstructorsForReflection( |
| 107 | + access, mySqlNonTransientConnectionException.getName()); |
| 108 | + } |
| 109 | + |
| 110 | + Class<?> mySqlNonTransientException = |
| 111 | + access.findClassByName("com.mysql.jdbc.exceptions.MySQLNonTransientException"); |
| 112 | + if (mySqlNonTransientException != null) { |
| 113 | + NativeImageUtils.registerConstructorsForReflection( |
| 114 | + access, mySqlNonTransientException.getName()); |
| 115 | + } |
| 116 | + |
| 117 | + // JDBC classes create socket connections which must be initialized at run time. |
| 118 | + RuntimeClassInitialization.initializeAtRunTime("com.mysql.cj.jdbc"); |
| 119 | + |
| 120 | + // Additional MySQL resources. |
| 121 | + resourcesRegistry.addResourceBundles( |
| 122 | + ConfigurationCondition.alwaysTrue(), "com.mysql.cj.LocalizedErrorMessages"); |
| 123 | + resourcesRegistry.addResourceBundles( |
| 124 | + ConfigurationCondition.alwaysTrue(), "com.mysql.jdbc.LocalizedErrorMessages"); |
| 125 | + } |
| 126 | + |
| 127 | + // This Netty class should be initialized at runtime |
| 128 | + // https://github.com/netty/netty/issues/11638 |
| 129 | + Class<?> bouncyCastleAlpnSslUtils = |
| 130 | + access.findClassByName("io.netty.handler.ssl.BouncyCastleAlpnSslUtils"); |
| 131 | + if (bouncyCastleAlpnSslUtils != null) { |
| 132 | + RuntimeClassInitialization.initializeAtRunTime(bouncyCastleAlpnSslUtils); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments