Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
PETALS - PETALS Services Platform. Copyright (c) 2007 EBM Websourcing, http://www.ebmwebsourcing.com/ 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ------------------------------------------------------------------------- $Id$ -------------------------------------------------------------------------
 
 
 package org.ow2.petals.component.framework.api.util;
 
 import java.io.File;
 import java.net.URL;
 import java.util.List;
 import java.util.Map;
 
Utility to handle a CDK properties file.

Author(s):
ofabre - EBM Websourcing
Since:
1.0
 
 public class PropertiesUtil {

    

Load a java.util.Properties object from a properties file path. The file path can be relative path based on Petals root path or a valid URL.

Exemples :
Relative path : /conf/external.properties
Absolute path : file:///C:/conf/external.properties
URL : ftp://user:pwd@host:port/external.properties

Parameters:
propertiesFilePath The file path can be relative path based on Petals root path or a valid URL
Returns:
a java.util.Properties object fill with properties value found in the targeted file
Throws:
PEtALSCFException if error occurs when reading external properties file
 
     public final static Properties loadProperties(String propertiesFilePath)
             throws PEtALSCDKException {
         Properties properties = new Properties();
 
         if (propertiesFilePath != null) {
             InputStream propFileStream = null;
             try {
                 propFileStream = loadPropertiesFileFromUrl(new URL(propertiesFilePath));
             } catch (MalformedURLException e) {
                 propFileStream = loadPropertiesFileFromRelativePath(propertiesFilePath);
             }
             if (propFileStream != null) {
                 try {
                     properties.load(propFileStream);
                 } catch (IOException e) {
                     throw new PEtALSCDKException("Cannot find properties at location : "
                             + propertiesFilePath);
                 }
             } else {
                 throw new PEtALSCDKException("Cannot find properties at location : "
                         + propertiesFilePath);
             }
         }
 
         return properties;
     }

    

Replace place holders in the toProperties java.util.Properties object by values found in external properties file denoted by the given propertiesFilePath. Place holders look like : ${property.name.in.fromProperties.object}

Exemple :
toProperties property place holder : ${foo.bar}
External properties file property : foo.bar
Property with value ${foo.bar} in toProperties object will be overiden by the value of foo.bar property of the external properties file

Parameters:
propertiesFilePath The file path can be relative path based on Petals root path or a valid URL

Exemples :
Relative path : /conf/external.properties
Absolute path : file:///C:/conf/external.properties
URL : ftp://user:pwd@host:port/external.properties

toProperties a java.util.Map instance with some place holder as value to replace
Throws:
PEtALSCFException if error occurs when reading external properties file or if a property specified in toProperties cannot be found in external properties file
    public static void fillPlaceHolder(String propertiesFilePathMap<StringStringextensions)
            throws PEtALSCDKException {
        Properties externalProperties = loadProperties(propertiesFilePath);
        fillPlaceHolder(externalPropertiesextensions);
    }

    

Replace place holders in the toProperties java.util.Properties object by values found in fromProperties java.util.Properties object. Place holders look like : ${property name in fromProperties object}

Exemple :
toProperties property place holder : ${foo.bar}
fromProperties property : foo.bar
Property with value ${foo.bar} in toProperties object will be overiden by the value of foo.bar property of the fromProperties object

Parameters:
fromProperties properties that will replace place holders in toProperties object
toProperties a java.util.Map instance with some place holder as value to replace
Throws:
PEtALSCFException if a property specified in toProperties cannot be found in fromProperties
    public static void fillPlaceHolder(final Properties fromProperties,
            final Map<StringStringextensions) {
        for (Entry<StringStringentry : extensions.entrySet()) {
            String entryValue = (Stringentry.getValue();
            String key = (Stringentry.getKey();
            if (entryValue != null && entryValue.indexOf("${") >= 0) {
                List<StringplaceHolderKeys = extractPlaceHolderKeys(entryValue);
                String newEntryValue = fillPlaceHolder(entryValueplaceHolderKeysfromProperties);
                extensions.put(keynewEntryValue);
            }
        }
    }
    public static List<StringextractPlaceHolderKeys(String entryValue) {
        List<Stringresult = new ArrayList<String>();
        String[] keys = entryValue.split("[${]");
        for (String string : keys) {
            if (string.indexOf("}") > 0) {
                result.add(string.split("[}]")[0]);
            }
        }
        return result;
    }
    public static String fillPlaceHolder(final String entryValue,
            final List<StringplaceHolderKeysfinal Properties fromProperties) {
        String result = entryValue;
        for (String key : placeHolderKeys) {
            String extPropertyValue = fromProperties.getProperty(key);
            if (extPropertyValue != null) {
                result = result.replace("${" + key + "}"extPropertyValue);
            } else {
                result = result.replace("${" + key + "}""");
            }
        }
        return result;
    }

    

Parameters:
propertiesFilePath
Returns:
    private static final InputStream loadPropertiesFileFromRelativePath(String propertiesFilePath) {
        InputStream result = null;
        // FIXME : really PEtALS dependent..
        String petalsHome = System.getenv("PETALS_HOME");
        if (petalsHome != null) {
            File propFile = new File(petalsHomepropertiesFilePath);
            if (propFile.exists()) {
                try {
                    return new FileInputStream(propFile);
                } catch (FileNotFoundException e) {
                    // TODO log cannot read specified properties file :
                    // propertiesFilePath;
                }
            }
        }
        return result;
    }
    private static final InputStream loadPropertiesFileFromUrl(URL url) {
        InputStream result = null;
        try {
            result = getInputStream(url);
        } catch (Exception e) {
            // TODO log can not retrieve properties file from URL :" + url);
        }
        return result;
    }
    private static final InputStream getInputStream(URL urlthrows PEtALSCDKException {
        URLConnection connection = null;
        InputStream is = null;
        try {
            connection = url.openConnection();
            is = connection.getInputStream();
        } catch (IOException e) {
            throw new PEtALSCDKException("Can not get InputStream from URL " + urle);
        }
        return is;
    }
New to GrepCode? Check out our FAQ X