If you followed the preceding chapters, you are now ready to actually use Togglz.
Checking if a feature is active or not from Java code is straight forward. Just call the isActive()
method on the corresponding enum.
if (MyFeatures.HOT_NEW_FEATURE.isActive()) { // do cool new stuff here }
You see that checking the feature state is very easy. You don't have to lookup any objects or something like this. Just call a regular method on the enum and you are done! :)
The JSF integration module provides a very simple way to render page content depending on whether a feature is active or not:
<h:panelGroup rendered="#{features['HOT_NEW_FEATURE']}"> <!-- Some part of the page required for HOT_NEW_FEATURE --> </h:panelGroup>
Togglz ships with some extra goodies for Spring users. If you are using Spring, make sure that you added the Spring integration module to your dependencies:
<!-- Spring integration (optional) --> <dependency> <groupId>org.togglz</groupId> <artifactId>togglz-spring-web</artifactId> <version>3.1.2</version> </dependency>
In some situations it may be required to use completely different implementation classes depending on whether a feature is active or not. Think for example of a case where you want to rewrite the implementation of some reason to improve performance.
The FeatureProxyFactoryBean
allows to create a proxy object, that delegates all calls to one of two beans,
depending on whether a given feature is active or not.
See the following part of a Spring XML configuration file for an example:
<bean id="optimizedService" class="com.example.services.HighPerformanceService" /> <bean id="oldService" class="com.example.services.DefaultService" /> <bean id="someService" class="org.togglz.spring.proxy.FeatureProxyFactoryBean"> <property name="feature" value="HOT_NEW_FEATURE" /> <property name="active" ref="optimizedService" /> <property name="inactive" ref="oldService" /> </bean>
This configuration shows how the FeatureProxyFactoryBean
can be used. The factory bean has three
required properties that have to be set. The property feature
is the name of the feature that should be checked
before delegating invocations. The properties active
and inactive
must refer to
the two alternative implementations.
Please note that the FeatureProxyFactoryBean
creates a standard JDK proxy. Therefore the two
beans which receive the invocations must share a common interface that will be used to create the proxy.
The interface to proxy will be auto detected, if both classes only share exactly one interface. In all
other cases you will have to tell the factory bean about the interface using the businessInterface
property.