All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vaadin.v7.data.util.sqlcontainer.CacheFlushNotifier Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2000-2024 Vaadin Ltd
 *
 * This program is available under Vaadin Commercial License and Service Terms.
 *
 * See  for the full
 * license.
 */
package com.vaadin.v7.data.util.sqlcontainer;

import java.io.Serializable;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import com.vaadin.v7.data.util.sqlcontainer.query.FreeformQuery;
import com.vaadin.v7.data.util.sqlcontainer.query.QueryDelegate;
import com.vaadin.v7.data.util.sqlcontainer.query.TableQuery;

/**
 * CacheFlushNotifier is a simple static notification mechanism to inform other
 * SQLContainers that the contents of their caches may have become stale.
 */
@Deprecated
class CacheFlushNotifier implements Serializable {
    /*
     * SQLContainer instance reference list and dead reference queue. Used for
     * the cache flush notification feature.
     */
    private static List> allInstances = new ArrayList>();
    private static ReferenceQueue deadInstances = new ReferenceQueue();

    /**
     * Adds the given SQLContainer to the cache flush notification receiver list
     *
     * @param c
     *            Container to add
     */
    public static void addInstance(SQLContainer c) {
        removeDeadReferences();
        if (c != null) {
            allInstances.add(new WeakReference(c, deadInstances));
        }
    }

    /**
     * Removes dead references from instance list
     */
    private static void removeDeadReferences() {
        Reference dead = deadInstances
                .poll();
        while (dead != null) {
            allInstances.remove(dead);
            dead = deadInstances.poll();
        }
    }

    /**
     * Iterates through the instances and notifies containers which are
     * connected to the same table or are using the same query string.
     *
     * @param c
     *            SQLContainer that issued the cache flush notification
     */
    public static void notifyOfCacheFlush(SQLContainer c) {
        removeDeadReferences();
        for (WeakReference wr : allInstances) {
            if (wr.get() != null) {
                SQLContainer wrc = wr.get();
                if (wrc == null) {
                    continue;
                }
                /*
                 * If the reference points to the container sending the
                 * notification, do nothing.
                 */
                if (wrc.equals(c)) {
                    continue;
                }
                /* Compare QueryDelegate types and tableName/queryString */
                QueryDelegate wrQd = wrc.getQueryDelegate();
                QueryDelegate qd = c.getQueryDelegate();
                if (wrQd instanceof TableQuery && qd instanceof TableQuery
                        && ((TableQuery) wrQd).getTableName()
                                .equals(((TableQuery) qd).getTableName())) {
                    wrc.refresh();
                } else if (wrQd instanceof FreeformQuery
                        && qd instanceof FreeformQuery
                        && ((FreeformQuery) wrQd).getQueryString().equals(
                                ((FreeformQuery) qd).getQueryString())) {
                    wrc.refresh();
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy