This repository has been archived by the owner on May 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
HazelcastPlugin.java
45 lines (40 loc) · 1.84 KB
/
HazelcastPlugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package org.gradle.caching.hazelcast;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.core.HazelcastInstance;
import org.gradle.api.Plugin;
import org.gradle.api.initialization.Settings;
import org.gradle.caching.BuildCacheService;
import org.gradle.caching.BuildCacheServiceFactory;
import org.gradle.caching.MapBasedBuildCacheService;
import org.gradle.caching.configuration.BuildCacheConfiguration;
/**
* {@link Settings} plugin to register Hazelcast as a build cache backend.
*
* @see HazelcastBuildCache
*/
public class HazelcastPlugin implements Plugin<Settings> {
@Override
public void apply(Settings settings) {
BuildCacheConfiguration buildCacheConfiguration = settings.getBuildCache();
buildCacheConfiguration.registerBuildCacheService(HazelcastBuildCache.class, HazelcastBuildCacheServiceFactory.class);
// Use Hazelcast as remote cache and disable local cache
buildCacheConfiguration.getLocal().setEnabled(false);
HazelcastBuildCache cache = buildCacheConfiguration.remote(HazelcastBuildCache.class);
}
static class HazelcastBuildCacheServiceFactory implements BuildCacheServiceFactory<HazelcastBuildCache> {
@Override
public BuildCacheService createBuildCacheService(HazelcastBuildCache cacheConfig, Describer describer) {
ClientConfig config = new ClientConfig();
String address = cacheConfig.getHost() + ":" + cacheConfig.getPort();
String name = cacheConfig.getName();
config.getNetworkConfig().addAddress(address);
HazelcastInstance instance = HazelcastClient.newHazelcastClient(config);
describer
.type("Hazelcast")
.config("name", name)
.config("address", address);
return new MapBasedBuildCacheService(instance.<String, byte[]>getMap(name));
}
}
}