package sikd.sikd5.sikd5bean.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.URL;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author detra
 */
public class PropertiesLoader implements Serializable{
    String dbServer;
    String dbName;
    String dbPort;

    public PropertiesLoader() {
    }
    
//    public static Properties loadProperties(String propertiesFile) throws Exception{
//        URL url = Thread.currentThread().getContextClassLoader().getResource(propertiesFile);
//        if (url == null) {
//            throw new Exception("The configuration could not be found: ");
////            System.out.println("The configuration could not be found: ");
//        } else {
//            InputStream iss = null;
//            try {
//                Properties props = new Properties();
//                iss = url.openStream();
//                props.load(iss);
//                
//                return props;
//            } catch (IOException ex) {
//                Logger.getLogger(PropertiesLoader.class.getName()).log(Level.SEVERE, null, ex);
//                throw new Exception(ex.getMessage());
//            }
//            finally{
//                if(iss!= null)
//                iss.close();
//            }
//        }
//    }
    
    public static Properties loadPropertiesWildfly(String propertiesFile) throws Exception{
        String fileName = System.getProperty("jboss.server.config.dir")+File.separator+"SIKD5"+File.separator+propertiesFile;
        File ff = new File(fileName);
         System.out.println("filename :" + fileName);
        if( !ff.exists() ){
            throw new Exception("The configuration "+propertiesFile+" could not be found: ");
//            throw new Exception("The configuration "+fileName+" could not be found: ");
        } else {
            FileInputStream fis=null;
            try {
                fis = new FileInputStream(fileName);
                Properties props = new Properties();
                props.load(fis);
                
                return props;
            } catch (IOException ex) {
                Logger.getLogger(PropertiesLoader.class.getName()).log(Level.SEVERE, null, ex);
                throw new Exception(ex.getMessage());
            }
            finally{
                if(fis!= null)
                fis.close();
            }
        }
    }
    
     public static Properties loadPropertiesGlassfish(String propertiesFile) throws Exception{
        String fileName = System.getProperty("com.sun.aas.instanceRoot")+File.separator+"config"+File.separator+"SIKD5"+File.separator+propertiesFile;
        File ff = new File(fileName);
         System.out.println("filename :" + fileName);
        if( !ff.exists() ){
            throw new Exception("The configuration "+propertiesFile+" could not be found: ");
//            throw new Exception("The configuration "+fileName+" could not be found: ");
        } else {
            FileInputStream fis=null;
            try {
                fis = new FileInputStream(fileName);
                Properties props = new Properties();
                props.load(fis);
                
                return props;
            } catch (IOException ex) {
                Logger.getLogger(PropertiesLoader.class.getName()).log(Level.SEVERE, null, ex);
                throw new Exception(ex.getMessage());
            }
            finally{
                if(fis!= null)
                fis.close();
            }
        }
    }
       public static Properties loadProperties(String propertiesFile) throws Exception{
        String folderProp ="";
        if (SIKDUtil.SERVER.equals("Wildfly")){
            folderProp = System.getProperty("jboss.server.config.dir")+File.separator+"SIKD5";
        }else if (SIKDUtil.SERVER.equals("Glassfish")){
            folderProp = System.getProperty("com.sun.aas.instanceRoot")+File.separator+"config"+File.separator+"SIKD5";
        }
        String fileName = folderProp+File.separator+propertiesFile;
        File ff = new File(fileName);
         System.out.println("filename :" + fileName);
        if( !ff.exists() ){
            throw new Exception("The configuration "+propertiesFile+" could not be found: ");
//            throw new Exception("The configuration "+fileName+" could not be found: ");
        } else {
            FileInputStream fis=null;
            try {
                fis = new FileInputStream(fileName);
                Properties props = new Properties();
                props.load(fis);
                
                return props;
            } catch (IOException ex) {
                Logger.getLogger(PropertiesLoader.class.getName()).log(Level.SEVERE, null, ex);
                throw new Exception(ex.getMessage());
            }
            finally{
                if(fis!= null)
                fis.close();
            }
        }
    }
}