use of org.kie.memorycompiler.resources.KiePath in project drools by kiegroup.
the class KieBuilderImpl method compileJavaClasses.
private void compileJavaClasses(ClassLoader classLoader, Predicate<String> classFilter) {
List<String> classFiles = new ArrayList<>();
for (KiePath filePath : srcMfs.getFilePaths()) {
if (filePath.endsWith(".class")) {
trgMfs.write(filePath, getResource(srcMfs, filePath), true);
classFiles.add(filePath.substring(0, filePath.asString().length() - ".class".length()).asString());
}
}
List<String> javaFiles = new ArrayList<>();
List<String> javaTestFiles = new ArrayList<>();
for (KiePath filePath : srcMfs.getFilePaths()) {
String fileName = filePath.asString();
if (isJavaSourceFile(fileName) && noClassFileForGivenSourceFile(classFiles, fileName) && notVetoedByFilter(classFilter, fileName)) {
if (!fileName.startsWith(JAVA_ROOT) && !fileName.startsWith(JAVA_TEST_ROOT)) {
results.addMessage(Level.WARNING, fileName, "Found Java file out of the Java source folder: \"" + fileName + "\"");
} else if (fileName.substring(JAVA_ROOT.length()).indexOf('/') < 0) {
results.addMessage(Level.ERROR, fileName, "A Java class must have a package: " + fileName.substring(JAVA_ROOT.length()) + " is not allowed");
} else {
if (fileName.startsWith(JAVA_ROOT)) {
javaFiles.add(fileName);
} else {
javaTestFiles.add(fileName);
}
}
}
}
if (!javaFiles.isEmpty() || !javaTestFiles.isEmpty()) {
KnowledgeBuilderConfigurationImpl kconf = new KnowledgeBuilderConfigurationImpl(classLoader);
JavaConfiguration javaConf = (JavaConfiguration) kconf.getDialectConfiguration("java");
compileJavaClasses(javaConf, classLoader, javaFiles, JAVA_ROOT);
compileJavaClasses(javaConf, classLoader, javaTestFiles, JAVA_TEST_ROOT);
}
}
use of org.kie.memorycompiler.resources.KiePath in project drools by kiegroup.
the class CanonicalModelKieProject method writeProjectOutput.
@Override
public void writeProjectOutput(MemoryFileSystem trgMfs, BuildContext buildContext) {
MemoryFileSystem srcMfs = new MemoryFileSystem();
ModelWriter modelWriter = new ModelWriter();
Collection<String> modelFiles = new HashSet<>();
Collection<String> sourceFiles = new HashSet<>();
Map<String, List<String>> modelsByKBase = new HashMap<>();
for (Map.Entry<String, ModelBuilderImpl> modelBuilder : modelBuilders.entrySet()) {
ModelWriter.Result result = modelWriter.writeModel(srcMfs, modelBuilder.getValue().getPackageSources());
modelFiles.addAll(result.getModelFiles());
sourceFiles.addAll(result.getSourceFiles());
List<String> modelFilesForKieBase = new ArrayList<>();
modelFilesForKieBase.addAll(result.getModelFiles());
modelFilesForKieBase.addAll(((CanonicalModelBuildContext) buildContext).getNotOwnedModelFiles(modelBuilders, modelBuilder.getKey()));
modelsByKBase.put(modelBuilder.getKey(), modelFilesForKieBase);
}
InternalKieModule kieModule = getInternalKieModule();
ModelSourceClass modelSourceClass = new ModelSourceClass(kieModule.getReleaseId(), kieModule.getKieModuleModel().getKieBaseModels(), modelsByKBase, hasDynamicClassLoader());
String projectSourcePath = modelWriter.getBasePath().asString() + "/" + modelSourceClass.getName();
srcMfs.write(projectSourcePath, modelSourceClass.generate().getBytes());
sourceFiles.add(projectSourcePath);
Set<KiePath> origFileNames = new HashSet<>(trgMfs.getFilePaths());
String[] sources = sourceFiles.toArray(new String[sourceFiles.size()]);
if (sources.length != 0) {
CompilationResult res = getCompiler().compile(sources, srcMfs, trgMfs, getClassLoader());
Stream.of(res.getErrors()).collect(groupingBy(CompilationProblem::getFileName)).forEach((name, errors) -> {
errors.forEach(m -> buildContext.getMessages().addMessage(new CompilationProblemAdapter(m)));
File srcFile = srcMfs.getFile(name);
if (srcFile instanceof MemoryFile) {
String src = new String(srcMfs.getFileContents((MemoryFile) srcFile));
buildContext.getMessages().addMessage(Message.Level.ERROR, name, "Java source of " + name + " in error:\n" + src);
}
});
for (CompilationProblem problem : res.getWarnings()) {
buildContext.getMessages().addMessage(new CompilationProblemAdapter(problem));
}
}
if (ProjectClassLoader.isEnableStoreFirst()) {
Set<KiePath> generatedClassPaths = new HashSet<>(trgMfs.getFilePaths());
generatedClassPaths.removeAll(origFileNames);
Set<String> generatedClassNames = generatedClassPaths.stream().map(KiePath::asString).map(ClassUtils::convertResourceToClassName).collect(Collectors.toSet());
modelWriter.writeGeneratedClassNamesFile(generatedClassNames, trgMfs, getInternalKieModule().getReleaseId());
}
modelWriter.writeModelFile(modelFiles, trgMfs, getInternalKieModule().getReleaseId());
}
use of org.kie.memorycompiler.resources.KiePath in project drools by kiegroup.
the class EclipseJavaCompiler method compile.
public CompilationResult compile(final String[] pSourceFiles, final ResourceReader pReader, final ResourceStore pStore, final ClassLoader pClassLoader, final JavaCompilerSettings pSettings) {
final Collection problems = new ArrayList();
final ICompilationUnit[] compilationUnits = new ICompilationUnit[pSourceFiles.length];
for (int i = 0; i < compilationUnits.length; i++) {
final KiePath sourceFile = KiePath.of(pSourceFiles[i]);
if (pReader.isAvailable(sourceFile)) {
compilationUnits[i] = new CompilationUnit(pReader, sourceFile.asString());
} else {
// log.error("source not found " + sourceFile);
final CompilationProblem problem = new CompilationProblem() {
public int getEndColumn() {
return 0;
}
public int getEndLine() {
return 0;
}
public String getFileName() {
return sourceFile.asString();
}
public String getMessage() {
return "Source " + sourceFile.asString() + " could not be found";
}
public int getStartColumn() {
return 0;
}
public int getStartLine() {
return 0;
}
public boolean isError() {
return true;
}
public String toString() {
return getMessage();
}
};
problems.add(problem);
}
}
if (problems.size() > 0) {
final CompilationProblem[] result = new CompilationProblem[problems.size()];
problems.toArray(result);
return new CompilationResult(result);
}
final IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.proceedWithAllProblems();
final IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault());
final INameEnvironment nameEnvironment = new INameEnvironment() {
public NameEnvironmentAnswer findType(final char[][] pCompoundTypeName) {
final StringBuilder result = new StringBuilder();
for (int i = 0; i < pCompoundTypeName.length; i++) {
if (i != 0) {
result.append('.');
}
result.append(pCompoundTypeName[i]);
}
return findType(result.toString());
}
public NameEnvironmentAnswer findType(final char[] pTypeName, final char[][] pPackageName) {
final StringBuilder result = new StringBuilder();
for (char[] chars : pPackageName) {
result.append(chars);
result.append('.');
}
result.append(pTypeName);
return findType(result.toString());
}
private NameEnvironmentAnswer findType(final String pClazzName) {
final String resourceName = ClassUtils.convertClassToResourcePath(pClazzName);
final byte[] clazzBytes = pStore.read(resourceName);
if (clazzBytes != null) {
try {
return createNameEnvironmentAnswer(pClazzName, clazzBytes);
} catch (final ClassFormatException e) {
throw new RuntimeException("ClassFormatException in loading class '" + pClazzName + "' with JCI.");
}
}
try (InputStream is = pClassLoader.getResourceAsStream(resourceName)) {
if (is == null) {
return null;
}
if (ClassUtils.isCaseSenstiveOS()) {
// check it really is a class, this issue is due to windows case sensitivity issues for the class org.kie.Process and path org/droosl/process
try {
pClassLoader.loadClass(pClazzName);
} catch (ClassNotFoundException | NoClassDefFoundError e) {
return null;
}
}
final byte[] buffer = new byte[8192];
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(buffer.length)) {
int count;
while ((count = is.read(buffer, 0, buffer.length)) > 0) {
baos.write(buffer, 0, count);
}
baos.flush();
return createNameEnvironmentAnswer(pClazzName, baos.toByteArray());
}
} catch (final IOException e) {
throw new RuntimeException("could not read class", e);
} catch (final ClassFormatException e) {
throw new RuntimeException("wrong class format", e);
}
}
private NameEnvironmentAnswer createNameEnvironmentAnswer(final String pClazzName, final byte[] clazzBytes) throws ClassFormatException {
final char[] fileName = pClazzName.toCharArray();
final ClassFileReader classFileReader = new ClassFileReader(clazzBytes, fileName, true);
return new NameEnvironmentAnswer(classFileReader, null);
}
private boolean isSourceAvailable(final String pClazzName, final ResourceReader pReader) {
// FIXME: this should not be tied to the extension
final String javaSource = pClazzName.replace('.', '/') + ".java";
final String classSource = pClazzName.replace('.', '/') + ".class";
return pReader.isAvailable(sourceFolder + javaSource) || pReader.isAvailable(sourceFolder + classSource);
}
private boolean isPackage(final String pClazzName) {
try (InputStream is = pClassLoader.getResourceAsStream(ClassUtils.convertClassToResourcePath(pClazzName))) {
if (is != null) {
if (ClassUtils.isWindows() || ClassUtils.isOSX()) {
try {
Class cls = pClassLoader.loadClass(pClazzName);
if (cls != null) {
return false;
}
} catch (ClassNotFoundException | NoClassDefFoundError e) {
return true;
}
}
}
return is == null && !isSourceAvailable(pClazzName, pReader);
} catch (IOException e) {
throw new RuntimeException("Cannot open or close resource stream!", e);
}
}
public boolean isPackage(char[][] parentPackageName, char[] pPackageName) {
final StringBuilder result = new StringBuilder();
if (parentPackageName != null) {
for (int i = 0; i < parentPackageName.length; i++) {
if (i != 0) {
result.append('.');
}
result.append(parentPackageName[i]);
}
}
if (parentPackageName != null && parentPackageName.length > 0) {
result.append('.');
}
result.append(pPackageName);
return isPackage(result.toString());
}
public void cleanup() {
}
};
final ICompilerRequestor compilerRequestor = pResult -> {
if (pResult.hasProblems()) {
final IProblem[] iproblems = pResult.getProblems();
for (final IProblem iproblem : iproblems) {
final CompilationProblem problem = new EclipseCompilationProblem(iproblem);
problems.add(problem);
}
}
if (!pResult.hasErrors()) {
final ClassFile[] clazzFiles = pResult.getClassFiles();
for (final ClassFile clazzFile : clazzFiles) {
final char[][] compoundName = clazzFile.getCompoundName();
final StringBuilder clazzName = new StringBuilder();
for (int j = 0; j < compoundName.length; j++) {
if (j != 0) {
clazzName.append('.');
}
clazzName.append(compoundName[j]);
}
pStore.write(clazzName.toString().replace('.', '/') + ".class", clazzFile.getBytes());
}
}
};
final Map settingsMap = new EclipseJavaCompilerSettings(pSettings).toNativeSettings();
CompilerOptions compilerOptions = new CompilerOptions(settingsMap);
compilerOptions.parseLiteralExpressionsAsConstants = false;
final Compiler compiler = new Compiler(nameEnvironment, policy, compilerOptions, compilerRequestor, problemFactory);
if (ClassBuilderFactory.DUMP_GENERATED_CLASSES) {
dumpUnits(compilationUnits, pReader);
}
compiler.compile(compilationUnits);
final CompilationProblem[] result = new CompilationProblem[problems.size()];
problems.toArray(result);
return new CompilationResult(result);
}
use of org.kie.memorycompiler.resources.KiePath in project drools by kiegroup.
the class MemoryFileSystem method remove.
public boolean remove(File file) {
if (file.exists()) {
KiePath filePath = file.getPath();
fileContents.remove(filePath);
if (modifiedFilesSinceLastMark != null) {
modifiedFilesSinceLastMark.add(filePath.asString());
}
folders.get(((MemoryFile) file).getFolder().getPath()).remove(file);
return true;
} else {
return false;
}
}
use of org.kie.memorycompiler.resources.KiePath in project drools by kiegroup.
the class ClasspathKieProject method discoverKieModules.
public void discoverKieModules() {
KiePath[] configFiles = { KieModuleModelImpl.KMODULE_JAR_PATH, KieModuleModelImpl.KMODULE_SPRING_JAR_PATH };
for (KiePath configFile : configFiles) {
final Set<URL> resources = new HashSet<>();
try {
ClassLoader currentClassLoader = classLoader;
while (currentClassLoader != null) {
Enumeration<URL> list = currentClassLoader.getResources(configFile.asString());
while (list.hasMoreElements()) {
resources.add(list.nextElement());
}
currentClassLoader = currentClassLoader.getParent();
}
} catch (IOException exc) {
log.error("Unable to find and build index of " + configFile.asString() + "." + exc.getMessage());
return;
}
// Map of kmodule urls
for (URL url : resources) {
notifyKieModuleFound(url);
try {
InternalKieModule kModule = fetchKModule(url);
if (kModule != null) {
ReleaseId releaseId = kModule.getReleaseId();
kieModules.put(releaseId, kModule);
log.debug("Discovered classpath module " + releaseId.toExternalForm());
kieRepository.addKieModule(kModule);
}
} catch (Exception exc) {
log.error("Unable to build index of kmodule.xml url=" + url.toExternalForm() + "\n" + exc.getMessage());
}
}
}
}
Aggregations