Search in sources :

Example 1 with AD

use of org.eclipse.kura.configuration.metatype.AD in project kura by eclipse.

the class XmlJavaMetadataMapper method marshal.

// 
// Public methods
// 
@Override
public Element marshal(Document doc, Object o) throws Exception {
    this.mashallDoc = doc;
    if (o instanceof Tocd) {
        Tocd configOCD = (Tocd) o;
        String ocdName = configOCD.getName();
        String ocdDescription = configOCD.getDescription();
        String ocdID = configOCD.getId();
        List<Icon> ocdIcons = configOCD.getIcon();
        List<AD> ocdADs = configOCD.getAD();
        configOCD.getAny();
        configOCD.getOtherAttributes();
        Element ocd = this.mashallDoc.createElement(OCD_NAMESPACE + ":" + METADATA_OCD);
        if (ocdName != null && !ocdName.trim().isEmpty()) {
            Attr ocdAttrName = this.mashallDoc.createAttribute(METADATA_OCD_NAME);
            ocdAttrName.setNodeValue(ocdName);
            ocd.setAttributeNode(ocdAttrName);
        }
        if (ocdDescription != null && !ocdDescription.trim().isEmpty()) {
            Attr ocdAttrDescription = this.mashallDoc.createAttribute(METADATA_OCD_DESCRIPTION);
            ocdAttrDescription.setNodeValue(ocdDescription);
            ocd.setAttributeNode(ocdAttrDescription);
        }
        if (ocdID != null && !ocdID.trim().isEmpty()) {
            Attr ocdAttrId = this.mashallDoc.createAttribute(METADATA_OCD_ID);
            ocdAttrId.setNodeValue(ocdID);
            ocd.setAttributeNode(ocdAttrId);
        }
        if (ocdADs != null) {
            for (AD ocdAD : ocdADs) {
                Element ad = this.mashallDoc.createElement(OCD_NAMESPACE + ":" + METADATA_AD);
                marshallAD(ocdAD, ad);
                ocd.appendChild(ad);
            }
        }
        if (ocdIcons != null) {
            for (Icon ocdIcon : ocdIcons) {
                Element icon = this.mashallDoc.createElement(OCD_NAMESPACE + ":" + METADATA_ICON);
                marshallIcon(ocdIcon, icon);
                ocd.appendChild(icon);
            }
        }
        return ocd;
    }
    return null;
}
Also used : AD(org.eclipse.kura.configuration.metatype.AD) Element(org.w3c.dom.Element) Tocd(org.eclipse.kura.core.configuration.metatype.Tocd) Icon(org.eclipse.kura.configuration.metatype.Icon) Attr(org.w3c.dom.Attr)

Example 2 with AD

use of org.eclipse.kura.configuration.metatype.AD in project kura by eclipse.

the class CollectionsUtil method dictionaryToMap.

public static Map<String, Object> dictionaryToMap(Dictionary<String, Object> dictionary, OCD ocd) {
    if (dictionary == null) {
        return null;
    }
    Map<String, AD> ads = new HashMap<String, AD>();
    if (ocd != null) {
        for (AD ad : ocd.getAD()) {
            ads.put(ad.getId(), ad);
        }
    }
    Map<String, Object> map = new HashMap<String, Object>();
    Enumeration<String> keys = dictionary.keys();
    while (keys.hasMoreElements()) {
        String key = keys.nextElement();
        Object value = dictionary.get(key);
        AD ad = ads.get(key);
        if (ad != null && ad.getType() != null && Scalar.PASSWORD.equals(ad.getType())) {
            if (value instanceof char[]) {
                map.put(key, new Password((char[]) value));
            } else {
                map.put(key, new Password(value.toString()));
            }
        } else {
            map.put(key, value);
        }
    }
    return map;
}
Also used : AD(org.eclipse.kura.configuration.metatype.AD) HashMap(java.util.HashMap) Password(org.eclipse.kura.configuration.Password)

Example 3 with AD

use of org.eclipse.kura.configuration.metatype.AD in project kura by eclipse.

the class ConfigurationServiceImpl method validateProperties.

private void validateProperties(String pid, ObjectClassDefinition ocd, Map<String, Object> updatedProps) throws KuraException {
    if (ocd != null) {
        // build a map of all the attribute definitions
        Map<String, AttributeDefinition> attrDefs = new HashMap<String, AttributeDefinition>();
        AttributeDefinition[] defs = ocd.getAttributeDefinitions(ObjectClassDefinition.ALL);
        for (AttributeDefinition def : defs) {
            attrDefs.put(def.getID(), def);
        }
        // and validate them against the definition
        for (Entry<String, Object> property : updatedProps.entrySet()) {
            String key = property.getKey();
            AttributeDefinition attrDef = attrDefs.get(key);
            // is attribute undefined?
            if (attrDef == null) {
                // just accept them.
                continue;
            }
            // validate the attribute value
            Object objectValue = property.getValue();
            String stringValue = StringUtil.valueToString(objectValue);
            if (stringValue != null) {
                String result = attrDef.validate(stringValue);
                if (result != null && !result.isEmpty()) {
                    throw new KuraException(KuraErrorCode.CONFIGURATION_ATTRIBUTE_INVALID, attrDef.getID() + ": " + result);
                }
            }
        }
        // make sure all required properties are set
        OCD ocdFull = getOCDForPid(pid);
        if (ocdFull != null) {
            for (AD attrDef : ocdFull.getAD()) {
                // to the required attributes make sure a value is defined.
                if (attrDef.isRequired()) {
                    if (updatedProps.get(attrDef.getId()) == null) {
                        // exception.
                        throw new KuraException(KuraErrorCode.CONFIGURATION_REQUIRED_ATTRIBUTE_MISSING, attrDef.getId());
                    }
                }
            }
        }
    }
}
Also used : OCD(org.eclipse.kura.configuration.metatype.OCD) AD(org.eclipse.kura.configuration.metatype.AD) HashMap(java.util.HashMap) KuraException(org.eclipse.kura.KuraException) AttributeDefinition(org.osgi.service.metatype.AttributeDefinition)

Example 4 with AD

use of org.eclipse.kura.configuration.metatype.AD in project kura by eclipse.

the class GwtComponentServiceImpl method findComponentConfiguration.

@Override
public List<GwtConfigComponent> findComponentConfiguration(GwtXSRFToken xsrfToken) throws GwtKuraException {
    checkXSRFToken(xsrfToken);
    ConfigurationService cs = ServiceLocator.getInstance().getService(ConfigurationService.class);
    List<GwtConfigComponent> gwtConfigs = new ArrayList<GwtConfigComponent>();
    try {
        List<ComponentConfiguration> configs = cs.getComponentConfigurations();
        // sort the list alphabetically by service name
        Collections.sort(configs, new Comparator<ComponentConfiguration>() {

            @Override
            public int compare(ComponentConfiguration arg0, ComponentConfiguration arg1) {
                String name0;
                int start = arg0.getPid().lastIndexOf('.');
                int substringIndex = start + 1;
                if (start != -1 && substringIndex < arg0.getPid().length()) {
                    name0 = arg0.getPid().substring(substringIndex);
                } else {
                    name0 = arg0.getPid();
                }
                String name1;
                start = arg1.getPid().lastIndexOf('.');
                substringIndex = start + 1;
                if (start != -1 && substringIndex < arg1.getPid().length()) {
                    name1 = arg1.getPid().substring(substringIndex);
                } else {
                    name1 = arg1.getPid();
                }
                return name0.compareTo(name1);
            }
        });
        for (ComponentConfiguration config : configs) {
            // ignore items we want to hide
            if (!config.getPid().endsWith("CommandCloudApp")) {
                continue;
            }
            OCD ocd = config.getDefinition();
            if (ocd != null) {
                GwtConfigComponent gwtConfig = new GwtConfigComponent();
                gwtConfig.setComponentId(ocd.getId());
                gwtConfig.setComponentName(ocd.getName());
                gwtConfig.setComponentDescription(ocd.getDescription());
                if (ocd.getIcon() != null && !ocd.getIcon().isEmpty()) {
                    Icon icon = ocd.getIcon().get(0);
                    gwtConfig.setComponentIcon(icon.getResource());
                }
                List<GwtConfigParameter> gwtParams = new ArrayList<GwtConfigParameter>();
                gwtConfig.setParameters(gwtParams);
                for (AD ad : ocd.getAD()) {
                    GwtConfigParameter gwtParam = new GwtConfigParameter();
                    gwtParam.setId(ad.getId());
                    gwtParam.setName(ad.getName());
                    gwtParam.setDescription(ad.getDescription());
                    gwtParam.setType(GwtConfigParameterType.valueOf(ad.getType().name()));
                    gwtParam.setRequired(ad.isRequired());
                    gwtParam.setCardinality(ad.getCardinality());
                    if (ad.getOption() != null && !ad.getOption().isEmpty()) {
                        Map<String, String> options = new HashMap<String, String>();
                        for (Option option : ad.getOption()) {
                            options.put(option.getLabel(), option.getValue());
                        }
                        gwtParam.setOptions(options);
                    }
                    gwtParam.setMin(ad.getMin());
                    gwtParam.setMax(ad.getMax());
                    if (config.getConfigurationProperties() != null) {
                        // handle the value based on the cardinality of the
                        // attribute
                        int cardinality = ad.getCardinality();
                        Object value = config.getConfigurationProperties().get(ad.getId());
                        if (value != null) {
                            if (cardinality == 0 || cardinality == 1 || cardinality == -1) {
                                gwtParam.setValue(value.toString());
                            } else {
                                // this could be an array value
                                if (value instanceof Object[]) {
                                    Object[] objValues = (Object[]) value;
                                    List<String> strValues = new ArrayList<String>();
                                    for (Object v : objValues) {
                                        if (v != null) {
                                            strValues.add(v.toString());
                                        }
                                    }
                                    gwtParam.setValues(strValues.toArray(new String[] {}));
                                }
                            }
                        }
                        gwtParams.add(gwtParam);
                    }
                }
                gwtConfigs.add(gwtConfig);
            }
        }
    } catch (Throwable t) {
        KuraExceptionHandler.handle(t);
    }
    return gwtConfigs;
}
Also used : AD(org.eclipse.kura.configuration.metatype.AD) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ComponentConfiguration(org.eclipse.kura.configuration.ComponentConfiguration) OCD(org.eclipse.kura.configuration.metatype.OCD) GwtConfigParameter(org.eclipse.kura.web.shared.model.GwtConfigParameter) Option(org.eclipse.kura.configuration.metatype.Option) ConfigurationService(org.eclipse.kura.configuration.ConfigurationService) Icon(org.eclipse.kura.configuration.metatype.Icon) GwtConfigComponent(org.eclipse.kura.web.shared.model.GwtConfigComponent)

Example 5 with AD

use of org.eclipse.kura.configuration.metatype.AD in project kura by eclipse.

the class GwtComponentServiceImpl method removeNoMetatypeProperties.

private GwtConfigComponent removeNoMetatypeProperties(GwtConfigComponent gwtConfig, ComponentConfiguration config) {
    GwtConfigComponent cleanedConfigComponent = null;
    OCD ocd = config.getDefinition();
    if (ocd != null && gwtConfig != null) {
        cleanedConfigComponent = new GwtConfigComponent();
        cleanedConfigComponent.setComponentDescription(gwtConfig.getComponentDescription());
        cleanedConfigComponent.setComponentId(gwtConfig.getComponentId());
        cleanedConfigComponent.setComponentIcon(gwtConfig.getComponentIcon());
        cleanedConfigComponent.setComponentName(gwtConfig.getComponentName());
        cleanedConfigComponent.setProperties(gwtConfig.getProperties());
        List<GwtConfigParameter> cleanedConfigParameters = new ArrayList<GwtConfigParameter>();
        for (AD ad : ocd.getAD()) {
            GwtConfigParameter parameter = gwtConfig.getParameter(ad.getId());
            if (parameter != null) {
                cleanedConfigParameters.add(parameter);
            }
        }
        cleanedConfigComponent.setParameters(cleanedConfigParameters);
    }
    return cleanedConfigComponent;
}
Also used : OCD(org.eclipse.kura.configuration.metatype.OCD) AD(org.eclipse.kura.configuration.metatype.AD) GwtConfigParameter(org.eclipse.kura.web.shared.model.GwtConfigParameter) ArrayList(java.util.ArrayList) GwtConfigComponent(org.eclipse.kura.web.shared.model.GwtConfigComponent)

Aggregations

AD (org.eclipse.kura.configuration.metatype.AD)9 OCD (org.eclipse.kura.configuration.metatype.OCD)6 HashMap (java.util.HashMap)5 ArrayList (java.util.ArrayList)4 GwtConfigParameter (org.eclipse.kura.web.shared.model.GwtConfigParameter)4 ComponentConfiguration (org.eclipse.kura.configuration.ComponentConfiguration)3 Icon (org.eclipse.kura.configuration.metatype.Icon)3 Option (org.eclipse.kura.configuration.metatype.Option)3 GwtConfigComponent (org.eclipse.kura.web.shared.model.GwtConfigComponent)3 KuraException (org.eclipse.kura.KuraException)2 ConfigurationService (org.eclipse.kura.configuration.ConfigurationService)2 Password (org.eclipse.kura.configuration.Password)1 SelfConfiguringComponent (org.eclipse.kura.configuration.SelfConfiguringComponent)1 Tocd (org.eclipse.kura.core.configuration.metatype.Tocd)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 ServiceReference (org.osgi.framework.ServiceReference)1 AttributeDefinition (org.osgi.service.metatype.AttributeDefinition)1 Attr (org.w3c.dom.Attr)1 Element (org.w3c.dom.Element)1