DefaultPreferencesHandler.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2023 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/thesefoolishthings-src
  23.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.util.impl;

  28. import javax.annotation.Nonnull;
  29. import java.util.HashMap;
  30. import java.util.Map;
  31. import java.util.Objects;
  32. import java.util.Optional;
  33. import java.io.IOException;
  34. import java.nio.file.Files;
  35. import java.nio.file.Path;
  36. import java.nio.file.Paths;
  37. import it.tidalwave.util.Key;
  38. import it.tidalwave.util.PreferencesHandler;
  39. import lombok.Getter;
  40. import lombok.RequiredArgsConstructor;

  41. /***********************************************************************************************************************
  42.  *
  43.  * @author  Fabrizio Giudici
  44.  *
  45.  **********************************************************************************************************************/
  46. @RequiredArgsConstructor
  47. // PreferencesHandler can be used to programmatically set the log folder, so don't inject logger
  48. public class DefaultPreferencesHandler implements PreferencesHandler
  49.   {
  50.     @Getter
  51.     private final Path appFolder;

  52.     @Getter
  53.     private final Path logFolder;

  54.     private final Map<Key<?>, Object> properties = new HashMap<>();

  55.     public DefaultPreferencesHandler()
  56.       {
  57.         try
  58.           {
  59.             final var appName = System.getProperty(PROP_APP_NAME);
  60.             Objects.requireNonNull(appName, "You must call PreferencesHandler.setAppName(\"...\") before getting here");

  61.             final var osName = System.getProperty("os.name").toLowerCase();
  62.             var pattern = "";

  63.             switch (osName)
  64.               {
  65.                 case "linux":
  66.                   pattern = "%s/.%s/";
  67.                   break;

  68.                 case "mac os x":
  69.                   pattern = "%s/Library/Application Support/%s/";
  70.                   break;

  71.                 case "windows":
  72.                   pattern = "%s/AppData/Local/%s/";
  73.                   break;

  74.                 default:
  75.                   throw new ExceptionInInitializerError("Unknown o.s.: " + osName);
  76.               }

  77.             final var home = System.getProperty("user.home", "/tmp");
  78.             appFolder = Paths.get(String.format(pattern, home, appName)).toAbsolutePath();
  79.             logFolder = appFolder.resolve("logs").toAbsolutePath();
  80.             Files.createDirectories(logFolder);
  81.             // PreferencesHandler can be used to programmatically set the log folder, so don't log yet
  82.             System.out.printf("App folder: %s\n", appFolder);
  83.             System.out.printf("Logging folder: %s\n", logFolder);
  84.           }
  85.         catch (IOException e)
  86.           {
  87.             throw new RuntimeException(e);
  88.           }
  89.       }

  90.     @Override @Nonnull
  91.     public <T> Optional<T> getProperty (@Nonnull final Key<T> name)
  92.       {
  93.         return Optional.ofNullable((T)properties.get(name));
  94.       }

  95.     @Override
  96.     public <T> void setProperty (@Nonnull final Key<T> name, @Nonnull final T value)
  97.       {
  98.         properties.put(name, value);
  99.         // FIXME: should be made persistent (JSON?)
  100.       }

  101.     @Override
  102.     public <T> void setDefaultProperty (@Nonnull final Key<T> name, @Nonnull final T value)
  103.       {
  104.         if (!properties.containsKey(name))
  105.           {
  106.             setProperty(name, value);
  107.           }
  108.       }
  109.   }