DefaultMediaFileSystem.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * blueMarine II: Semantic Media Centre
  5.  * http://tidalwave.it/projects/bluemarine2
  6.  *
  7.  * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *********************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  12.  * the License. You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  17.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations under the License.
  19.  *
  20.  * *********************************************************************************************************************
  21.  *
  22.  * git clone https://bitbucket.org/tidalwave/bluemarine2-src
  23.  * git clone https://github.com/tidalwave-it/bluemarine2-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.bluemarine2.model.impl;

  28. import javax.annotation.Nonnull;
  29. import java.util.concurrent.CountDownLatch;
  30. import java.nio.file.Path;
  31. import it.tidalwave.util.NotFoundException;
  32. import it.tidalwave.util.annotation.VisibleForTesting;
  33. import it.tidalwave.util.spi.PriorityAsSupport;
  34. import it.tidalwave.messagebus.annotation.ListensTo;
  35. import it.tidalwave.messagebus.annotation.SimpleMessageSubscriber;
  36. import it.tidalwave.bluemarine2.message.PowerOnNotification;
  37. import it.tidalwave.bluemarine2.model.MediaFileSystem;
  38. import it.tidalwave.bluemarine2.model.MediaFolder;
  39. import it.tidalwave.bluemarine2.model.ModelPropertyNames;
  40. import lombok.experimental.Delegate;
  41. import lombok.Getter;
  42. import lombok.extern.slf4j.Slf4j;
  43. import static java.util.concurrent.TimeUnit.SECONDS;

  44. /***********************************************************************************************************************
  45.  *
  46.  * @author  Fabrizio Giudici
  47.  *
  48.  **********************************************************************************************************************/
  49. @SimpleMessageSubscriber @Slf4j
  50. public class DefaultMediaFileSystem implements MediaFileSystem
  51.   {
  52.     private final CountDownLatch initialized = new CountDownLatch(1);

  53.     @Getter
  54.     private Path rootPath;

  55.     @Delegate
  56.     private final PriorityAsSupport asSupport = new PriorityAsSupport(this);

  57.     /*******************************************************************************************************************
  58.      *
  59.      *
  60.      ******************************************************************************************************************/
  61.     @Override @Nonnull
  62.     public MediaFolder getRoot()
  63.       {
  64.         waitForPowerOn();
  65.         return new FileSystemMediaFolder(rootPath, null, rootPath);
  66.       }

  67.     /*******************************************************************************************************************
  68.      *
  69.      *
  70.      ******************************************************************************************************************/
  71.     @VisibleForTesting public void onPowerOnNotification (@ListensTo @Nonnull final PowerOnNotification notification)
  72.       throws NotFoundException
  73.       {
  74.         log.info("onPowerOnNotification({})", notification);
  75.         rootPath = notification.getProperties().get(ModelPropertyNames.ROOT_PATH).resolve("Music");
  76.         log.info(">>>> rootPath: {}", rootPath);
  77.         initialized.countDown();
  78.       }

  79.     /*******************************************************************************************************************
  80.      *
  81.      *
  82.      ******************************************************************************************************************/
  83.     private void waitForPowerOn()
  84.       {
  85.         try
  86.           {
  87.             if (!initialized.await(10, SECONDS))
  88.               {
  89.                 throw new IllegalStateException("rooPath null: did not receive PowerOnNotification");
  90.               }
  91.           }
  92.         catch (InterruptedException ex)
  93.           {
  94.             throw new IllegalStateException("Interrupted while waiting for PowerOnNotification");
  95.           }
  96.       }
  97.   }