📄 etcproperties.java
字号:
if (isStringNotEmpty(value)) {
int val = Integer.parseInt(value);
if (val < minValue) {
return defaultValue;
}
return val;
}
} catch (NumberFormatException e) {
if (log != null) {
log.log(Level.CONFIG, e.getMessage(), e);
}
}
return defaultValue;
}
/**
* Asks the properties of this process for a certain key.
*
* @param key
* the name of the property.
* @param defaultValue
* use this if property value is not available.
* @return the value of the property, or the given default value if not
* found.
*/
public long getLongProperty(String key, long defaultValue) {
String value = getProperty(key);
try {
if (isStringNotEmpty(value)) {
return Long.parseLong(value);
}
} catch (NumberFormatException e) {
if (log != null) {
log.log(Level.CONFIG, e.getMessage(), e);
}
}
return defaultValue;
}
/**
* Asks the properties of this process for a certain key.
*
* @param key
* the name of the property.
* @param minValue
* the minimum allowed value.
* @param defaultValue
* use this if property value is not available or too small.
* @return the value of the property, or the given default value if not
* found.
*/
public long getLongProperty(String key, long minValue, long defaultValue) {
String value = getProperty(key);
try {
if (isStringNotEmpty(value)) {
long val = Long.parseLong(value);
if (val < minValue) {
return defaultValue;
}
return val;
}
} catch (NumberFormatException e) {
if (log != null) {
log.log(Level.CONFIG, e.getMessage(), e);
}
}
return defaultValue;
}
/**
* Builds and returns a string which tells that the given 'text' has a key
* 'key' with value 'value'. E.g. <code>"Property 'x' has value 'y'"</code>
* or <code>"Property 'x' has null value"</code>.
*
* @param text
* The type of property searched for.
* @param key
* The name of the property.
* @param value
* The non-null and non-empty property value.
* @return A string suitable for tracing.
*/
public static String traceKeyHasValue(String text, String key, String value) {
if (value != null) {
return text + " '" + key + "' has value " + '\'' + value + '\'';
}
return text + " '" + key + "' has null value";
}
/**
* Builds and returns a string which tells that the default value
* 'defaultValue' is used for the key 'key'. E.g.
* <code>"Using default value 'defaultValue' for key 'key'"</code> or
* <code>"Using null default value for key 'key'"</code>.
*
* @param key
* The name of the property.
* @param defaultValue
* The default value (which could be <code>null</code> or
* empty).
* @return A string suitable for tracing.
*/
public static String traceUsingDefaultForKey(String key, String defaultValue) {
if (defaultValue != null) {
return "Using default value '" + defaultValue
+ "' for property key '" + key + "'";
}
return "Using null default value for property key '" + key + "'";
}
/**
* Builds and returns a string which tells that the key 'key' has no value.
* E.g. <code>"No value for property 'key'"</code>.
*
* @param text
* The type of property searched for.
* @param key
* The name of the property.
* @return A string suitable for tracing.
*/
public static String traceKeyNoValue(String text, String key) {
return "No value for " + text + " '" + key + "'";
}
/**
* Overrides the base class method
* {@link Properties#setProperty(java.lang.String, java.lang.String)} to be
* able to know when properties were changed.
*
* @param key
* the key to be placed into this property list.
* @param value
* the value corresponding to <tt>key</tt>.
* @return the previous value of the specified key in this property list, or
* <code>null</code> if it did not have one.
*/
public synchronized Object setProperty(String key, String value) {
reloadedTimeStamp = System.currentTimeMillis();
reloadCheckTimeStamp = reloadedTimeStamp;
return super.setProperty(key, value);
}
/**
* Adds the given properties to the current properties, or overwrites
* existing properties. <br />
* This is mainly used for application testing, and has nothing to do with
* {@link #setDefaultProperties(Properties)}.
*
* @param props
* the properties that should override the current set of
* properties.
*/
public synchronized void addProperties(Properties props) {
reloadedTimeStamp = System.currentTimeMillis();
reloadCheckTimeStamp = reloadedTimeStamp;
putAll(props);
}
/**
* Prints this property list out to the specified output stream. This method
* is useful for debugging. Tries to sort the output.
*
* @param out
* an output stream.
*/
public void list(PrintStream out) {
String hello = "Listing FFroperties";
if (fileName != null) {
hello = hello + " from " + fileName;
}
outln(out, hello);
writePropsSorted(out, " ", this, null);
if (defaultProperties != null) {
// Only list those entries which are seen because I
// don't have them.
writePropsSorted(out, "Default ", defaultProperties, this);
}
}
/**
* This method is doing a <code>out.println(str)</code> if out is not
* <code>null</code>. The main purpose of this method is to concentrate
* the CheckStyle warnings into only one line. This method is available for
* use by the whole package.
*
* @param out
* The stream where to write <code>str</code> to.
* @param str
* The string to write to <code>out</code>.
*/
static void outln(PrintStream out, String str) {
// This could theoretically be null during the very first
// stages of VM initialization:
if (out != null) {
// Provoke only one CheckStyle warning for this file:
// CHECKSTYLE OFF: GenericIllegalRegexpCheck for println
// to implement rudimentary tracing
out.println(str);
// CHECKSTYLE ON: GenericIllegalRegexpCheck
}
}
private static void writePropsSorted(PrintStream out, String prefix,
Properties props, Properties excludedProps) {
String[] h = new String[props.size()];
int size = 0;
if (excludedProps == null) {
// Include only props' own keys
for (Enumeration e = props.keys(); e.hasMoreElements();) {
String key = (String) e.nextElement();
h[size++] = key;
}
} else {
// Also include defaults of props. But exclude those of
// excludedProps
for (Enumeration e = props.propertyNames(); e.hasMoreElements();) {
String key = (String) e.nextElement();
if (excludedProps.get(key) == null) {
h[size++] = key;
}
}
}
Arrays.sort(h, 0, size);
for (int i = 0; i < size; i++) {
String key = h[i];
String val = (String) props.get(key);
// Cut off too-long strings
if (val.length() > 1000 + 2) {
val = val.substring(0, 1000 - 2) + "...";
}
outln(out, prefix + '\'' + key + "'='" + val + '\'');
}
}
/**
* Reload the properties if the properties file has changed recently. If the
* file has changed, all properties which are found in the file are
* overwritten in memory. <br />
* This method is called implicitly by each of the methods which read
* property values, i.e. all
* {@link FFProperties#getProperty(String) getXXXProperty(...)} and
* {@link #getValue(String)}. If you don't want this feature, don't tell
* this object the name of the file which contained the properties. You can
* do this by choosing a suitable constructor, e.g. {@link #FFProperties()}
* or {@link #FFProperties(InputStream)}.
* <p />
* In case of problems with reading the file, this method silently follows a
* 'best effort' approach:
* <ul>
* <li>If
* {@link #setCleanupBeforeLoading(boolean) setCleanupBeforeLoading(true)}
* has been called, the properties are unchanged,</li>
* <li>if it has not been called, all properties which could be read are
* updated.</li>
* </ul>
* This method is <em>not</em> returning whether it did a reload or not,
* as this method is called relatively often and thus such a return value
* would not be reliable. Instead, call {@link #getReloadedTimeStamp()}
* twice to determine whether the file was reloaded between these two calls.
* <p />
* {@linkplain #FFPROPERTIES_RELOAD_CHECK_INTERVAL One of the properties} is
* used to determine how often a real file system check is done, to see
* whether the file may have changed. Each call to this method first checks
* if the last filesystem call was done that many milliseconds before, and
* if it was, the file data is checked with a relatively costly file system
* call ( {@link IdlFile#lastModified()}). And the file will not be
* reloaded (which is even more costly) unless that file system call
* indicates that the file was written to since it was last read by this
* FFProperty object. The time when the file was last read can be obtained
* using {@link #getReloadedTimeStamp()}.
*/
public synchronized void reloadIfNeeded() {
if (fileName == null) {
return;
}
// This is cheap.
long currentTimeStamp = System.currentTimeMillis();
long delta = currentTimeStamp - reloadCheckTimeStamp;
// Check for reloads only every ... seconds (default is ten).
if (delta <= reloadCheckInterval) {
return;
}
// This is a file system access, which is costly.
File propertiesFile = new File(fileName);
long fileTimeStamp = propertiesFile.lastModified();
// We did a check for the file time, so don't repeat this too
// soon:
reloadCheckTimeStamp = currentTimeStamp;
// Don't reload if properties were recently modified by
// calling setProperty(). But reload if property file was
// recently modified.
// fileTimeStamp is 0 if the file does not exist; make sure
// this fact is traced by calling tryToReload() in this case.
if (fileTimeStamp != 0 && fileTimeStamp - reloadedTimeStamp <= 0) {
log.log(Level.FINEST, "Property file time (" + fileTimeStamp
+ ") did not change: " + fileName);
return;
}
tryToReload(propertiesFile, fileTimeStamp);
// Reloaded - thus update my own properties.
// Don't allow to reload too often, minimum value is one
// second.
reloadCheckInterval = getLongProperty(
FFPROPERTIES_RELOAD_CHECK_INTERVAL,
MINIMUM_RELOAD_CHECK_INTERVAL, reloadCheckInterval);
}
/**
* Try to reload the file using a best-effort approach. May be called only
* while synchronized with this object.
*
* @param propertiesFile
* The file to open
* @param fileTimeStamp
* modification time of the properties file.
*/
private void tryToReload(File propertiesFile, long fileTimeStamp) {
Exception exception = null;
try {
// Use the absolutePath to have clearer exception messages
String absolutePath = propertiesFile.getAbsolutePath();
InputStream stream = new FileInputStream(absolutePath);
// The next call may modify the properties.
reloadedTimeStamp = fileTimeStamp;
tryToReload(stream);
} catch (FileNotFoundException e) {
exception = e;
// Just keep the information we have. We did not modify
// it. The timestamp has not been updated.
} catch (IOException e) {
exception = e;
// Just keep what we have at the moment. We might have
// modified it and thus the reloadedTimeStamp was made
// current, in order to not reload this again if we
// currently have problems.
}
// Treat all exceptions the same.
if (exception != null) {
if (log != null) {
log.log(Level.SEVERE, exception.getMessage(), exception);
} else {
// Can't LOG.log() this, we are just initializing.
outln(System.err, "Error reading property file \"" + fileName
+ "\": " + exception.toString());
}
}
}
/**
* Try to reload the file using a best-effort approach. May be called only
* while synchronized with this object.
*
* @param stream
* the stream to read the properties from.
* @throws IOException
* in case parsing the stream contents fails.
*/
private void tryToReload(InputStream stream) throws IOException {
// If requested, clear all properties
if (cleanupBeforeLoading) {
// Loading may fail. Use a temporary object to avoid
// destroying myself in such a case
Properties p = new Properties();
p.load(stream);
clear();
putAll(p);
} else {
// Just overwrite what is readable in that file.
load(stream);
}
}
/**
* Returns the name of this process as defined by the system property
* "process.name". This may not change.
*
* @return the name of this process.
*/
public static String getTheProcessName() {
// This is a static variable which is filled once
if (theProcessName == null) {
theProcessName = System.getProperty("process.name");
}
return theProcessName;
}
public static String fileNameFromHomeDir(String dirPropertyName,
String fName) {
String dirName = System.getProperty(dirPropertyName);
if (dirName == null || dirName.length() <= 0) {
dirName = ".";
}
String fPath = dirName + File.separator + fName;
return fPath;
}
/**
* Returns true if the String contains at least one character.
*
* @param value
* the String to check.
* @return false if the String is null or empty, true otherwise.
*/
public static boolean isStringNotEmpty(String value) {
return value != null && value.length() > 0;
}
}
/* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -