/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved * * The contents of this file are subject to the terms * of the Common Development and Distribution License * (the License). You may not use this file except in * compliance with the License. * * You can obtain a copy of the License at * https://opensso.dev.java.net/public/CDDLv1.0.html or * opensso/legal/CDDLv1.0.txt * See the License for the specific language governing * permission and limitations under the License. * * When distributing Covered Code, include this CDDL * Header Notice in each file and include the License file * at opensso/legal/CDDLv1.0.txt. * If applicable, add the following below the CDDL Header, * with the fields enclosed by brackets [] replaced by * your own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * * Portions Copyrighted 2010-2016 ForgeRock AS. */ package com.iplanet.am.util; import static org.forgerock.openam.utils.CollectionUtils.*; import static org.forgerock.openam.utils.Time.*; import java.io.FileInputStream; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.net.InetAddress; import java.security.AccessController; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.MissingResourceException; import java.util.Properties; import java.util.ResourceBundle; import java.util.Set; import java.util.concurrent.atomic.AtomicReference; import javax.annotation.Nullable; import org.forgerock.guava.common.base.Predicate; import org.forgerock.guava.common.collect.ImmutableMap; import org.forgerock.guava.common.collect.Maps; import org.forgerock.openam.utils.StringUtils; import com.iplanet.sso.SSOToken; import com.sun.identity.common.AttributeStruct; import com.sun.identity.common.PropertiesFinder; import com.sun.identity.common.configuration.ConfigurationListener; import com.sun.identity.common.configuration.ConfigurationObserver; import com.sun.identity.common.configuration.ServerConfiguration; import com.sun.identity.security.AdminTokenAction; import com.sun.identity.shared.Constants; import com.sun.identity.sm.SMSEntry; /** * This class provides functionality that allows single-point-of-access to all * related system properties. *
* The system properties can be set in couple of ways: programmatically by
* calling the initializeProperties
method, or can be statically
* loaded at startup from a file named:
* AMConfig.[class,properties]
.
* Setting the properties through the API takes precedence and will replace the
* properties loaded via file. For statically loading the properties via a file,
* this class tries to first find a class, AMConfig.class
, and
* then a file, AMConfig.properties
in the CLASSPATH accessible
* to this code. The AMConfig.class
takes precedence over the
* flat file AMConfig.properties
.
*
* If multiple servers are running, each may have their own configuration file.
* The naming convention for such scenarios is
* AMConfig-<serverName>
.
* @supported.all.api
*/
public class SystemProperties {
/**
* Runtime flag to be set, in order to override the path of the
* configuration file.
*/
public static final String CONFIG_PATH = "com.iplanet.services.configpath";
/**
* Default name of the configuration file.
*/
public static final String CONFIG_FILE_NAME = "serverconfig.xml";
/**
* New configuration file extension
*/
public static final String PROPERTIES = "properties";
public static final String NEWCONFDIR = "NEW_CONF_DIR";
private static final String SERVER_NAME_PROPERTY = "server.name";
private static final String CONFIG_NAME_PROPERTY = "amconfig";
private static final String AMCONFIG_FILE_NAME = "AMConfig";
/** Regular expression pattern for a sequence of 1 or more white space characters. */
private static final String WHITESPACE = "\\s+";
private static final MapString
key. The method first tries to read the property
* from java.lang.System followed by a lookup in the config file.
*
* @param key
* type String
, the key whose value one is
* looking for.
* @return the value if the key exists; otherwise returns null
*/
public static String get(String key) {
String answer = null;
// look up values in SMS services only if in server mode.
if (isServerMode() || SITEMONITOR_DISABLED) {
AttributeStruct ast = ATTRIBUTE_MAP.get(key);
if (ast != null) {
answer = PropertiesFinder.getProperty(key, ast);
}
}
if (answer == null) {
answer = getProp(key);
final MapString
key.
*
* @param key the key whose value one is looking for.
* @param def the default value if the key does not exist.
* @return the value if the key exists; otherwise returns default value.
*/
public static String get(String key, String def) {
String value = get(key);
return value == null ? def : value;
}
/**
* Returns the property value as a boolean
*
* @param key the key whose value one is looking for.
* @return the boolean value if the key exists; otherwise returns false
*/
public static boolean getAsBoolean(String key) {
String value = get(key);
return Boolean.parseBoolean(value);
}
/**
* Returns the property value as a boolean
*
* @param key the property name.
* @param defaultValue value if key is not found.
* @return the boolean value if the key exists; otherwise the default value
*/
public static boolean getAsBoolean(String key, boolean defaultValue) {
String value = get(key);
if (value == null) {
return defaultValue;
}
return Boolean.parseBoolean(value);
}
/**
* @param key The System Property key to lookup.
* @param defaultValue If the property was not set, or could not be parsed to an int.
* @return Either the defaultValue, or the numeric value assigned to the System Property.
*/
public static int getAsInt(String key, int defaultValue) {
String value = get(key);
if (value == null) {
return defaultValue;
}
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
return defaultValue;
}
}
/**
* @param key The System Property key to lookup.
* @param defaultValue If the property was not set, or could not be parsed to a long.
* @return Either the defaultValue, or the numeric value assigned to the System Property.
*/
public static long getAsLong(String key, long defaultValue) {
String value = get(key);
if (value == null) {
return defaultValue;
}
try {
return Long.parseLong(value);
} catch (NumberFormatException e) {
return defaultValue;
}
}
/**
* Parses a system property as a set of strings by splitting the value on the given delimiter expression.
*
* @param key The System Property key to lookup.
* @param delimiterRegex The regular expression to use to split the value into elements in the set.
* @param defaultValue The default set to return if the property does not exist.
* @return the value of the property parsed as a set of strings.
*/
public static SetgetAll()
*
* @return the platform properties
*/
public static Properties getPlatform() {
return getAll();
}
/**
* Initializes properties bundle from the file
* passed.
*
* @param file type
String
, file name for the resource bundle
* @exception MissingResourceException
*/
public static void initializeProperties(String file) throws MissingResourceException {
Properties props = new Properties();
ResourceBundle bundle = ResourceBundle.getBundle(file);
// Copy the properties to props
for (String key : bundle.keySet()) {
props.put(key, bundle.getString(key));
}
initializeProperties(props, false, false);
}
public static void initializeProperties(Properties properties) {
initializeProperties(properties, false);
}
/**
* Initializes the properties to be used by OpenAM. Ideally this
* must be called first before any other method is called within OpenAM.
* This method provides a programmatic way to set the properties, and will
* override similar properties if loaded for a properties file.
*
* @param properties properties for OpenAM
* @param reset true
to reset existing properties.
*/
public static void initializeProperties(Properties properties, boolean reset) {
initializeProperties(properties, reset, false);
}
/**
* Initializes the properties to be used by OpenAM. Ideally this
* must be called first before any other method is called within OpenAM.
* This method provides a programmatic way to set the properties, and will
* override similar properties if loaded for a properties file.
*
* @param properties properties for OpenAM.
* @param reset true
to reset existing properties.
* @param withDefaults true
to include default properties.
*/
public static void initializeProperties(Properties properties, boolean reset, boolean withDefaults) {
Properties defaultProp = null;
if (withDefaults) {
SSOToken appToken = AccessController.doPrivileged(AdminTokenAction.getInstance());
defaultProp = ServerConfiguration.getDefaults(appToken);
}
PropertiesHolder oldProps;
PropertiesHolder newProps;
do {
oldProps = propertiesHolderRef.get();
final Properties combined = new Properties();
if (defaultProp != null) {
combined.putAll(defaultProp);
}
if (!reset) {
combined.putAll(oldProps.properties);
}
combined.putAll(properties);
newProps = new PropertiesHolder(Maps.fromProperties(combined));
} while (!propertiesHolderRef.compareAndSet(oldProps, newProps));
}
/**
* Initializes a property to be used by OpenAM. Ideally this
* must be called first before any other method is called within OpenAM.
* This method provides a programmatic way to set a specific property, and
* will override similar property if loaded for a properties file.
*
* @param propertyName property name.
* @param propertyValue property value.
*/
public static void initializeProperties(String propertyName, String propertyValue) {
Properties newProps = new Properties();
newProps.put(propertyName, propertyValue);
initializeProperties(newProps, false, false);
}
/**
* Returns a counter for last modification. The counter is incremented if
* the properties are changed by calling the following method
* initializeProperties
. This is a convenience method for
* applications to track changes to OpenAM properties.
*
* @return counter of the last modification
*/
public static long lastModified() {
return propertiesHolderRef.get().lastModified;
}
/**
* Returns error messages during initialization, else null
.
*
* @return error messages during initialization
*/
public static String getInitializationError() {
return initError;
}
/**
* Returns error messages during initialization using the single war
* deployment, else null
.
*
* @return error messages during initialization of OpenAM as single war
*/
public static String getSecondaryInitializationError() {
return initSecondaryError;
}
/**
* Sets the server instance name of which properties are retrieved
* to initialized this object.
*
* @param name Server instance name.
*/
public static void setServerInstanceName(String name) {
instanceName = name;
}
/**
* Returns the server instance name of which properties are retrieved
* to initialized this object.
*
* @return Server instance name.
*/
public static String getServerInstanceName() {
return instanceName;
}
/**
* Returns true
if instance is running in server mode.
*
* @return true
if instance is running in server mode.
*/
public static boolean isServerMode() {
return IsServerModeHolder.isServerMode;
}
/**
* Returns the property name to service attribute schema name mapping.
*
* @return Property name to service attribute schema name mapping.
*/
public static Map getAttributeMap() {
return ATTRIBUTE_MAP;
}
private static Map