fairnet/src/main/java/seed/utils/SeedProperties.java

189 lines
5.7 KiB
Java

package seed.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.apache.log4j.Logger;
public class SeedProperties {
private Logger log = Logger.getLogger(this.getClass());
private static Properties configProperties = null;
private static String configPath = null;
private static File configFile = null;
private static long propertiesLastModifyTime = 0l;
public SeedProperties(){}
public String configPropertiesInit(){
if(configProperties == null || propertiesLastModifyTime < configFile.lastModified()){
System.out.println("configProperties load");
String getPath = SeedUtils.setReplaceNull(SeedSqlCon.class.getProtectionDomain().getCodeSource().getLocation().getPath());
if(!getPath.equals("")){
configPath = SeedUtils.setReplaceNull(SeedSqlCon.class.getProtectionDomain().getCodeSource().getLocation().getPath());
int index = configPath.indexOf("WEB-INF");
if(index > 0) configPath = configPath.substring(0, index+7);
configPath = configPath + "/classes/config/seed/config.properties";
System.out.println("configProperties load="+configPath);
configFile = new File(configPath);
if(configFile == null || !configFile.isFile()){
log.error("Error configFile not file");
}
FileInputStream fileInputStream=null;
try{
fileInputStream = new FileInputStream(configFile);
configProperties = new Properties();
configProperties.load(fileInputStream);
propertiesLastModifyTime = configFile.lastModified();
}catch(FileNotFoundException fe){
log.error("SeedUtils configPropertiesInit FileNotFoundException");
}catch(IOException ie){
log.error("SeedUtils configPropertiesInit IOException");
}catch(Exception ie){
log.error("SeedUtils configPropertiesInit IOException");
}finally{
if (fileInputStream!=null) try{fileInputStream.close();}catch(IOException ie){log.error("SeedUtils configPropertiesInit IOException");}
fileInputStream = null;
}
}
}
return configPath;
}
public String getConfigValue(String keyName){
this.configPropertiesInit();
return this.getConfigProperties().getProperty(keyName);
}
public String getConfigRootContextpath() {
return getConfigValue("root.contextpath");
}
public Properties getConfigProperties() {
return configProperties;
}
public static Properties getStaticConfigProperties() {
return configProperties;
}
//메모리상 처리
/*
public void setProperties(String keyName, String sValue){
getConfigProperties().setProperty(keyName, sValue);
}
//파일 내부 처리
public String setReplaceConfFile(String keyName, String sValue){
String sTarget = keyName+"="+getConfigProperties().getProperty(keyName);
String sReplacement = keyName+"="+sValue;
StringReplace sr = new StringReplace(configPath, sTarget, sReplacement);
sr.processStringReplace();
return sr.getMessage();
}
public class StringReplace {
private static final Logger LOG = Logger.getLogger(StringReplace.class.getName());
private String sFilename = null;
private String sTarget = null;
private String sReplacement = null;
private String sMessage = null;
public StringReplace(){}
public StringReplace(String sFilename, String sTarget, String sReplacement){
this.sFilename = sFilename;
this.sTarget = sTarget;
this.sReplacement = sReplacement;
}
public String getMessage() {
return sMessage;
}
private void setMessage(String message) {
sMessage = message;
}
public void processStringReplace(){
if(sFilename==null || sFilename.equals("")) {
setMessage("#1 파일이 선택되지 않았습니다.");
return;
}
if(sTarget==null || sTarget.equals("")){
setMessage("#2 대상 문자열이 선택되지 않았습니다.");
return;
}
if(sReplacement==null){
setMessage("#3 치환할 문자가 선택되지 않았습니다.");
return;
}
if(this.setStringReplace() == 1){
setMessage("#4 정상적으로 치환되지 않았습니다.");
return;
}else{
setMessage("#5 정상적으로 처리되었습니다.");
return;
}
}
private int setStringReplace(){
BufferedReader brReader = null;
BufferedWriter bwWriter = null;
int iReturnValue = 0;
sFilename= SeedUtils.setFilePathReplaceAll(sFilename);
try{
brReader = new BufferedReader(new InputStreamReader(new FileInputStream(sFilename),"UTF-8"));
String sTmp = null;
ArrayList al = new ArrayList();
while((sTmp = brReader.readLine()) != null) {
al.add(sTmp.replaceAll(sTarget, sReplacement));
}
bwWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(sFilename ),"UTF-8"));
for(int i = 0 ; i < al.size(); i++){
bwWriter.write((String)al.get(i));
bwWriter.newLine();
}
}catch(IOException e){
LOG.error("IGNORE:",e);
iReturnValue = 1;
}finally{
if(bwWriter != null) try { bwWriter.flush(); } catch (IOException e) {LOG.error("IGNORE:",e);}
if(bwWriter != null) try { bwWriter.close(); } catch (IOException e) {LOG.error("IGNORE:",e);}
if(brReader != null) try { brReader.close(); } catch (IOException e) {LOG.error("IGNORE:",e);}
}
return iReturnValue;
}
}
*/
}