All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.skltp.ei.intsvc.dynamicFlows.PropertyUtil Maven / Gradle / Ivy

/**
 * Copyright (c) 2013 Sveriges Kommuner och Landsting (SKL). 
 *
 * This file is part of SKLTP.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package se.skltp.ei.intsvc.dynamicFlows;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.soitoolkit.commons.mule.util.RecursiveResourceBundle;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class PropertyUtil {

	// Pattern for a multiple PARTNER parameter: PARTNER(n)_(name)
    public static final String PARTNER_LIST_NAME     = "AGMTS"; // The map-key for agreements, does not exist in the property-file
    public static final String PARTNER_PREFIX        = "AGMT";
    public static final int    PARTNER_PREFIX_LENGTH = 4; // todo: PARTNER_PREFIX.length(); gives 0 ???
    public static final String PARTNER_INBOUND_MANAGE_STOPFILE = "INBOUND_MANAGE_STOPFILE";
    public static final String PARTNER_INBOUND_FOLDER = "INBOUND_FOLDER";
    
    public static final String ID                    = "ID";
    public static final char   SEPARATOR             = '_';

    public static final String SUPERVISE_FILES_LIST_NAME = "SUPERVISE_FILES";  // The map-key for files to supervise, does not exist in the property-file
    public static final String SUPERVISE_FILES_PREFIX    = "SUPERVISE_FILES_";
    public static final int    SUPERVISE_FILES_PREFIX_LENGTH = 16; // todo: SUPERVISE_FILES_PREFIX.length(); gives 0 ???
    public static final String SUPERVISE_FILES_FOLDERS   = "FOLDERS";
    public static final String SUPERVISE_FILES_MAXAGE    = "MAXAGE_MINUTES";
    public static final String SUPERVISE_FILES_FILTER    = "FILE_FILTER";
    public static final String SUPERVISE_FILES_STARTTIME = "STARTTIME";
    public static final String SUPERVISE_FILES_STOPTIME  = "STOPTIME";

    public static final String SUPERVISE_ACTIVEMQ_LIST_NAME       = "SUPERVISE_AMQ_BROKERS"; // The map-key for amq brokers to supervise, does not exist in the property-file
    public static final String SUPERVISE_ACTIVEMQ_PREFIX          = "SUPERVISE_ACTIVEMQ_";
    public static final int    SUPERVISE_ACTIVEMQ_PREFIX_LENGTH = 19; // todo: SUPERVISE_ACTIVEMQ_PREFIX.length(); gives 0 ???
    public static final String SUPERVISE_ACTIVEMQ_URL             = "URL";
    public static final String SUPERVISE_ACTIVEMQ_QUEUE_DEPTH_AGE = "QUEUE_DEPTH_AGE";
    
    public static final String STOPFILE_NAME = "eRcptRtr.fel";
    
	private static final Logger log = LoggerFactory.getLogger(PropertyUtil.class);

	private static Map resovledProperties = null;
	private static Map agreementsMap = null;

	static public Map getResovledProperties() {
    	if (resovledProperties == null) {
    		// FIXME. Inject the name of the property file!
    		resovledProperties = new PropertyUtil().getResovledProperties("ei-config","ei-config-override");
    	}
		return resovledProperties;
    }
	
	static public Map getAgreementsMap() {
    	if (agreementsMap == null) {
    		agreementsMap = new HashMap();
    		
    		List agreementList = (List)getResovledProperties().get(PARTNER_LIST_NAME);
    		for (Map agmt : agreementList) {
    			agreementsMap.put((Integer)agmt.get(ID), agmt);
			}
    	}

    	return agreementsMap;
    }

	static public List getSuperviseFilesList() {
		return (List)getResovledProperties().get(SUPERVISE_FILES_LIST_NAME);
	}

	static public List getSupervisemqsList() {
		return (List)getResovledProperties().get(SUPERVISE_ACTIVEMQ_LIST_NAME);
	}

	private Map getResovledProperties(String... bundleNames) {
        RecursiveResourceBundle rb = new RecursiveResourceBundle(bundleNames);

        Set keys = (Set)rb.getProperties().keySet();

        Map map = new HashMap();
        for (String key : keys) {
            String value = rb.getString(key); // Here is where the recursive property resolve process takes place!
            put(map, key, value); // Here tmp maps of agreements, files and amq brokers to supervise are created
        }
        
//        // Create a list of the found partners, validate each partner entry and add it to the map
//        List partnerList = createListProperty(PARTNER_LIST_NAME, partnerRootMap, new PartnerValidator(), map);
//        
//        // Create a list of the found files to supervise, validate each entry and add it to the map
//        List superviseFilesList = createListProperty(SUPERVISE_FILES_LIST_NAME, superviseFilesRootMap, new SuperviseFilesValidator(), map);
//        
//        // Create a list of the found amq brokers to supervise, validate each entry and add it to the map
//        List superviseAmqsList = createListProperty(SUPERVISE_ACTIVEMQ_LIST_NAME, superviseAmqsRootMap, new SuperviseAmqsValidator(), map);
//        
//        log.info("Found {} properties, where {} are agreements, {} are supervise-files and {} are supervise-activemq brokers, ", 
//        		new Object[] {map.size(), partnerList.size(), superviseFilesList, superviseAmqsList});        
        return map;
    }

	@SuppressWarnings("unused")
	private List createListProperty(final String listName,
			final Map rootMap, Validator validator,
			Map map) {
		// Create a list of the found entities, validate each partner entry and add it to the map
        List partnerList = new ArrayList();
        
        if (rootMap != null) {
			Set> partnerEntries = rootMap.entrySet();
	        for (Entry entry : partnerEntries) {
	            if (validator.validateProperties(map, entry)) {
	                partnerList.add(entry.getValue());
	            }
	        }
        }

        // Finally add the partner list
		map.put(listName, partnerList);
		return partnerList;
	}
    
    private void put(Map map, String key, String value) {
        
        // Is this a PARTNER(n)_(name) - parameter?
        if (key.startsWith(PARTNER_PREFIX)) {
            
            try {
                // Try to get the partner number and the PARTNER parameter name
                int numberStartPos = PARTNER_PREFIX_LENGTH;
                int numberEndpos = key.indexOf(SEPARATOR);
                String idStr = key.substring(numberStartPos, numberEndpos);
                int id = Integer.parseInt(idStr);
                
                String name = key.substring(numberEndpos + 1); // SKip the separator character
                

                Map partnerMap = getPartnerMap(id);
                partnerMap.put(name, value);
                
            } catch (NumberFormatException e) {
                // NO, something went wrong with the partner parsing, lets assume its a normal parameter.
                log.warn("Create partner parameter failed, handled as normal paramter, error: " + e.getMessage());
                e.printStackTrace();
                map.put(key, value);
            }

    
	    // Is this a SUPERVISOR_FILES_(n)_(name) - parameter?
	    } else if (key.startsWith(SUPERVISE_FILES_PREFIX)) {
            
            try {
                // Try to get the partner number and the SUPERVISE_FILES parameter name
                int numberStartPos = SUPERVISE_FILES_PREFIX_LENGTH;
                int numberEndpos = key.indexOf(SEPARATOR, numberStartPos);
                String idStr = key.substring(numberStartPos, numberEndpos);
                int id = Integer.parseInt(idStr);
                
                String name = key.substring(numberEndpos + 1); // SKip the separator character
                

                Map superviseFilesMap = getSuperviseFilesMap(id);
                superviseFilesMap.put(name, value);
                
                log.debug("Supervise file #" + id + " property: " + name + " = " + value);
                
            } catch (NumberFormatException e) {
                // NO, something went wrong with the parsing, lets assume its a normal parameter.
                log.warn("Create supervise files parameter failed, handled as normal paramter, error: " + e.getMessage());
                e.printStackTrace();
                map.put(key, value);
            }

    
	    // Is this a SUPERVISOR_ACTIVEMQ_(n)_(name) - parameter?
	    } else if (key.startsWith(SUPERVISE_ACTIVEMQ_PREFIX)) {
            
            try {
                // Try to get the partner number and the SUPERVISE_ACTIVEMQ parameter name
                int numberStartPos = SUPERVISE_ACTIVEMQ_PREFIX_LENGTH;
                int numberEndpos = key.indexOf(SEPARATOR);
                String idStr = key.substring(numberStartPos, numberEndpos);
                int id = Integer.parseInt(idStr);
                
                String name = key.substring(numberEndpos + 1); // SKip the separator character
                

                Map superviseAmqsMap = getSuperviseAmqsMap(id);
                superviseAmqsMap.put(name, value);
                
            } catch (NumberFormatException e) {
                // NO, something went wrong with the parsing, lets assume its a normal parameter.
                log.warn("Create supervise amqs parameter failed, handled as normal paramter, error: " + e.getMessage());
                e.printStackTrace();
                map.put(key, value);
            }

            
	    } else {
	        
	        // NO, its a normal parameter, just store it
	        map.put(key, value);
	    }

    
    }

    private Map partnerRootMap = null;

    private Map getPartnerMap(int id) {

        if (partnerRootMap == null) {
            partnerRootMap = new TreeMap();
        }
        
        Map partnerMap = (Map)partnerRootMap.get(id);
        if (partnerMap == null) {
        	// No map exists for this partner, create one and add a standard ID-property
        	// Also add the new partner-map to the root-map of all partners
            partnerMap = new HashMap();
            partnerMap.put(ID, id);
            partnerRootMap.put(id, partnerMap);
        }

        return partnerMap;
    }
    
    
    private Map superviseFilesRootMap = null;

    private Map getSuperviseFilesMap(int id) {

        if (superviseFilesRootMap == null) {
        	superviseFilesRootMap = new TreeMap();
        }
        
        Map superviseFilesMap = (Map)superviseFilesRootMap.get(id);
        if (superviseFilesMap == null) {
        	// No map exists for this superviseFiles - element, create one and add a standard ID-property
        	// Also add the new superviseFiles-map to the root-map of all files to supervise
        	superviseFilesMap = new HashMap();
        	superviseFilesMap.put(ID, id);
        	superviseFilesRootMap.put(id, superviseFilesMap);
        }

        return superviseFilesMap;
    }
    

    private Map superviseAmqsRootMap = null;

    private Map getSuperviseAmqsMap(int id) {

        if (superviseAmqsRootMap == null) {
        	superviseAmqsRootMap = new TreeMap();
        }
        
        Map superviseAmqsMap = (Map)superviseAmqsRootMap.get(id);
        if (superviseAmqsMap == null) {
        	// No map exists for this superviseAmqs - element, create one and add a standard ID-property
        	// Also add the new superviseAmqs-map to the root-map of all amq brokers to supervise
        	superviseAmqsMap = new HashMap();
        	superviseAmqsMap.put(ID, id);
        	superviseAmqsRootMap.put(id, superviseAmqsMap);
        }

        return superviseAmqsMap;
    }
    
//    private Set uniquePartnerNames = new HashSet();
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy