Skip to content

Commit

Permalink
inserting feature toggle for spring apis
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagodolphine committed Nov 16, 2020
1 parent f8894b9 commit 17e4a01
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 14 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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()));
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 1 addition & 1 deletion integration-tests/integration-tests-springboot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
<profile>spring-web</profile>
</profiles>
<properties>
<SPRING_WEB_API_ENABLED>true</SPRING_WEB_API_ENABLED>
<endpoints.spring.api.enabled>true</endpoints.spring.api.enabled>
<spring.mvc.servlet.path></spring.mvc.servlet.path>
</properties>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemProperties>
<SPRING_WEB_API_ENABLED>${SPRING_WEB_API_ENABLED}</SPRING_WEB_API_ENABLED>
<endpoints.spring.api.enabled>${endpoints.spring.api.enabled}</endpoints.spring.api.enabled>
<spring.mvc.servlet.path>${spring.mvc.servlet.path}</spring.mvc.servlet.path>
</systemProperties>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemProperties>
<SPRING_WEB_API_ENABLED>${SPRING_WEB_API_ENABLED}</SPRING_WEB_API_ENABLED>
<endpoints.spring.api.enabled>${endpoints.spring.api.enabled}</endpoints.spring.api.enabled>
<spring.mvc.servlet.path>${spring.mvc.servlet.path}</spring.mvc.servlet.path>
</systemProperties>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -39,6 +40,7 @@ public class TemplatedGenerator {

private DependencyInjectionAnnotator annotator;
private final String targetTypeName;
private FeatureToggle featureToggle;

public TemplatedGenerator(
String packageName,
Expand All @@ -62,6 +64,7 @@ public TemplatedGenerator(
this.resourceCdi = resourceCdi;
this.resourceSpring = resourceSpring;
this.resourceDefault = resourceDefault;
this.featureToggle = new FeatureToggle();
}

public TemplatedGenerator(
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -194,7 +195,8 @@ public ProcessCodegen(Collection<? extends Process> 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) {
Expand Down
16 changes: 9 additions & 7 deletions kogito-springboot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,21 @@
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>

<dependencies>
<!-- FIXME: spring-boot-starter-web should be the default, it should be removed the resteasy-spring-boot-starter
after all the Endpoint generation are completed to use Spring Web API -->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-web</artifactId>-->
<!-- </dependency>-->

<!-- this should be removed when all endpoints for spring have been migrated to spring-web apis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-spring-boot-starter</artifactId>
</dependency>

<!-- <dependency>-->
<!-- <groupId>org.jboss.resteasy</groupId>-->
<!-- <artifactId>resteasy-spring-boot-starter</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-api</artifactId>
Expand Down

0 comments on commit 17e4a01

Please sign in to comment.