-
Notifications
You must be signed in to change notification settings - Fork 228
/
AbstractInfinispanManagedConnectionFactory.java
731 lines (597 loc) · 21.1 KB
/
AbstractInfinispanManagedConnectionFactory.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
/*
* JBoss, Home of Professional Open Source.
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
package org.teiid.resource.adapter.infinispan.hotrod;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.resource.ResourceException;
import javax.resource.spi.InvalidPropertyException;
import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.protostream.BaseMarshaller;
import org.infinispan.protostream.SerializationContext;
import org.jboss.modules.Module;
import org.jboss.modules.ModuleIdentifier;
import org.jboss.modules.ModuleLoadException;
import org.teiid.core.util.Assertion;
import org.teiid.core.util.StringUtil;
import org.teiid.logging.LogConstants;
import org.teiid.logging.LogManager;
import org.teiid.resource.spi.BasicConnectionFactory;
import org.teiid.resource.spi.BasicManagedConnectionFactory;
import org.teiid.translator.TranslatorException;
import org.teiid.translator.infinispan.hotrod.InfinispanPlugin;
import org.teiid.translator.object.CacheNameProxy;
import org.teiid.translator.object.ClassRegistry;
import org.teiid.translator.object.Version;
public abstract class AbstractInfinispanManagedConnectionFactory extends
BasicManagedConnectionFactory {
/**
*/
private static final long serialVersionUID = -4791974803005018658L;
private enum CACHE_TYPE {
USE_JNDI, REMOTE_SERVER_LISTS, REMOTE_HOT_ROD_PROPERTIES
}
private String remoteServerList = null;
private String hotrodClientPropertiesFile = null;
private String cacheJndiName = null;
private Class<?> cacheTypeClass = null; // cacheName ==> ClassType
private String cacheTypes = null;
@SuppressWarnings("rawtypes")
protected Map<String, BaseMarshaller> messageMarshallerMap = null;
private ClassRegistry methodUtil = new ClassRegistry();
private String protobufDefFile = null;
private String messageMarshallers = null;
private String messageDescriptor = null;
private boolean usingAnnotations = false;
private RemoteCacheManager cacheContainer = null;
private String stagingCacheName;
private String aliasCacheName;
private String pkKey;
private Class<?> pkCacheKeyJavaType = null;
private CACHE_TYPE cacheType;
private String module;
private ClassLoader cl;
private CacheNameProxy cacheNameProxy;
private Version version = null;
@Override
public BasicConnectionFactory<InfinispanConnectionImpl> createConnectionFactory()
throws ResourceException {
// if all the properties are null,then its assumed the pojo has the protobuf annotations for indexing columns
if (protobufDefFile == null && messageMarshallers == null && messageDescriptor == null) {
usingAnnotations = true;
} else {
// if any of the following properties are specified, all 3 must be specified
if (protobufDefFile == null) {
throw new InvalidPropertyException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25030));
}
if (messageMarshallers == null) {
throw new InvalidPropertyException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25029));
}
if (messageDescriptor == null) {
throw new InvalidPropertyException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25020));
}
}
if (this.cacheTypes == null) {
throw new InvalidPropertyException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25021));
}
if (remoteServerList == null
&& hotrodClientPropertiesFile == null && cacheJndiName == null) {
throw new InvalidPropertyException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25023));
}
determineCacheType();
if (cacheType == null) {
throw new InvalidPropertyException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25022));
}
/*
* the creation of the cacheContainer has to be done within the
* call to get the connection so that the classloader is driven
* from the caller.
*/
return new BasicConnectionFactory<InfinispanConnectionImpl>() {
private static final long serialVersionUID = 1L;
@Override
public InfinispanConnectionImpl getConnection()
throws ResourceException {
AbstractInfinispanManagedConnectionFactory.this.createCacheContainer();
return new InfinispanConnectionImpl(AbstractInfinispanManagedConnectionFactory.this);
}
};
}
public String getCacheName() {
// return the cacheName that is mapped as the alias
return cacheNameProxy.getPrimaryCacheAliasName();
}
public String getCacheStagingName() {
return cacheNameProxy.getStageCacheAliasName();
}
@SuppressWarnings("rawtypes")
public RemoteCache getCache(String cacheName) {
if (cacheName == null) {
Assertion.isNotNull(cacheName, "Program Error: Cache Name is null");
}
return cacheContainer.getCache(cacheName);
}
/**
* Get the <code>cacheName:className[;pkFieldName[:cacheJavaType]]</code> cache
* type mappings.
*
* @return <code>cacheName:className[;pkFieldName[:cacheJavaType]]</code> cache
* type mappings
* @see #setCacheTypeMap(String)
*/
public String getCacheTypeMap() {
return cacheTypes;
}
/**
* Set the cache type mapping
* <code>cacheName:className[;pkFieldName[:cacheJavaType]]</code> that represent
* the root node class type for an available cache for access.
* The following is how the string parsed:
* <li>cacheNam = is the name used to retrieve the named cache
* <li>className = is the class that is stored in the cache, and will used to create new instances
* <li> [optional] pkFieldName = defined which attribute is defined as the key to the cache, this will be
* the key used to access the cache for updates. If not defined, updates will be disabled.
* Also, pkFieldName is required if the root object contains child objects that will accessible.
* <li> [optional] cacheJavaType = is defined when the pkFieldName java type defined in class is different
* than how its stored as the key of the cache. This will enable the correct java type to be used
* when updates are performed and the object can be directly accessed.
*
* @param cacheTypeMap
* the cache type mappings passed in the form of
* <code>cacheName:className[;pkFieldName[:cacheJavaType]]</code>
* @see #getCacheTypeMap()
*/
public void setCacheTypeMap(String cacheTypeMap) {
this.cacheTypes = cacheTypeMap;
}
/**
* Sets the (optional) module(s) where the ClassName class is defined, that
* will be loaded <code> (module,[module,..])</code>
*
* @param module
* @see #getModule
*/
public void setModule(String module) {
this.module = module;
}
/**
* Called to get the module(s) that are to be loaded
*
* @see #setModule
* @return String
*/
public String getModule() {
return module;
}
/**
* Get the Protobuf Definition File Name
*
* @return Name of the Protobuf Definition File
* @see #setProtobufDefinitionFile(String)
*/
public String getProtobufDefinitionFile() {
return protobufDefFile;
}
/**
* Set the Google Protobuf Definition File name that describes the objects to be serialized.
*
* @param protobufDefFile
* the file name of the protobuf definition file to use
* @see #getProtobufDefinitionFile()
*/
public void setProtobufDefinitionFile(String protobufDefFile) {
this.protobufDefFile = protobufDefFile;
}
/**
* Get the Message Marshaller class names
*
* @return String comma delimited, class names of Message Marshallers
* @see #setMessageMarshallers(String)
*/
public String getMessageMarshallers() {
return messageMarshallers;
}
/**
* Set the Protobin Marshallers pojoClassName:marshallerClassName[,pojoClassName:marshallerClassName,..]
*
* @param messageMarshallers
* the class names of the marshallers to use
* @see #getMessageMarshallers()
*/
public void setMessageMarshallers(String messageMarshallers) {
this.messageMarshallers = messageMarshallers;
}
/**
* Get the Message descriptor class name for the root object in cache
*
* @return Message Descriptor name
* @see #setMessageDescriptor(String)
*/
public String getMessageDescriptor() {
return messageDescriptor;
}
/**
* Set the name of the Message Descriptor
*
* @param messageDescriptor
* the name of the message descriptor
* @see #getMessageDescriptor()
*/
public void setMessageDescriptor(String messageDescriptor) {
this.messageDescriptor = messageDescriptor;
}
public String getStagingCacheName() {
return this.stagingCacheName;
}
/**
* An option to configure the staging cache name to use when using JDG to materialize data.
* @param cacheName
*/
protected void setStagingCacheName(String cacheName) {
this.stagingCacheName = cacheName;
}
protected String getAliasCacheName() {
return this.aliasCacheName;
}
/**
* An option to configure the alias cache name to use when using JDG to materialize data.
* @param cacheName
*/
protected void setAliasCacheName(String cacheName) {
this.aliasCacheName = cacheName;
}
public String getPk() {
return pkKey;
}
/**
* This is an optional argument when defining the <code>CacheTypeMap</code>
* on the resouce adapter.
*
* @return Class<?>
*/
public Class<?> getCacheKeyClassType() {
return pkCacheKeyJavaType;
}
/**
* Return the Class that identifies the cache object
* @return Class<?>
*/
public Class<?> getCacheClassType() {
return this.cacheTypeClass;
}
public void setCacheClassTypeClass(Class<?> classCacheType) {
this.cacheTypeClass = classCacheType;
}
public ClassRegistry getClassRegistry() {
return methodUtil;
}
/**
* Returns the <code>host:port[;host:port...]</code> list that identifies
* the remote servers to include in this cluster;
*
* @return <code>host:port[;host:port...]</code> list
*/
public String getRemoteServerList() {
return remoteServerList;
}
/**
* Set the list of remote servers that make up the Infinispan cluster. The
* servers must be Infinispan HotRod servers. The list must be in the
* appropriate format of <code>host:port[;host:port...]</code> that would be
* used when defining an Infinispan remote cache manager instance. If
* the value is missing, <code>localhost:11311</code> is assumed.
*
* @param remoteServerList
* the server list in appropriate
* <code>server:port;server2:port2</code> format.
*/
public void setRemoteServerList(String remoteServerList) {
this.remoteServerList = remoteServerList;
}
/**
* Get the name of the HotRod client properties file that should be used to
* configure a remoteCacheManager.
*
* @return the name of the HotRod client properties file to be used to
* configure remote cache manager
* @see #setHotRodClientPropertiesFile(String)
*/
public String getHotRodClientPropertiesFile() {
return hotrodClientPropertiesFile;
}
/**
* Set the name of the HotRod client properties file that should be used to
* configure a remoteCacheManager.
*
* @param propertieFileName
* the name of the HotRod client properties file that should be
* used to configure the remote cache manager
*
* @see #getHotRodClientPropertiesFile()
*/
public void setHotRodClientPropertiesFile(String propertieFileName) {
this.hotrodClientPropertiesFile = propertieFileName;
}
/**
* Get the JNDI Name of the cache.
*
* @return JNDI Name of cache
*/
public String getCacheJndiName() {
return cacheJndiName;
}
/**
* Set the JNDI name to a {@link Map cache} instance that should be used as
* this source.
*
* @param jndiName
* the JNDI name of the {@link Map cache} instance that should be
* used
* @see #setCacheJndiName(String)
*/
public void setCacheJndiName(String jndiName) {
this.cacheJndiName = jndiName;
}
/**
* Call to set the name of the cache to access when calling getCache
* @param cacheName
* @throws ResourceException
*/
protected void setCacheName(String cacheName) throws ResourceException {
if (getAliasCacheName() != null && getStagingCacheName() != null) {
cacheNameProxy = new CacheNameProxy(cacheName, getStagingCacheName(),getAliasCacheName() );
} else if (getStagingCacheName() != null || getAliasCacheName() != null) {
throw new InvalidPropertyException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25011));
} else {
cacheNameProxy = new CacheNameProxy(cacheName);
}
}
public CacheNameProxy getCacheNameProxy() {
return cacheNameProxy;
}
public boolean isAlive() {
return this.cacheContainer != null;
}
protected RemoteCacheManager getCacheContainer() {
return this.cacheContainer;
}
protected void setCacheContainer(RemoteCacheManager rcm) {
this.cacheContainer = rcm;
}
abstract protected SerializationContext getContext();
protected ClassLoader getClassLoader() {
return this.cl;
}
protected Class<?> loadClass(String className) throws ResourceException {
try {
return Class.forName(className, false, getClassLoader());
} catch (ClassNotFoundException e) {
throw new ResourceException(e);
}
}
@SuppressWarnings("rawtypes")
protected synchronized ClassLoader loadClasses() throws ResourceException {
cl = null;
if (getModule() != null) {
try {
List<String> mods = StringUtil.getTokens(getModule(), ","); //$NON-NLS-1$
for (String mod : mods) {
Module x = Module.getContextModuleLoader().loadModule(
ModuleIdentifier.create(mod));
// the first entry must be the module associated with the
// cache
if (cl == null) {
cl = x.getClassLoader();
}
}
} catch (ModuleLoadException e) {
throw new ResourceException(e);
}
}
if (cl == null) {
cl = Thread.currentThread().getContextClassLoader();
}
/*
* Parsing based on format: cacheName:className[;pkFieldName[:cacheKeyJavaType]]
*
*/
List<String> parms = StringUtil.getTokens(getCacheTypeMap(), ";"); //$NON-NLS-1$
String leftside = parms.get(0);
List<String> cacheClassparm = StringUtil.getTokens(leftside, ":");
if (cacheClassparm.size() != 2) {
throw new InvalidPropertyException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25022));
}
String cn = cacheClassparm.get(0);
setCacheName(cn);
String className = cacheClassparm.get(1);
cacheTypeClass = loadClass(className);
try {
methodUtil.registerClass(cacheTypeClass);
} catch (TranslatorException e1) {
throw new ResourceException(e1);
}
if (parms.size() == 2) {
String rightside = parms.get(1);
List<String> pkKeyparm = StringUtil.getTokens(rightside, ":");
pkKey = pkKeyparm.get(0);
if (pkKeyparm.size() == 2) {
String pktype = pkKeyparm.get(1);
if (pktype != null) {
pkCacheKeyJavaType = loadClass(pktype);
}
}
}
if (!this.usingAnnotations) {
List<String> marshallers = StringUtil.getTokens(this.getMessageMarshallers(), ","); //$NON-NLS-1$
Map<String, BaseMarshaller> mmp = new HashMap<String, BaseMarshaller>(marshallers.size());
for (String mm : marshallers) {
List<String> marshallMap = StringUtil.getTokens(mm, ":"); //$NON-NLS-1$
if (marshallMap.size() != 2) {
throw new InvalidPropertyException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25031, new Object[] {mm}));
}
final String clzName = marshallMap.get(0);
final String m = marshallMap.get(1);
try {
Object bmi = (loadClass(m)).newInstance();
Class ci = loadClass(clzName);
mmp.put(clzName, (BaseMarshaller) bmi);
methodUtil.registerClass(ci);
} catch (InstantiationException e) {
throw new ResourceException(e);
} catch (IllegalAccessException e) {
throw new ResourceException(e);
} catch (TranslatorException e) {
throw new ResourceException(e);
}
}
messageMarshallerMap=Collections.unmodifiableMap(mmp);
}
return cl;
}
protected synchronized void createCacheContainer() throws ResourceException {
if (getCacheContainer() != null)
return;
RemoteCacheManager cc = null;
ClassLoader lcl = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(
this.getClass().getClassLoader());
ClassLoader classLoader = loadClasses();
switch (cacheType) {
case USE_JNDI:
cc = getRemoteCacheFromJNDI(this.getCacheJndiName(), classLoader);
break;
case REMOTE_HOT_ROD_PROPERTIES:
cc = createRemoteCacheFromProperties(classLoader);
break;
case REMOTE_SERVER_LISTS:
cc = createRemoteCacheFromServerList(classLoader);
break;
}
setCacheContainer(cc);
registerWithCacheManager(getContext(), cc, classLoader, this.usingAnnotations);
// if configured for materialization, initialize the cacheNameProxy
if (cacheNameProxy.getAliasCacheName() != null) {
RemoteCache aliasCache = cc.getCache(cacheNameProxy.getAliasCacheName());
if (aliasCache == null) {
throw new ResourceException(
InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25010, new Object[] {cacheNameProxy.getAliasCacheName()}));
}
cacheNameProxy.initializeAliasCache(aliasCache);
}
} finally {
Thread.currentThread().setContextClassLoader(lcl);
}
}
private void determineCacheType() {
String jndiName = getCacheJndiName();
if (jndiName != null && jndiName.trim().length() != 0) {
cacheType = CACHE_TYPE.USE_JNDI;
} else if (this.getHotRodClientPropertiesFile() != null) {
cacheType = CACHE_TYPE.REMOTE_HOT_ROD_PROPERTIES;
} else if (this.getRemoteServerList() != null
&& !this.getRemoteServerList().isEmpty()) {
cacheType = CACHE_TYPE.REMOTE_SERVER_LISTS;
}
}
protected abstract RemoteCacheManager createRemoteCacheFromProperties(
ClassLoader classLoader) throws ResourceException;
protected abstract RemoteCacheManager createRemoteCacheFromServerList(
ClassLoader classLoader) throws ResourceException;
private RemoteCacheManager getRemoteCacheFromJNDI(
String jndiName, ClassLoader classLoader) throws ResourceException {
Object cache = null;
try {
Context context = new InitialContext();
cache = context.lookup(jndiName);
} catch (Exception err) {
if (err instanceof RuntimeException)
throw (RuntimeException) err;
throw new ResourceException(err);
}
if (cache == null) {
throw new ResourceException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25025, jndiName));
}
if (cache instanceof RemoteCacheManager) {
LogManager.logInfo(LogConstants.CTX_CONNECTOR,
"=== Using RemoteCacheManager (loaded from JNDI " + jndiName + ") ==="); //$NON-NLS-1$
return cacheContainer;
}
throw new ResourceException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25026, cacheContainer.getClass().getName()));
}
protected void registerWithCacheManager(SerializationContext ctx, RemoteCacheManager cc, ClassLoader cl, boolean useAnnotations) throws ResourceException {
}
public Version getVersion() throws TranslatorException {
RemoteCache rc = this.getCache(this.getCacheNameProxy().getPrimaryCacheKey());
return Version.getVersion(rc.getProtocolVersion());
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime
* result
+ (protobufDefFile.hashCode());
result = prime
* result
+ ((remoteServerList == null) ? 0 : remoteServerList.hashCode());
result = prime
* result
+ ((hotrodClientPropertiesFile == null) ? 0
: hotrodClientPropertiesFile.hashCode());
result = prime * result
+ ((cacheJndiName == null) ? 0 : cacheJndiName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AbstractInfinispanManagedConnectionFactory other = (AbstractInfinispanManagedConnectionFactory) obj;
if (!checkEquals(this.remoteServerList, other.remoteServerList)) {
return false;
}
if (!checkEquals(this.hotrodClientPropertiesFile,
other.hotrodClientPropertiesFile)) {
return false;
}
if (!checkEquals(this.cacheJndiName, other.cacheJndiName)) {
return false;
}
return false;
}
public void cleanUp() {
messageMarshallerMap = null;
cacheType = null;
cacheContainer = null;
cl = null;
methodUtil.cleanUp();
}
}