001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.commons.utils;
018
019import static org.apache.juneau.commons.utils.ObjectUtils.*;
020
021import java.util.*;
022import java.util.concurrent.*;
023import java.util.function.*;
024
025import org.apache.juneau.commons.logging.*;
026import org.apache.juneau.commons.settings.*;
027
028/**
029 * System utilities.
030 */
031public class SystemUtils {
032
033   private static final RichLogger LOG = RichLogger.getLogger(SystemUtils.class);
034
035   /**
036    * Prevents instantiation.
037    */
038   private SystemUtils() {}
039
040   static final List<Supplier<String>> SHUTDOWN_MESSAGES = new CopyOnWriteArrayList<>();
041
042   static {
043      Runtime.getRuntime().addShutdownHook(new Thread() {
044         @Override
045         public void run() {
046            if (isFalse(Settings.get().get("juneau.shutdown.quiet").asBoolean().orElse(false))) // HTT - shutdown hook; true branch tested, false requires JVM shutdown with system property set
047               SHUTDOWN_MESSAGES.forEach(x -> LOG.info(x.get()));
048         }
049      });
050   }
051
052   /**
053    * Adds a console message to display when the JVM shuts down.
054    *
055    * @param message The message to display.  Must not be <jk>null</jk> (evaluated at JVM shutdown).
056    */
057   public static void shutdownMessage(Supplier<String> message) {
058      SHUTDOWN_MESSAGES.add(message);
059   }
060
061   /**
062    * Looks up a system property or environment variable.
063    *
064    * @param name The property name.
065    * @return A StringSetting wrapping the value.
066    */
067   public static StringSetting env(String name) {
068      return Settings.get().get(name);
069   }
070
071   /**
072    * Looks up a system property or environment variable, returning a default if not found.
073    *
074    * @param <T> The type to convert the value to.
075    * @param name The property name.
076    * @param def The default value.  Can be <jk>null</jk> (returned as-is when the property is not found).
077    * @return The found value, or the default.
078    */
079   public static <T> T env(String name, T def) {
080      var s = Settings.get();
081      return s != null ? s.get(name, def) : def;
082   }
083}