I am trying to implement togglz with my spring boot application. Here my approach towards it is in two ways.
Firstly below are the common files for my both usecase approaches.
ToggleController.java
package com.learn.poc.toggle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.togglz.core.manager.FeatureManager;
@Controller
public class ToggleController {
@Autowired
private FeatureManager manager;
@GetMapping("/testing")
@ResponseBody
String display() {
if(manager.isActive(MyFeatures.FEATURE_ONE)){
return "FeatureOne is active woking "+MyFeatures.FEATURE_ONE.isActive();
}
else {
return "FeatureOne is Inactive redirected "+MyFeatures.FEATURE_ONE.isActive();
}
}
}
MyFeatures.java
package com.learn.poc.toggle;
import org.togglz.core.Feature;
import org.togglz.core.annotation.Label;
import org.togglz.core.context.FeatureContext;
public enum MyFeatures implements Feature {
@Label("First Feature")
FEATURE_ONE,
@Label("Second Feature")
FEATURE_TWO;
public boolean isActive() {
return FeatureContext.getFeatureManager().isActive(this);
}
}
Application.properties:(PS: I don't want to set anything directly in application properties like feature-enum etc...)
togglz.console.enabled=true
togglz.console.path=/togglz-console
server.servlet.context-path=/api
server.port=8080
togglz.console.use-management-port=false
#togglz.console.secured=false
#togglz.feature-enums=com.learn.poc.toggle.MyFeatures
Now for UseCase-1:
I am using configuration file implementing TogglzConfig.
MyTogglzConfiguration.java
package com.learn.poc.toggle;
import java.io.File;
import org.springframework.stereotype.Component;
import org.togglz.core.Feature;
import org.togglz.core.manager.TogglzConfig;
import org.togglz.core.repository.StateRepository;
import org.togglz.core.repository.file.FileBasedStateRepository;
import org.togglz.core.user.FeatureUser;
import org.togglz.core.user.SimpleFeatureUser;
import org.togglz.core.user.UserProvider;
@Component
public class MyTogglzConfiguration implements TogglzConfig {
public Class<? extends Feature> getFeatureClass() {
return MyFeatures.class;
}
public StateRepository getStateRepository() {
return new FileBasedStateRepository(new File("PATH\features.properties"));
}
@Override
public UserProvider getUserProvider() {
return new UserProvider() {
@Override
public FeatureUser getCurrentUser() {
return new SimpleFeatureUser("admin", true);
}
};
}
}
So if i follow the above way its not getting autowired i.e not taking properties file from path and its not getting features anything at all.
UseCase-2: Insted i used spring ConfigFile and created bean for individual property as shown below.
configClass.java
package com.learn.poc.toggle;
import java.io.File;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.togglz.core.manager.EnumBasedFeatureProvider;
import org.togglz.core.repository.StateRepository;
import org.togglz.core.repository.file.FileBasedStateRepository;
import org.togglz.core.spi.FeatureProvider;
import org.togglz.core.user.FeatureUser;
import org.togglz.core.user.SimpleFeatureUser;
import org.togglz.core.user.UserProvider;
@Configuration
public class configClass {
@Bean
public FeatureProvider featureProvider() {
return new EnumBasedFeatureProvider(MyFeatures.class);
}
@Bean
public StateRepository getStateRepository() {
System.out.println("in state");
return new FileBasedStateRepository(new File("PATH\features.properties"));
}
@Bean
public UserProvider getUserProvider() {
System.out.println("in user provider");
return new UserProvider() {
@Override
public FeatureUser getCurrentUser() {
return new SimpleFeatureUser("admin", true);
}
};
}
}
If implement as above file its everything working fine. I don't know that first usecase is failing. Because its mentioned in the togglz documentation that if you mention configuration file as @Component it will automatically wired for spring boot.
ref: https://www.togglz.org/documentation/configuration.html
Its said that:
Togglz offers a special integration module for Spring. This module will automatically search for
implementations of the TogglzConfig in Spring's ApplicationContext. Therefore you just have to
declare your implementation as a class managed by Spring. If you are using the Spring annotation
support you can just add a @Component annotation to your class: