use of org.eclipse.kura.configuration.metatype.OCD in project kura by eclipse.
the class ComponentUtil method getObjectClassDefinition.
/**
* Returns a Map with all the MetaType Object Class Definitions contained in the bundle.
*
* @param ctx
* @param bnd
* @return
*/
public static Map<String, OCD> getObjectClassDefinition(BundleContext ctx, Bundle bnd) {
Map<String, OCD> bundleDefaults = new HashMap<String, OCD>();
ServiceReference<MetaTypeService> ref = ctx.getServiceReference(MetaTypeService.class);
MetaTypeService metaTypeService = ctx.getService(ref);
MetaTypeInformation mti = metaTypeService.getMetaTypeInformation(bnd);
if (mti != null) {
String[] pids = mti.getPids();
if (pids != null) {
for (String pid : pids) {
OCD ocd = null;
try {
ocd = readObjectClassDefinition(bnd, pid);
if (ocd != null) {
bundleDefaults.put(pid, ocd);
}
} catch (Exception e) {
// ignore: OCD for the specified pid is not found
s_logger.warn("Error loading OCD for pid " + pid, e);
}
}
}
}
return bundleDefaults;
}
use of org.eclipse.kura.configuration.metatype.OCD in project kura by eclipse.
the class ConfigurationServiceImpl method updateConfigurationInternal.
private void updateConfigurationInternal(String pid, Map<String, Object> properties, boolean snapshotOnConfirmation) throws KuraException {
s_logger.debug("Attempting update configuration for {}", pid);
if (!this.m_allActivatedPids.contains(pid)) {
s_logger.info("UpdatingConfiguration ignored as ConfigurableComponent {} is NOT tracked.", pid);
return;
}
if (properties == null) {
s_logger.info("UpdatingConfiguration ignored as properties for ConfigurableComponent {} are NULL.", pid);
return;
}
// get the OCD from the registered ConfigurableComponents
OCD registerdOCD = getRegisteredOCD(pid);
if (registerdOCD == null) {
s_logger.info("UpdatingConfiguration ignored as OCD for pid {} cannot be found.", pid);
return;
}
Map<String, Object> mergedProperties = new HashMap<String, Object>();
mergeWithDefaults(registerdOCD, mergedProperties);
if (!this.m_activatedSelfConfigComponents.contains(pid)) {
try {
// get the current running configuration for the selected component
Configuration config = this.m_configurationAdmin.getConfiguration(this.m_servicePidByPid.get(pid), "?");
Map<String, Object> runningProps = CollectionsUtil.dictionaryToMap(config.getProperties(), registerdOCD);
mergedProperties.putAll(runningProps);
} catch (IOException e) {
s_logger.info("merge with running failed!");
throw new KuraException(KuraErrorCode.CONFIGURATION_UPDATE, e, pid);
}
}
mergedProperties.putAll(properties);
try {
updateComponentConfiguration(pid, mergedProperties, snapshotOnConfirmation);
s_logger.info("Updating Configuration of ConfigurableComponent {} ... Done.", pid);
} catch (IOException e) {
s_logger.warn("Error updating Configuration of ConfigurableComponent with pid {}", pid, e);
throw new KuraException(KuraErrorCode.CONFIGURATION_UPDATE, e, pid);
}
}
use of org.eclipse.kura.configuration.metatype.OCD 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());
}
}
}
}
}
}
use of org.eclipse.kura.configuration.metatype.OCD in project kura by eclipse.
the class ConfigurationServiceImpl method createFactoryConfiguration.
@Override
public synchronized void createFactoryConfiguration(String factoryPid, String pid, Map<String, Object> properties, boolean takeSnapshot) throws KuraException {
if (pid == null) {
throw new KuraException(KuraErrorCode.INVALID_PARAMETER, "pid cannot be null");
} else if (this.m_servicePidByPid.containsKey(pid)) {
throw new KuraException(KuraErrorCode.INVALID_PARAMETER, "pid " + pid + " already exists");
}
try {
// Second argument in createFactoryConfiguration is a bundle location. If left null the new bundle location
// will be bound to the location of the first bundle that registers a Managed Service Factory with a
// corresponding PID
s_logger.info("Creating new configuration for factory pid {} and pid {}", factoryPid, pid);
String servicePid = this.m_configurationAdmin.createFactoryConfiguration(factoryPid, null).getPid();
s_logger.info("Updating newly created configuration for pid {}", pid);
Map<String, Object> mergedProperties = new HashMap<String, Object>();
if (properties != null) {
mergedProperties.putAll(properties);
}
OCD ocd = this.m_ocds.get(factoryPid);
mergeWithDefaults(ocd, mergedProperties);
mergedProperties.put(ConfigurationService.KURA_SERVICE_PID, pid);
Dictionary<String, Object> dict = CollectionsUtil.mapToDictionary(mergedProperties);
Configuration config = this.m_configurationAdmin.getConfiguration(servicePid, "?");
config.update(dict);
registerComponentConfiguration(pid, servicePid, factoryPid);
if (takeSnapshot) {
snapshot();
}
} catch (IOException e) {
throw new KuraException(KuraErrorCode.CONFIGURATION_ERROR, e, "Cannot create component instance for factory " + factoryPid);
}
}
use of org.eclipse.kura.configuration.metatype.OCD in project kura by eclipse.
the class ComponentMetaTypeBundleTracker method processBundleMetaType.
// ----------------------------------------------------------------
//
// Private APIs
//
// ----------------------------------------------------------------
private void processBundleMetaType(Bundle bundle) {
// Push the latest configuration merging the properties in ConfigAdmin
// with the default properties read from the component's meta-type.
// This allows components to incrementally add new configuration
// properties in the meta-type.
// Only the new default properties are merged with the configuration
// properties in ConfigurationAdmin.
// Note: configuration properties in snapshots no longer present in
// the meta-type are not purged.
Map<String, Tmetadata> metas = ComponentUtil.getMetadata(this.m_context, bundle);
for (String metatypePid : metas.keySet()) {
try {
// register the OCD for all the contained services
Tmetadata metadata = metas.get(metatypePid);
if (metadata != null) {
// check if this component is a factory
boolean isFactory = false;
Designate designate = ComponentUtil.getDesignate(metadata, metatypePid);
if (designate.getFactoryPid() != null && !designate.getFactoryPid().isEmpty()) {
isFactory = true;
}
// register the pid with the OCD and whether it is a factory
OCD ocd = ComponentUtil.getOCD(metadata, metatypePid);
this.m_configurationService.registerComponentOCD(metatypePid, (Tocd) ocd, isFactory);
}
} catch (Exception e) {
s_logger.error("Error seeding configuration for pid: " + metatypePid, e);
}
}
}
Aggregations