|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are |
| 6 | + * met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following disclaimer |
| 12 | + * in the documentation and/or other materials provided with the |
| 13 | + * distribution. |
| 14 | + * * Neither the name of Google LLC nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived from |
| 16 | + * this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | + |
| 31 | +package com.google.api.gax.nativeimage; |
| 32 | + |
| 33 | +import static com.google.api.gax.nativeimage.NativeImageUtils.registerClassForReflection; |
| 34 | + |
| 35 | +import com.oracle.svm.core.annotate.AutomaticFeature; |
| 36 | +import com.oracle.svm.core.configure.ResourcesRegistry; |
| 37 | +import org.graalvm.nativeimage.ImageSingletons; |
| 38 | +import org.graalvm.nativeimage.hosted.Feature; |
| 39 | +import org.graalvm.nativeimage.impl.ConfigurationCondition; |
| 40 | + |
| 41 | +/** Configures Native Image settings for the Google JSON Client. */ |
| 42 | +@AutomaticFeature |
| 43 | +final class GoogleJsonClientFeature implements Feature { |
| 44 | + |
| 45 | + private static final String GOOGLE_API_CLIENT_CLASS = |
| 46 | + "com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient"; |
| 47 | + |
| 48 | + private static final String GOOGLE_API_CLIENT_REQUEST_CLASS = |
| 49 | + "com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest"; |
| 50 | + |
| 51 | + private static final String GENERIC_JSON_CLASS = "com.google.api.client.json.GenericJson"; |
| 52 | + |
| 53 | + @Override |
| 54 | + public void beforeAnalysis(BeforeAnalysisAccess access) { |
| 55 | + loadApiClient(access); |
| 56 | + loadHttpClient(access); |
| 57 | + loadMiscClasses(access); |
| 58 | + } |
| 59 | + |
| 60 | + private void loadApiClient(BeforeAnalysisAccess access) { |
| 61 | + // For com.google.api-client:google-api-client |
| 62 | + Class<?> googleApiClientClass = access.findClassByName(GOOGLE_API_CLIENT_CLASS); |
| 63 | + |
| 64 | + if (googleApiClientClass != null) { |
| 65 | + // All reachable instances of the AbstractGoogleJsonClient must be registered. |
| 66 | + access.registerSubtypeReachabilityHandler( |
| 67 | + (duringAccess, subtype) -> registerClassForReflection(access, subtype.getName()), |
| 68 | + googleApiClientClass); |
| 69 | + |
| 70 | + // All reachable instances of the AbstractGoogleJsonClientRequest must be registered. |
| 71 | + access.registerSubtypeReachabilityHandler( |
| 72 | + (duringAccess, subtype) -> registerClassForReflection(access, subtype.getName()), |
| 73 | + access.findClassByName(GOOGLE_API_CLIENT_REQUEST_CLASS)); |
| 74 | + |
| 75 | + // Resources |
| 76 | + ResourcesRegistry resourcesRegistry = ImageSingletons.lookup(ResourcesRegistry.class); |
| 77 | + resourcesRegistry.addResources( |
| 78 | + ConfigurationCondition.alwaysTrue(), |
| 79 | + "\\Qcom/google/api/client/googleapis/google-api-client.properties\\E"); |
| 80 | + resourcesRegistry.addResources( |
| 81 | + ConfigurationCondition.alwaysTrue(), "\\Qcom/google/api/client/googleapis/google.p12\\E"); |
| 82 | + resourcesRegistry.addResources( |
| 83 | + ConfigurationCondition.alwaysTrue(), |
| 84 | + "\\Qcom/google/api/client/http/google-http-client.properties\\E"); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private void loadHttpClient(BeforeAnalysisAccess access) { |
| 89 | + // For com.google.http-client:google-http-client |
| 90 | + Class<?> genericJsonClass = access.findClassByName(GENERIC_JSON_CLASS); |
| 91 | + |
| 92 | + if (genericJsonClass != null) { |
| 93 | + // All reachable instances of GenericJson must be registered. |
| 94 | + access.registerSubtypeReachabilityHandler( |
| 95 | + (duringAccess, subtype) -> registerClassForReflection(access, subtype.getName()), |
| 96 | + genericJsonClass); |
| 97 | + |
| 98 | + registerClassForReflection(access, "com.google.api.client.util.GenericData"); |
| 99 | + registerClassForReflection(access, "com.google.api.client.json.webtoken.JsonWebToken"); |
| 100 | + registerClassForReflection(access, "com.google.api.client.json.webtoken.JsonWebToken$Header"); |
| 101 | + registerClassForReflection( |
| 102 | + access, "com.google.api.client.json.webtoken.JsonWebToken$Payload"); |
| 103 | + registerClassForReflection( |
| 104 | + access, "com.google.api.client.json.webtoken.JsonWebSignature$Header"); |
| 105 | + registerClassForReflection(access, "com.google.api.client.json.webtoken.JsonWebSignature"); |
| 106 | + registerClassForReflection(access, "com.google.api.client.http.UrlEncodedContent"); |
| 107 | + registerClassForReflection(access, "com.google.api.client.http.GenericUrl"); |
| 108 | + registerClassForReflection(access, "com.google.api.client.http.HttpRequest"); |
| 109 | + registerClassForReflection(access, "com.google.api.client.http.HttpHeaders"); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private void loadMiscClasses(BeforeAnalysisAccess access) { |
| 114 | + registerClassForReflection(access, "com.google.common.util.concurrent.AbstractFuture"); |
| 115 | + |
| 116 | + registerClassForReflection(access, "com.google.common.util.concurrent.AbstractFuture$Waiter"); |
| 117 | + } |
| 118 | +} |
0 commit comments