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

it.tidalwave.metadata.persistence.node.impl.MetadataNodeUpdater Maven / Gradle / Ivy

/*******************************************************************************
 *
 * blueMarine - open source photo workflow
 * =======================================
 *
 * Copyright (C) 2003-2009 by Fabrizio Giudici
 * Project home page: http://bluemarine.tidalwave.it
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *
 *******************************************************************************
 *
 * $Id: MetadataNodeUpdater.java,v b89b0b568aeb 2010/02/17 15:26:52 fabrizio $
 *
 ******************************************************************************/
package it.tidalwave.metadata.persistence.node.impl;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.List;
import it.tidalwave.util.logging.Logger;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import it.tidalwave.metadata.persistence.MetadataPersistence;
import it.tidalwave.metadata.persistence.event.MetadataPersistenceAdapter;
import it.tidalwave.metadata.persistence.event.MetadataPersistenceEvent;
import it.tidalwave.metadata.persistence.node.MetadataPersistenceNodeManager;

/*******************************************************************************
 *
 * This class listens for {@link MetadataPersistence} about properties that have
 * been persisted and updates the related nodes. The delivery is decoupled in
 * two ways:
 * 
 * 
    *
  1. First, the metadata events are delivered in a generic thread, while * nodes must be updated in the EDT thread.
  2. *
  3. Second, events are buffered and node update is delayed by a few seconds, * in order to avoid wasting CPU for too frequent updates.
  4. *
* * @author Fabrizio Giudici * @version $Id: MetadataNodeUpdater.java,v b89b0b568aeb 2010/02/17 15:26:52 fabrizio $ * ******************************************************************************/ public class MetadataNodeUpdater extends MetadataPersistenceAdapter { private static final String CLASS = MetadataNodeUpdater.class.getName(); private static final Logger logger = Logger.getLogger(CLASS); private static final int PERIOD = 5000; /** The set of pending events. Being a set, duplicate events are merged, * thus coalescing the events to deliver. */ private final Set pendingEvents = new HashSet(); // TODO: maybe refreshNode() could be added to the public interface of nodeManager? private final MetadataPersistenceNodeManagerImpl nodeManager = (MetadataPersistenceNodeManagerImpl) MetadataPersistenceNodeManager.Locator.findMetadataPersistenceNodeManager(); /*************************************************************************** * * **************************************************************************/ private final ActionListener actionListener = new ActionListener() { public void actionPerformed (@Nonnull final ActionEvent e) { final List events = new ArrayList(); synchronized (pendingEvents) { events.addAll(pendingEvents); pendingEvents.clear(); } logger.fine(">>>> timer fired, now delivering %d events...", events.size()); for (final MetadataPersistenceEvent event : events) { nodeManager.refreshNode(event.getPropertySetClass(), event.getPropertyName()); } } }; private final Timer timer = new Timer(PERIOD, actionListener); /*************************************************************************** * * **************************************************************************/ @Override public void notifyPropertyPersisted (@Nonnull final MetadataPersistenceEvent event) { synchronized (pendingEvents) { pendingEvents.add(event); } logger.finer("notifyPropertyPersisted(%s) - %d pending events", event, pendingEvents.size()); if (!timer.isRunning()) { logger.finest(">>>> restarting timer..."); timer.setRepeats(false); timer.restart(); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy