Skip to content

Commit

Permalink
Clean up Enhancer.execute() (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
tzaeschke authored Jul 27, 2023
1 parent 470883f commit a16b43b
Showing 1 changed file with 176 additions and 139 deletions.
315 changes: 176 additions & 139 deletions exectck/src/main/java/org/apache/jdo/exectck/Enhance.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,163 +133,200 @@ public void execute() throws MojoExecutionException, MojoFailureException {

String[] metadataExtensions = {"jdo", "jdoquery", "orm", "xml", "properties"};
String[] srcDirs = {"jdo", "orm", "testdata"};
File toFile = null;
File fromFile = null;
String fromFileName = null;
String pkgName = null;
int startIdx = -1;
Iterator<File> fi = null;
String[] classArray = new String[10];
String enhancedIdDirName = null;
String classesDirName = buildDirectory + File.separator + "classes" + File.separator;
ArrayList<String> classes = null;

// Copy metadata from src to enhanced
for (String idtype : idtypes) {
String enhancedIdDirName = enhancedDirName + idtype + File.separator;
for (String srcDir : srcDirs) {
String srcDirName = srcDirectory + File.separator + srcDir;
// iterator over list of abs name of metadata files in src
fi = FileUtils.iterateFiles(new File(srcDirName), metadataExtensions, true);

while (fi.hasNext()) {
try {
fromFile = fi.next();
fromFileName = fromFile.toString();
if ((startIdx = fromFileName.indexOf(idtype + File.separator)) > -1) {
// fully specified name of file (idtype + package + filename)
pkgName = fromFileName.substring(startIdx);
toFile = new File(enhancedDirName + File.separator + pkgName);
FileUtils.copyFile(fromFile, toFile);
} else if (srcDir.equals("testdata")) {
startIdx = fromFileName.indexOf("org" + File.separator);
pkgName = fromFileName.substring(startIdx);
toFile =
new File(enhancedDirName + File.separator + idtype + File.separator + pkgName);
FileUtils.copyFile(fromFile, toFile);
} else {
// idtype not in pathname, do not copy
}
} catch (IOException ex) {
throw new MojoExecutionException(
"Failed to copy files from "
+ fromFileName
+ " to "
+ toFile.toString()
+ ": "
+ ex.getLocalizedMessage());
}
}

// Copy pc and pa classes from target/classes to enhanced
String[] extensions = {"class"};
enhancedIdDirName = enhancedDirName + idtype + File.separator;
classes = new ArrayList<>();
for (String pcPkgName : PC_PKG_DIRS) {
// iterator over list of abs name of class files in target/classes
fi = FileUtils.iterateFiles(new File(classesDirName + pcPkgName), extensions, true);
while (fi.hasNext()) {
try {
fromFile = fi.next();
fromFileName = fromFile.toString();
// fully specified name of file (package + filename)
int index = fromFileName.indexOf(pcPkgName);
if (index == -1) {
throw new MojoExecutionException(
"Cannot get index of package path "
+ pcPkgName
+ " in file name"
+ fromFileName);
}
toFile = new File(enhancedIdDirName + fromFileName.substring(index));
FileUtils.copyFile(fromFile, toFile);
classes.add(toFile.toString());
} catch (IOException ex) {
throw new MojoExecutionException(
"Failed to copy files from "
+ fromFileName
+ " to "
+ toFile.toString()
+ ": "
+ ex.getLocalizedMessage());
}
}
}
copyMetadata(idtype, srcDir, metadataExtensions, enhancedDirName);
}
// Copy pc and pa classes from target/classes to enhanced
ArrayList<String> classes = copyPClasses(classesDirName, enhancedIdDirName);

// Enhance classes

// Build ClassLoader for finding enhancer
URL[] classPathURLs1 = new URL[2];
ArrayList<URL> cpList1 = new ArrayList<>();
ClassLoader enhancerLoader = null;
try {
// Must add enhancedIdDirName first!!
cpList1.add((new File(enhancedIdDirName)).toURI().toURL());
cpList1.add((new File(classesDirName)).toURI().toURL());
for (String dependency : this.dependencyClasspath.split(File.pathSeparator)) {
cpList1.add(new File(dependency).toURI().toURL());
ClassLoader enhancerLoader = buildClassLoader(classesDirName, enhancedIdDirName);

ClassLoader prevCl = Thread.currentThread().getContextClassLoader();
try (URLClassLoader loggingPropsCl =
insertLog4jClassLoader(classesDirName, enhancedIdDirName)) {
// Context classloader for finding log4j2 configuration
if (loggingPropsCl != null) {
Thread.currentThread().setContextClassLoader(loggingPropsCl);
}
enhancerLoader =
new URLClassLoader(cpList1.toArray(classPathURLs1), getClass().getClassLoader());
System.out.println("ClassLoader enhancerLoader:");
Utilities.printClasspath(enhancerLoader);
} catch (MalformedURLException ex) {
Logger.getLogger(Enhance.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("ClassLoader ContextClassLoader:");
Utilities.printClasspath(Thread.currentThread().getContextClassLoader());
System.out.println("Get enhancer");
JDOEnhancer enhancer = JDOHelper.getEnhancer(enhancerLoader);
System.out.println("enhancer.setVerbose()");
enhancer.setVerbose(true);
System.out.println("enhancer.setClassLoader()");
enhancer.setClassLoader(enhancerLoader);
String[] classArr = classes.toArray(new String[0]);
enhancer.addClasses(classArr);
System.out.println("Enhancing classes for identity type " + idtype);
// enhancer needs org/apache/jdo/tck/util/DeepEquality
enhancer.enhance();
Thread.currentThread().setContextClassLoader(prevCl);
} catch (IOException e) {
Logger.getLogger(Enhance.class.getName())
.log(Level.SEVERE, "Failed to create log4j resource", e);
}

// Context classloader for finding log4j2 configuration
ClassLoader prevCl = Thread.currentThread().getContextClassLoader();
// Move log to per-test location
moveLogs(idtype);
}
System.out.println();
}

private void copyMetadata(
String idType, String srcDir, String[] metadataExtensions, String enhancedDirName)
throws MojoExecutionException {
// Copy metadata from src to enhanced
String srcDirName = srcDirectory + File.separator + srcDir;
// iterator over list of abs name of metadata files in src
Iterator<File> fi = FileUtils.iterateFiles(new File(srcDirName), metadataExtensions, true);

while (fi.hasNext()) {
String fromFileName = null;
String toFileName = null;
try {
URL enhancedClassesUrl = (new File(enhancedIdDirName)).toURI().toURL();
// Classes dir needed for org.apache.jdo.tck.util.TCKFileAppender
URL classesUrl = (new File(classesDirName)).toURI().toURL();
ClassLoader loggingPropsCl =
URLClassLoader.newInstance(new URL[] {enhancedClassesUrl, classesUrl}, prevCl);
Thread.currentThread().setContextClassLoader(loggingPropsCl);
} catch (Exception e) {
e.printStackTrace();
File fromFile = fi.next();
fromFileName = fromFile.toString();
int startIdx = -1;
if ((startIdx = fromFileName.indexOf(idType + File.separator)) > -1) {
// fully specified name of file (idtype + package + filename)
String pkgName = fromFileName.substring(startIdx);
File toFile = new File(enhancedDirName + File.separator + pkgName);
toFileName = toFile.toString();
FileUtils.copyFile(fromFile, toFile);
} else if (srcDir.equals("testdata")) {
startIdx = fromFileName.indexOf("org" + File.separator);
String pkgName = fromFileName.substring(startIdx);
File toFile =
new File(enhancedDirName + File.separator + idType + File.separator + pkgName);
toFileName = toFile.toString();
FileUtils.copyFile(fromFile, toFile);
} else {
// idType not in pathname, do not copy
}
} catch (IOException ex) {
throw new MojoExecutionException(
"Failed to copy files from "
+ fromFileName
+ " to "
+ toFileName
+ ": "
+ ex.getLocalizedMessage());
}
System.out.println("ClassLoader ContextClassLoader:");
Utilities.printClasspath(Thread.currentThread().getContextClassLoader());
System.out.println("Get enhancer");
JDOEnhancer enhancer = JDOHelper.getEnhancer(enhancerLoader);
System.out.println("enhancer.setVerbose()");
enhancer.setVerbose(true);
System.out.println("enhancer.setClassLoader()");
enhancer.setClassLoader(enhancerLoader);
String[] classArr = classes.toArray(classArray);
enhancer.addClasses(classArr);
System.out.println("Enhancing classes for identity type " + idtype);
// enhancer needs org/apache/jdo/tck/util/DeepEquality
enhancer.enhance();
Thread.currentThread().setContextClassLoader(prevCl);
}
}

// Move log to per-test location
String idname = "dsid";
if (idtype.trim().equals("applicationidentity")) {
idname = "app";
private ArrayList<String> copyPClasses(String classesDirName, String enhancedIdDirName)
throws MojoExecutionException {
// Copy pc and pa classes from target/classes to enhanced
String[] extensions = {"class"};
ArrayList<String> classes = new ArrayList<>();
for (String pcPkgName : PC_PKG_DIRS) {
// iterator over list of abs name of class files in target/classes
Iterator<File> fi =
FileUtils.iterateFiles(new File(classesDirName + pcPkgName), extensions, true);
while (fi.hasNext()) {
String fromFileName = null;
String toFileName = null;
try {
File fromFile = fi.next();
fromFileName = fromFile.toString();
// fully specified name of file (package + filename)
int index = fromFileName.indexOf(pcPkgName);
if (index == -1) {
throw new MojoExecutionException(
"Cannot get index of package path " + pcPkgName + " in file name" + fromFileName);
}
File toFile = new File(enhancedIdDirName + fromFileName.substring(index));
toFileName = toFile.toString();
FileUtils.copyFile(fromFile, toFile);
classes.add(toFile.toString());
} catch (IOException ex) {
throw new MojoExecutionException(
"Failed to copy files from "
+ fromFileName
+ " to "
+ toFileName
+ ": "
+ ex.getLocalizedMessage());
}
}
String testLogFilename =
logsDirectory
+ File.separator
+ ENHANCED_DIR_NAME
+ File.separator
+ idname
+ "-"
+ impl
+ ".txt";
System.out.println("testLogFilename is " + testLogFilename);
try {
File logFile = new File(implLogFile);
File testLogFile = new File(testLogFilename);
FileUtils.copyFile(logFile, testLogFile);
// reset file content
FileUtils.write(logFile, "", Charset.defaultCharset());
FileUtils.forceDeleteOnExit(logFile);
} catch (Exception e) {
System.out.println(">> Error moving implementation log file: " + e.getMessage());
}
return classes;
}

private ClassLoader buildClassLoader(String classesDirName, String enhancedIdDirName) {
// Enhance classes

// Build ClassLoader for finding enhancer
URL[] classPathURLs1 = new URL[2];
ArrayList<URL> cpList1 = new ArrayList<>();
ClassLoader enhancerLoader = null;
try {
// Must add enhancedIdDirName first!!
cpList1.add((new File(enhancedIdDirName)).toURI().toURL());
cpList1.add((new File(classesDirName)).toURI().toURL());
for (String dependency : this.dependencyClasspath.split(File.pathSeparator)) {
cpList1.add(new File(dependency).toURI().toURL());
}
enhancerLoader =
new URLClassLoader(cpList1.toArray(classPathURLs1), getClass().getClassLoader());
System.out.println("ClassLoader enhancerLoader:");
Utilities.printClasspath(enhancerLoader);
} catch (MalformedURLException ex) {
Logger.getLogger(Enhance.class.getName()).log(Level.SEVERE, null, ex);
}
return enhancerLoader;
}

private URLClassLoader insertLog4jClassLoader(String classesDirName, String enhancedIdDirName) {
// Context classloader for finding log4j2 configuration
ClassLoader prevCl = Thread.currentThread().getContextClassLoader();
try {
URL enhancedClassesUrl = (new File(enhancedIdDirName)).toURI().toURL();
// Classes dir needed for org.apache.jdo.tck.util.TCKFileAppender
URL classesUrl = (new File(classesDirName)).toURI().toURL();
return URLClassLoader.newInstance(new URL[] {enhancedClassesUrl, classesUrl}, prevCl);
} catch (MalformedURLException e) {
Logger.getLogger(Enhance.class.getName())
.log(Level.SEVERE, "Failed to create log4j classloader", e);
}
return null;
}

private void moveLogs(String idType) {
// Move log to per-test location
String idname = "dsid";
if (idType.trim().equals("applicationidentity")) {
idname = "app";
}
String testLogFilename =
logsDirectory
+ File.separator
+ ENHANCED_DIR_NAME
+ File.separator
+ idname
+ "-"
+ impl
+ ".txt";
System.out.println("testLogFilename is " + testLogFilename);
try {
File logFile = new File(implLogFile);
File testLogFile = new File(testLogFilename);
FileUtils.copyFile(logFile, testLogFile);
// reset file content
FileUtils.write(logFile, "", Charset.defaultCharset());
FileUtils.forceDeleteOnExit(logFile);
} catch (Exception e) {
System.out.println(">> Error moving implementation log file: " + e.getMessage());
}
System.out.println("");
}
}

0 comments on commit a16b43b

Please sign in to comment.