DefaultPreferencesHandler.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2025 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 the License.
  12.  * 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 an "AS IS" BASIS, WITHOUT WARRANTIES OR
  17.  * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
  18.  *
  19.  * *************************************************************************************************************************************************************
  20.  *
  21.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  22.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.util.impl;

  27. import jakarta.annotation.Nonnull;
  28. import java.util.HashMap;
  29. import java.util.Locale;
  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 it.tidalwave.util.Key;
  37. import it.tidalwave.util.PreferencesHandler;
  38. import lombok.Getter;
  39. import lombok.RequiredArgsConstructor;

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

  51.     @Getter
  52.     private final Path logFolder;

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

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

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

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

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

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

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

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

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

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

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