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

it.tidalwave.bluebill.mobile.observation.impl.AutoPersistentObservationManager Maven / Gradle / Ivy

The newest version!
/***********************************************************************************************************************
 *
 * blueBill Mobile - Android - open source birding
 * Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.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.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://bluebill.tidalwave.it/mobile
 * SCM: https://java.net/hg/bluebill-mobile~android-src
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.observation.impl;

import javax.inject.Provider;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import org.openide.util.lookup.ServiceProvider;
import it.tidalwave.util.Id;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.mobile.io.FileSystem;
import it.tidalwave.mobile.io.MasterFileSystem;
import it.tidalwave.observation.ObservationManager;
import it.tidalwave.observation.ObservationSet;
import it.tidalwave.observation.event.ObservationEvent;
import it.tidalwave.observation.event.ObservationListener;
import it.tidalwave.observation.simple.SimpleObservationSet;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
import static it.tidalwave.observation.Observation.Observation;

/***********************************************************************************************************************
 *
 * A specific implementation of {@link ObservationManager} that automatically persists the managed 
 * {@link ObservationSet} to the filesystem after any change.
 * 
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
@ServiceProvider(service=ObservationManager.class) @Slf4j
public class AutoPersistentObservationManager implements ObservationManager
  {
    public static final String NAME = "ObservationSet.ser";
    
    @CheckForNull
    private SimpleObservationSet observationSet;

    private FileSystem fileSystem;

    private final Provider masterFileSystem = Locator.createProviderFor(MasterFileSystem.class);

    /*******************************************************************************************************************
     *
     * Tracks changes in the {@code ObservationSet} and persists them.
     *
     ******************************************************************************************************************/
    private final ObservationListener saveAnyChangeListener = new ObservationListener()
      {
        public void notifyObservationAdded (final @Nonnull ObservationEvent event)
          {
            save();
          }

        public void notifyObservationRemoved (final @Nonnull ObservationEvent event)
          {
            save();
          }

        public void notifyObservationChanged (final @Nonnull ObservationEvent event)
          {
            save();
          }

        public void notifyCleared (final @Nonnull ObservationEvent event)
          {
            save();
          }
      };

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public AutoPersistentObservationManager()
      {
        try
          {
            fileSystem = masterFileSystem.get().getInternalFileSystem();
          }
        catch (Locator.NotFoundException e) // FIXME: shouldn't we just throw it?
          {
            log.warn("No MasterFileSystem");
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public ObservationSet createObservationSet()
      {
        throw new UnsupportedOperationException("Not supported yet.");
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public ObservationSet createObservationSet (final @Nonnull Object repository)
      {
        throw new UnsupportedOperationException("Not supported yet.");
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public synchronized ObservationSet findOrCreateObservationSetById (final @Nonnull Id id)
      {
        if (observationSet == null)
          {
            try
              {
                log.info("Loading observations from {}...", NAME);
                final long time = System.currentTimeMillis();
                final @Cleanup ObjectInputStream ois = new ObjectInputStream(fileSystem.openFileInput(NAME));
                observationSet = (SimpleObservationSet)ois.readObject();
                log.info(">>>> loaded %d items in {} msec", observationSet.find(Observation).count(),
                                                            System.currentTimeMillis() - time);
              }
            catch (Throwable t)
              {
                log.warn("Cannot load observations", t);
                observationSet = new SimpleObservationSet();
              }

            observationSet.addListener(saveAnyChangeListener);
          }

        return observationSet;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void save()
      {
        if (fileSystem == null)
          {
            log.warn("Not saving observations, no FileSystem availble");
          }
        else
          {
            log.info(">>>> saving {}...", observationSet);

            try
              {
                log.info("Saving observations to {}...", NAME);
                final long time = System.currentTimeMillis();
                final @Cleanup ObjectOutputStream oos = new ObjectOutputStream(fileSystem.openFileOutput(NAME));
                oos.writeObject(observationSet);
                log.info(">>>> saved {} items in {} msec", observationSet.find(Observation).count(),
                                                              System.currentTimeMillis() - time);
              }
            catch (Throwable t)
              {
                log.error("Can't save observations", t);
              }
          }
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy