Skip to content
Permalink
Browse files
Add checks to verify script packaged JAR
  • Loading branch information
davecromberge committed Sep 1, 2021
1 parent dd3299b commit cbaff4525cf0d2dd0cb5dc3cfd3d3959d3fde768
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 11 deletions.
@@ -24,7 +24,7 @@
* Some field names in the VM internal class have changed in
* later versions. The appropriate class will be loaded by the class loader
* depending on the Java version that is used.
* * For more information, see: https://openjdk.java.net/jeps/238
* For more information, see: https://openjdk.java.net/jeps/238
*/
class NioBitsFields {
static String COUNT_FIELD_NAME = "count";
@@ -0,0 +1,99 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.datasketches.memory.tools.scripts;

import java.io.File;

import org.apache.datasketches.memory.MapHandle;
import org.apache.datasketches.memory.Memory;
import org.apache.datasketches.memory.WritableHandle;
import org.apache.datasketches.memory.WritableMemory;

public class CheckMemoryJar {

public void checkHeapWritableMemory() {
try {
String str = "1 - Heap WritableMemory Successful";
WritableMemory mem = WritableMemory.allocate(2 * str.length());
writeReadAndPrintString(mem, str);
} catch (Exception ex) {
exitOnError("Heap Writable Memory", ex);
}
}

public void checkAllocateDirect() throws Exception {
try {
String str = "2 - Allocate Direct Successful";
WritableHandle wh = WritableMemory.allocateDirect(2 * str.length());
WritableMemory wmem = wh.getWritable();
writeReadAndPrintString(wmem, str);
wh.close();
} catch (Exception ex) {
exitOnError("Allocate Direct", ex);
}
}

public void checkMap(String mappedFilePath) throws Exception {
try {
String str = "3 - Memory Map Successful";
File file = new File(mappedFilePath);
MapHandle mh = Memory.map(file);
Memory mem = mh.get();
mh.close();
println(str);
} catch (Exception ex) {
exitOnError("Memory Map", ex);
}
}

/**********************/

private static void writeReadAndPrintString(WritableMemory wmem, String str) {
int len = str.length();
char[] cArr1 = str.toCharArray();
wmem.putCharArray(0, cArr1, 0, len);
char[] cArr2 = new char[len];
wmem.getCharArray(0, cArr2, 0, len);
String s2 = String.valueOf(cArr2);
println(s2);
}

private static void exitOnError(String checkName, Exception ex){
println(checkName + " check failed. Error: " + ex.toString());
System.exit(1);
}

public static void main(final String[] args) throws Exception {
if (args.length < 1) {
System.out.println("Please provide the full path to the memory mapped file!");
System.exit(1);
}

String mappedFilePath = args[0];
CheckMemoryJar check = new CheckMemoryJar();
check.checkHeapWritableMemory();
check.checkAllocateDirect();
check.checkMap(mappedFilePath);
println("");
println("All checks passed.");
}

static void println(Object obj) { System.out.println(obj.toString()); }
}
@@ -38,21 +38,22 @@ if [ -z "$1" ]; then echo "Missing JDK home"; exit 1; fi
if [ -z "$2" ]; then echo "Missing Git Tag"; exit 1; fi
if [ -z "$3" ]; then echo "Missing project.basedir"; exit 1; fi

#### Extract JDKHome and Version from input parameters ####
#### Extract JDKHome, Version and ProjectBaseDir from input parameters ####
JDKHome=$1
GitTag=$2
ProjectBaseDir=$3 #this must be an absolute path

#### Setup absolute directory references ####
ProjectBaseDir=$3 #this must be an absolute path
ScriptsDir=${ProjectBaseDir}/tools/scripts/
ProjectArtifactId="memory"
ScriptsDir=${ProjectBaseDir}/tools/scripts/

#### Initialise path dependent variables ####
OutputDir=target
OutputJar=${OutputDir}/org.apache.datasketches.memory-${GitTag}.jar

PackageDir=${OutputDir}/package
PackageSrc=${PackageDir}/src
PackageTests=${PackageDir}/test-classes
PackageContents=${PackageDir}/contents
PackageMeta=${PackageContents}/META-INF
PackageManifest=${PackageMeta}/MANIFEST.MF
@@ -97,8 +98,10 @@ fi
#### Cleanup and setup output directories ####
echo
echo "--- CLEAN & COMPILE ---"
rm -r $OutputDir
if [ -d "$OutputDir" ]; then rm -r $OutputDir; fi
mkdir -p $OutputDir
mkdir -p $PackageSrc
mkdir -p $PackageTests
mkdir -p $PackageContents
mkdir -p $PackageMeta

@@ -117,7 +120,7 @@ EOF
echo "$($ScriptsDir/getGitProperties.sh $ProjectBaseDir $ProjectArtifactId $GitTag)" >> $PackageManifest

#### Copy base tree to target/src
rsync -a $MemoryJava8Src $PackageSrc
rsync -a -I $MemoryJava8Src $PackageSrc

# version too low
if [[ $JavaVersion -lt 8 ]]; then
@@ -132,7 +135,7 @@ elif [[ $JavaVersion -lt 9 ]]; then
elif [[ $JavaVersion -lt 11 ]]; then
echo "Compiling with JDK version $JavaVersion..."
#### Copy java 9 src tree to target/src, overwriting replacements
rsync -a $MemoryJava9Src $PackageSrc
rsync -a -I $MemoryJava9Src $PackageSrc
# Compile with JPMS exports
${Javac_} \
--add-exports java.base/jdk.internal.ref=org.apache.datasketches.memory \
@@ -143,9 +146,9 @@ elif [[ $JavaVersion -lt 11 ]]; then
elif [[ $JavaVersion -lt 14 ]]; then
echo "Compiling with JDK version $JavaVersion..."
#### Copy java 9 src tree to target/src, overwriting replacements
rsync -a $MemoryJava9Src $PackageSrc
rsync -a -I $MemoryJava9Src $PackageSrc
#### Copy java 11 src tree to target/src, overwriting replacements
rsync -a $MemoryJava11Src $PackageSrc
rsync -a -I $MemoryJava11Src $PackageSrc
# Compile with JPMS exports
${Javac_} \
--add-exports java.base/jdk.internal.ref=org.apache.datasketches.memory \
@@ -161,7 +164,30 @@ echo "--- JAR ---"
echo "Building JAR from ${PackageContents}..."
${Jar_} cf $OutputJar -C $PackageContents .
echo
echo "--- JAR CONTENTS ---"
${Jar_} tf ${OutputJar}
# Uncomment for debugging purposes
# echo "--- JAR CONTENTS ---"
# ${Jar_} tf ${OutputJar}
# echo
echo "--- RUN JAR CHECKS ---"
echo
MemoryMapFile=$ScriptsDir/CheckMemoryJar.java # Memory map the textual source code for the JAR checks

if [[ $JavaVersion -eq 8 ]]; then
${Javac_} -cp $OutputJar -d $PackageTests $(find $ScriptsDir -name '*.java')

${Java_} -cp $PackageTests:$OutputJar org.apache.datasketches.memory.tools.scripts.CheckMemoryJar $MemoryMapFile
else
${Javac_} \
--add-modules org.apache.datasketches.memory \
-p "$OutputJar" -d $PackageTests $(find $ScriptsDir -name '*.java')

${Java_} \
--add-modules org.apache.datasketches.memory \
--add-exports java.base/jdk.internal.misc=org.apache.datasketches.memory \
--add-exports java.base/jdk.internal.ref=org.apache.datasketches.memory \
--add-opens java.base/java.nio=org.apache.datasketches.memory \
--add-opens java.base/sun.nio.ch=org.apache.datasketches.memory \
-p $OutputJar -cp $PackageTests org.apache.datasketches.memory.tools.scripts.CheckMemoryJar $MemoryMapFile
fi
echo
echo "Successfully built ${OutputJar}"

0 comments on commit cbaff45

Please sign in to comment.