1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.fjank.jcache;
20
21 import java.io.File;
22 import java.io.Serializable;
23 import java.util.Iterator;
24 import javax.util.jcache.Attributes;
25 import javax.util.jcache.CacheException;
26 import javax.util.jcache.CacheFullException;
27 import javax.util.jcache.CacheLoader;
28 import javax.util.jcache.CacheLogger;
29 import javax.util.jcache.DiskCacheException;
30 import javax.util.jcache.NotARetrievableObjectException;
31 import javax.util.jcache.ObjectNotFoundException;
32 import org.fjank.jcache.persistence.DiskCache;
33
34
35
36
37
38
39
40
41
42 public final class CacheAccessImpl2 {
43
44 private boolean valid = true;
45
46 private CacheRegion region;
47 private Class lastException;
48 private String lastMessage;
49
50
51
52
53
54
55
56 CacheAccessImpl2(final CacheRegion aRegion) {
57 this.region = aRegion;
58 }
59
60
61
62
63
64
65
66
67
68
69
70 public void cancelResponse() {
71 if (!isValid()) {
72 return;
73 }
74 }
75
76
77
78
79
80
81 private boolean isValid() {
82 if (this.valid) {
83 return true;
84 }
85 return false;
86 }
87
88
89
90
91
92 public void close() {
93 if (!isValid()) {
94 return;
95 }
96 region = null;
97 this.valid = false;
98 }
99
100
101
102
103
104
105
106 public void defineGroup(final String name) {
107 if (!isValid()) {
108 return;
109 }
110 defineGroupImpl(name, null, this.getAttributes());
111 }
112
113
114
115
116
117
118
119
120 public void defineGroup(final String name, final Attributes attributes) {
121 if (!isValid()) {
122 return;
123 }
124 defineGroupImpl(name, null, attributes);
125 }
126
127
128
129
130
131
132
133
134
135 public void defineGroup(final String name, final String group) {
136 if (!isValid()) {
137 return;
138 }
139 defineGroupImpl(name, group, null);
140 }
141
142
143
144
145
146
147
148
149
150 public void defineGroup(final String name, final String groupName, final Attributes attributes) {
151 if (!isValid()) {
152 return;
153 }
154 defineGroupImpl(name, groupName, attributes);
155 }
156
157
158
159
160
161
162
163 private void defineGroupImpl(final String name, final String groupName, final Attributes attributes) {
164 if (name == null) {
165 return;
166 }
167 if (name.equals("")) {
168 return;
169 }
170 CacheGroup group;
171 if (groupName == null) {
172 group = region;
173 } else {
174 group = region.getGroup(groupName);
175 if (group == null) {
176 return;
177 }
178 }
179 group.put(new CacheGroup(name, new AttributesImpl(attributes)));
180 }
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 public void defineObject(final Object name, final Attributes attributes) {
196 if (!isValid()) {
197 return;
198 }
199 putImpl(name, null, attributes, NullObject.getInstance());
200 }
201
202
203
204
205
206
207
208
209
210
211 public void defineObject(final Object name, final String group, final Attributes attributes) {
212 if (!isValid()) {
213 return;
214 }
215 putImpl(name, group, attributes, NullObject.getInstance());
216 }
217
218
219
220
221
222
223
224
225 public void destroy() {
226 if (!isValid()) {
227 return;
228 }
229 if (region.getName() != null) {
230 destroy(region.getName());
231 } else {
232 region.destroy();
233 }
234 close();
235 }
236
237
238
239
240
241
242
243
244
245 public void destroy(final Object name) {
246 if (!isValid()) {
247 return;
248 }
249 CacheImpl.getCache(true).destroyRegion(name);
250 }
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270 public Object get(final Object name) {
271 if (!isValid()) {
272 return null;
273 }
274 return getImpl(name, null, null);
275 }
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290 public Object get(final Object name, final Object arguments) {
291 if (!isValid()) {
292 return null;
293 }
294 return getImpl(name, null, arguments);
295 }
296
297
298
299
300
301
302 private Object getImpl(final Object name, final String group, final Object arguments) {
303 CacheObject objHandle = getCacheObject(name, group);
304 if (objHandle == null) {
305 return null;
306 }
307 return getTrueObject(objHandle, arguments);
308 }
309
310 private Object getTrueObject(final CacheObject objHandle, final Object arguments) {
311 if (objHandle.needsLoading(arguments)) {
312 Attributes attribs = objHandle.getAttributes();
313 CacheLoader loader = attribs.getLoader();
314 Object retrievedObject;
315 try {
316 retrievedObject = loader.load(objHandle, arguments);
317 if (retrievedObject == null) {
318 throw new ObjectNotFoundException("The returned object from the CacheLoader " + loader.getClass().getName() + " was null.");
319 }
320 if (retrievedObject instanceof CacheOutputStream) {
321
322
323
324
325
326
327
328
329
330
331
332
333
334 return ((CacheOutputStream) retrievedObject).getStreamObject().getInputStream();
335 } else if (retrievedObject instanceof File) {
336
337
338
339
340 return ((File) retrievedObject).getAbsolutePath();
341 }
342 attribs.setCreateTime(System.currentTimeMillis());
343 } catch (CacheException e) {
344 this.lastException = e.getClass();
345 this.lastMessage = e.getMessage();
346 return null;
347 }
348 CacheGroup group = objHandle.getGroup();
349 CacheObject newRef = new CacheObject(objHandle.getKey(), retrievedObject, group, region, CacheImpl.getCache().getReferenceQueue());
350 group.replace(objHandle.getKey(), newRef);
351 return newRef.get();
352 }
353 return objHandle.get();
354 }
355
356
357
358
359
360
361
362
363 private CacheObject getCacheObject(final Object name, final String group) {
364 Object object = null;
365
366 CacheGroup parentCacheObject = region;
367 if (group != null) {
368 parentCacheObject = region.getGroup(group);
369 if (parentCacheObject == null) {
370 return null;
371 }
372 }
373 if (parentCacheObject.contains(name)) {
374 object = parentCacheObject.get(name);
375 } else if (name instanceof Serializable) {
376
377 try {
378 DiskCache diskCache = CacheImpl.getCache().getDiskCache();
379 if (diskCache != null) {
380 object = diskCache.getObject((Serializable) name);
381 if (object == null) {
382 return null;
383 }
384 }
385 } catch (DiskCacheException e) {
386 this.lastException = DiskCacheException.class;
387 this.lastMessage = e.getMessage();
388 return null;
389 }
390 }
391 if (object == null) {
392 return null;
393 }
394 if (object instanceof CacheGroup) {
395 this.lastException = NotARetrievableObjectException.class;
396 this.lastMessage = ("This object is a group and cannot be retrieved.");
397 return null;
398 }
399 CacheObject objHandle = (CacheObject) object;
400 return objHandle;
401 }
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418 public Object get(final Object name, final String group, final Object arguments) {
419 if (!isValid()) {
420 return null;
421 }
422 return getImpl(name, group, arguments);
423 }
424
425
426
427
428
429
430
431
432 public Attributes getAttributes() {
433 if (!isValid()) {
434 return null;
435 }
436 return region.getAttributes();
437 }
438
439
440
441
442
443
444
445
446
447
448 public Attributes getAttributes(final Object name) {
449 if (!isValid()) {
450 return null;
451 }
452 if (name == null) {
453 return null;
454 }
455 Object obj = region.get(name);
456 if (obj == null) {
457 return null;
458 }
459 if (obj instanceof CacheObject) {
460 return ((CacheObject) obj).getAttributes();
461 } else if (obj instanceof CacheGroup) {
462 return ((CacheGroup) obj).getAttributes();
463 }
464 this.lastException = CacheException.class;
465 this.lastMessage = "The object " + name + " is not valid. (" + obj.getClass().getName() + ')';
466 return null;
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 public boolean getOwnership(final Object name, final int timeout) {
492 if (!isValid()) {
493 return false;
494 }
495 return false;
496 }
497
498
499
500
501
502 public void invalidate() {
503 if (!isValid()) {
504 return;
505 }
506 region.invalidate();
507
508 }
509
510
511
512
513
514
515
516
517
518
519
520
521
522 public void invalidate(final Object name) {
523 if (!isValid()) {
524 return;
525 }
526 invalidateWithoutDistribution(name);
527 if (CacheImpl.getCache().isDistributed()) {
528 CacheImpl.getCache().getDistributionEngine().cacheObjectInvalidated(getRegionName(), (Serializable) name);
529 }
530 }
531
532 private void invalidateWithoutDistribution(final Object name) {
533 if (!region.contains(name)) {
534 return;
535 }
536 Object object = region.get(name);
537 if (object == null) {
538 return;
539 }
540 if (object instanceof CacheGroup) {
541 CacheGroup group = (CacheGroup) object;
542 group.invalidate();
543 } else {
544
545 if (object instanceof CacheObject) {
546 CacheObject obj = (CacheObject) object;
547 obj.invalidate();
548 }
549 }
550 }
551
552
553
554
555
556
557
558
559
560 public boolean isPresent(final Object name) {
561 if (!isValid()) {
562 return false;
563 }
564 return region.contains(name);
565 }
566
567
568
569
570
571
572
573
574
575
576
577 public void preLoad(final Object name) {
578 if (!isValid()) {
579 return;
580 }
581 preLoad(name, null);
582 }
583
584
585
586
587
588
589
590
591
592 public void preLoad(final Object name, final Object arguments) {
593 if (!isValid()) {
594 return;
595 }
596 preLoad(name, null, arguments);
597 }
598
599
600
601
602
603
604
605
606
607
608
609 public void preLoad(final Object name, final String group, final Object arguments) {
610 if (!isValid()) {
611 return;
612 }
613 if (name == null) {
614
615 return;
616 }
617 final CacheLogger logger = CacheImpl.getCache().getAttributes().getLogger();
618 try {
619 final CacheObject obj = getCacheObject(name, group);
620 final CacheLoader loader = obj.getAttributes().getLoader();
621 if (loader == null) {
622 throw new ObjectNotFoundException("The object has no CacheLoader associated with it.");
623 }
624 Runnable runnable = new Runnable() {
625 public void run() {
626 try {
627 loader.load(obj, arguments);
628 } catch (CacheException e) {
629 if (logger != null) {
630 logger.log("The object was not found in the cache.", e);
631 }
632 }
633 }
634 };
635 CacheImpl.getCache().getExecPool().execute(runnable);
636 } catch (ObjectNotFoundException e) {
637 if (logger != null) {
638 logger.log("The object was not found in the cache.", e);
639 }
640 } catch (InterruptedException e) {
641 if (logger != null) {
642 logger.log("Some strange concurrency issue have occured.", e);
643 }
644 }
645 }
646
647
648
649
650
651
652
653
654
655
656
657
658
659 public void put(final Object name, final Attributes attributes, final Object object) {
660 if (!isValid()) {
661 return;
662 }
663 putImpl(name, null, attributes, object);
664 }
665
666
667
668
669
670
671
672
673
674
675
676
677
678 public void put(final Object name, final String group, final Attributes attributes, final Object object) {
679 putImpl(name, group, attributes, object);
680 }
681
682
683
684
685 private int getCurrentObjectCount() {
686 int count = 0;
687 count += CacheImpl.getCache().getRegion().getObjectCount();
688 Iterator regions = CacheImpl.getCache().userRegionNames();
689 while (regions.hasNext()) {
690 count += CacheImpl.getCache().getRegion(regions.next()).getObjectCount();
691 }
692 return count;
693 }
694
695
696
697
698
699
700
701
702
703
704 public CacheException getException(boolean cached) {
705 if (lastException == null) {
706 return null;
707 }
708 try {
709 CacheException ex = (CacheException) lastException.newInstance();
710 ex.setMessage(lastMessage);
711 this.lastException = null;
712 this.lastMessage = null;
713 return ex;
714 } catch (InstantiationException e) {
715 throw new IllegalStateException(e.getMessage() + ":" + lastMessage);
716 } catch (IllegalAccessException e) {
717 throw new IllegalStateException(e.getMessage() + ":" + lastMessage);
718 }
719 }
720
721
722
723
724
725
726
727
728
729
730 private boolean putImpl(final Object name, final String group, final Attributes attributes, final Object object) {
731 CacheGroup objGr = region;
732 if (group != null) {
733 objGr = region.getGroup(group);
734 if (objGr == null) {
735 this.lastException = ObjectNotFoundException.class;
736 this.lastMessage = "The object " + group + " is not present in this cache.";
737 return false;
738 }
739 }
740 CacheObject o = new CacheObject(name, object, objGr, region, CacheImpl.getCache().getReferenceQueue());
741 o.setAttributes(attributes);
742 int maxObjects = CacheImpl.getCache().getAttributes().getMaxObjects();
743 int currentObjectCount = getCurrentObjectCount();
744 if (currentObjectCount >= maxObjects) {
745 DiskCache diskCache = CacheImpl.getCache().getDiskCache();
746 if (diskCache == null) {
747
748 this.lastException = CacheFullException.class;
749 this.lastMessage = "The maximum number of objects in the cache has been reached.";
750 return false;
751 }
752 boolean updated = diskCache.update(o);
753 if (!updated) {
754 this.lastException = CacheFullException.class;
755 this.lastMessage = "The maximum size for the diskCache has been reached.";
756 }
757 return updated;
758 }
759 if ((attributes != null) && (attributes.getSize() != 0)) {
760 if ((region.getCurrentSize() + attributes.getSize()) > (CacheImpl.getCache().getAttributes().getMemoryCacheSize() * 1024 * 1024)) {
761 DiskCache diskCache = CacheImpl.getCache().getDiskCache();
762 if (diskCache == null) {
763
764 this.lastException = CacheFullException.class;
765 this.lastMessage = "The maximum size for the memory cache has been reached.";
766 return false;
767 }
768 boolean updated = diskCache.update(o);
769 if (!updated) {
770 this.lastException = CacheFullException.class;
771 this.lastMessage = "The maximum size for the diskCache has been reached.";
772 }
773 return updated;
774 }
775 }
776 objGr.put(name, o, object);
777 return true;
778 }
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793 public boolean put(final Object name, final Object object) {
794 if (!isValid()) {
795 return false;
796 }
797 return putImpl(name, null, null, object);
798 }
799
800
801
802
803
804
805
806
807
808
809
810
811 public void put(Object name, String group, Object object) {
812 if (!isValid()) {
813 return;
814 }
815 putImpl(name, group, null, object);
816 }
817
818
819
820
821
822
823
824
825 public void releaseOwnership() {
826 if (!isValid()) {
827 return;
828 }
829 }
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846 public Object replace(Object name, Object object) {
847 if (!isValid()) {
848 return null;
849 }
850 if (name == null)
851 return null;
852 if (object == null)
853 return null;
854 Object replacedObject = replaceWithoutDistribution(name, object);
855 if (CacheImpl.getCache().isDistributed()) {
856 CacheImpl.getCache().getDistributionEngine().cacheObjectUpdated(getRegionName(), null, (Serializable) name, (Serializable) object);
857 }
858 return replacedObject;
859 }
860
861
862
863
864
865 public Object replaceWithoutDistribution(Object name, Object object) {
866 if (!isValid()) {
867 return null;
868 }
869 return region.replace(name, new CacheObject(name, object, region, region, CacheImpl.getCache().getReferenceQueue()));
870 }
871
872
873
874
875
876
877
878
879
880
881
882 public Object replace(Object name, String group, Object object) {
883 if (!isValid()) {
884 return null;
885 }
886 if (name == null)
887 return null;
888 if (object == null)
889 return null;
890 if (group == null)
891 return null;
892 Object replacedObject = replaceWithoutDistribution(name, group, object);
893 if (CacheImpl.getCache().isDistributed()) {
894 CacheImpl.getCache().getDistributionEngine().cacheObjectUpdated(getRegionName(), group, (Serializable) name, (Serializable) object);
895 }
896 return replacedObject;
897 }
898
899
900
901
902
903 public Object replaceWithoutDistribution(Object name, String group, Object object) {
904 if (!isValid()) {
905 return null;
906 }
907 CacheGroup cachegroup = (CacheGroup) region.get(group);
908 if (cachegroup == null)
909 return null;
910 return cachegroup.replace(name, new CacheObject(name, object, region, region, CacheImpl.getCache().getReferenceQueue()));
911 }
912
913
914
915
916
917
918
919
920
921
922
923
924
925 public void resetAttributes(Attributes attributes) {
926 if (!isValid()) {
927 return;
928 }
929 Attributes att = region.getAttributes();
930 att.reset();
931 att.applyAttributes(attributes);
932 }
933
934
935
936
937
938
939
940
941 public void resetAttributes(Object name, Attributes attributes) {
942 if (!isValid()) {
943 return;
944 }
945 Attributes att = CacheImpl.getCache(true).getRegion(name).getAttributes();
946 att.reset();
947 att.applyAttributes(attributes);
948 }
949
950
951
952
953
954
955
956
957
958 public void save() {
959 if (!isValid()) {
960 return;
961 }
962 saveImpl(region);
963 }
964
965
966
967
968
969
970
971
972 private void saveImpl(CacheRegion region) {
973 }
974
975
976
977
978
979
980
981
982
983
984
985 public void save(Object name) {
986 if (!isValid()) {
987 return;
988 }
989 saveImpl(CacheImpl.getCache().getRegion(name));
990 }
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005 public void waitForResponse(int timeout) {
1006 if (!isValid()) {
1007 return;
1008 }
1009 }
1010
1011
1012
1013
1014
1015
1016 public CacheRegion getRegion() {
1017 return region;
1018 }
1019
1020 private String getRegionName() {
1021 return region.getName() == null ? null : region.getName().toString();
1022 }
1023 }