You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CacheFlushNotifier.java 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util.sqlcontainer;
  5. import java.io.Serializable;
  6. import java.lang.ref.ReferenceQueue;
  7. import java.lang.ref.WeakReference;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import com.vaadin.data.util.sqlcontainer.query.FreeformQuery;
  11. import com.vaadin.data.util.sqlcontainer.query.QueryDelegate;
  12. import com.vaadin.data.util.sqlcontainer.query.TableQuery;
  13. /**
  14. * CacheFlushNotifier is a simple static notification mechanism to inform other
  15. * SQLContainers that the contents of their caches may have become stale.
  16. */
  17. class CacheFlushNotifier implements Serializable {
  18. /*
  19. * SQLContainer instance reference list and dead reference queue. Used for
  20. * the cache flush notification feature.
  21. */
  22. private static List<WeakReference<SQLContainer>> allInstances = new ArrayList<WeakReference<SQLContainer>>();
  23. private static ReferenceQueue<SQLContainer> deadInstances = new ReferenceQueue<SQLContainer>();
  24. /**
  25. * Adds the given SQLContainer to the cache flush notification receiver list
  26. *
  27. * @param c
  28. * Container to add
  29. */
  30. public static void addInstance(SQLContainer c) {
  31. removeDeadReferences();
  32. if (c != null) {
  33. allInstances.add(new WeakReference<SQLContainer>(c, deadInstances));
  34. }
  35. }
  36. /**
  37. * Removes dead references from instance list
  38. */
  39. private static void removeDeadReferences() {
  40. java.lang.ref.Reference<? extends SQLContainer> dead = deadInstances
  41. .poll();
  42. while (dead != null) {
  43. allInstances.remove(dead);
  44. dead = deadInstances.poll();
  45. }
  46. }
  47. /**
  48. * Iterates through the instances and notifies containers which are
  49. * connected to the same table or are using the same query string.
  50. *
  51. * @param c
  52. * SQLContainer that issued the cache flush notification
  53. */
  54. public static void notifyOfCacheFlush(SQLContainer c) {
  55. removeDeadReferences();
  56. for (WeakReference<SQLContainer> wr : allInstances) {
  57. if (wr.get() != null) {
  58. SQLContainer wrc = wr.get();
  59. if (wrc == null) {
  60. continue;
  61. }
  62. /*
  63. * If the reference points to the container sending the
  64. * notification, do nothing.
  65. */
  66. if (wrc.equals(c)) {
  67. continue;
  68. }
  69. /* Compare QueryDelegate types and tableName/queryString */
  70. QueryDelegate wrQd = wrc.getQueryDelegate();
  71. QueryDelegate qd = c.getQueryDelegate();
  72. if (wrQd instanceof TableQuery
  73. && qd instanceof TableQuery
  74. && ((TableQuery) wrQd).getTableName().equals(
  75. ((TableQuery) qd).getTableName())) {
  76. wrc.refresh();
  77. } else if (wrQd instanceof FreeformQuery
  78. && qd instanceof FreeformQuery
  79. && ((FreeformQuery) wrQd).getQueryString().equals(
  80. ((FreeformQuery) qd).getQueryString())) {
  81. wrc.refresh();
  82. }
  83. }
  84. }
  85. }
  86. }