Skip to content

Commit

Permalink
Merge pull request #470 from apache/feature/type_support_for_properties
Browse files Browse the repository at this point in the history
type support for properties
  • Loading branch information
pnoltes committed Nov 21, 2023
2 parents 25463cf + 95460e9 commit e9642b9
Show file tree
Hide file tree
Showing 86 changed files with 5,197 additions and 2,072 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ cmake-build*
nbproject
target
html
CMakeUserPresets.json

9 changes: 9 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ limitations under the License.
- The `OSGI_FRAMEWORK_UUID` ("org.osgi.framework.uuid") config property, replacement is `CELIX_FRAMEWORK_UUID`.
- Removed support for bundle activator symbols without a `celix_` prefix.
- Removed service property constant `CELIX_FRAMEWORK_SERVICE_PID`.
- Support and usage of "service.lang" service property is removed.
- pubsub_serializer.h is removed and no longer supported. Use pubsub_message_serialization_service.h instead.
- C Properties are no longer a direct typedef of `hashmap`.
- celix_string/long_hashmap put functions now return a celix_status_t instead of bool (value replaced).
THe celix_status_t is used to indicate an ENOMEM error.

## New Features

- Basic type support for value in celix Properties.

# Noteworthy Changes for 2.4.0 (2023-09-27)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ int pubsub_subscriber_recv(void* handle, const char* msgType, unsigned int msgTy
if (metadata == NULL || celix_properties_size(metadata) == 0) {
printf("No metadata\n");
} else {
const char *key;
CELIX_PROPERTIES_FOR_EACH(metadata, key) {
const char *val = celix_properties_get(metadata, key, "!Error!");
printf("%s=%s\n", key, val);
CELIX_PROPERTIES_ITERATE(metadata, iter) {
printf("%s=%s\n", iter.key, iter.entry.value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,8 @@ class PubSubInterceptorTestSuite : public ::testing::Test {
const auto *msg = static_cast<const msg_t*>(rawMsg);
EXPECT_GE(msg->seqNr, 0);
EXPECT_STREQ(celix_properties_get(metadata, "test", nullptr), "preSend");
const char *key;
CELIX_PROPERTIES_FOR_EACH(metadata, key) {
printf("got property %s=%s\n", key, celix_properties_get(metadata, key, nullptr));
CELIX_PROPERTIES_ITERATE(metadata, iter) {
printf("got property %s=%s\n", iter.key, iter.entry.value);
}
fprintf(stdout, "Got message in postSend interceptor %s/%s for type %s and ser %s with seq nr %i\n", intProps->scope, intProps->topic, intProps->psaType, intProps->serializationType, msg->seqNr);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,8 @@ static celix_status_t pubsub_websocketAdmin_connectEndpointToReceiver(pubsub_web

if (publisher && (sockAddress == NULL || sockPort < 0)) {
L_WARN("[PSA WEBSOCKET] Error got endpoint without websocket address/port or endpoint type. Properties:");
const char *key = NULL;
CELIX_PROPERTIES_FOR_EACH(endpoint, key) {
L_WARN("[PSA WEBSOCKET] |- %s=%s\n", key, celix_properties_get(endpoint, key, NULL));
CELIX_PROPERTIES_ITERATE(endpoint, iter) {
L_WARN("[PSA WEBSOCKET] |- %s=%s\n", iter.key, iter.entry.value);
}
status = CELIX_BUNDLE_EXCEPTION;
} else {
Expand Down Expand Up @@ -436,9 +435,8 @@ static celix_status_t pubsub_websocketAdmin_disconnectEndpointFromReceiver(pubsu

if (publisher && (sockAddress == NULL || sockPort < 0)) {
L_WARN("[PSA WEBSOCKET] Error got endpoint without websocket address/port or endpoint type. Properties:");
const char *key = NULL;
CELIX_PROPERTIES_FOR_EACH(endpoint, key) {
L_WARN("[PSA WEBSOCKET] |- %s=%s\n", key, celix_properties_get(endpoint, key, NULL));
CELIX_PROPERTIES_ITERATE(endpoint, iter) {
L_WARN("[PSA WEBSOCKET] |- %s=%s\n", iter.key, iter.entry.value);
}
status = CELIX_BUNDLE_EXCEPTION;
} else if (eTopic != NULL && strncmp(eTopic, topic, 1024 * 1024) == 0) {
Expand Down
6 changes: 2 additions & 4 deletions bundles/pubsub/pubsub_discovery/src/pubsub_discovery_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,10 +558,8 @@ static char* pubsub_discovery_createJsonEndpoint(const celix_properties_t *props
//note props is already check for validity (pubsubEndpoint_isValid)

json_t *jsEndpoint = json_object();
const char* propKey = NULL;
PROPERTIES_FOR_EACH((celix_properties_t*)props, propKey) {
const char* val = celix_properties_get(props, propKey, NULL);
json_object_set_new(jsEndpoint, propKey, json_string(val));
CELIX_PROPERTIES_ITERATE(props, iter) {
json_object_set_new(jsEndpoint, iter.key, json_string(iter.entry.value));
}
char* str = json_dumps(jsEndpoint, JSON_COMPACT);
json_decref(jsEndpoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@
static celix_status_t pubsubProtocol_addNetstringEntryToBuffer(char* buffer, size_t bufferSize, size_t* offsetInOut, const char* str) {
size_t offset = *offsetInOut;

size_t strLen = strnlen(str, CELIX_UTILS_MAX_STRLEN);
if (strLen == CELIX_UTILS_MAX_STRLEN) {
return CELIX_ILLEGAL_ARGUMENT;
}
size_t strLen = celix_utils_strlen(str);

char strLenString[32]; //note the str needed to print the strLen of str.
int written = snprintf(strLenString, sizeof(strLenString), "%zu", strLen);
Expand Down Expand Up @@ -76,7 +73,7 @@ static celix_status_t pubsubProtocol_addNetstringEntryToBuffer(char* buffer, siz
}

celix_status_t pubsubProtocol_encodeMetadata(pubsub_protocol_message_t* message, char** bufferInOut, size_t* bufferLengthInOut, size_t* bufferContentLengthOut) {
int metadataSize = message->metadata.metadata == NULL ? 0 : celix_properties_size(message->metadata.metadata);
size_t metadataSize = message->metadata.metadata == NULL ? 0 : celix_properties_size(message->metadata.metadata);
bool reallocBuffer = false;
bool encoded = false;

Expand All @@ -101,19 +98,17 @@ celix_status_t pubsubProtocol_encodeMetadata(pubsub_protocol_message_t* message,
*bufferInOut = newBuffer;
*bufferLengthInOut = newLength;
}
const char* key;
if (metadataSize == 0) {
encoded = true;
continue;
}
celix_status_t status = CELIX_SUCCESS;
CELIX_PROPERTIES_FOR_EACH(message->metadata.metadata, key) {
const char *val = celix_properties_get(message->metadata.metadata, key, "");
CELIX_PROPERTIES_ITERATE(message->metadata.metadata, iter) {
if (status == CELIX_SUCCESS) {
status = pubsubProtocol_addNetstringEntryToBuffer(*bufferInOut, *bufferLengthInOut, &offset, key);
status = pubsubProtocol_addNetstringEntryToBuffer(*bufferInOut, *bufferLengthInOut, &offset, iter.key);
}
if (status == CELIX_SUCCESS) {
status = pubsubProtocol_addNetstringEntryToBuffer(*bufferInOut, *bufferLengthInOut, &offset, val);
status = pubsubProtocol_addNetstringEntryToBuffer(*bufferInOut, *bufferLengthInOut, &offset, iter.entry.value);
}
}
if (status == CELIX_FILE_IO_EXCEPTION) {
Expand Down
5 changes: 2 additions & 3 deletions bundles/pubsub/pubsub_spi/src/pubsub_endpoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ static void pubsubEndpoint_setFields(celix_properties_t *ep, const char* fwUUID,

//copy topic properties
if (topic_props != NULL) {
const char *key = NULL;
CELIX_PROPERTIES_FOR_EACH((celix_properties_t *) topic_props, key) {
celix_properties_set(ep, key, celix_properties_get(topic_props, key, NULL));
CELIX_PROPERTIES_ITERATE((celix_properties_t *) topic_props, iter) {
celix_properties_set(ep, iter.key, iter.entry.value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,15 @@ static celix_status_t endpointDescriptorWriter_writeEndpoint(endpoint_descriptor
} else {
xmlTextWriterStartElement(writer->writer, ENDPOINT_DESCRIPTION);

hash_map_iterator_pt iter = hashMapIterator_create(endpoint->properties);
while (hashMapIterator_hasNext(iter)) {
hash_map_entry_pt entry = hashMapIterator_nextEntry(iter);

void* propertyName = hashMapEntry_getKey(entry);
const xmlChar* propertyValue = (const xmlChar*) hashMapEntry_getValue(entry);

CELIX_PROPERTIES_ITERATE(endpoint->properties, iter) {
const xmlChar* propertyValue = (const xmlChar*) celix_properties_get(endpoint->properties, iter.key, "");
xmlTextWriterStartElement(writer->writer, PROPERTY);
xmlTextWriterWriteAttribute(writer->writer, NAME, propertyName);
xmlTextWriterWriteAttribute(writer->writer, NAME, (const xmlChar*)iter.key);

if (strcmp(CELIX_FRAMEWORK_SERVICE_NAME, (char*) propertyName) == 0) {
if (strcmp(CELIX_FRAMEWORK_SERVICE_NAME, (char*) iter.key) == 0) {
// objectClass *must* be represented as array of string values...
endpointDescriptorWriter_writeArrayValue(writer->writer, propertyValue);
} else if (strcmp(OSGI_RSA_ENDPOINT_SERVICE_ID, (char*) propertyName) == 0) {
} else if (strcmp(OSGI_RSA_ENDPOINT_SERVICE_ID, (char*) iter.key) == 0) {
// endpoint.service.id *must* be represented as long value...
endpointDescriptorWriter_writeTypedValue(writer->writer, VALUE_TYPE_LONG, propertyValue);
} else {
Expand All @@ -162,7 +157,6 @@ static celix_status_t endpointDescriptorWriter_writeEndpoint(endpoint_descriptor

xmlTextWriterEndElement(writer->writer);
}
hashMapIterator_destroy(iter);

xmlTextWriterEndElement(writer->writer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,21 +342,21 @@ static void discoveryZeroconfAnnouncer_revokeEndpoints(discovery_zeroconf_announ
static bool discoveryZeroconfAnnouncer_copyPropertiesToTxtRecord(discovery_zeroconf_announcer_t *announcer, celix_properties_iterator_t *propIter, TXTRecordRef *txtRecord, uint16_t maxTxtLen, bool splitTxtRecord) {
const char *key;
const char *val;
celix_properties_t *props;
while (celix_propertiesIterator_hasNext(propIter)) {
key = celix_propertiesIterator_nextKey(propIter);
props = celix_propertiesIterator_properties(propIter);
val = celix_properties_get(props, key, "");
while (!celix_propertiesIterator_isEnd(propIter)) {
key = propIter->key;
val = propIter->entry.value;
if (key) {
DNSServiceErrorType err = TXTRecordSetValue(txtRecord, key, strlen(val), val);
if (err != kDNSServiceErr_NoError) {
celix_logHelper_error(announcer->logHelper, "Announcer: Failed to set txt value, %d.", err);
return false;
}
if (splitTxtRecord && TXTRecordGetLength(txtRecord) >= maxTxtLen - UINT8_MAX) {
celix_propertiesIterator_next(propIter);
break;
}
}
celix_propertiesIterator_next(propIter);
}
return true;
}
Expand All @@ -374,11 +374,11 @@ static void discoveryZeroconfAnnouncer_announceEndpoints(discovery_zeroconf_anno
}
char txtBuf[DZC_MAX_TXT_RECORD_SIZE] = {0};
TXTRecordRef txtRecord;
celix_properties_iterator_t propIter = celix_propertiesIterator_construct(entry->properties);
celix_properties_iterator_t propIter = celix_properties_begin(entry->properties);

TXTRecordCreate(&txtRecord, sizeof(txtBuf), txtBuf);
char propSizeStr[16]= {0};
sprintf(propSizeStr, "%d", celix_properties_size(entry->properties) + 1);
sprintf(propSizeStr, "%zu", celix_properties_size(entry->properties) + 1);
(void)TXTRecordSetValue(&txtRecord, DZC_SERVICE_PROPERTIES_SIZE_KEY, strlen(propSizeStr), propSizeStr);
if (!discoveryZeroconfAnnouncer_copyPropertiesToTxtRecord(announcer, &propIter, &txtRecord, sizeof(txtBuf), splitTxtRecord)) {
TXTRecordDeallocate(&txtRecord);
Expand Down Expand Up @@ -411,7 +411,7 @@ static void discoveryZeroconfAnnouncer_announceEndpoints(discovery_zeroconf_anno

if (registered) {
entry->registerRef = dsRef;
while (celix_propertiesIterator_hasNext(&propIter)) {
while (!celix_propertiesIterator_isEnd(&propIter)) {
TXTRecordCreate(&txtRecord, sizeof(txtBuf), txtBuf);
if (!discoveryZeroconfAnnouncer_copyPropertiesToTxtRecord(announcer, &propIter, &txtRecord, sizeof(txtBuf), true)) {
TXTRecordDeallocate(&txtRecord);
Expand Down Expand Up @@ -541,4 +541,4 @@ static void *discoveryZeroconfAnnouncer_refreshEndpointThread(void *data) {
celix_arrayList_destroy(revokedEndpoints);
celix_arrayList_destroy(announcedEndpoints);
return NULL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -745,10 +745,7 @@ static celix_status_t remoteServiceAdmin_createEndpointDescription(remote_servic
}
}

hash_map_entry_pt entry = hashMap_getEntry(endpointProperties, (void *) CELIX_FRAMEWORK_SERVICE_ID);

char* key = hashMapEntry_getKey(entry);
char *serviceId = (char *) hashMap_remove(endpointProperties, (void *) CELIX_FRAMEWORK_SERVICE_ID);
const char* serviceId = celix_properties_get(endpointProperties, CELIX_FRAMEWORK_SERVICE_ID, "-1");
const char *uuid = NULL;

char buf[512];
Expand All @@ -775,12 +772,9 @@ static celix_status_t remoteServiceAdmin_createEndpointDescription(remote_servic
}

if (props != NULL) {
hash_map_iterator_pt propIter = hashMapIterator_create(props);
while (hashMapIterator_hasNext(propIter)) {
hash_map_entry_pt entry = hashMapIterator_nextEntry(propIter);
celix_properties_set(endpointProperties, (char*)hashMapEntry_getKey(entry), (char*)hashMapEntry_getValue(entry));
CELIX_PROPERTIES_ITERATE(props, iter) {
celix_properties_set(endpointProperties, iter.key, iter.entry.value);
}
hashMapIterator_destroy(propIter);
}

*endpoint = calloc(1, sizeof(**endpoint));
Expand All @@ -794,8 +788,6 @@ static celix_status_t remoteServiceAdmin_createEndpointDescription(remote_servic
(*endpoint)->properties = endpointProperties;
}

free(key);
free(serviceId);
free(keys);

return status;
Expand Down Expand Up @@ -1002,14 +994,10 @@ static celix_status_t remoteServiceAdmin_send(void *handle, endpoint_description
} else {
struct curl_slist *metadataHeader = NULL;
if (metadata != NULL && celix_properties_size(metadata) > 0) {
const char *key = NULL;
CELIX_PROPERTIES_FOR_EACH(metadata, key) {
const char *val = celix_properties_get(metadata, key, "");
size_t length = strlen(key) + strlen(val) + 18; // "X-RSA-Metadata-key: val\0"

CELIX_PROPERTIES_ITERATE(metadata, iter) {
size_t length = strlen(iter.key) + strlen(iter.entry.value) + 18; // "X-RSA-Metadata-key: val\0"
char header[length];

snprintf(header, length, "X-RSA-Metadata-%s: %s", key, val);
snprintf(header, length, "X-RSA-Metadata-%s: %s", iter.key, iter.entry.value);
metadataHeader = curl_slist_append(metadataHeader, header);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ celix_status_t rsaShmClientManager_sendMsgTo(rsa_shm_client_manager_t *clientMan
|| response == NULL) {
return CELIX_ILLEGAL_ARGUMENT;
}
const char *key = NULL;
size_t metadataStringSize = 0;
FILE *fp = NULL;
rsa_shm_msg_control_t *msgCtrl = NULL;
Expand All @@ -279,9 +278,8 @@ celix_status_t rsaShmClientManager_sendMsgTo(rsa_shm_client_manager_t *clientMan
return CELIX_ERROR_MAKE(CELIX_FACILITY_CERRNO, errno);
}
if (metadata != NULL) {
CELIX_PROPERTIES_FOR_EACH(metadata, key) {
const char * value = celix_properties_get(metadata, key,"");
fprintf(fp,"%s=%s\n", key, value);
CELIX_PROPERTIES_ITERATE(metadata, iter) {
fprintf(fp,"%s=%s\n", iter.key, iter.entry.value);
}
}
fclose(fp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,29 +231,26 @@ static void rsaShm_overlayProperties(celix_properties_t *additionalProperties, c

/*The property keys of a service are case-insensitive,while the property keys of the specified additional properties map are case sensitive.
* A property key in the additional properties map must therefore override any case variant property key in the properties of the specified Service Reference.*/
const char *additionalPropKey = NULL;
const char *servicePropKey = NULL;
CELIX_PROPERTIES_FOR_EACH(additionalProperties, additionalPropKey) {
if (strcmp(additionalPropKey,(char*) CELIX_FRAMEWORK_SERVICE_NAME) != 0
&& strcmp(additionalPropKey,(char*) CELIX_FRAMEWORK_SERVICE_ID) != 0) {
CELIX_PROPERTIES_ITERATE(additionalProperties, additionalPropIter) {
if (strcmp(additionalPropIter.key,(char*) CELIX_FRAMEWORK_SERVICE_NAME) != 0
&& strcmp(additionalPropIter.key,(char*) CELIX_FRAMEWORK_SERVICE_ID) != 0) {
bool propKeyCaseEqual = false;

CELIX_PROPERTIES_FOR_EACH(serviceProperties, servicePropKey) {
if (strcasecmp(additionalPropKey,servicePropKey) == 0) {
const char* val = celix_properties_get(additionalProperties,additionalPropKey,NULL);
celix_properties_set(serviceProperties,servicePropKey,val);
CELIX_PROPERTIES_ITERATE(serviceProperties, servicePropIter) {
if (strcasecmp(additionalPropIter.key,servicePropIter.key) == 0) {
const char* val = additionalPropIter.entry.value;
celix_properties_set(serviceProperties,servicePropIter.key,val);
propKeyCaseEqual = true;
break;
}
}

if (!propKeyCaseEqual) {
const char* val = celix_properties_get(additionalProperties,additionalPropKey,NULL);
celix_properties_set(serviceProperties,additionalPropKey,val);
const char* val = additionalPropIter.entry.value;
celix_properties_set(serviceProperties,additionalPropIter.key,val);
}
}
}
return;
}

static bool rsaShm_isConfigTypeMatched(celix_properties_t *properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,7 @@ celix_status_t topologyManager_importScopeChanged(void *handle, char *service_na
hash_map_entry_pt entry = hashMapIterator_nextEntry(importedServicesIterator);
endpoint = hashMapEntry_getKey(entry);

entry = hashMap_getEntry(endpoint->properties, (void *) CELIX_FRAMEWORK_SERVICE_NAME);
char* name = (char *) hashMapEntry_getValue(entry);
const char* name = celix_properties_get(endpoint->properties, CELIX_FRAMEWORK_SERVICE_NAME, "");
// Test if a service with the same name is imported
if (strcmp(name, service_name) == 0) {
found = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,7 @@ extern "C" {
for (unsigned int i = 0; i < arrayList_size(epList); i++) {
endpoint_description_t *ep = (endpoint_description_t *) arrayList_get(epList, i);
celix_properties_t *props = ep->properties;
hash_map_entry_pt entry = hashMap_getEntry(props, (void*)"key2");
char* value = (char*) hashMapEntry_getValue(entry);
const char* value = celix_properties_get(props, "key2", "");
EXPECT_STREQ("inaetics", value);
/*
printf("Service: %s ", ep->service);
Expand Down Expand Up @@ -433,8 +432,7 @@ extern "C" {
for (unsigned int i = 0; i < arrayList_size(epList); i++) {
endpoint_description_t *ep = (endpoint_description_t *) arrayList_get(epList, i);
celix_properties_t *props = ep->properties;
hash_map_entry_pt entry = hashMap_getEntry(props, (void*)"key2");
char* value = (char*) hashMapEntry_getValue(entry);
const char* value = celix_properties_get(props, "key2", "");
EXPECT_STREQ("inaetics", value);
}
printf("End: %s\n", __func__);
Expand All @@ -458,8 +456,7 @@ extern "C" {
for (unsigned int i = 0; i < arrayList_size(epList); i++) {
endpoint_description_t *ep = (endpoint_description_t *) arrayList_get(epList, i);
celix_properties_t *props = ep->properties;
hash_map_entry_pt entry = hashMap_getEntry(props, (void *)"key2");
char* value = (char*) hashMapEntry_getValue(entry);
const char* value = celix_properties_get(props, "key2", "");
EXPECT_STREQ("inaetics", value);
}
printf("End: %s\n", __func__);
Expand Down
Loading

0 comments on commit e9642b9

Please sign in to comment.