Commit eef63118 authored by Vincent Horváth's avatar Vincent Horváth

created application configuration interface, created generator state, fixed...

created application configuration interface, created generator state, fixed solution for transmit class loader, created solution for load custom templates and mappers
parent 85a21221
package sk.tuke.bfi.core;
public interface ApplicationConfiguration {
String getBfiProjectPath();
String getFrontendJar();
String getFrontendProjectPath();
String getFrontendResourcePath();
String getBackendJar();
String getBackendProjectPath();
String getFrontendClass();
String getBackendClass();
String getBackendResourcePath();
}
package sk.tuke.bfi.core;
public class GeneratorState {
private static GeneratorState inst;
private ClassLoader classLoader;
private ApplicationConfiguration applicationConfiguration;
private GeneratorState() {
}
public static GeneratorState getGeneratorState() {
if (inst == null) {
inst = new GeneratorState();
}
return inst;
}
public void initState(final ClassLoader classLoader, final ApplicationConfiguration applicationConfiguration) {
this.classLoader = classLoader;
this.applicationConfiguration = applicationConfiguration;
}
public ClassLoader getClassLoader() {
return classLoader;
}
public ApplicationConfiguration getApplicationConfiguration() {
return applicationConfiguration;
}
}
......@@ -15,9 +15,6 @@ import sk.tuke.bfi.core.generator.TemplateGeneratorInterface;
import sk.tuke.bfi.core.generator.annotation.Generator;
import sk.tuke.bfi.core.generator.annotation.GeneratorMethod;
import sk.tuke.bfi.core.generator.annotation.GeneratorSaveTo;
import sk.tuke.bfi.core.helper.impl.ResourceHelper;
import sk.tuke.bfi.core.preprocessor.annotation.Preprocessor;
import sk.tuke.bfi.core.preprocessor.html.HtmlPreprocessor;
import sk.tuke.bfi.core.helper.HelperFactory;
import sk.tuke.bfi.core.helper.HelperParameter;
import sk.tuke.bfi.core.helper.impl.FileHelper;
......@@ -26,6 +23,8 @@ import sk.tuke.bfi.core.helper.impl.TemplateHelper;
import sk.tuke.bfi.core.interpret.types.BackendInterface;
import sk.tuke.bfi.core.interpret.types.BaseInterface;
import sk.tuke.bfi.core.interpret.types.FrontendInterface;
import sk.tuke.bfi.core.preprocessor.annotation.Preprocessor;
import sk.tuke.bfi.core.preprocessor.html.HtmlPreprocessor;
import sk.tuke.bfi.core.stringtemplate.listener.AbstractErrorTemplateListener;
import java.io.File;
......@@ -35,8 +34,9 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import static sk.tuke.bfi.core.preprocessor.html.BfiTag.BFI;
import static sk.tuke.bfi.core.GeneratorState.getGeneratorState;
import static sk.tuke.bfi.core.helper.constant.StringConstant.EMPTY_STRING;
import static sk.tuke.bfi.core.preprocessor.html.BfiTag.BFI;
public class GeneratorImpl implements sk.tuke.bfi.core.generator.Generator {
......@@ -46,7 +46,6 @@ public class GeneratorImpl implements sk.tuke.bfi.core.generator.Generator {
private String projectRoot;
private List<Class<TemplateGeneratorInterface>> templateGeneratorInterfaceClasses = new ArrayList<>();
private boolean enableOverwrite = false;
private ClassLoader classLoader;
private FileHelper fileHelper = HelperFactory.getHelper(FileHelper.class);
private final static Logger LOGGER = Logger.getLogger(GeneratorImpl.class);
......@@ -96,7 +95,6 @@ public class GeneratorImpl implements sk.tuke.bfi.core.generator.Generator {
}
private <T> void runTemplateGenerator(Class<TemplateGeneratorInterface> generatorClass, T subject) {
ResourceHelper.classLoader=classLoader;
final Generator annGenerator = generatorClass.getAnnotation(Generator.class);
Constructor<?> ctor = null;
try {
......@@ -337,7 +335,7 @@ public class GeneratorImpl implements sk.tuke.bfi.core.generator.Generator {
.enableExternalClasses()
.enableAnnotationInfo()
.whitelistPackages(searchPackage.getName())
.addClassLoader(classLoader)
.addClassLoader(getGeneratorState().getClassLoader())
.scan();
final ClassInfoList routes =
......@@ -345,7 +343,7 @@ public class GeneratorImpl implements sk.tuke.bfi.core.generator.Generator {
final List<Class<?>> classes = new ArrayList<>();
for (String className : routes.getNames()) {
try {
classes.add(classLoader.loadClass(className));
classes.add(getGeneratorState().getClassLoader().loadClass(className));
} catch (ClassNotFoundException e) {
LOGGER.error(e.getMessage(), e);
continue;
......@@ -365,7 +363,4 @@ public class GeneratorImpl implements sk.tuke.bfi.core.generator.Generator {
LOGGER.info("Save generated template " + templateGenerator.getClass());
}
public void setClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}
}
......@@ -4,20 +4,32 @@ import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import sk.tuke.bfi.core.helper.Helper;
import sk.tuke.bfi.core.helper.HelperFactory;
import sk.tuke.bfi.core.interpret.types.BackendInterface;
import sk.tuke.bfi.core.interpret.types.FrontendInterface;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
import static java.nio.charset.StandardCharsets.UTF_8;
import static sk.tuke.bfi.core.GeneratorState.getGeneratorState;
public class ResourceHelper implements Helper {
private final static Logger LOGGER = Logger.getLogger(ResourceHelper.class);
public static ClassLoader classLoader;
private ApplicationHelper applicationHelper = HelperFactory.getHelper(ApplicationHelper.class);
private Class<?> baseInterfaceClass;
public String getContentFromFile(String path) {
public String getContentFromFile(final String path, Class<?> baseInterfaceClass) {
this.baseInterfaceClass = baseInterfaceClass;
try {
final String customConntent = getCustomResources(path);
if (customConntent != null) {
return customConntent;
}
} catch (FileNotFoundException e) {
LOGGER.error(e.getMessage(), e);
}
if (applicationHelper.isRunningWithInJar()) {
InputStream in = classLoader.getResourceAsStream(File.separator + path);
InputStream in = getGeneratorState().getClassLoader().getResourceAsStream(File.separator + path);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
try {
return org.apache.commons.io.IOUtils.toString(reader);
......@@ -26,11 +38,33 @@ public class ResourceHelper implements Helper {
}
} else {
try {
return IOUtils.toString(classLoader.getResourceAsStream(path), "UTF-8");
return IOUtils.toString(getGeneratorState().getClassLoader().getResourceAsStream(path), UTF_8.name());
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
}
return null;
}
private String getCustomResources(final String path) throws FileNotFoundException {
String resourceRoot = null;
if (FrontendInterface.class.isAssignableFrom(baseInterfaceClass)) {
resourceRoot = getGeneratorState().getApplicationConfiguration().getFrontendResourcePath();
} else if (BackendInterface.class.isAssignableFrom(baseInterfaceClass)) {
resourceRoot = getGeneratorState().getApplicationConfiguration().getBackendResourcePath();
}
if (resourceRoot == null) {
return null;
}
final File file = new File(resourceRoot + path);
if (file.exists() && file.isFile()) {
Scanner scanner = new Scanner(file, UTF_8.name());
final String content = scanner.useDelimiter("\\A").next();
scanner.close();
return content;
}
return null;
}
}
......@@ -73,7 +73,7 @@ public class TemplateHelper implements Helper {
}
public String getTemplateContent(String templatePath) {
return resourceHelper.getContentFromFile(HelperFunctions.joinString(rootTemplateDir, templatePath, HelpVariable.FILE_EXTENSIONS_SEPARATOR, TEMPLATE_EXTENSIONS));
return resourceHelper.getContentFromFile(HelperFunctions.joinString(rootTemplateDir, templatePath, HelpVariable.FILE_EXTENSIONS_SEPARATOR, TEMPLATE_EXTENSIONS), Thread.currentThread().getStackTrace().getClass());
}
public static void clearFile(String folder) {
......
......@@ -25,7 +25,7 @@ public class DataTypeMapper implements Mapper {
}
private void load(String filePath) {
final String content = resourceHelper.getContentFromFile(HelperFunctions.joinString(ROOT_PATH, filePath, StringConstant.DOT_STRING, FILE_EXTENSION));
final String content = resourceHelper.getContentFromFile(HelperFunctions.joinString(ROOT_PATH, filePath, StringConstant.DOT_STRING, FILE_EXTENSION), Thread.currentThread().getStackTrace().getClass());
if (content == null || content.length() == 0) {
LOGGER.error("Content for mapping is empty.");
......
......@@ -3,6 +3,8 @@ package sk.tuke.bfi.main.service;
import org.apache.commons.cli.*;
import org.ini4j.Ini;
import org.ini4j.IniPreferences;
import sk.tuke.bfi.core.ApplicationConfiguration;
import sk.tuke.bfi.core.GeneratorState;
import sk.tuke.bfi.core.builder.impl.LanguageBuilder;
import sk.tuke.bfi.core.generator.Generator;
import sk.tuke.bfi.core.generator.impl.GeneratorImpl;
......@@ -101,7 +103,7 @@ public class RunService {
}
private void init() throws Exception {
if(!runOptions.getCmd().hasOption(RunOptions.DEV)){
if (!runOptions.getCmd().hasOption(RunOptions.DEV)) {
classLoaderHelper.load(configuration.getBackendJar(), configuration.getFrontendJar());
}
Class[] parameters = new Class[1];
......@@ -114,8 +116,8 @@ public class RunService {
final FrontendInterface frontend = classLoaderHelper.createObject(FrontendInterface.class, configuration.getFrontendClass(), parameters, values);
generator = new GeneratorImpl(backend, frontend);
((GeneratorImpl) generator).setClassLoader(classLoaderHelper.getClassLoader());
languageBuilder = LanguageBuilder.getBuilder(configuration.getBfiProjectPath(), generator);
GeneratorState.getGeneratorState().initState(classLoaderHelper.getClassLoader(),configuration);
}
private void loadConfiguration(final String filename) throws Exception {
......@@ -173,7 +175,7 @@ public class RunService {
}
}
public class Configuration {
public class Configuration implements ApplicationConfiguration {
public static final String NODE_BFI = "bfi";
public static final String NODE_FRONTEND = "frontend";
public static final String NODE_BACKEND = "backend";
......@@ -184,10 +186,12 @@ public class RunService {
private String frontendJar;
private String frontendClass;
private String frontendProjectPath;
private String frontendResourcePath;
private String backendJar;
private String backendClass;
private String backendProjectPath;
private String backendResourcePath;
public Configuration(Preferences preferences) {
setBfiPreferences(preferences.node(NODE_BFI));
......@@ -200,72 +204,64 @@ public class RunService {
return getBackendJar() != null && getBackendProjectPath() != null && getBfiProjectPath() != null && getFrontendProjectPath() != null && getFrontendJar() != null;
}
@Override
public String getBfiProjectPath() {
return bfiProjectPath;
}
public void setBfiProjectPath(String bfiProjectPath) {
this.bfiProjectPath = bfiProjectPath;
}
@Override
public String getFrontendJar() {
return frontendJar;
}
public void setFrontendJar(String frontendJar) {
this.frontendJar = frontendJar;
}
@Override
public String getFrontendProjectPath() {
return frontendProjectPath;
}
public void setFrontendProjectPath(String frontendProjectPath) {
this.frontendProjectPath = frontendProjectPath;
}
@Override
public String getBackendJar() {
return backendJar;
}
public void setBackendJar(String backendJar) {
this.backendJar = backendJar;
}
@Override
public String getBackendProjectPath() {
return backendProjectPath;
}
public void setBackendProjectPath(String backendProjectPath) {
this.backendProjectPath = backendProjectPath;
}
@Override
public String getFrontendClass() {
return frontendClass;
}
public void setFrontendClass(String frontendClass) {
this.frontendClass = frontendClass;
}
@Override
public String getBackendClass() {
return backendClass;
}
public void setBackendClass(String backendClass) {
this.backendClass = backendClass;
@Override
public String getFrontendResourcePath() {
return frontendResourcePath;
}
@Override
public String getBackendResourcePath() {
return backendResourcePath;
}
private void setFrontendPreferences(Preferences preferences) {
frontendJar = preferences.get("jar", null);
frontendClass = preferences.get("class", null);
frontendProjectPath = preferences.get("projectPath", null);
frontendResourcePath = preferences.get("resourcePath", null);
}
private void setBackendPreferences(Preferences preferences) {
backendJar = preferences.get("jar", null);
backendClass = preferences.get("class", null);
backendProjectPath = preferences.get("projectPath", null);
backendResourcePath = preferences.get("resourcePath", null);
}
private void setBfiPreferences(Preferences preferences) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment