Content of file DefaultPreferencesHandler.java.html
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>DefaultPreferencesHandler.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">TheseFoolishThings :: Examples :: DCI :: Displayable</a> > <a href="../index.html" class="el_bundle">it-tidalwave-util</a> > <a href="index.source.html" class="el_package">it.tidalwave.util.impl</a> > <span class="el_source">DefaultPreferencesHandler.java</span></div><h1>DefaultPreferencesHandler.java</h1><pre class="source lang-java linenums">/*
* *********************************************************************************************************************
*
* TheseFoolishThings: Miscellaneous utilities
* http://tidalwave.it/projects/thesefoolishthings
*
* Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://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.
*
* *********************************************************************************************************************
*
* git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
* git clone https://github.com/tidalwave-it/thesefoolishthings-src
*
* *********************************************************************************************************************
*/
package it.tidalwave.util.impl;
import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import it.tidalwave.util.Key;
import it.tidalwave.util.PreferencesHandler;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
*
**********************************************************************************************************************/
<span class="nc" id="L48">@RequiredArgsConstructor</span>
// PreferencesHandler can be used to programmatically set the log folder, so don't inject logger
public class DefaultPreferencesHandler implements PreferencesHandler
{
<span class="nc" id="L52"> @Getter</span>
private final Path appFolder;
<span class="nc" id="L55"> @Getter</span>
private final Path logFolder;
<span class="nc" id="L58"> private final Map<Key<?>, Object> properties = new HashMap<>();</span>
public DefaultPreferencesHandler()
<span class="nc" id="L61"> {</span>
try
{
<span class="nc" id="L64"> final var appName = System.getProperty(PROP_APP_NAME);</span>
<span class="nc" id="L65"> Objects.requireNonNull(appName, "You must call PreferencesHandler.setAppName(\"...\") before getting here");</span>
<span class="nc" id="L67"> final var osName = System.getProperty("os.name").toLowerCase();</span>
<span class="nc" id="L68"> var pattern = "";</span>
<span class="nc bnc" id="L70" title="All 4 branches missed."> switch (osName)</span>
{
case "linux":
<span class="nc" id="L73"> pattern = "%s/.%s/";</span>
<span class="nc" id="L74"> break;</span>
case "mac os x":
<span class="nc" id="L77"> pattern = "%s/Library/Application Support/%s/";</span>
<span class="nc" id="L78"> break;</span>
case "windows":
<span class="nc" id="L81"> pattern = "%s/AppData/Local/%s/";</span>
<span class="nc" id="L82"> break;</span>
default:
<span class="nc" id="L85"> throw new ExceptionInInitializerError("Unknown o.s.: " + osName);</span>
}
<span class="nc" id="L88"> final var home = System.getProperty("user.home", "/tmp");</span>
<span class="nc" id="L89"> appFolder = Paths.get(String.format(pattern, home, appName)).toAbsolutePath();</span>
<span class="nc" id="L90"> logFolder = appFolder.resolve("logs").toAbsolutePath();</span>
<span class="nc" id="L91"> Files.createDirectories(logFolder);</span>
// PreferencesHandler can be used to programmatically set the log folder, so don't log yet
<span class="nc bnc" id="L93" title="All 2 branches missed."> if (!Boolean.getBoolean(PROP_SUPPRESS_CONSOLE))</span>
{
<span class="nc" id="L95"> System.out.printf("App folder: %s\n", appFolder);</span>
<span class="nc" id="L96"> System.out.printf("Logging folder: %s\n", logFolder);</span>
}
}
<span class="nc" id="L99"> catch (IOException e)</span>
{
<span class="nc" id="L101"> throw new RuntimeException(e);</span>
<span class="nc" id="L102"> }</span>
<span class="nc" id="L103"> }</span>
@Override @Nonnull
public <T> Optional<T> getProperty (@Nonnull final Key<T> name)
{
<span class="nc" id="L108"> return Optional.ofNullable((T)properties.get(name));</span>
}
@Override
public <T> void setProperty (@Nonnull final Key<T> name, @Nonnull final T value)
{
<span class="nc" id="L114"> properties.put(name, value);</span>
// FIXME: should be made persistent (JSON?)
| should be made persistent (JSON?) | |
<span class="nc" id="L116"> }</span>
@Override
public <T> void setDefaultProperty (@Nonnull final Key<T> name, @Nonnull final T value)
{
<span class="nc bnc" id="L121" title="All 2 branches missed."> if (!properties.containsKey(name))</span>
{
<span class="nc" id="L123"> setProperty(name, value);</span>
}
<span class="nc" id="L125"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.9.202303310957</span></div></body></html>