Skip to content

Commit

Permalink
Refactored compilers
Browse files Browse the repository at this point in the history
  • Loading branch information
frank-a-otc committed Nov 19, 2023
1 parent ad71146 commit 81dc1d7
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 193 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.otcframework.compiler;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.otcframework.common.config.OtcConfig;
import org.otcframework.common.dto.RegistryDto;
import org.otcframework.common.util.OtcUtils;
import org.otcframework.compiler.exception.OtcCompilerException;

import java.io.FileOutputStream;
import java.io.IOException;

abstract class AbstractCompiler {

protected static final String OTC_TMD_LOCATION = OtcConfig.getOtcTmdDirectoryPath();

protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

/**
* Creates the registration file.
*
* @param registryDto the registry dto
*/
protected void createRegistrationFile(RegistryDto registryDto) {
OtcUtils.creteDirectory(OTC_TMD_LOCATION);
try (FileOutputStream fos = new FileOutputStream(registryDto.registryFileName)) {
String str = OBJECT_MAPPER.writeValueAsString(registryDto);
fos.write(str.getBytes());
fos.flush();
} catch (IOException e) {
throw new OtcCompilerException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,4 @@ public interface OtcsCompiler {
*/
List<CompilationReport> compileOtcsFiles();

/**
* Compile source code.
*/
void compileSourceCode();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
*/
package org.otcframework.compiler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.otcframework.common.OtcConstants;
import org.otcframework.common.compiler.CompilationReport;
import org.otcframework.common.config.OtcConfig;
Expand All @@ -32,89 +30,44 @@
import org.otcframework.common.exception.OtcException;
import org.otcframework.common.util.CommonUtils;
import org.otcframework.common.util.OtcUtils;
import org.otcframework.compiler.command.JavaCodeStringObject;
import org.otcframework.compiler.exception.CodeGeneratorException;
import org.otcframework.compiler.exception.OtcCompilerException;
import org.otcframework.compiler.utils.CompilerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.tools.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

/**
* The Class OtcCompilerImpl.
*/
public final class OtcsCompilerImpl implements OtcsCompiler {
public final class OtcsCompilerImpl extends AbstractCompiler implements OtcsCompiler {

/** The Constant LOGGER. */
private static final Logger LOGGER = LoggerFactory.getLogger(OtcsCompilerImpl.class);

/** The Constant otcCompilerImpl. */
private static final OtcsCompiler otcsCompiler = new OtcsCompilerImpl();
private static final OtcsCompiler OTCS_COMPILER = new OtcsCompilerImpl();

/** The Constant otcCodeGenerator. */
private static final OtcCodeGenerator otcCodeGenerator = OtcCodeGeneratorImpl.getInstance();
private static final OtcCodeGenerator OTC_CODE_GENERATOR = OtcCodeGeneratorImpl.getInstance();

/** The Constant otcSrcDir. */
private static final String UNIT_TEST_LOCATION = OtcConfig.getUnitTestDirectoryPath();

/** The Constant srcDir. */
private static final String SOURCE_CODE_LOCATION = OtcConfig.getSourceCodeDirectoryPath();

/** The Constant otcTargetDir. */
private static final String OTC_TARGET_LOCATION = OtcConfig.getTargetDirectoryPath();

/** The Constant otcTmdDir. */
private static final String OTC_TMD_LOCATION = OtcConfig.getOtcTmdDirectoryPath();

/** The Constant otcFileFilter. */
private static final FileFilter OTC_FILE_FILTER = CommonUtils.createFilenameFilter(OtcConstants.OTC_SCRIPT_EXTN);

/** The Constant depFileFilter. */
private static final FileFilter TMD_FILE_FILTER = CommonUtils.createFilenameFilter(OtcConstants.OTC_TMD_EXTN);

/** The Constant objectMapper. */
private static final ObjectMapper objectMapper;

/** The Constant optionList. */
private static final List<String> optionList = new ArrayList<>();

static {
objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, false);
optionList.add("-classpath");
String otcLibLocation = OtcConfig.getOtcLibDirectoryPath();
File directory = new File(otcLibLocation);
FileFilter fileFilter = CommonUtils.createFilenameFilter(".jar");
StringBuilder otcLibClassPath = null;
for (File file : directory.listFiles(fileFilter)) {
if (otcLibClassPath == null) {
otcLibClassPath = new StringBuilder();
}
if (file.getName().endsWith(".jar") || file.getName().endsWith(".class")) {
otcLibClassPath.append(File.pathSeparator + file.getAbsolutePath());
}
}
if (otcLibClassPath == null || otcLibClassPath.length() == 0) {
optionList.add(System.getProperty("java.class.path") + File.pathSeparator + OTC_TARGET_LOCATION);
} else {
optionList.add(System.getProperty("java.class.path") + File.pathSeparator + OTC_TARGET_LOCATION
+ otcLibClassPath.toString());
}
}

/**
* Gets the single instance of OtcsCompilerImpl.
*
* @return single instance of OtcsCompilerImpl
*/
public static OtcsCompiler getInstance() {
return otcsCompiler;
return OTCS_COMPILER;
}

/**
Expand Down Expand Up @@ -195,22 +148,6 @@ private List<CompilationReport> compileAll(File directory, String otcNamespace)
return compilationReports;
}

/**
* Creates the registration file.
*
* @param registryDto the registry dto
*/
private void createRegistrationFile(RegistryDto registryDto) {
OtcUtils.creteDirectory(OTC_TMD_LOCATION);
try (FileOutputStream fos = new FileOutputStream(registryDto.registryFileName)) {
String str = objectMapper.writeValueAsString(registryDto);
fos.write(str.getBytes());
fos.flush();
} catch (IOException e) {
throw new OtcCompilerException(e);
}
}

/**
* Creates the registry dto.
*
Expand Down Expand Up @@ -314,7 +251,7 @@ private CompilationReport compileOtcFile(File file, String otcNamespace) {
message = String.format("Successfully compiled OTCS file in %s millis - OTC-Filename: %s -> %s",
((endTime - startTime) / 1000000.0), otcNamespace, otcFileName);
LOGGER.info(message);
otcCodeGenerator.generateSourcecode(otcDto);
OTC_CODE_GENERATOR.generateSourcecode(otcDto);
compilationReportBuilder.addDidSucceed(true).addOtcDto(otcDto).addMessage(message);
} catch (OtcException ex) {
LOGGER.error(ex.getMessage(), ex);
Expand All @@ -328,124 +265,4 @@ private CompilationReport compileOtcFile(File file, String otcNamespace) {
return compilationReportBuilder.build();
}

/**
* Compile source code.
*/
@Override
public void compileSourceCode() {
LOGGER.info("Compiling source-code files. Please wait.......");
long startTime = System.nanoTime();
File binDir = new File(OTC_TMD_LOCATION);
File[] files = binDir.listFiles(TMD_FILE_FILTER);
if (files == null) {
LOGGER.info("No Token-Metadata file(s) found in '{}' for registration", OTC_TMD_LOCATION);
return;
}
List<RegistryDto> registryDtos = null;
Thread.currentThread().setContextClassLoader(OtcUtils.fetchCurrentURLClassLoader());
for (File depFile : files) {
try (FileInputStream fis = new FileInputStream(depFile)) {
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
String str = new String(bytes);
RegistryDto registryDto = objectMapper.readValue(str, RegistryDto.class);
if (registryDtos == null) {
registryDtos = new ArrayList<>();
}
registryDtos.add(registryDto);
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
}
try {
createCompilationUnitsAndCompile(registryDtos, null);
} catch (OtcCompilerException e) {
LOGGER.error(e.getMessage(), e);
throw e;
}
long endTime = System.nanoTime();
LOGGER.info("Completed source-Code compilation-phase in {} millis.", ((endTime - startTime) / 1000000.0));
}

/**
* Creates the compilation units and compile.
*
* @param registryDtos the registry dtos
* @param javaFileObjects the java file objects
* @return the list
*/
private List<JavaFileObject> createCompilationUnitsAndCompile(List<RegistryDto> registryDtos,
List<JavaFileObject> javaFileObjects) {
if (registryDtos == null) {
LOGGER.info("Registry has no record of Java source-code file(s) for compilation");
return null;
}
for (RegistryDto registryDto : registryDtos) {
String mainClz = registryDto.mainClass;
String absoluteFileName = SOURCE_CODE_LOCATION + mainClz.replace(".", File.separator)
+ OtcConstants.SOURCE_CODE_EXTN;
File file = new File(absoluteFileName);
if (!file.exists()) {
throw new OtcCompilerException("", "Main-class " + mainClz + " is missing!.");
}
if (javaFileObjects == null) {
javaFileObjects = new ArrayList<>();
}
javaFileObjects.add(new JavaCodeStringObject(file));
for (CompiledInfo compiledInfo : registryDto.compiledInfos.values()) {
String factoryClassName = compiledInfo.factoryClassName;
String otcNamespace = registryDto.otcNamespace;
if (!CommonUtils.isTrimmedAndEmpty(otcNamespace) && !factoryClassName.startsWith(otcNamespace)) {
factoryClassName = otcNamespace + "." + factoryClassName;
}
absoluteFileName = SOURCE_CODE_LOCATION + File.separator + factoryClassName.replace(".", File.separator)
+ OtcConstants.SOURCE_CODE_EXTN;
file = new File(absoluteFileName);
if (!file.exists()) {
throw new OtcCompilerException("", "Factory-class " + factoryClassName + " is missing!.");
}
javaFileObjects.add(new JavaCodeStringObject(file));
}
// -- compile source-code files...
compileSourceCode(javaFileObjects, registryDto);
}
return javaFileObjects;
}

/**
* Compile source code.
*
* @param javaFileObjects the java file objects
* @param registryDto the registry dto
*/
private void compileSourceCode(List<JavaFileObject> javaFileObjects, RegistryDto registryDto) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
File fileClzPathRoot = new File(OTC_TARGET_LOCATION);
try {
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(fileClzPathRoot));
File fileSrcPathRoot = new File(SOURCE_CODE_LOCATION);
fileManager.setLocation(StandardLocation.SOURCE_OUTPUT, Arrays.asList(fileSrcPathRoot));
} catch (IOException e) {
LOGGER.error("Could not set root locations!. ", e);
}
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, optionList, null,
javaFileObjects);
if (Boolean.FALSE.equals(task.call())) {
diagnostics.getDiagnostics().forEach(diagnostic -> {
if (!registryDto.hasError && diagnostic.getCode().contains("compiler.err")) {
registryDto.hasError = true;
}
LOGGER.debug(diagnostic.toString());
});
if (registryDto.hasError) {
throw new OtcCompilerException("", "Source code compilation failed.");
}
createRegistrationFile(registryDto);
} else {
javaFileObjects.forEach(javaFile ->
LOGGER.debug("Compiled source code : {}", javaFile.getName()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.otcframework.compiler;

public interface SourceCodeCompiler {
void compileSourceCode();
}
Loading

0 comments on commit 81dc1d7

Please sign in to comment.