📄 configuration.java
字号:
package com.esri.solutions.jitk.common.config;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
/**
* Extends the Java Properties object in order to set the path to the properties resource so that
* in can be used within a JSF bean management environment. After the resource path is set, then
* the properties object is loaded with the property values.
*/
public class Configuration extends Properties {
private static final String ERROR_IOEXCEPTION = "Failed to initialize Configuration: cannot read configuration file.";
private static final String ERROR_NULL_INPUT_STREAM = "Failed to initialize Configuration: cannot retrieve handle to configuration file";
private static final String ERROR_NULL_SERVLET_CONTEXT = "Failed to initialize Configuration: cannot retrieve handle to ServletContext";
private static final String ERROR_NULL_FACES_CONTEXT = "Failed to initialize Configuration: cannot retrieve handle to FacesContext";
private static final Logger LOG = LogManager.getLogger(Configuration.class);
private static final long serialVersionUID = 4817240692631653041L;
private String m_resourcePath;
/**
* Sets the ServletContext-relative path to the configuration property file. The path is
* required and cannot be <code>null</code>. This object will be initialized with the
* property values within the property file. Other beans will call into this bean to get
* property values. The property file must be resolvable via a call to
* {@link ServletContext#getResourceAsStream(String)}.
*
* @param path ServletContext-relative path to configuration property file, cannot be <code>null</code>
* @throws NullPointerException Thrown if the <code>path</code> argument is <code>null</code>.
*/
public void setResourcePath (String path) {
if (path == null) {
throw new NullPointerException ();
}
m_resourcePath = path;
init();
}
private void init() {
if (m_resourcePath == null) {
return;
}
FacesContext fCtx = FacesContext.getCurrentInstance();
if (fCtx == null) {
LOG.error(ERROR_NULL_FACES_CONTEXT);
return;
}
if (!(fCtx.getExternalContext().getContext() instanceof ServletContext)) {
LOG.error(ERROR_NULL_SERVLET_CONTEXT);
return;
}
ServletContext sCtx = (ServletContext) fCtx.getExternalContext().getContext();
InputStream in = sCtx.getResourceAsStream(m_resourcePath);
if (in == null) {
LOG.error(ERROR_NULL_INPUT_STREAM);
return;
}
try {
load(in);
} catch (IOException e) {
LOG.error(ERROR_IOEXCEPTION, e);
return;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -