Skip to content

Commit

Permalink
DROOLS-3833: Ensure CDI annotations are only generated in Maven plugin (
Browse files Browse the repository at this point in the history
  • Loading branch information
evacchi authored and mariofusco committed May 3, 2019
1 parent e68564f commit af8cbfb
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@

public class ModelWriter {

private boolean hasCdi;

public ModelWriter withCdi() {
this.hasCdi = true;
return this;
}

public Result writeModel(MemoryFileSystem srcMfs, Collection<PackageModel> packageModels) {
List<String> sourceFiles = new ArrayList<>();
List<String> modelFiles = new ArrayList<>();
Expand Down Expand Up @@ -53,7 +60,7 @@ public Result writeModel(MemoryFileSystem srcMfs, Collection<PackageModel> packa

String sourceName = "src/main/java/" + folderName + "/" + DOMAIN_CLASSESS_METADATA_FILE_NAME + pkgModel.getPackageUUID() + ".java";
addSource( srcMfs, sourceFiles, pkgModel, sourceName, pkgModel.getDomainClassesMetadataSource() );
pkgModel.getModuleGenerator().write(srcMfs);
pkgModel.getModuleGenerator().withCdi(hasCdi).write(srcMfs);
}

return new Result(sourceFiles, modelFiles);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@

import org.drools.compiler.compiler.io.memory.MemoryFileSystem;
import org.drools.model.Model;
import org.drools.modelcompiler.builder.generator.CdiContainers;
import org.kie.api.KieBase;
import org.kie.api.builder.model.KieBaseModel;
import org.kie.api.runtime.KieSession;

public class ProjectSourceClass {

final KieModuleModelMethod modelMethod;
private boolean hasCdi;

public ProjectSourceClass(KieModuleModelMethod modelMethod) {
this.modelMethod = modelMethod;
}

public ProjectSourceClass withCdi() {
this.hasCdi = true;
return this;
}

public String generate() {
StringBuilder sb = new StringBuilder();
sb.append(
Expand All @@ -25,7 +30,7 @@ public String generate() {
"import " + KieBaseModel.class.getCanonicalName() + ";\n" +
"import " + KieSession.class.getCanonicalName() + ";\n" +
"\n" +
( CdiContainers.isRunningInContainer() ? "@javax.enterprise.context.ApplicationScoped\n" : "" ) +
( hasCdi ? "@javax.enterprise.context.ApplicationScoped\n" : "" ) +
"public class ProjectRuntime implements org.drools.modelcompiler.KieRuntimeBuilder {\n" +
"\n");
sb.append(modelMethod.getConstructor());
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class ModuleSourceClass {
private final List<RuleUnitSourceClass> ruleUnits;
private final List<RuleUnitInstanceSourceClass> ruleUnitInstances;
private String targetTypeName;
private boolean hasCdi;

public ModuleSourceClass() {
this.packageName = "org.drools.project.model";
Expand All @@ -43,7 +44,7 @@ public void addRuleUnitInstance(RuleUnitInstanceSourceClass ruisc) {
}

public void write(MemoryFileSystem srcMfs) {
ruleUnits.forEach(r -> r.write(srcMfs));
ruleUnits.forEach(r -> r.withCdi(hasCdi).write(srcMfs));
ruleUnitInstances.forEach(r -> r.write(srcMfs));
srcMfs.write(completePath, generate().getBytes());
}
Expand Down Expand Up @@ -78,4 +79,9 @@ public static ClassOrInterfaceType ruleUnitType(String canonicalName) {
return new ClassOrInterfaceType(null, RuleUnit.class.getCanonicalName())
.setTypeArguments(new ClassOrInterfaceType(null, canonicalName));
}

public ModuleSourceClass withCdi(boolean hasCdi) {
this.hasCdi = hasCdi;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class RuleUnitSourceClass {
private final String canonicalName;
private final String targetCanonicalName;
private String targetTypeName;
private boolean hasCdi = false;

public RuleUnitSourceClass(String packageName, String typeName, String generatedSourceFile) {
this.packageName = packageName;
Expand Down Expand Up @@ -120,7 +121,7 @@ public ClassOrInterfaceDeclaration classDeclaration() {
ClassOrInterfaceDeclaration cls = new ClassOrInterfaceDeclaration()
.setName(targetTypeName)
.setModifiers(Modifier.Keyword.PUBLIC);
if (CdiContainers.isRunningInContainer()) {
if (hasCdi) {
cls.addAnnotation("javax.enterprise.context.ApplicationScoped");
}
String ruleUnitInstanceFQCN = RuleUnitInstanceSourceClass.qualifiedName(packageName, typeName);
Expand All @@ -130,4 +131,9 @@ public ClassOrInterfaceDeclaration classDeclaration() {
.addMember(methodDeclaration);
return cls;
}

public RuleUnitSourceClass withCdi(boolean hasCdi) {
this.hasCdi = true;
return this;
}
}
25 changes: 0 additions & 25 deletions kie-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,35 +122,10 @@
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<exclusions>
<exclusion>
<groupId>org.drools</groupId>
<artifactId>drools-core-dynamic</artifactId>
</exclusion>
<exclusion>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
</exclusion>
</exclusions>

</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core-static</artifactId>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-model-compiler</artifactId>
<exclusions>
<exclusion>
<groupId>org.drools</groupId>
<artifactId>drools-core-dynamic</artifactId>
</exclusion>
<exclusion>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ public void writeProjectOutput(MemoryFileSystem trgMfs, ResultsImpl messages) {
getInternalKieModule().getReleaseId(), modelMethod, generatedSourceFiles)
.write(srcMfs);
new ProjectSourceClass(modelMethod)
.withCdi()
.write(srcMfs);

srcMfs.copyFolder(srcMfs.getFolder("src/main/java"), trgMfs, trgMfs.getFolder("."));
Expand Down

0 comments on commit af8cbfb

Please sign in to comment.