Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Hazelcast IMDG Tutorial: Implement Variable Replacer

DZone's Guide to

Hazelcast IMDG Tutorial: Implement Variable Replacer

With Hazelcast, you can use Variables within XML configuration files. Here, we implement and use a custom Variable Replacer for a Hazelcast XML configuration file.

· Big Data Zone ·
Free Resource

The open source HPCC Systems platform is a proven, easy to use solution for managing data at scale. Visit our Easy Guide to learn more about this completely free platform, test drive some code in the online Playground, and get started today.

Hazelcast IMDG 3.10 comes with a new feature: support for Variable Replacers. The Replacers allow for using placeholders (Variables) in configuration XML files. You can use Replacers to mask sensitive strings (such as passwords) or to provide values that depend on the current environment.

There was already a partial support for placeholders before version 3.10; it allows for referencing system properties:

<group>
  <name>${groupName}</name>
</group>

Hazelcast version 3.10 extends the feature and adds the possibility to introduce your own Replacer implementations. Variable Replacers are classes that implement the ConfigReplacer interface:

public interface ConfigReplacer {
    void init(Properties properties);
    String getPrefix();
    String getReplacement(String maskedValue);
}

A Variable to be replaced within the configuration file has the following form:

"$" PREFIX "{" STRING-TO-BE-REPLACED "}"

...where the PREFIX is the value returned by the getPrefix() method and STRING-TO-BE-REPLACED is a value provided to the getReplacement(String) method. The result of the getReplacement(String) method call replaces the whole Variable String.

Hazelcast contains the example implementation EncryptionReplacer in the codebase — it allows for masking sensitive strings in the configuration file. Another example implementation called ExecReplacer is placed in the Hazelcast code samples — it allows for executing external commands and using its output as the replacement.

Custom Implementation

Now, let's show how simple is to implement and use your own custom replacer. The replacer will get a class name and create a new object from it. Then, it will call the toString() method on it to get the replacement value. Let's use a "TOSTRING" prefix for our new replacer.

package com.hazelcast.tutorial;

import com.hazelcast.config.replacer.spi.ConfigReplacer;

public class ToStringReplacer implements ConfigReplacer {

    @Override
    public void init(java.util.Properties properties) {
        // we don't need init properties
    }

    @Override
    public String getPrefix() {
        return "TOSTRING";
    }

    @Override
    public String getReplacement(String value) {
        try {
            return Class.forName(value).newInstance().toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // Return null when we are not able to find proper replacement.
        return null;
    }
}

Once the new implementation is enabled within the configuration file's element, we can use it to provide values for variables:

<hazelcast xmlns="http://www.hazelcast.com/schema/config"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.hazelcast.com/schema/config
           http://www.hazelcast.com/schema/config/hazelcast-config-3.10.xsd">

    <config-replacers>
        <replacer class-name="com.hazelcast.config.replacer.ToStringReplacer"/>
    </config-replacers>

    <group>
        <name>$TOSTRING{com.hazelcast.config.replacer.GroupNameProvider}</name>
    </group>

    <instance-name>Instance $TOSTRING{java.util.Date}</instance-name>
</hazelcast>

When you start such a server instance and the GroupNameProvider.toString() call returns TestGroup constant, then you will see it as the group name in the server console output:

...
INFO: [LOCAL] [TestGroup] [3.10] Prefer IPv4 stack is true.
Apr 30, 2018 11:00:05 AM com.hazelcast.instance.AddressPicker
INFO: [LOCAL] [TestGroup] [3.10] Picked [192.168.1.1]:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is true
Apr 30, 2018 11:00:05 AM com.hazelcast.system
...

Find the full source code in this GitHub repository.

Summary

With Hazelcast, you can use Variables within XML configuration files. Moreover, you can provide own replacer implementations that will evaluate the Variables and provide replacements. We have implemented and used a custom Variable Replacer for a Hazelcast XML configuration file.

Is there any replacer you would like to have implemented? Stop by here and give us a shout!

Links:

Managing data at scale doesn’t have to be hard. Find out how the completely free, open source HPCC Systems platform makes it easier to update, easier to program, easier to integrate data, and easier to manage clusters. Download and get started today.

Topics:
imdg ,big data ,custom variables ,variable replacer ,tutorial ,hazelcast

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}