Skip to content

Commit

Permalink
Allow to pass instances of ClientConfig and Config to Hazelcast data …
Browse files Browse the repository at this point in the history
…store factory (#8600)

* Allow to pass instances of ClientConfig and Config to Hazelcast data store factory

This increases flexibility when configuring it programmatically.

Patterns such as

HazelcastSessionDataStoreFactory f = new HazelcastSessionDataStoreFactory();
f.setOnlyClient(true);
f.setClientConfig(ClientConfig.load());

or

HazelcastSessionDataStoreFactory f = new HazelcastSessionDataStoreFactory();
f.setServerConfig(Config.load());

can then be used to configure Hazelcast according to standard Hazelcast
locations if needed.

Co-authored-by: Jesse Glick <[email protected]>
  • Loading branch information
Vlatombe and jglick authored Sep 21, 2022
1 parent 35fe649 commit 090104d
Showing 1 changed file with 75 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public class HazelcastSessionDataStoreFactory

private String addresses;

private ClientConfig clientConfig;

private Config serverConfig;

public boolean isUseQueries()
{
return useQueries;
Expand All @@ -84,7 +88,14 @@ public SessionDataStore getSessionDataStore(SessionHandler handler)
ClientConfig config;
if (StringUtil.isEmpty(configurationLocation))
{
config = new ClientConfig();
if (clientConfig == null)
{
config = new ClientConfig();
}
else
{
config = clientConfig;
}

if (addresses != null && !addresses.isEmpty())
{
Expand All @@ -98,7 +109,15 @@ public SessionDataStore getSessionDataStore(SessionHandler handler)
}
else
{
config = new XmlClientConfigBuilder(configurationLocation).build();
if (clientConfig == null)
{
config = new XmlClientConfigBuilder(configurationLocation).build();
}
else
{
LOG.warn("Both configurationLocation and clientConfig are set, using clientConfig");
config = clientConfig;
}
if (config.getSerializationConfig().getSerializerConfigs().stream().noneMatch(s ->
SessionData.class.getName().equals(s.getTypeClassName()) && s.getImplementation() instanceof SessionDataSerializer))
LOG.warn("Hazelcast xml config is missing org.eclipse.jetty.hazelcast.session.SessionDataSerializer - sessions may not serialize correctly");
Expand All @@ -114,7 +133,14 @@ public SessionDataStore getSessionDataStore(SessionHandler handler)
SerializerConfig sc = new SerializerConfig()
.setImplementation(new SessionDataSerializer())
.setTypeClass(SessionData.class);
config = new Config();
if (serverConfig == null)
{
config = new Config();
}
else
{
config = serverConfig;
}
config.getSerializationConfig().addSerializerConfig(sc);
// configure a default Map if null
if (mapConfig == null)
Expand All @@ -131,7 +157,15 @@ public SessionDataStore getSessionDataStore(SessionHandler handler)
}
else
{
config = new XmlConfigBuilder(configurationLocation).build();
if (serverConfig == null)
{
config = new XmlConfigBuilder(configurationLocation).build();
}
else
{
LOG.warn("Both configurationLocation and serverConfig are set, using serverConfig");
config = serverConfig;
}
if (config.getSerializationConfig().getSerializerConfigs().stream().noneMatch(s ->
SessionData.class.getName().equals(s.getTypeClassName()) && s.getImplementation() instanceof SessionDataSerializer))
LOG.warn("Hazelcast xml config is missing org.eclipse.jetty.hazelcast.session.SessionDataSerializer - sessions may not serialize correctly");
Expand Down Expand Up @@ -160,7 +194,7 @@ public boolean isOnlyClient()

/**
* @param onlyClient if <code>true</code> the session manager will only connect to an external Hazelcast instance
* and not use this JVM to start an Hazelcast instance
* and not use this JVM to start a Hazelcast instance
*/
public void setOnlyClient(boolean onlyClient)
{
Expand All @@ -172,6 +206,12 @@ public String getConfigurationLocation()
return configurationLocation;
}

/**
* @param configurationLocation the location of the XML Hazelcast configuration file to load.
* Depending on whether {@link #setOnlyClient(boolean)} is set to {@code true}
* or not, this will be used to configure either a Hazelcast client or a Hazelcast server.
* This parameter is mutually exclusive with {@link #setClientConfig(ClientConfig)} and {@link #setServerConfig(Config)}.
*/
public void setConfigurationLocation(String configurationLocation)
{
this.configurationLocation = configurationLocation;
Expand Down Expand Up @@ -226,4 +266,34 @@ public void setAddresses(String addresses)
{
this.addresses = addresses;
}

public ClientConfig getClientConfig()
{
return clientConfig;
}

/**
* @param clientConfig the client configuration to use to connect to Hazelcast.
* Only used if {@link #setOnlyClient(boolean)} is set to {@code true}.
* Overrides any configuration set via {@link #setConfigurationLocation(String)}
*/
public void setClientConfig(ClientConfig clientConfig)
{
this.clientConfig = clientConfig;
}

public Config getServerConfig()
{
return serverConfig;
}

/**
* @param serverConfig the server configuration to use to configure the embedded Hazelcast cluster.
* Only used if {@link #setOnlyClient(boolean)} is set to {@code false}.
* Overrides any configuration set via {@link #setConfigurationLocation(String)}
*/
public void setServerConfig(Config serverConfig)
{
this.serverConfig = serverConfig;
}
}

0 comments on commit 090104d

Please sign in to comment.