상세 컨텐츠

본문 제목

2021.03.21 ini4j 샘플

보안/java

by MustThanks 2021. 3. 21. 00:12

본문

반응형

아래내용을 참고 첨부된 파일은 라이브러리

ini4j-0.5.4-bin.zip
0.19MB

 

package sftp.service.util;


import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import java.util.Vector;
import java.util.Map;
import java.util.Map.Entry;

import org.ini4j.Ini;
import org.ini4j.InvalidFileFormatException;
import org.ini4j.Profile;

/*
ini 파일 샘플
//Key words : HOST, FILE_LIST
[HOST] 
ABL_SERVICE	=1
ABL_HOST	=1
ABL_WWW		=0
ABL_WEB		=1


[ABL_SERVICE]
ip=1.1.1.1
port=10
FileList=1
[ABL_SERVICE.FileList]
ABL_SERVICE.SERVICE01= S:asjf:aasf
SERVICE02= R:123:abc
SERVICE03= R:ZXC:!@#

[ABL_HOST]
ip=1.12.13.14
port=101
FileList=0
[ABL_SERVICE.FileList]
SERVICE01= T:asjf:aasf
SERVICE02= X:123:abc
SERVICE03= Y:ZXC:!@#

[ABL_WWW]
ip=1.1.1.17
port=103
url=www.hanmail.net
FileList=1
[ABL_SERVICE.FileList]
SERVICE01= S:asjf:aasf
SERVICE02= R:123:abc
SERVICE03= R:ZXC:!@#

[ABL_WEB]
ip=100.10.15.17
port=107
url=www.google.net
FileList=0
[ABL_WEB.FileList]
ABL_WEB.SERVICE0T= S:asjf:aasf
SERVICE0W= R:123:abc
SERVICE0K= R:ZXC:!@#

 */
 /*
 ================
 실행 결과 물
 
 *****************
******getIniObject**********
******getHost**********
--------------start------------
service [ABL_SERVICE]
Key [port]
Value [10]
Key [ip]
Value [1.1.1.1]
Key [FileList]
Value [1]
--------------end------------
--------------start------------
service [ABL_SERVICE.FileList]
Key [SERVICE02]
Value [R:123:abc]
Key [SERVICE01]
Value [S:asjf:aasf]
Key [SERVICE03]
Value [R:ZXC:!@#]
Key [ABL_SERVICE.SERVICE01]
Value [S:asjf:aasf]
--------------end------------
--------------start------------
service [ABL_WEB]
Key [url]
Value [www.google.net]
Key [ip]
Value [100.10.15.17]
Key [FileList]
Value [0]
Key [port]
Value [107]
--------------end------------
--------------start------------
service [ABL_HOST]
Key [ip]
Value [1.12.13.14]
Key [port]
Value [101]
Key [FileList]
Value [0]
--------------end------------

===================================
*/

public class IniUtil {

    Ini iniObject ;
    Profile.Section section;
    
    List<IniInfo> iniInfoList;
    
    final String strHostName = "HOST";
    final String strHostBatchFileList = "FILE_LIST";
    Vector<String> vectorServiceNames;
    
    final String strEmpty = "";
    final String strOne = "1";
    final String strDoc = ".";
    
    //파일경로
    File file = new File("process.ini");


    public void getHostName() throws Exception
    {

    	System.out.println("*****************");
    	getIniObject();

    	//[HOST] section 으로 데이터를 읽기 위한 설정 
    	section= iniObject.get(strHostName);
    	
    	getHost();

    }
    
    //Ini Object에 ini file을 로드 
    private void getIniObject() throws Exception
    {
    	//Object clear
    	if(iniObject!=null)
    	{
    		iniObject.clear();
    		iniObject = null;
    	}
    	
    	iniObject = new Ini(file);
    	
    	iniInfoList = new ArrayList<IniInfo>();
    	System.out.println("******getIniObject**********");
    }
    
    //서비스 목록철 처을 가지고오는 기능
    private void getHost() throws Exception
    {

    	if(section == null) throw new Exception ("Host Section is null and you are checked section name [" + section.getName() + "]");
    	
    	 System.out.println("******getHost**********");
    	 
    	 Iterator<Entry<String, String>> iterator = section.entrySet().iterator();
         
         Entry<String, String> entry;
          
         String strService="";
         String strValue="";
         
         while (iterator.hasNext())
         {
        	 IniInfo iniInfo = new IniInfo();
 
        	 try
         	{
         		entry = iterator.next();
         		
         		strService   = entry.getKey();
         		strValue = entry.getValue();
         		
         		if(strValue==null || strEmpty.equalsIgnoreCase(strValue.trim()))  continue;
         		else if(strOne.equalsIgnoreCase(strValue.trim())) 
         		{
         			
         			//서비스명과 서비스가 가지공 있는 옵션을 저장
         			iniInfo.setSectionName(strService);
         			String strBatchFile = getService(strService,iniInfo);
         			
         			//서비스가 배치를 가지고 있으면 배치에 대한 목록을 가지고 오는 기능
         			//배치파일리스트가 있음
         			if(strBatchFile!=null && strOne.equalsIgnoreCase(strBatchFile.trim()))
         			{
         				//host에 들어 있는 실질적인 서비스를 가지고 오는 기능
         				getService(strService+strDoc+strHostBatchFileList,iniInfo);
         				
         			}
         			
         		}
         		
         		//서비스에대한 정보를 객체에 저장하고 그것을 리스트에 저장 
         		iniInfoList.add(iniInfo);
         			
         	}
         	catch(Exception e)
         	{
         		e.printStackTrace();
         	}
        	 
        	
         }
    	
    }
    
    
    private  Profile.Section  getSection(String strSectionName)
    {
    	return iniObject.get(strSectionName);
    }
  
    
    private String getService(String paramService, IniInfo paramIniInfo) throws Exception
    {
    	//host에 들어 있는 실질적인 서비스를 가지고 오는 기능
		Profile.Section hostSection = getSection(paramService);
		
		//서비스를 하기위한 옵션
		Vector<String> sectionOptions = new Vector<String>();
		//옵션과 옵션에 들어있는 값
		Map<String,String> hashMapOption = new HashMap<String,String>();
		
		System.out.println("--------------start------------");
		
		System.out.println("service [" + paramService + "]");
		
		//결과를 분석해서 데이터를 가지고오는 기능
		String strBatchFile = getOptionKeyValues(sectionOptions,hashMapOption,hostSection);
		
		//clear
		hostSection.clear();
		sectionOptions.clear();
		hashMapOption.clear();
		
		//paramService.indexOf(strDoc+strHostBatchFileList)는 .FileList를 포함하는지 확인
		if(paramService!=null && paramService.indexOf(strDoc+strHostBatchFileList)!=-1)
		{
			//batch list
			paramIniInfo.setMapFileValue(hashMapOption);
			paramIniInfo.setFileNames(sectionOptions);
		}
		else
		{
			//서비스 option 
			paramIniInfo.setSectionOptions(sectionOptions);
			paramIniInfo.setMapSectionValue(hashMapOption);

		}
		
		System.out.println("--------------end------------");
		
		return strBatchFile;
    }
    
    //선택한 section으로 이동하여 section에 있는 값을 로드
    private String  getOptionKeyValues(Vector<String> paramVector, Map<String,String> paramMap,Profile.Section paramSection) throws Exception
    {
    	String strBatchFile = "";
    	
    	if(paramSection == null) throw new Exception ("Section is null");
    	if(paramVector 	== null) throw new Exception ("Vector is null");
    	if(paramMap 	== null) throw new Exception ("Map is null");
    	
        Iterator<Entry<String, String>> iterator = paramSection.entrySet().iterator();
       
        Entry<String, String> entry;
         
        String strKey="";
        String strValue="";
        
        while (iterator.hasNext())
        {
        	try
        	{
        		entry = iterator.next();
        		
        		strKey   = entry.getKey();
        		strValue = entry.getValue();
        		
        		if(strValue==null || strEmpty.equalsIgnoreCase(strValue.trim()))  continue;
        		
        		//배치리스트를 가지고 있는지 확인
        		if( strKey  !=null && 
        			 strHostBatchFileList.equalsIgnoreCase(strKey.trim())  && 
        			  strOne.equalsIgnoreCase(strValue.trim())
        		  )
        		{
        			strBatchFile = strValue;
        		}
        		
        		System.out.println("Key [" + strKey + "]");
        		System.out.println("Value [" + strValue + "]");
        		
           		paramVector.add(strKey);
           		paramMap.put(strKey,strValue);
        		
        	}
        	catch(Exception e)
        	{
        		
        		e.printStackTrace();
        	}
        	
        }//while
        
        return strBatchFile;

    }
    
    

    public static void main(String[] args)  
    {
        IniUtil iniUtil = new IniUtil();
        try {
            iniUtil.getHostName();
        }
        catch(Exception e)
        {
     		System.out.println(e.toString());
     		e.printStackTrace();

        }
        finally {
            ;
        }
    }


}

====================================
자료구조 Object
====================================


package sftp.service.util;

import java.util.Map;
import java.util.Vector;
import java.util.HashMap;
public class IniInfo {

   

	//section name
    String sectionName;

    //section option
    Vector<String> sectionOptions;

    
	//section option(key) and section value
    Map <String,String> mapSectionValue;

    
    //batch file list
    Vector<String> fileNames;
    
    //file send or receive condition
    Map <String,String> mapFileValue;
    

    public String getSectionName() {
		return sectionName;
	}

	public void setSectionName(String sectionName) {
		this.sectionName = sectionName;
	}

	public Vector<String> getSectionOptions() {
		return sectionOptions;
	}

	public void setSectionOptions(Vector<String> sectionOptions) {
		this.sectionOptions = sectionOptions;
	}

	public Map<String, String> getMapSectionValue() {
		return mapSectionValue;
	}

	public void setMapSectionValue(Map<String, String> mapSectionValue) {
		this.mapSectionValue = mapSectionValue;
	}

	public Vector<String> getFileNames() {
		return fileNames;
	}

	public void setFileNames(Vector<String> fileNames) {
		this.fileNames = fileNames;
	}

	public Map<String, String> getMapFileValue() {
		return mapFileValue;
	}

	public void setMapFileValue(Map<String, String> mapFileValue) {
		this.mapFileValue = mapFileValue;
	}


}



관련글 더보기

댓글 영역