From 17e4a01766ce4ec1ae24ecdea163dc293067d2e9 Mon Sep 17 00:00:00 2001 From: Tiago Dolphine Date: Wed, 4 Nov 2020 10:57:26 -0300 Subject: [PATCH] inserting feature toggle for spring apis --- .../org/kie/kogito/conf/FeatureToggle.java | 2 - .../feature/DefaultFeatureToggleManager.java | 28 ++++++++++++ .../kogito/conf/feature/FeatureToggle.java | 43 +++++++++++++++++++ .../conf/feature/FeatureToggleManager.java | 22 ++++++++++ .../integration-tests-springboot/pom.xml | 2 +- .../integration-tests-springboot-it/pom.xml | 2 +- .../pom.xml | 2 +- .../kogito/codegen/TemplatedGenerator.java | 7 ++- .../codegen/process/ProcessCodegen.java | 4 +- kogito-springboot/pom.xml | 16 ++++--- 10 files changed, 114 insertions(+), 14 deletions(-) delete mode 100644 api/kogito-api/src/main/java/org/kie/kogito/conf/FeatureToggle.java create mode 100644 api/kogito-api/src/main/java/org/kie/kogito/conf/feature/DefaultFeatureToggleManager.java create mode 100644 api/kogito-api/src/main/java/org/kie/kogito/conf/feature/FeatureToggle.java create mode 100644 api/kogito-api/src/main/java/org/kie/kogito/conf/feature/FeatureToggleManager.java diff --git a/api/kogito-api/src/main/java/org/kie/kogito/conf/FeatureToggle.java b/api/kogito-api/src/main/java/org/kie/kogito/conf/FeatureToggle.java deleted file mode 100644 index 3c7fa16f563..00000000000 --- a/api/kogito-api/src/main/java/org/kie/kogito/conf/FeatureToggle.java +++ /dev/null @@ -1,2 +0,0 @@ -package org.kie.kogito.conf;public class FeatureToggle { -} diff --git a/api/kogito-api/src/main/java/org/kie/kogito/conf/feature/DefaultFeatureToggleManager.java b/api/kogito-api/src/main/java/org/kie/kogito/conf/feature/DefaultFeatureToggleManager.java new file mode 100644 index 00000000000..2dfbe020973 --- /dev/null +++ b/api/kogito-api/src/main/java/org/kie/kogito/conf/feature/DefaultFeatureToggleManager.java @@ -0,0 +1,28 @@ +/* + * Copyright 2020 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.conf.feature; + +/** + * Default implementation of the {@link FeatureToggleManager} based on system properties. + */ +public class DefaultFeatureToggleManager implements FeatureToggleManager { + + @Override + public boolean isEnabled(String featureKey) { + return Boolean.parseBoolean(System.getProperty(featureKey, Boolean.FALSE.toString())); + } +} \ No newline at end of file diff --git a/api/kogito-api/src/main/java/org/kie/kogito/conf/feature/FeatureToggle.java b/api/kogito-api/src/main/java/org/kie/kogito/conf/feature/FeatureToggle.java new file mode 100644 index 00000000000..39588d8588f --- /dev/null +++ b/api/kogito-api/src/main/java/org/kie/kogito/conf/feature/FeatureToggle.java @@ -0,0 +1,43 @@ +/* + * Copyright 2020 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.conf.feature; + +/** + * Centralize the features under development or not completed to be released, that could be enabled or disabled during + * runtime or build time. After the features are completed, the toggle could be removed along with the code checking + * this feature. + * Using this class make things easier to track the features in different points of code facilitating the removal of + * them. + */ +public class FeatureToggle { + + public static final String ENDPOINTS_SPRING_API_ENABLED = "endpoints.spring.api.enabled"; + + private final FeatureToggleManager manager; + + public FeatureToggle() { + this(new DefaultFeatureToggleManager()); + } + + public FeatureToggle(FeatureToggleManager manager) { + this.manager = manager; + } + + public boolean isEnabled(String featureKey){ + return manager.isEnabled(featureKey); + } +} \ No newline at end of file diff --git a/api/kogito-api/src/main/java/org/kie/kogito/conf/feature/FeatureToggleManager.java b/api/kogito-api/src/main/java/org/kie/kogito/conf/feature/FeatureToggleManager.java new file mode 100644 index 00000000000..68074b194c4 --- /dev/null +++ b/api/kogito-api/src/main/java/org/kie/kogito/conf/feature/FeatureToggleManager.java @@ -0,0 +1,22 @@ +/* + * Copyright 2020 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.conf.feature; + +public interface FeatureToggleManager { + + boolean isEnabled(String featureKey); +} diff --git a/integration-tests/integration-tests-springboot/pom.xml b/integration-tests/integration-tests-springboot/pom.xml index eb5c887f7fc..190fd2d617e 100644 --- a/integration-tests/integration-tests-springboot/pom.xml +++ b/integration-tests/integration-tests-springboot/pom.xml @@ -137,7 +137,7 @@ spring-web - true + true diff --git a/integration-tests/integration-tests-springboot/src/it/integration-tests-springboot-it/pom.xml b/integration-tests/integration-tests-springboot/src/it/integration-tests-springboot-it/pom.xml index 337f6065b86..5b087a492df 100644 --- a/integration-tests/integration-tests-springboot/src/it/integration-tests-springboot-it/pom.xml +++ b/integration-tests/integration-tests-springboot/src/it/integration-tests-springboot-it/pom.xml @@ -228,7 +228,7 @@ maven-surefire-plugin - ${SPRING_WEB_API_ENABLED} + ${endpoints.spring.api.enabled} ${spring.mvc.servlet.path} diff --git a/integration-tests/integration-tests-springboot/src/it/integration-tests-springboot-kafka-it/pom.xml b/integration-tests/integration-tests-springboot/src/it/integration-tests-springboot-kafka-it/pom.xml index 3acf5564ced..1eba07103c6 100644 --- a/integration-tests/integration-tests-springboot/src/it/integration-tests-springboot-kafka-it/pom.xml +++ b/integration-tests/integration-tests-springboot/src/it/integration-tests-springboot-kafka-it/pom.xml @@ -232,7 +232,7 @@ maven-surefire-plugin - ${SPRING_WEB_API_ENABLED} + ${endpoints.spring.api.enabled} ${spring.mvc.servlet.path} diff --git a/kogito-codegen/src/main/java/org/kie/kogito/codegen/TemplatedGenerator.java b/kogito-codegen/src/main/java/org/kie/kogito/codegen/TemplatedGenerator.java index 4079131115c..b58d939c62f 100644 --- a/kogito-codegen/src/main/java/org/kie/kogito/codegen/TemplatedGenerator.java +++ b/kogito-codegen/src/main/java/org/kie/kogito/codegen/TemplatedGenerator.java @@ -25,6 +25,7 @@ import org.kie.kogito.codegen.di.CDIDependencyInjectionAnnotator; import org.kie.kogito.codegen.di.DependencyInjectionAnnotator; import org.kie.kogito.codegen.di.SpringDependencyInjectionAnnotator; +import org.kie.kogito.conf.feature.FeatureToggle; import static com.github.javaparser.StaticJavaParser.parse; @@ -39,6 +40,7 @@ public class TemplatedGenerator { private DependencyInjectionAnnotator annotator; private final String targetTypeName; + private FeatureToggle featureToggle; public TemplatedGenerator( String packageName, @@ -62,6 +64,7 @@ public TemplatedGenerator( this.resourceCdi = resourceCdi; this.resourceSpring = resourceSpring; this.resourceDefault = resourceDefault; + this.featureToggle = new FeatureToggle(); } public TemplatedGenerator( @@ -121,7 +124,9 @@ private String selectResource() { } else { return resourceDefault; } - } else if (annotator instanceof CDIDependencyInjectionAnnotator) { + } else if (annotator instanceof CDIDependencyInjectionAnnotator + //can be removed after spring apis are completed + || (targetTypeName.contains("Resource") && !featureToggle.isEnabled(FeatureToggle.ENDPOINTS_SPRING_API_ENABLED))) { return resourceCdi; } else if (annotator instanceof SpringDependencyInjectionAnnotator) { return resourceSpring; diff --git a/kogito-codegen/src/main/java/org/kie/kogito/codegen/process/ProcessCodegen.java b/kogito-codegen/src/main/java/org/kie/kogito/codegen/process/ProcessCodegen.java index dfba4d97643..83692b3a931 100644 --- a/kogito-codegen/src/main/java/org/kie/kogito/codegen/process/ProcessCodegen.java +++ b/kogito-codegen/src/main/java/org/kie/kogito/codegen/process/ProcessCodegen.java @@ -62,6 +62,7 @@ import org.kie.kogito.codegen.process.events.CloudEventsMessageProducerGenerator; import org.kie.kogito.codegen.process.events.CloudEventsResourceGenerator; import org.kie.kogito.codegen.process.events.TopicsInformationResourceGenerator; +import org.kie.kogito.conf.feature.FeatureToggle; import org.kie.kogito.rules.units.UndefinedGeneratedRuleUnitVariable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -194,7 +195,8 @@ public ProcessCodegen(Collection processes) { //FIXME: once all endpoint generators are implemented it should be changed to ResourceGeneratorFactory, to // consider Spring generators. - resourceGeneratorFactory = new ResourceGeneratorFactory(); + resourceGeneratorFactory = new FeatureToggle().isEnabled(FeatureToggle.ENDPOINTS_SPRING_API_ENABLED) ? + new ResourceGeneratorFactory() : new DefaultResourceGeneratorFactory(); } public static String defaultWorkItemHandlerConfigClass(String packageName) { diff --git a/kogito-springboot/pom.xml b/kogito-springboot/pom.xml index 00f22aa8641..6cb02410430 100644 --- a/kogito-springboot/pom.xml +++ b/kogito-springboot/pom.xml @@ -44,19 +44,21 @@ - + + + + + + + - org.springframework.boot - spring-boot-starter-web + org.jboss.resteasy + resteasy-spring-boot-starter - - - - org.kie.kogito kogito-api