001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.internal; 014 015/** 016 * System utilities. 017 */ 018public class SystemUtils { 019 020 /** 021 * Returns the first non-<jk>null</jk> system property. 022 * 023 * @param def 024 * The default value if none are found. 025 * Can be <jk>null</jk>. 026 * @param keys 027 * The system properties to look for. 028 * @return 029 * The first non-<jk>null</jk> system property, or the default value if non were found. 030 */ 031 public static String getFirstString(String def, String...keys) { 032 for (String key : keys) { 033 String v = System.getProperty(key); 034 if (v != null) 035 return v; 036 } 037 return def; 038 } 039 040 /** 041 * Returns the first non-<jk>null</jk> boolean system property. 042 * 043 * @param def 044 * The default value if none are found. 045 * Can be <jk>null</jk>. 046 * @param keys 047 * The system properties to look for. 048 * @return 049 * The first non-<jk>null</jk> system property, or the default value if non were found. 050 */ 051 public static Boolean getFirstBoolean(Boolean def, String...keys) { 052 String s = getFirstString(null, keys); 053 return s == null ? def : Boolean.parseBoolean(s); 054 } 055 056 /** 057 * Returns the first non-<jk>null</jk> integer system property. 058 * 059 * @param def 060 * The default value if none are found. 061 * Can be <jk>null</jk>. 062 * @param keys 063 * The system properties to look for. 064 * @return 065 * The first non-<jk>null</jk> system property, or the default value if non were found. 066 */ 067 public static Integer getFirstInteger(Integer def, String...keys) { 068 String s = getFirstString(null, keys); 069 return s == null ? def : Integer.parseInt(s); 070 } 071 072 /** 073 * Convenience method for setting a system property value. 074 * 075 * @param key The system property key. 076 * @param value The system property value. 077 * @param overwrite Overwrite the previous value if it exists. 078 */ 079 public static void setProperty(String key, Object value, boolean overwrite) { 080 if (value != null) { 081 if (System.getProperty(key) == null || overwrite) 082 System.setProperty(key, StringUtils.asString(value)); 083 } 084 } 085 086 /** 087 * Convenience method for setting a system property value. 088 * 089 * @param key The system property key. 090 * @param value The system property value. 091 */ 092 public static void setProperty(String key, Object value) { 093 setProperty(key, value, true); 094 } 095}