'Java'에 해당되는 글 1건

  1. 2008.09.19 서버설정을 properties로 만든 거.


지금 관리하는 사이트를 만드신 분께서 서버 아이피니, 파일 저장 절대경로니...기타등등을 죄다 서블릿 안에 박아놔서 로컬에서 개발할 때 에로사항이 쫌 꽃피길래 만들어 봤다오.

server.properties 라는 파일에 설정을 하고.

Server_mode=DEV
Server_ip=http://xx.xx.xx.xx:xxxx
##Report_path=/upload/Report/
##Notice_path=/upload/Notice/
Report_path=C:\\webapp\\FileAttach\\Report\\
Notice_path=C:\\webapp\\FileAttach\\Notice\\


Constructor.java

public class Constructor {

 private java.util.Properties mProperties;

 private String Server_mode = "";
 private String Server_ip = "";
 private String Report_path = "";
 private String Notice_path = "";

 //properties 파일 이름
 private String cfg_file  = "server.properties";

 public String getServer() {  
  try {
   loadConfig(cfg_file);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return Server_ip;
 }

 public String getMode() {

  try {
   loadConfig(cfg_file);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return Server_mode;
 }
 
 public String getReportPath() {

  try {
   loadConfig(cfg_file);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return Report_path;
 }
 
 public String getNoticePath() {

  try {
   loadConfig(cfg_file);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return Notice_path;
 }

 private boolean loadConfig(String name) throws Exception {
  boolean rc = false;

  ClassLoader cl = getClass().getClassLoader();

  java.io.InputStream in;

  if (cl != null) {
   in = cl.getResourceAsStream(name);
  } else {
   in = ClassLoader.getSystemResourceAsStream(name);
  }
 
  // 만일 InputStream이 null이면 configuration 파일이 없다.
  if (in == null) {
   throw new Exception("configuration file " + name + " was not found.");
  } else {
   try {
    mProperties = new java.util.Properties();
    mProperties.load(in);

    Server_mode = consume(mProperties, "Server_mode");
    Server_ip = consume(mProperties, "Server_ip");
    Report_path = consume(mProperties, "Report_path");
    Notice_path = consume(mProperties, "Report_path");
    rc = true;
   } finally {
    if (in != null) {
     try {
      in.close();
     } catch (Exception ex) {
     }
    }
   }
  }
  return true;
 }

 private String consume(java.util.Properties p, String key) {
  String s = null;
  if ((p != null) && (key != null)) {
   s = p.getProperty(key);
   System.out.println("#### key : " + key);
   System.out.println("#### var : " + s);
   if (s != null) {
    p.remove(key);
   }
  }
  return s;
 }

}


요렇게 돌리면, 각 페이지에서 Constructor.getServer() 이런 식으로 불러다가 쓸 수 있지롱. 서버설정이 바뀌어도 server.properties 파일에서 변경하면 되니까 서버 리스타트 안해도 되고, 일관적인 관리가 가능하다는 말씀임.

※ 근데 버그가 하나 있음다. 찾아보세요~*

Posted by 좀모씨
이전버튼 1 이전버튼

블로그 이미지
Under Construction
좀모씨
Yesterday
Today
Total

달력

 « |  » 2024.4
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함