|
| 1 | +package io.redskap.java.aws.dynamodb.example.local.testing; |
| 2 | + |
| 3 | +import com.google.common.base.Splitter; |
| 4 | +import com.google.common.collect.Lists; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Locale; |
| 10 | +import java.util.function.Supplier; |
| 11 | + |
| 12 | +/** |
| 13 | + * Helper class for initializing AWS DynamoDB to run with sqlite4java for local testing. |
| 14 | + * |
| 15 | + * Copied from: https://github.com/redskap/aws-dynamodb-java-example-local-testing |
| 16 | + */ |
| 17 | +public class AwsDynamoDbLocalTestUtils { |
| 18 | + |
| 19 | + private static final String BASE_LIBRARY_NAME = "sqlite4java"; |
| 20 | + |
| 21 | + /** |
| 22 | + * Static helper class. |
| 23 | + */ |
| 24 | + private AwsDynamoDbLocalTestUtils() { |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Sets the sqlite4java library path system parameter if it is not set already. |
| 29 | + */ |
| 30 | + public static void initSqLite() { |
| 31 | + initSqLite(() -> { |
| 32 | + final List<String> classPath = getClassPathList(System.getProperty("java.class.path"), File.pathSeparator); |
| 33 | + |
| 34 | + final String libPath = getLibPath(System.getProperty("os.name"), System.getProperty("java.runtime.name"), |
| 35 | + System.getProperty("os.arch"), classPath); |
| 36 | + |
| 37 | + return libPath; |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Sets the sqlite4java library path system parameter if it is not set already. |
| 43 | + * |
| 44 | + * @param libPathSupplier Calculates lib path for sqlite4java. |
| 45 | + */ |
| 46 | + public static void initSqLite(Supplier<String> libPathSupplier) { |
| 47 | + if (System.getProperty("sqlite4java.library.path") == null) { |
| 48 | + System.setProperty("sqlite4java.library.path", libPathSupplier.get()); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Calculates the possible Library Names for finding the proper sqlite4j native library and returns the directory with the most specific matching library. |
| 54 | + * |
| 55 | + * @param osName The value of <code>"os.name"</code> system property (<code>System.getProperty("os.name")</code>). |
| 56 | + * @param runtimeName The value of <code>"java.runtime.name"</code> system property (<code>System.getProperty("java.runtime.name")</code>). |
| 57 | + * @param osArch The value of <code>"os.arch"</code> system property (<code>System.getProperty("os.arch")</code>). |
| 58 | + * @param osArch The classpath split into strings by path separator. Value of <code>"java.class.path"</code> system property |
| 59 | + * (<code>System.getProperty("os.arch")</code>) split by <code>File.pathSeparator</code>. |
| 60 | + * @return |
| 61 | + */ |
| 62 | + public static String getLibPath(final String osName, final String runtimeName, final String osArch, final List<String> classPath) { |
| 63 | + final String os = getOs(osName, runtimeName); |
| 64 | + final List<String> libNames = getLibNames(os, getArch(os, osArch)); |
| 65 | + |
| 66 | + for (final String libName : libNames) { |
| 67 | + for (final String classPathLib : classPath) { |
| 68 | + if (classPathLib.contains(libName)) { |
| 69 | + return new File(classPathLib).getParent(); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + throw new IllegalStateException("SQLite library \"" + libNames + "\" is missing from classpath"); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Calculates the possible Library Names for finding the proper sqlite4java native library. |
| 79 | + * |
| 80 | + * Based on the internal calculation of the sqlite4java wrapper <a href="https://bitbucket |
| 81 | + * .org/almworks/sqlite4java/src/fa4bb0fe7319a5f1afe008284146ac83e027de60/java/com/almworks/sqlite4java/Internal |
| 82 | + * .java?at=master&fileviewer=file-view-default#Internal.java-160">Internal |
| 83 | + * class</a>. |
| 84 | + * |
| 85 | + * @param os Operating System Name used by sqlite4java to get native library. |
| 86 | + * @param arch Operating System Architecture used by sqlite4java to get native library. |
| 87 | + * @return Possible Library Names used by sqlite4java to get native library. |
| 88 | + */ |
| 89 | + public static List<String> getLibNames(final String os, final String arch) { |
| 90 | + List<String> result = new ArrayList<>(); |
| 91 | + |
| 92 | + final String base = BASE_LIBRARY_NAME + "-" + os; |
| 93 | + |
| 94 | + result.add(base + "-" + arch); |
| 95 | + |
| 96 | + if (arch.equals("x86_64") || arch.equals("x64")) { |
| 97 | + result.add(base + "-amd64"); |
| 98 | + } else if (arch.equals("x86")) { |
| 99 | + result.add(base + "-i386"); |
| 100 | + } else if (arch.equals("i386")) { |
| 101 | + result.add(base + "-x86"); |
| 102 | + } else if (arch.startsWith("arm") && arch.length() > 3) { |
| 103 | + if (arch.length() > 5 && arch.startsWith("armv") && Character.isDigit(arch.charAt(4))) { |
| 104 | + result.add(base + "-" + arch.substring(0, 5)); |
| 105 | + } |
| 106 | + result.add(base + "-arm"); |
| 107 | + } |
| 108 | + |
| 109 | + result.add(base); |
| 110 | + result.add(BASE_LIBRARY_NAME); |
| 111 | + |
| 112 | + return result; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Calculates the Operating System Architecture for finding the proper sqlite4java native library. |
| 117 | + * |
| 118 | + * Based on the internal calculation of the sqlite4java wrapper <a href="https://bitbucket |
| 119 | + * .org/almworks/sqlite4java/src/fa4bb0fe7319a5f1afe008284146ac83e027de60/java/com/almworks/sqlite4java/Internal |
| 120 | + * .java?at=master&fileviewer=file-view-default#Internal.java-204">Internal |
| 121 | + * class</a>. |
| 122 | + * |
| 123 | + * @param osArch The value of <code>"os.arch"</code> system property (<code>System.getProperty("os.arch")</code>). |
| 124 | + * @param os Operating System Name used by sqlite4java to get native library. |
| 125 | + * @return Operating System Architecture used by sqlite4java to get native library. |
| 126 | + */ |
| 127 | + public static String getArch(final String os, final String osArch) { |
| 128 | + String result; |
| 129 | + |
| 130 | + if (osArch == null) { |
| 131 | + result = "x86"; |
| 132 | + } else { |
| 133 | + final String loweCaseOsArch = osArch.toLowerCase(Locale.US); |
| 134 | + result = loweCaseOsArch; |
| 135 | + if ("win32".equals(os) && "amd64".equals(loweCaseOsArch)) { |
| 136 | + result = "x64"; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return result; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Calculates the Operating System Name for finding the proper sqlite4java native library. |
| 145 | + * |
| 146 | + * Based on the internal calculation of the sqlite4java wrapper <a href="https://bitbucket |
| 147 | + * .org/almworks/sqlite4java/src/fa4bb0fe7319a5f1afe008284146ac83e027de60/java/com/almworks/sqlite4java/Internal |
| 148 | + * .java?at=master&fileviewer=file-view-default#Internal.java-219">Internal |
| 149 | + * class</a>.* |
| 150 | + * |
| 151 | + * @param osName The value of <code>"os.name"</code> system property (<code>System.getProperty("os.name")</code>). |
| 152 | + * @param runtimeName The value of <code>"java.runtime.name"</code> system property (<code>System.getProperty("java.runtime.name")</code>). |
| 153 | + * @return Operating System Name used by sqlite4java to get native library. |
| 154 | + */ |
| 155 | + public static String getOs(final String osName, final String runtimeName) { |
| 156 | + |
| 157 | + String result; |
| 158 | + if (osName == null) { |
| 159 | + result = "linux"; |
| 160 | + } else { |
| 161 | + final String loweCaseOsName = osName.toLowerCase(Locale.US); |
| 162 | + if (loweCaseOsName.startsWith("mac") || loweCaseOsName.startsWith("darwin") || loweCaseOsName.startsWith("os x")) { |
| 163 | + result = "osx"; |
| 164 | + } else if (loweCaseOsName.startsWith("windows")) { |
| 165 | + result = "win32"; |
| 166 | + } else { |
| 167 | + if (runtimeName != null && runtimeName.toLowerCase(Locale.US).contains("android")) { |
| 168 | + result = "android"; |
| 169 | + } else { |
| 170 | + result = "linux"; |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + return result; |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Splits classpath string by path separator value. |
| 180 | + * |
| 181 | + * @param classPath Value of <code>"java.class.path"</code> system property (<code>System.getProperty("os.arch")</code>). |
| 182 | + * @param pathSeparator Value of path separator (<code>File.pathSeparator</code>). |
| 183 | + * @return The list of each classpath elements. |
| 184 | + */ |
| 185 | + public static List<String> getClassPathList(final String classPath, final String pathSeparator) { |
| 186 | + return Lists.newArrayList(Splitter.on(pathSeparator).split(classPath)); |
| 187 | + } |
| 188 | + |
| 189 | +} |
0 commit comments