Togglz is throwing an IllegalStateException error

3.9k Views Asked by At

I keep on receiving a IllegalStateException: Could not find the FeatureManager when installing togglz in a web app on maven. I've followed the instuctions exactly. In my maven application, I have the following settings in my pom.xml file:

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

<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-console</artifactId>
<version>2.1.0.Final</version>
</dependency>

<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-servlet</artifactId>
<version>2.1.0.Final</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

As well as the following in the web.xml file:

<context-param>
<param-name>org.togglz.core.manager.TogglzConfig</param-name>
<param-value>com.test.test.ana.FeatureFlagConfiguration</param-value>
</context-param>

<servlet>
<servlet-name>TogglzConsoleServlet</servlet-name>
<servlet-class>org.togglz.console.TogglzConsoleServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>TogglzConsoleServlet</servlet-name>
<url-pattern>/togglz/*</url-pattern>
</servlet-mapping>

This sample maven app runs fine, but when I try to go to the togglz virtual directory, it throws IllegalStateException: Could not find the FeatureManager error, it doesn't give much more detail than that. Any suggestions as to what that error really means? I'm just trying to get a sample project that uses togglz that works. I can't use spring or cdi, just servlets. (yes, i have servlet 3.0 configured)

Thanks in advance,

Edit: Here is the exact stack trace:

    [ERROR   ] SRVE0777E: Exception thrown by application class 'org.togglz.core.context.FeatureContext.getFeatureManager:49'
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.
    at org.togglz.core.context.FeatureContext.getFeatureManager(FeatureContext.java:49)
    at org.togglz.core.manager.LazyResolvingFeatureManager.getDelegate(LazyResolvingFeatureManager.java:24)
    at org.togglz.core.manager.LazyResolvingFeatureManager.getCurrentFeatureUser(LazyResolvingFeatureManager.java:49)
    at org.togglz.console.TogglzConsoleServlet.isFeatureAdmin(TogglzConsoleServlet.java:68)
    at org.togglz.console.TogglzConsoleServlet.service(TogglzConsoleServlet.java:55)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1285)
    at [internal classes]
1

There are 1 best solutions below

2
On

If you are in a plain Servlet environment, you will have to do the following things after adding the Maven dependencies:

Implement your feature enum

This typically looks like this:

public enum MyFeatures implements Feature {

    @EnabledByDefault
    @Label("First Feature")
    FEATURE_ONE,

    @Label("Second Feature")
    FEATURE_TWO;

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

}

Configure Togglz by implementing TogglzConfig

A typical example looks like this:

public class MyTogglzConfiguration implements TogglzConfig {

    public Class<? extends Feature> getFeatureClass() {
        return MyFeatures.class;
    }

    public StateRepository getStateRepository() {
        return new FileBasedStateRepository(new File("/tmp/features.properties"));
    }

    public UserProvider getUserProvider() {
        return new ServletUserProvider();
    }

}

Register your configuration class in web.xml

In a plain Servlet environment you will now have to register your TogglzConfig implementation by adding something like this to your web.xml:

<context-param>
  <param-name>org.togglz.core.manager.TogglzConfig</param-name>
  <param-value>com.example.myapp.MyTogglzConfiguration</param-value>
</context-param>

You should also explicitly tell Togglz that you do not want it to lookup the FeatureManager from Spring or CDI but to create and manage an instance itself:

<context-param>
  <param-name>org.togglz.FEATURE_MANAGER_PROVIDED</param-name>
  <param-value>true</param-value>
</context-param>

In Servlet 3.0 environments, the TogglzFilter is typically picked up automatically. However, you can also register it manually:

<filter>
  <filter-name>TogglzFilter</filter-name>
  <filter-class>org.togglz.servlet.TogglzFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>TogglzFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

I hope this help. If this still doesn't work, please include the full stacktrace in your question.