Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable generator if an engine is not available at runtime #1507

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2021 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.kogito.dmn;

/**
* Marker interface used to check DMN engine availability
*/
public interface KogitoDecisions {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2021 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.kogito;

/**
* Marker interface used to check Drools engine availability
*/
public interface KogitoDrools {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2021 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.kogito.pmml;

/**
* Marker interface used to check PMML engine availability
*/
public interface KogitoPredictions {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2021 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.kogito.process;

/**
* Marker interface used to check BPMN engine availability
*/
public interface KogitoProcesses {
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
Expand All @@ -29,6 +32,7 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.kie.kogito.KogitoDrools;
import org.kie.kogito.KogitoGAV;
import org.kie.kogito.codegen.api.GeneratedFile;
import org.kie.kogito.codegen.api.Generator;
Expand All @@ -44,10 +48,27 @@
import org.kie.kogito.codegen.process.ProcessCodegen;
import org.kie.kogito.codegen.process.persistence.PersistenceGenerator;
import org.kie.kogito.codegen.rules.IncrementalRuleCodegen;
import org.kie.kogito.dmn.KogitoDecisions;
import org.kie.kogito.maven.plugin.util.MojoUtil;
import org.kie.kogito.pmml.KogitoPredictions;
import org.kie.kogito.process.KogitoProcesses;
import org.kie.kogito.serverless.workflow.KogitoServerlessWorkflow;

public abstract class AbstractKieMojo extends AbstractMojo {

protected static final Map<String, Collection<String>> engineToRuntimeClass = new HashMap<>();

static {
engineToRuntimeClass.put(IncrementalRuleCodegen.GENERATOR_NAME,
Collections.singletonList(KogitoDrools.class.getCanonicalName()));
engineToRuntimeClass.put(ProcessCodegen.GENERATOR_NAME,
Arrays.asList(KogitoProcesses.class.getCanonicalName(), KogitoServerlessWorkflow.class.getCanonicalName()));
engineToRuntimeClass.put(PredictionCodegen.GENERATOR_NAME,
Collections.singletonList(KogitoPredictions.class.getCanonicalName()));
engineToRuntimeClass.put(DecisionCodegen.GENERATOR_NAME,
Collections.singletonList(KogitoDecisions.class.getCanonicalName()));
}

@Parameter(required = true, defaultValue = "${project.basedir}")
protected File projectDir;

Expand All @@ -67,7 +88,7 @@ public abstract class AbstractKieMojo extends AbstractMojo {
protected File generatedResources;

@Parameter(property = "kogito.codegen.persistence", defaultValue = "true")
protected boolean persistence;
protected String persistence;
danielezonca marked this conversation as resolved.
Show resolved Hide resolved

@Parameter(property = "kogito.codegen.rules", defaultValue = "true")
protected String generateRules;
Expand Down Expand Up @@ -134,11 +155,32 @@ private void additionalProperties(KogitoBuildContext context) {
}
});

context.setApplicationProperty(Generator.CONFIG_PREFIX + IncrementalRuleCodegen.GENERATOR_NAME, generateRules);
context.setApplicationProperty(Generator.CONFIG_PREFIX + ProcessCodegen.GENERATOR_NAME, generateProcesses);
context.setApplicationProperty(Generator.CONFIG_PREFIX + PredictionCodegen.GENERATOR_NAME, generatePredictions);
context.setApplicationProperty(Generator.CONFIG_PREFIX + DecisionCodegen.GENERATOR_NAME, generateDecisions);
context.setApplicationProperty(Generator.CONFIG_PREFIX + PersistenceGenerator.GENERATOR_NAME, persistence);
disableGeneratorIfNecessary(context, IncrementalRuleCodegen.GENERATOR_NAME, generateRules);
disableGeneratorIfNecessary(context, ProcessCodegen.GENERATOR_NAME, generateProcesses);
disableGeneratorIfNecessary(context, PredictionCodegen.GENERATOR_NAME, generateProcesses);
danielezonca marked this conversation as resolved.
Show resolved Hide resolved
disableGeneratorIfNecessary(context, DecisionCodegen.GENERATOR_NAME, generateProcesses);
danielezonca marked this conversation as resolved.
Show resolved Hide resolved
disableGeneratorIfNecessary(context, PersistenceGenerator.GENERATOR_NAME, generateProcesses);
danielezonca marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* A generator can be disabled explicitly with a property or if the corresponding runtime marker class is not available
*/
private void disableGeneratorIfNecessary(KogitoBuildContext context, String generatorName, String enableProperty) {
if ("false".equalsIgnoreCase(enableProperty) || !engineToRuntimeClass.containsKey(generatorName)) {
ricardozanini marked this conversation as resolved.
Show resolved Hide resolved
context.setApplicationProperty(Generator.CONFIG_PREFIX + generatorName, enableProperty);
return;
}

Collection<String> runtimeMarkerClasses = engineToRuntimeClass.get(generatorName);
for (String runtimeMarkerClass : runtimeMarkerClasses) {
if (context.hasClassAvailable(runtimeMarkerClass)) {
context.setApplicationProperty(Generator.CONFIG_PREFIX + generatorName, "true");
return;
}
}

getLog().info(String.format("Disabling '%s' generator because runtime classes not available (%s)", generatorName, String.join(",", runtimeMarkerClasses)));
context.setApplicationProperty(Generator.CONFIG_PREFIX + generatorName, "false");
}

private KogitoBuildContext.Builder contextBuilder() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2021 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.kogito.serverless.workflow;

/**
* Marker interface used to check SW engine availability
*/
public interface KogitoServerlessWorkflow {
}