Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Author: Jan Galinski
Submitted: 25.02.2010

Description

Mapping of custom property file to WDConfiguration

package de.holisticon.wd.helper;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;

import com.sap.tc.webdynpro.services.sal.config.api.IWDConfiguration;
import com
    .sap
    .tc
    .webdynpro
    .services
    .sal
    .config
    .api
    .WDInvalidConfigParameterException;


/**
 * @author x0003604
 */
public class KonfigurationAdapter implements IWDConfiguration {

    private final Properties properties;

    /**
     *
     */
    public KonfigurationAdapter(final Properties properties) {
        super();
        this.properties = properties;
    }

    /* (non-Javadoc)
     * @see com.sap.tc.webdynpro.services.sal.config.api.IWDConfiguration#getStringEntry(java.lang.String)
     */
    public String getStringEntry(final String key)
        throws WDInvalidConfigParameterException {
        return StringUtils.trimToEmpty(properties.getProperty(key));
    }

    /* (non-Javadoc)
     * @see com.sap.tc.webdynpro.services.sal.config.api.IWDConfiguration#getStringEntry(java.lang.String, java.lang.String)
     */
    public String getStringEntry(final String key, final String value) {
        return properties.getProperty(key, value);
    }

    /* (non-Javadoc)
     * @see com.sap.tc.webdynpro.services.sal.config.api.IWDConfiguration#getStringEntries(java.lang.String)
     */
    public String[] getStringEntries(String key)
        throws WDInvalidConfigParameterException {
        String[] values = StringUtils.split(getStringEntry(key), ",");

        for (int i = 0; i < values.length; i++) {
            values[i] = StringUtils.trimToNull(values[i]);
        }

        return values;
    }

    /* (non-Javadoc)
     * @see com.sap.tc.webdynpro.services.sal.config.api.IWDConfiguration#getDateEntry(java.lang.String)
     */
    public Date getDateEntry(final String key)
        throws WDInvalidConfigParameterException {
        String value = getStringEntry(key);
        try {
            return DateFormat.getInstance().parse(value);
        } catch (ParseException e) {
            throw new WDInvalidConfigParameterException(e);
        }
    }

    /* (non-Javadoc)
     * @see com.sap.tc.webdynpro.services.sal.config.api.IWDConfiguration#getDateEntry(java.lang.String, java.util.Date)
     */
    public Date getDateEntry(String key, Date date) {
        Date d = null;
        try {
            d = getDateEntry(key);
        } catch (WDInvalidConfigParameterException e) {
            d = date;
            // fallthrough
        }

        return d;
    }

    /* (non-Javadoc)
     * @see com.sap.tc.webdynpro.services.sal.config.api.IWDConfiguration#getDateEntries(java.lang.String)
     */
    public Date[] getDateEntries(String key)
        throws WDInvalidConfigParameterException {
        String[] values = getStringEntries(key);

        Date[] dates = new Date[values.length];

        for (int i = 0; i < values.length; i++) {
            try {
                dates[i] = DateFormat.getInstance().parse(values[i]);
            } catch (ParseException e) {
                throw new WDInvalidConfigParameterException(e);
            }
        }

        return dates;
    }

    /* (non-Javadoc)
     * @see com.sap.tc.webdynpro.services.sal.config.api.IWDConfiguration#getIntEntry(java.lang.String)
     */
    public int getIntEntry(String key)
        throws WDInvalidConfigParameterException {
        String value = getStringEntry(key);
        return Integer.parseInt(value);
    }

    /* (non-Javadoc)
     * @see com.sap.tc.webdynpro.services.sal.config.api.IWDConfiguration#getIntEntry(java.lang.String, int)
     */
    public int getIntEntry(String key, int value) {
        try {
            value = getIntEntry(key);
        } catch (Exception e) {
            // fallthrough
        }

        return value;
    }

    /* (non-Javadoc)
     * @see com.sap.tc.webdynpro.services.sal.config.api.IWDConfiguration#getIntEntries(java.lang.String)
     */
    public int[] getIntEntries(String key)
        throws WDInvalidConfigParameterException {
        String[] values = getStringEntries(key);

        int[] ints = new int[values.length];

        for (int i = 0; i < values.length; i++) {
            try {
                ints[i] = Integer.parseInt(values[i]);
            } catch (RuntimeException e) {
                throw new WDInvalidConfigParameterException(e);
            }
        }

        return ints;
    }

    /* (non-Javadoc)
     * @see com.sap.tc.webdynpro.services.sal.config.api.IWDConfiguration#getLongEntry(java.lang.String)
     */
    public long getLongEntry(String key)
        throws WDInvalidConfigParameterException {
        String value = getStringEntry(key);
        return Long.parseLong(value);
    }

    /* (non-Javadoc)
     * @see com.sap.tc.webdynpro.services.sal.config.api.IWDConfiguration#getLongEntry(java.lang.String, long)
     */
    public long getLongEntry(String key, long value) {
        try {
            value = getLongEntry(key);
        } catch (Exception e) {
            // fallthrough
        }

        return value;
    }

    /* (non-Javadoc)
     * @see com.sap.tc.webdynpro.services.sal.config.api.IWDConfiguration#getLongEntries(java.lang.String)
     */
    public long[] getLongEntries(final String key)
        throws WDInvalidConfigParameterException {
        String[] values = getStringEntries(key);

        long[] longs = new long[values.length];

        for (int i = 0; i < values.length; i++) {
            try {
                longs[i] = Long.parseLong(values[i]);
            } catch (RuntimeException e) {
                throw new WDInvalidConfigParameterException(e);
            }
        }

        return longs;
    }

    /* (non-Javadoc)
     * @see com.sap.tc.webdynpro.services.sal.config.api.IWDConfiguration#getBooleanEntry(java.lang.String)
     */
    public boolean getBooleanEntry(final String key)
        throws WDInvalidConfigParameterException {
        return BooleanUtils.toBoolean(getStringEntry(key));
    }

    /* (non-Javadoc)
     * @see com.sap.tc.webdynpro.services.sal.config.api.IWDConfiguration#getBooleanEntry(java.lang.String, boolean)
     */
    public boolean getBooleanEntry(final String key, boolean value) {
        try {
            value = getBooleanEntry(key);
        } catch (WDInvalidConfigParameterException e) {
            // fallthrough
        }

        return value;
    }

    /* (non-Javadoc)
     * @see com.sap.tc.webdynpro.services.sal.config.api.IWDConfiguration#getBooleanEntries(java.lang.String)
     */
    public boolean[] getBooleanEntries(String key)
        throws WDInvalidConfigParameterException {
        String[] values = getStringEntries(key);

        boolean[] result = new boolean[values.length];

        for (int i = 0; i < values.length; i++) {
            try {
                result[i] = BooleanUtils.toBoolean(values[i]);
            } catch (RuntimeException e) {
                throw new WDInvalidConfigParameterException(e);
            }
        }

        return result;
    }

    /* (non-Javadoc)
     * @see com.sap.tc.webdynpro.services.sal.config.api.IWDConfiguration#getAllKeys()
     */
    public String[] getAllKeys() {
        String[] k = new String[properties.size()];
        int i = 0;

        Enumeration e = properties.propertyNames();

        while (e.hasMoreElements()) {
            k[i++] = (String) e.nextElement();
        }
        return k;
    }

    /* (non-Javadoc)
     * @see com.sap.tc.webdynpro.services.sal.api.IWDService#getServiceName()
     */
    public String getServiceName() {
        return "PropertiesAdapter";
    }

    /* (non-Javadoc)
     * @see com.sap.tc.webdynpro.services.sal.api.IWDService#getDescription()
     */
    public String getDescription() {
        return properties.toString();
    }

    /* (non-Javadoc)
     * @see com.sap.tc.webdynpro.services.sal.api.IWDService#getVersion()
     */
    public String getVersion() {
        return "1.0";
    }

}