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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data.util.sqlcontainer;
  17. import java.io.Serializable;
  18. import java.lang.ref.ReferenceQueue;
  19. import java.lang.ref.WeakReference;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import com.vaadin.data.util.sqlcontainer.query.FreeformQuery;
  23. import com.vaadin.data.util.sqlcontainer.query.QueryDelegate;
  24. import com.vaadin.data.util.sqlcontainer.query.TableQuery;
  25. /**
  26. * CacheFlushNotifier is a simple static notification mechanism to inform other
  27. * SQLContainers that the contents of their caches may have become stale.
  28. */
  29. class CacheFlushNotifier implements Serializable {
  30. /*
  31. * SQLContainer instance reference list and dead reference queue. Used for
  32. * the cache flush notification feature.
  33. */
  34. private static List<WeakReference<SQLContainer>> allInstances = new ArrayList<WeakReference<SQLContainer>>();
  35. private static ReferenceQueue<SQLContainer> deadInstances = new ReferenceQueue<SQLContainer>();
  36. /**
  37. * Adds the given SQLContainer to the cache flush notification receiver list
  38. *
  39. * @param c
  40. * Container to add
  41. */
  42. public static void addInstance(SQLContainer c) {
  43. removeDeadReferences();
  44. if (c != null) {
  45. allInstances.add(new WeakReference<SQLContainer>(c, deadInstances));
  46. }
  47. }
  48. /**
  49. * Removes dead references from instance list
  50. */
  51. private static void removeDeadReferences() {
  52. java.lang.ref.Reference<? extends SQLContainer> dead = deadInstances
  53. .poll();
  54. while (dead != null) {
  55. allInstances.remove(dead);
  56. dead = deadInstances.poll();
  57. }
  58. }
  59. /**
  60. * Iterates through the instances and notifies containers which are
  61. * connected to the same table or are using the same query string.
  62. *
  63. * @param c
  64. * SQLContainer that issued the cache flush notification
  65. */
  66. public static void notifyOfCacheFlush(SQLContainer c) {
  67. removeDeadReferences();
  68. for (WeakReference<SQLContainer> wr : allInstances) {
  69. if (wr.get() != null) {
  70. SQLContainer wrc = wr.get();
  71. if (wrc == null) {
  72. continue;
  73. }
  74. /*
  75. * If the reference points to the container sending the
  76. * notification, do nothing.
  77. */
  78. if (wrc.equals(c)) {
  79. continue;
  80. }
  81. /* Compare QueryDelegate types and tableName/queryString */
  82. QueryDelegate wrQd = wrc.getQueryDelegate();
  83. QueryDelegate qd = c.getQueryDelegate();
  84. if (wrQd instanceof TableQuery
  85. && qd instanceof TableQuery
  86. && ((TableQuery) wrQd).getTableName().equals(
  87. ((TableQuery) qd).getTableName())) {
  88. wrc.refresh();
  89. } else if (wrQd instanceof FreeformQuery
  90. && qd instanceof FreeformQuery
  91. && ((FreeformQuery) wrQd).getQueryString().equals(
  92. ((FreeformQuery) qd).getQueryString())) {
  93. wrc.refresh();
  94. }
  95. }
  96. }
  97. }
  98. }