#####################################
#  ÇÁ·ÎÆÛƼ ¼³Á¤Ç챉                       # 
#####################################

ÀÛ¼ºÀÚ : ÀåÇüÈ­(hhjang97@venus.uos.ac.kr)
ÀÛ¼ºÀÏ : 2004. 12. 30
¼öÁ¤ÀÏ : 
  2006. 03. 17 : TestProperty  ¼Ò½º Ãß°¡
  2005. 01. 05 : ȯ°æ º¯¼ö ÀÌ¿ëÇÑ ¹æ¹ý Ãß°¡


¿øº» :
¼³¸í :

Property ÆÄÀÏ¿¡ ÇÁ·Î±×·¥¿¡ »ç¿ëÇÏ´Â ¸ðµç º¯¼ö¸¦ ¼³Á¤ÇÑ´Ù.
ȯ°æ¸¶´Ù º¯È­´Â Á¤º¸´Â db.property ¿Í °°ÀÌ ÆÄÀÏÀ» ¸¸µé°í »ç¿ëÇÏ¸é µÈ´Ù.

db.property ÆÄÀÏÀÇ À§Ä¡´Â 2°³·Î ³ª´­¼ö ÀÖ´Ù.

  1) Property.java °¡ ÀÖ´Â µð·ºÅ丮¿¡ °°ÀÌ ÀÖ´Ù.
  2) ƯÁ¤ À§Ä¡¿¡ ÀÖ´Ù.

1) ¹øÀÇ °æ¿ì getClass().getResourceAsStream()À» ÀÌ¿ëÇؼ­ ÆÄÀÏÀ» ÀÐÀ¸¸é µÇ°í
2) ¹øÀÇ °æ¿ì´Â File Ŭ·¡½º¸¦ ÀÌ¿ëÇÏ¸é µÈ´Ù.
¶ÇÇÑ JAVA ½Ã½ºÅÛ º¯¼ö³ª ȯ°æº¯¼ö¸¦ ÀÌ¿ëÇÒ ¼öµµ ÀÖ´Ù.
  * System.getProperty("user.dir")
  * java -Dproperty.dir=%WS_HOME%/property com.PropertyTest
    (WS_HOMEÀº ½Ã½ºÅÛ È¯°æº¯¼öÀÓ)


################################# ################################# #################################


»ç¿ë ¹æ¹ý : 
// ¼­¹ö ȯ°æº° Á¤º¸
Property p = new Property().getInstance();
System.out.println("driverUrl = " + p.driverUrl);
System.out.println("databaseUser = " + p.databaseUser);
System.out.println("databasePasswd = " + p.databasePasswd);


// °íÁ¤ Á¤º¸
System.out.println("Default driverUrl = " + Property.driverUrl);


#################################
# Property.java
#################################

package com.portal.common;

import java.util.*;
import java.io.*;


public class Property {

	public Property(){
	}	
	
	// DB ¿¬°á
	public static String driverClass="oracle.jdbc.OracleDriver";
	public static String driverUrl="jdbc:oracle:thin:@111.222.333.444:1521:ORASID"; // 
	public static String databaseUser="UserID";
	public static String databasePasswd="UserPasswd";


	static private Property instance;       // The single instance
	static private String PROPERTY_FILE = "db.property";
	static synchronized public Property getInstance() {
		if (instance == null) {
			instance = new Property(PROPERTY_FILE);
		}
		return instance;
	}   
	
	private Property(String propertyFile) {
		try{
			PROPERTY_FILE = propertyFile;
			loadProperties();
		}catch(Exception e) {}
	}
	
	
	public void loadProperties()
	{
		Properties props = new Properties();
		try {
			System.out.println("ÇÁ·ÎÆÛƼ Á¤º¸¸¦ ·ÎµùÇÕ´Ï´Ù.");

			if (System.getProperty("property.dir") == null) {
				System.out.println("=== Property.java °¡ ÀÖ´Â µð·ºÅ丮ÀÇ db.property Read ===");

				propertyPath = PROPERTY_FILE;
			
				InputStream is = getClass().getResourceAsStream(propertyPath);
				props.load(is);
			} else {
				System.out.println("=== property.dir µð·ºÅ丮ÀÇ db.property Read ===");

				propertyPath = System.getProperty("property.dir") + "/" + PROPERTY_FILE;
				System.out.println("=== file read ===");
				
				File propFile = new File(propertyPath);
				FileInputStream is = new FileInputStream(propFile);
				

				props.load(is);				
			}
			System.out.println("Porperty Path : " + propertyPath);



			
			this.driverClass     			= props.getProperty("DRIVERCLASS").trim();
			this.driverUrl     				= props.getProperty("DRIVER").trim();
			this.databaseUser     		= props.getProperty("USER").trim();
			this.databasePasswd     = props.getProperty("PASSWORD").trim();
		}
		catch (Exception e) {
			System.err.println("Can't read the properties file. ");				
			return;
		}
	}


}


#################################
# db.property
#################################


DRIVER = jdbc:oracle:thin:@111.111.111.111:1521:ORA92
DRIVERCLASS = oracle.jdbc.OracleDriver
USER = User2
PASSWORD = Passwd2


#################################
# TestProperty
#################################

package test;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

import common.util.PropertyFile;

public class TestProperty {
    
    
    public static String STRING_VAL;
    public static boolean BOOLEAN_VAL;
    public static int INT_VAL = 10;
    
    static private TestProperty instance;       // The single instance
//    static private String PROPERTY_FILE = "/properties/test.properties";    // ¼Ò½º ³»ºÎ
    static private String PROPERTY_HOME = "D:/2006Work/Alert/properties";
    static private String PROPERTY_FILE = "test.properties";
    
    static synchronized public TestProperty getInstance() {
        if (instance == null) {
            instance = new TestProperty();
        }
        return instance;
    }    
    static synchronized public TestProperty getInstance(String propertyFile) {             
        if (instance == null) {
                PROPERTY_FILE = propertyFile;
            instance = new TestProperty();
        }
        System.out.println(PROPERTY_FILE);
        return instance;
    }

    /**
     * A private constructor since this is a Singleton
     */
    private TestProperty() {
        try{
//            loadProperties();
            loadProperties2();
        }catch(Exception e) {}
    }

    
    private void setPropertyHome() {
        if (System.getProperty("property.dir") != null) {
            PROPERTY_HOME = System.getProperty("property.dir");
        }
    }
    
    // ¼Ò½º ¾ÈÀÇ Property load
    private void loadProperties()
    {
        InputStream is = getClass().getResourceAsStream(PROPERTY_FILE);
        Properties props = new Properties();

        try {
            System.out.println("¼Ò½º¾ÈÀÇ ÇÁ·ÎÆÛƼ Á¤º¸¸¦ ·ÎµùÇÕ´Ï´Ù. [ " + PROPERTY_FILE + " ] ");
            props.load(is);
        }
        catch (Exception e) {
            System.err.println("Can't read the properties file. [ " + PROPERTY_FILE + " ] ");
            return;
        }
        
        load(props);
    }
    
    // ¿ÜºÎ ÆÄÀÏÀ» Àд °æ¿ì..
    private void loadProperties2 () {
        PropertyFile props = null;
        setPropertyHome();
        String propertyPath = PROPERTY_HOME + "/" + PROPERTY_FILE;
        try {
            System.out.println("¿ÜºÎÀÇ ÇÁ·ÎÆÛƼ Á¤º¸¸¦ ·ÎµùÇÕ´Ï´Ù. [ " + propertyPath + " ] ");
            props = new PropertyFile(propertyPath);
        }
        catch (Exception e) {
            System.err.println("Can't read the properties file. [ " + propertyPath + " ] ");
            return;
        }
        
        load(props);
    }

    
    private void load(Properties props) {
        TestProperty.STRING_VAL = props.getProperty("STRING_VAL",  "String Test");          // ¹®ÀÚ¿­
        TestProperty.BOOLEAN_VAL = new Boolean(props.getProperty("BOOLEAN_VAL",  "false")).booleanValue(); // boolean 
        TestProperty.INT_VAL = Integer.parseInt(props.getProperty("INT_VAL", "10"));        // int
        
        printInfo();       
    }
    
    public static void printInfo() {
        System.out.println("STRING_VAL = " + STRING_VAL);
        System.out.println("BOOLEAN_VAL = " + BOOLEAN_VAL);
        System.out.println("INT_VAL = " + INT_VAL);
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        TestProperty.getInstance();
    }

}