|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Oracle and/or its affiliates. |
| 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 | +package io.helidon.integrations.db.pgsql; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.ObjectInputStream; |
| 20 | +import java.io.ObjectOutputStream; |
| 21 | +import java.lang.reflect.Field; |
| 22 | +import java.util.Properties; |
| 23 | + |
| 24 | +import com.oracle.svm.core.annotate.Alias; |
| 25 | +import com.oracle.svm.core.annotate.Delete; |
| 26 | +import com.oracle.svm.core.annotate.Substitute; |
| 27 | +import com.oracle.svm.core.annotate.TargetClass; |
| 28 | +import org.postgresql.ds.common.BaseDataSource; |
| 29 | + |
| 30 | +/** |
| 31 | + * Replace serialization code. |
| 32 | + */ |
| 33 | +@TargetClass(className = "org.postgresql.ds.common.BaseDataSource") |
| 34 | +public final class BaseDataSourceSubstitution { |
| 35 | + |
| 36 | + @Alias |
| 37 | + private String[] serverNames; |
| 38 | + @Alias |
| 39 | + private String databaseName; |
| 40 | + @Alias |
| 41 | + private String user; |
| 42 | + @Alias |
| 43 | + private String password; |
| 44 | + @Alias |
| 45 | + private int[] portNumbers; |
| 46 | + @Alias |
| 47 | + private Properties properties; |
| 48 | + |
| 49 | + /* |
| 50 | + * Original PgSQL prototype uses Serialization to create deep clone of BaseDataSource |
| 51 | + * class. This replacement makes just shallow copy but it seems to be enough. |
| 52 | + * IOException is thrown to match prototype's failures. |
| 53 | + */ |
| 54 | + /** |
| 55 | + * Initialize PgSQL {@code BaseDataSource} from another instance. |
| 56 | + * |
| 57 | + * @param source source instance |
| 58 | + * @throws IOException when any issue with data copying occurs |
| 59 | + */ |
| 60 | + @Substitute |
| 61 | + public void initializeFrom(BaseDataSource source) throws IOException { |
| 62 | + final String[] serverNamesSrc = source.getServerNames(); |
| 63 | + final int[] portNumbersSrc = source.getPortNumbers(); |
| 64 | + if (serverNamesSrc != null) { |
| 65 | + serverNames = new String[serverNamesSrc.length]; |
| 66 | + for (int i = 0; i < serverNamesSrc.length; i++) { |
| 67 | + serverNames[i] = serverNamesSrc[i] != null ? serverNamesSrc[i] : null; |
| 68 | + } |
| 69 | + } else { |
| 70 | + serverNames = null; |
| 71 | + } |
| 72 | + databaseName = source.getDatabaseName(); |
| 73 | + user = source.getUser(); |
| 74 | + password = source.getPassword(); |
| 75 | + if (portNumbersSrc != null) { |
| 76 | + portNumbers = new int[portNumbersSrc.length]; |
| 77 | + System.arraycopy(portNumbersSrc, 0, portNumbers, 0, portNumbersSrc.length); |
| 78 | + } else { |
| 79 | + portNumbers = null; |
| 80 | + } |
| 81 | + try { |
| 82 | + Field propertiesField = BaseDataSource.class.getDeclaredField("properties"); |
| 83 | + boolean propertiesAcc = propertiesField.canAccess(source); |
| 84 | + propertiesField.setAccessible(true); |
| 85 | + final Properties propertiesSrc = (Properties) propertiesField.get(source); |
| 86 | + propertiesField.setAccessible(propertiesAcc); |
| 87 | + properties = new Properties(); |
| 88 | + properties.putAll(propertiesSrc); |
| 89 | + } catch (IllegalAccessException | NoSuchFieldException | SecurityException e) { |
| 90 | + throw new IOException("Could not initialize Properties class", e); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /* |
| 95 | + * Removing writeBaseObject from original. It shall not be accessible now. |
| 96 | + */ |
| 97 | + @Delete |
| 98 | + protected void writeBaseObject(ObjectOutputStream out) throws IOException { |
| 99 | + } |
| 100 | + |
| 101 | + /* |
| 102 | + * Removing readBaseObject from original. It shall not be accessible now. |
| 103 | + */ |
| 104 | + @Delete |
| 105 | + protected void readBaseObject(ObjectInputStream in) throws IOException, ClassNotFoundException { |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Creates an instance of PgSQL BaseDataSource substitution class. |
| 110 | + * For testing purposes only. |
| 111 | + */ |
| 112 | + BaseDataSourceSubstitution() { |
| 113 | + this.serverNames = null; |
| 114 | + this.databaseName = null; |
| 115 | + this.user = null; |
| 116 | + this.password = null; |
| 117 | + this.portNumbers = null; |
| 118 | + this.properties = null; |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments