-
Notifications
You must be signed in to change notification settings - Fork 616
/
SessionImpl.java
194 lines (171 loc) · 6.44 KB
/
SessionImpl.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package org.infinispan.cli.interpreter.session;
import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_OBJECT;
import java.util.Collection;
import javax.transaction.TransactionManager;
import org.infinispan.AdvancedCache;
import org.infinispan.Cache;
import org.infinispan.cli.interpreter.codec.Codec;
import org.infinispan.cli.interpreter.codec.CodecException;
import org.infinispan.cli.interpreter.codec.CodecRegistry;
import org.infinispan.cli.interpreter.codec.NoneCodec;
import org.infinispan.cli.interpreter.logging.Log;
import org.infinispan.commands.CommandsFactory;
import org.infinispan.commands.CreateCacheCommand;
import org.infinispan.commons.api.BasicCacheContainer;
import org.infinispan.commons.dataconversion.IdentityEncoder;
import org.infinispan.commons.dataconversion.MediaType;
import org.infinispan.commons.time.TimeService;
import org.infinispan.configuration.ConfigurationManager;
import org.infinispan.configuration.cache.Configuration;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.remoting.rpc.RpcManager;
import org.infinispan.util.logging.LogFactory;
public class SessionImpl implements Session {
public static final Log log = LogFactory.getLog(SessionImpl.class, Log.class);
private static final Codec NO_OP_CODEC = new NoneCodec();
private final EmbeddedCacheManager cacheManager;
private final CodecRegistry codecRegistry;
private final String id;
private final TimeService timeService;
private ConfigurationManager configurationManager;
private Cache<?, ?> cache = null;
private String cacheName = null;
private long timestamp;
private Codec codec;
public SessionImpl(final CodecRegistry codecRegistry, final EmbeddedCacheManager cacheManager, final String id,
TimeService timeService, ConfigurationManager configurationManager) {
if (timeService == null) {
throw new IllegalArgumentException("TimeService cannot be null");
}
this.codecRegistry = codecRegistry;
this.cacheManager = cacheManager;
this.timeService = timeService;
this.configurationManager = configurationManager;
this.id = id;
timestamp = timeService.time();
codec = this.codecRegistry.getCodec("none");
}
@Override
public EmbeddedCacheManager getCacheManager() {
return cacheManager;
}
@Override
public String getId() {
return id;
}
@Override
public <K, V> Cache<K, V> getCurrentCache() {
return (Cache<K, V>) cache;
}
@Override
public String getCurrentCacheName() {
return cacheName;
}
@Override
public <K, V> Cache<K, V> getCache(final String cacheName) {
Cache<K, V> c;
if (cacheName != null) {
c = cacheManager.getCache(cacheName, false);
} else {
c = getCurrentCache();
}
if (c == null) {
throw log.nonExistentCache(cacheName);
}
return (Cache<K, V>) c.getAdvancedCache().withEncoding(IdentityEncoder.class);
}
@Override
public void setCurrentCache(final String cacheName) {
cache = getCache(cacheName);
this.cacheName = cacheName;
}
@Override
public void createCache(String cacheName, String baseCacheName) {
Configuration configuration;
if (baseCacheName != null) {
configuration = configurationManager.getConfiguration(baseCacheName, true);
if (configuration == null) {
throw log.nonExistentCache(baseCacheName);
}
} else {
configuration = cacheManager.getDefaultCacheConfiguration();
baseCacheName = BasicCacheContainer.DEFAULT_CACHE_NAME;
}
if (cacheManager.cacheExists(cacheName)) {
throw log.cacheAlreadyExists(cacheName);
}
if (configuration.clustering().cacheMode().isClustered()) {
AdvancedCache<?, ?> clusteredCache = cacheManager.getCache(baseCacheName).getAdvancedCache();
RpcManager rpc = clusteredCache.getRpcManager();
CommandsFactory factory = clusteredCache.getComponentRegistry().getComponent(CommandsFactory.class);
CreateCacheCommand ccc = factory.buildCreateCacheCommand(cacheName, baseCacheName);
try {
rpc.invokeRemotely(null, ccc, rpc.getDefaultRpcOptions(true));
ccc.init(cacheManager, configurationManager);
ccc.invoke();
} catch (Throwable e) {
throw log.cannotCreateClusteredCaches(e, cacheName);
}
} else {
ConfigurationBuilder b = new ConfigurationBuilder();
b.read(configuration);
cacheManager.defineConfiguration(cacheName, b.build());
cacheManager.getCache(cacheName);
}
}
@Override
public void reset() {
if (configurationManager.getGlobalConfiguration().defaultCacheName().isPresent())
resetCache(cacheManager.getCache());
for (String cacheName : cacheManager.getCacheNames()) {
resetCache(cacheManager.getCache(cacheName));
}
timestamp = timeService.time();
}
private void resetCache(final Cache<Object, Object> cache) {
Configuration configuration = SecurityActions.getCacheConfiguration(cache.getAdvancedCache());
if (configuration.invocationBatching().enabled()) {
SecurityActions.endBatch(cache.getAdvancedCache());
}
TransactionManager tm = cache.getAdvancedCache().getTransactionManager();
try {
if (tm != null && tm.getTransaction() != null) {
tm.rollback();
}
} catch (Exception e) {
// Ignore the exception
}
}
@Override
public long getTimestamp() {
return timestamp;
}
@Override
public void setCodec(String codec) throws CodecException {
this.codec = getCodec(codec);
}
@Override
public Collection<Codec> getCodecs() {
return codecRegistry.getCodecs();
}
@Override
public Codec getCodec() {
if (isObjectStorage()) return NO_OP_CODEC;
return codec;
}
boolean isObjectStorage() {
MediaType storageMediaType = cache.getAdvancedCache().getValueDataConversion().getStorageMediaType();
return APPLICATION_OBJECT.match(storageMediaType);
}
@Override
public Codec getCodec(String codec) throws CodecException {
Codec c = codecRegistry.getCodec(codec);
if (c == null) {
throw log.noSuchCodec(codec);
} else {
if (isObjectStorage()) return NO_OP_CODEC;
return c;
}
}
}