2

I am getting below exception when integrating Togglz with my Spring MVC app.

exception

java.lang.IllegalStateException: Could not find the FeatureManager. For web applications please verify that the TogglzFilter starts up correctly. In other deployment scenarios you will typically have to implement a FeatureManagerProvider as described in the 'Advanced Configuration' chapter of the documentation.
    org.togglz.core.context.FeatureContext.getFeatureManager(FeatureContext.java:53)
    org.togglz.core.manager.LazyResolvingFeatureManager.getDelegate(LazyResolvingFeatureManager.java:24)
    org.togglz.core.manager.LazyResolvingFeatureManager.getCurrentFeatureUser(LazyResolvingFeatureManager.java:49)
    org.togglz.console.TogglzConsoleServlet.isFeatureAdmin(TogglzConsoleServlet.java:75)
    org.togglz.console.TogglzConsoleServlet.service(TogglzConsoleServlet.java:62)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.togglz.servlet.TogglzFilter.doFilter(TogglzFilter.java:100)

Below dependencies are defined in maven -

<dependency>
  <groupId>org.togglz</groupId>
  <artifactId>togglz-core</artifactId>
  <version>2.3.0.Final</version>
</dependency>

<!-- Spring integration (optional) -->
<dependency>
  <groupId>org.togglz</groupId>
  <artifactId>togglz-spring-web</artifactId>
  <version>2.3.0.Final</version>
</dependency>
<!-- Togglz Admin Console -->
<dependency>
  <groupId>org.togglz</groupId>
  <artifactId>togglz-console</artifactId>
  <version>2.3.0.Final</version>
</dependency>

My Config class -

@Component
public class MyTogglzConfiguration implements TogglzConfig {

    /* ..... */

    @Override
    public UserProvider getUserProvider() {
        return new UserProvider() {
            @Override
            public FeatureUser getCurrentUser() {
                return new SimpleFeatureUser("admin", true);
            }
        };
    }

    @Override
    public Class<? extends Feature> getFeatureClass() {
        // TODO Auto-generated method stub
        return MyFeatures.class;
    }

    @Override
    public StateRepository getStateRepository() {
        // TODO Auto-generated method stub
         return new FileBasedStateRepository(new File("c:/tmp/features.properties"));
    }
}

My Feature Class-

public enum MyFeatures implements Feature {


    @Label("First Feature")
    FEATURE_ONE,

    @EnabledByDefault
    @Label("Second Feature")
    FEATURE_TWO;

    public boolean isActive() {
        return FeatureContext.getFeatureManager().isActive(this);

    }
}

Above Configuration should have worked but I was I was getting "java.lang.IllegalStateException: Could not find any implementation of TogglzConfig or TogglzBootstrap." This error was gone after adding below context-params but now I have "Could not find the FeatureManager"

public class WebAppInitializer implements WebApplicationInitializer  {

    @Override
    public void onStartup(ServletContext servletContext) {

        servletContext.setInitParameter("org.togglz.FEATURE_MANAGER_PROVIDED", "true");
    }


 }
Slava Semushin
  • 13,753
  • 7
  • 47
  • 66
user2112430
  • 73
  • 1
  • 8

2 Answers2

0

It looks like the Spring integration isn't working in your app for some reason. Basically Togglz is using Spring-specific BeanFinder implementations to lookup instances from the Spring application context. This way Togglz looks up TogglzConfig to bootstrap a FeatureManager.

My guess is that this block here isn't able to locate the ApplicationContext for your app. You could verify this by setting a corresponding break point:

https://github.com/togglz/togglz/blob/2.3.0.Final/spring-web/src/main/java/org/togglz/spring/web/spi/SpringWebBeanFinder.java#L30-L40

Please also make sure that Spring's ContextLoaderListener is registered and correctly invoked. Without it, Togglz lookup code won't work correctly.

I hope this helps.

chkal
  • 5,532
  • 18
  • 26
  • Just to add easier steps to reproduce. I have the exact same error if I start with sample mvc project - https://github.com/spring-projects/spring-mvc-showcase and then add my togglz configuration. – user2112430 Feb 01 '17 at 15:20
0

I have exact same problem with you, and I found the reason for my case is that TogglzFilter get started before the TogglzConfig component registered in spring context. So you must ensure the spring context registering executed at the very beginning of web container starting up.

So I just made the following change to my web.xml to get it resolved.

before change:

<display-name>REST HelloWorld</display-name>
<servlet>
    <servlet-name>rest-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/rest-dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>rest-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<!-- Welcome files -->
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

after change:

<display-name>REST HelloWorld</display-name>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/rest-dispatcher-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>rest-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>rest-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<!-- Welcome files -->
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

Note that component-scan for context registering is defined in file rest-dispatcher-servlet.xml

<context:component-scan
    base-package="com.xxx.togglz" />

Hope it helps

Tom Li
  • 1