Search in sources :

Example 1 with ConfigPropertyType

use of org.eclipse.kura.core.configuration.XmlConfigPropertyAdapted.ConfigPropertyType in project kura by eclipse.

the class XmlConfigPropertiesAdapter method unmarshal.

public Map<String, Object> unmarshal(XmlConfigPropertiesAdapted adaptedPropsAdapted) throws Exception {
    Map<String, Object> properties = new HashMap<String, Object>();
    XmlConfigPropertyAdapted[] adaptedProps = adaptedPropsAdapted.getProperties();
    if (adaptedProps == null) {
        return properties;
    }
    for (XmlConfigPropertyAdapted adaptedProp : adaptedProps) {
        String propName = adaptedProp.getName();
        ConfigPropertyType type = adaptedProp.getType();
        if (type != null) {
            Object propvalue = null;
            if (adaptedProp.getArray() == false) {
                switch(adaptedProp.getType()) {
                    case STRING_TYPE:
                        propvalue = adaptedProp.getValues()[0];
                        break;
                    case LONG_TYPE:
                        propvalue = Long.parseLong(adaptedProp.getValues()[0]);
                        break;
                    case DOUBLE_TYPE:
                        propvalue = Double.parseDouble(adaptedProp.getValues()[0]);
                        break;
                    case FLOAT_TYPE:
                        propvalue = Float.parseFloat(adaptedProp.getValues()[0]);
                        break;
                    case INTEGER_TYPE:
                        propvalue = Integer.parseInt(adaptedProp.getValues()[0]);
                        break;
                    case BYTE_TYPE:
                        propvalue = Byte.parseByte(adaptedProp.getValues()[0]);
                        break;
                    case CHAR_TYPE:
                        String s = adaptedProp.getValues()[0];
                        propvalue = Character.valueOf(s.charAt(0));
                        break;
                    case BOOLEAN_TYPE:
                        propvalue = Boolean.parseBoolean(adaptedProp.getValues()[0]);
                        break;
                    case SHORT_TYPE:
                        propvalue = Short.parseShort(adaptedProp.getValues()[0]);
                        break;
                    case PASSWORD_TYPE:
                        BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
                        ServiceReference<CryptoService> cryptoServiceRef = bundleContext.getServiceReference(CryptoService.class);
                        CryptoService cryptoService = bundleContext.getService(cryptoServiceRef);
                        propvalue = adaptedProp.getValues()[0];
                        if (adaptedProp.isEncrypted()) {
                            try {
                                propvalue = new Password(cryptoService.decryptAes(((String) propvalue).toCharArray()));
                            } catch (KuraException e) {
                                propvalue = new Password(cryptoService.decodeBase64((String) propvalue));
                            }
                        } else {
                            propvalue = new Password((String) propvalue);
                        }
                        break;
                }
            } else {
                // Starting from 1.2.0 an empty array will never be present in a snapshot.
                if (adaptedProp.getValues() == null) {
                    continue;
                }
                switch(adaptedProp.getType()) {
                    case STRING_TYPE:
                        propvalue = adaptedProp.getValues();
                        break;
                    case LONG_TYPE:
                        Long[] longValues = new Long[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                longValues[i] = Long.parseLong(adaptedProp.getValues()[i]);
                            }
                        }
                        propvalue = longValues;
                        break;
                    case DOUBLE_TYPE:
                        Double[] doubleValues = new Double[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                doubleValues[i] = Double.parseDouble(adaptedProp.getValues()[i]);
                            }
                        }
                        propvalue = doubleValues;
                        break;
                    case FLOAT_TYPE:
                        Float[] floatValues = new Float[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                floatValues[i] = Float.parseFloat(adaptedProp.getValues()[i]);
                            }
                        }
                        propvalue = floatValues;
                        break;
                    case INTEGER_TYPE:
                        Integer[] intValues = new Integer[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                intValues[i] = Integer.parseInt(adaptedProp.getValues()[i]);
                            }
                        }
                        propvalue = intValues;
                        break;
                    case BYTE_TYPE:
                        Byte[] byteValues = new Byte[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                byteValues[i] = Byte.parseByte(adaptedProp.getValues()[i]);
                            }
                        }
                        propvalue = byteValues;
                        break;
                    case CHAR_TYPE:
                        Character[] charValues = new Character[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                String s = adaptedProp.getValues()[i];
                                charValues[i] = Character.valueOf(s.charAt(0));
                            }
                        }
                        propvalue = charValues;
                        break;
                    case BOOLEAN_TYPE:
                        Boolean[] booleanValues = new Boolean[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                booleanValues[i] = Boolean.parseBoolean(adaptedProp.getValues()[i]);
                            }
                        }
                        propvalue = booleanValues;
                        break;
                    case SHORT_TYPE:
                        Short[] shortValues = new Short[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                shortValues[i] = Short.parseShort(adaptedProp.getValues()[i]);
                            }
                        }
                        propvalue = shortValues;
                        break;
                    case PASSWORD_TYPE:
                        BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
                        ServiceReference<CryptoService> cryptoServiceRef = bundleContext.getServiceReference(CryptoService.class);
                        CryptoService cryptoService = bundleContext.getService(cryptoServiceRef);
                        Password[] pwdValues = new Password[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                if (adaptedProp.isEncrypted()) {
                                    try {
                                        pwdValues[i] = new Password(cryptoService.decryptAes(adaptedProp.getValues()[i].toCharArray()));
                                    } catch (KuraException e) {
                                        pwdValues[i] = new Password(cryptoService.decodeBase64(adaptedProp.getValues()[i]));
                                    }
                                } else {
                                    pwdValues[i] = new Password(adaptedProp.getValues()[i]);
                                }
                            }
                        }
                        propvalue = pwdValues;
                        break;
                }
            }
            properties.put(propName, propvalue);
        }
    }
    return properties;
}
Also used : HashMap(java.util.HashMap) ConfigPropertyType(org.eclipse.kura.core.configuration.XmlConfigPropertyAdapted.ConfigPropertyType) CryptoService(org.eclipse.kura.crypto.CryptoService) KuraException(org.eclipse.kura.KuraException) Password(org.eclipse.kura.configuration.Password) BundleContext(org.osgi.framework.BundleContext)

Example 2 with ConfigPropertyType

use of org.eclipse.kura.core.configuration.XmlConfigPropertyAdapted.ConfigPropertyType in project kura by eclipse.

the class XmlJavaComponentConfigurationsMapper method parseProperty.

private XmlConfigPropertyAdapted parseProperty(Element property) {
    NodeList valuesList = property.getChildNodes();
    Element[] valuesArray = getElementNodes(valuesList);
    String[] values = new String[valuesArray.length];
    // get values
    for (int valIndex = 0; valIndex < valuesArray.length; valIndex++) {
        values[valIndex] = valuesArray[valIndex].getTextContent();
    }
    String name = property.getAttribute(CONFIGURATIONS_CONFIGURATION_PROPERTY_NAME);
    String type = property.getAttribute(CONFIGURATIONS_CONFIGURATION_PROPERTY_TYPE);
    String array = property.getAttribute(CONFIGURATIONS_CONFIGURATION_PROPERTY_ARRAY);
    String encrypted = property.getAttribute(CONFIGURATIONS_CONFIGURATION_PROPERTY_ENCRYPTED);
    ConfigPropertyType cct = getType(type);
    XmlConfigPropertyAdapted xmlProperty = new XmlConfigPropertyAdapted(name, cct, values);
    xmlProperty.setArray(Boolean.parseBoolean(array));
    xmlProperty.setEncrypted(Boolean.parseBoolean(encrypted));
    return xmlProperty;
}
Also used : ConfigPropertyType(org.eclipse.kura.core.configuration.XmlConfigPropertyAdapted.ConfigPropertyType) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) XmlConfigPropertyAdapted(org.eclipse.kura.core.configuration.XmlConfigPropertyAdapted)

Example 3 with ConfigPropertyType

use of org.eclipse.kura.core.configuration.XmlConfigPropertyAdapted.ConfigPropertyType in project kura by eclipse.

the class XmlJavaComponentConfigurationsMapper method marshallProperty.

private Element marshallProperty(XmlConfigPropertyAdapted propertyObj) {
    String name = propertyObj.getName();
    Boolean array = propertyObj.getArray();
    Boolean encrypted = propertyObj.isEncrypted();
    ConfigPropertyType cpt = propertyObj.getType();
    String[] values = propertyObj.getValues();
    if (values != null) {
        Element property = this.mashallDoc.createElement(ESF_NAMESPACE + ":" + CONFIGURATIONS_CONFIGURATION_PROPERTY);
        Attr attName = this.mashallDoc.createAttribute(CONFIGURATIONS_CONFIGURATION_PROPERTY_NAME);
        attName.setNodeValue(name);
        property.setAttributeNode(attName);
        Attr attArray = this.mashallDoc.createAttribute(CONFIGURATIONS_CONFIGURATION_PROPERTY_ARRAY);
        attArray.setNodeValue(array.toString());
        property.setAttributeNode(attArray);
        Attr attEncrypted = this.mashallDoc.createAttribute(CONFIGURATIONS_CONFIGURATION_PROPERTY_ENCRYPTED);
        attEncrypted.setNodeValue(encrypted.toString());
        property.setAttributeNode(attEncrypted);
        Attr attType = this.mashallDoc.createAttribute(CONFIGURATIONS_CONFIGURATION_PROPERTY_TYPE);
        attType.setNodeValue(getStringValue(cpt));
        property.setAttributeNode(attType);
        for (String value : values) {
            Element valueElem = this.mashallDoc.createElement(ESF_NAMESPACE + ":" + CONFIGURATIONS_CONFIGURATION_PROPERTY_VALUE);
            valueElem.setTextContent(value);
            property.appendChild(valueElem);
        }
        return property;
    }
    return null;
}
Also used : ConfigPropertyType(org.eclipse.kura.core.configuration.XmlConfigPropertyAdapted.ConfigPropertyType) Element(org.w3c.dom.Element) Attr(org.w3c.dom.Attr)

Aggregations

ConfigPropertyType (org.eclipse.kura.core.configuration.XmlConfigPropertyAdapted.ConfigPropertyType)3 Element (org.w3c.dom.Element)2 HashMap (java.util.HashMap)1 KuraException (org.eclipse.kura.KuraException)1 Password (org.eclipse.kura.configuration.Password)1 XmlConfigPropertyAdapted (org.eclipse.kura.core.configuration.XmlConfigPropertyAdapted)1 CryptoService (org.eclipse.kura.crypto.CryptoService)1 BundleContext (org.osgi.framework.BundleContext)1 Attr (org.w3c.dom.Attr)1 NodeList (org.w3c.dom.NodeList)1