web.xml
<!-- DCS参数配置 -->
<servlet>
<display-name>DcsService Initializer Servlet</display-name>
<servlet-name>DcsServiceInitializer</servlet-name>
<servlet-class>
com.iss.file.dcs.DcsServiceInitializerServlet
</servlet-class>
<init-param>
<param-name>config-dcs</param-name>
<param-value>/dcs.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
DcsConfig.java (实体类
package com.iss.file.dcs.entity;
public class DcsConfig {
private String url;
private String size;
private String path;
private String delsize;
private String day;
public String getDelsize() {
return delsize;
}
public void setDelsize(String delsize) {
this.delsize = delsize;
}
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
DcsService.java
package com.iss.file.dcs.utils;
import java.util.Properties;
import org.apache.log4j.Logger;
import com.iss.file.dcs.entity.DcsConfig;
import com.iss.file.ftp.utils.PropertiesUtil;
public class DcsService {
protected static Logger logger = Logger.getLogger(DcsService.class
.getName());
private static DcsService dcsService;
private DcsConfig dcsConfig;
public DcsConfig getDcsConfig() {
return dcsConfig;
}
public void setDcsConfig(DcsConfig dcsConfig) {
this.dcsConfig = dcsConfig;
}
public static DcsService getInstance() {
if (dcsService == null) {
dcsService = new DcsService();
}
return dcsService;
}
/**
* 初始化DCS服务
*
* @param configFile
* @throws Exception
*/
public void initDcsService(String configFile) throws Exception {
logger.info("初始化DCS服务的配置");
Properties props = PropertiesUtil.getProperties(configFile);
DcsConfig dcsConfig = new DcsConfig();
if (props.containsKey("file.dcs.url")) {
dcsConfig.setUrl(props.getProperty("file.dcs.url"));
} else {
logger.info("配置项:file.dcs.url 不存在!");
}
if (props.containsKey("file.dcs.size")) {
dcsConfig.setSize(props.getProperty("file.dcs.size"));
} else {
logger.info("配置项:file.dcs.size 不存在!");
}
if (props.containsKey("file.temp.path")) {
dcsConfig.setPath(props.getProperty("file.temp.path"));
} else {
logger.info("配置项:file.temp.path 不存在!");
}
if (props.containsKey("file.del.size")) {
dcsConfig.setDelsize(props.getProperty("file.del.size"));
} else {
logger.info("配置项:file.del.size 不存在!");
}
if (props.containsKey("file.del.day")) {
dcsConfig.setDay(props.getProperty("file.del.day"));
} else {
logger.info("配置项:file.del.day 不存在!");
}
logger.info("DCS服务器配置,file.dcs.url:" + dcsConfig.getUrl()
+ ",file.temp.path:" + dcsConfig.getPath() + ",file.dcs.size:"
+ dcsConfig.getSize() + ",file.del.size:"
+ dcsConfig.getDelsize()+ ",file.del.day:"
+ dcsConfig.getDay());
setDcsConfig(dcsConfig);
}
}
DcsServiceInitializerServlet.java
package com.iss.file.dcs;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import com.iss.file.dcs.utils.DcsService;
import javax.servlet.http.HttpServlet;
public class DcsServiceInitializerServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
private DcsService dcsService;
public DcsServiceInitializerServlet(){
dcsService = DcsService.getInstance();
}
public void init(ServletConfig cfg) throws ServletException {
super.init(cfg);
log("DCS Service Initializer Servlet loaded...");
String configFile = cfg.getInitParameter("config-dcs");
try {
dcsService.initDcsService(configFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}