Skip to content

Commit

Permalink
Hacking
Browse files Browse the repository at this point in the history
  • Loading branch information
snicoll committed Feb 11, 2021
1 parent 75fc896 commit 2a9533c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,7 @@
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

/**
* Configuration for Hazelcast client.
Expand All @@ -49,12 +50,15 @@ class HazelcastClientConfiguration {
static class HazelcastClientConfigFileConfiguration {

@Bean
HazelcastInstance hazelcastInstance(HazelcastProperties properties) throws IOException {
Resource config = properties.resolveConfigLocation();
if (config != null) {
return new HazelcastClientFactory(config).getHazelcastInstance();
}
return HazelcastClient.newHazelcastClient();
HazelcastInstance hazelcastInstance(HazelcastProperties properties, ResourceLoader resourceLoader)
throws IOException {
return createHazelcastClientFactory(properties.resolveConfigLocation())
.getHazelcastInstance(resourceLoader.getClassLoader());
}

private HazelcastClientFactory createHazelcastClientFactory(Resource config) throws IOException {
return (config != null) ? new HazelcastClientFactory(config)
: new HazelcastClientFactory(ClientConfig.load());
}

}
Expand All @@ -65,6 +69,7 @@ static class HazelcastClientConfigConfiguration {

@Bean
HazelcastInstance hazelcastInstance(ClientConfig config) {
// TODO should we replace the ClassLoader here?
return new HazelcastClientFactory(config).getHazelcastInstance();
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -67,10 +67,22 @@ private ClientConfig getClientConfig(Resource clientConfigLocation) throws IOExc
}

/**
* Get the {@link HazelcastInstance}.
* Get the {@link HazelcastInstance} with the default {@link ClassLoader}.
* @return the {@link HazelcastInstance}
*/
public HazelcastInstance getHazelcastInstance() {
return getHazelcastInstance(null);
}

/**
* Get the {@link HazelcastInstance} with the specified {@link ClassLoader}.
* @param classLoader the classloader to use for serialization
* @return the {@link HazelcastInstance}
*/
public HazelcastInstance getHazelcastInstance(ClassLoader classLoader) {
if (classLoader != null) {
this.clientConfig.setClassLoader(classLoader);
}
if (StringUtils.hasText(this.clientConfig.getInstanceName())) {
return HazelcastClient.getOrCreateHazelcastClient(this.clientConfig);
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -81,10 +81,22 @@ private static Config createConfig(URL configUrl) throws IOException {
}

/**
* Get the {@link HazelcastInstance}.
* Get the {@link HazelcastInstance} with the default {@link ClassLoader}.
* @return the {@link HazelcastInstance}
*/
public HazelcastInstance getHazelcastInstance() {
return getHazelcastInstance(null);
}

/**
* Get the {@link HazelcastInstance} with the specified {@link ClassLoader}.
* @param classLoader the classloader to use for serialization
* @return the {@link HazelcastInstance}
*/
public HazelcastInstance getHazelcastInstance(ClassLoader classLoader) {
if (classLoader != null) {
this.config.setClassLoader(classLoader);
}
if (StringUtils.hasText(this.config.getInstanceName())) {
return Hazelcast.getOrCreateHazelcastInstance(this.config);
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,7 +19,6 @@
import java.io.IOException;

import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Expand All @@ -28,6 +27,7 @@
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

/**
* Configuration for Hazelcast server.
Expand All @@ -47,12 +47,15 @@ class HazelcastServerConfiguration {
static class HazelcastServerConfigFileConfiguration {

@Bean
HazelcastInstance hazelcastInstance(HazelcastProperties properties) throws IOException {
Resource config = properties.resolveConfigLocation();
if (config != null) {
return new HazelcastInstanceFactory(config).getHazelcastInstance();
}
return Hazelcast.newHazelcastInstance();
HazelcastInstance hazelcastInstance(HazelcastProperties properties, ResourceLoader resourceLoader)
throws IOException {
return createHazelcastInstanceFactory(properties.resolveConfigLocation())
.getHazelcastInstance(resourceLoader.getClassLoader());
}

private HazelcastInstanceFactory createHazelcastInstanceFactory(Resource config) throws IOException {
return (config != null) ? new HazelcastInstanceFactory(config)
: new HazelcastInstanceFactory(Config.load());
}

}
Expand All @@ -63,6 +66,7 @@ static class HazelcastServerConfigConfiguration {

@Bean
HazelcastInstance hazelcastInstance(Config config) {
// TODO should we replace the ClassLoader here?
return new HazelcastInstanceFactory(config).getHazelcastInstance();
}

Expand Down

0 comments on commit 2a9533c

Please sign in to comment.