celix_status_t deviceManager_isDriverBundle(device_manager_pt manager, bundle_pt bundle, bool *isDriver) { celix_status_t status = CELIX_SUCCESS; (*isDriver) = false; array_list_pt refs = NULL; status = bundle_getRegisteredServices(bundle, &refs); if (status == CELIX_SUCCESS) { if (refs != NULL) { int i; for (i = 0; i < arrayList_size(refs); i++) { celix_status_t substatus = CELIX_SUCCESS; service_reference_pt ref = arrayList_get(refs, i); service_registration_pt registration = NULL; substatus = serviceReference_getServiceRegistration(ref, ®istration); if (substatus == CELIX_SUCCESS) { properties_pt properties = NULL; substatus = serviceRegistration_getProperties(registration, &properties); if (substatus == CELIX_SUCCESS) { char *object = properties_get(properties, (char *) OSGI_FRAMEWORK_OBJECTCLASS); if (strcmp(object, "driver") == 0) { *isDriver = true; break; } } } } arrayList_destroy(refs); } } return status; }
celix_status_t serviceRegistry_getServiceReferences(service_registry_pt registry, bundle_pt owner, const char *serviceName, filter_pt filter, array_list_pt *references) { celix_status_t status = CELIX_SUCCESS; hash_map_values_pt registrations; hash_map_iterator_pt iterator; arrayList_create(references); celixThreadMutex_lock(®istry->mutex); registrations = hashMapValues_create(registry->serviceRegistrations); iterator = hashMapValues_iterator(registrations); while (hashMapIterator_hasNext(iterator)) { array_list_pt regs = (array_list_pt) hashMapIterator_nextValue(iterator); unsigned int regIdx; for (regIdx = 0; (regs != NULL) && regIdx < arrayList_size(regs); regIdx++) { service_registration_pt registration = (service_registration_pt) arrayList_get(regs, regIdx); properties_pt props = NULL; status = serviceRegistration_getProperties(registration, &props); if (status == CELIX_SUCCESS) { bool matched = false; bool matchResult = false; if (filter != NULL) { filter_match(filter, props, &matchResult); } if ((serviceName == NULL) && ((filter == NULL) || matchResult)) { matched = true; } else if (serviceName != NULL) { char *className = NULL; bool matchResult = false; serviceRegistration_getServiceName(registration, &className); if (filter != NULL) { filter_match(filter, props, &matchResult); } if ((strcmp(className, serviceName) == 0) && ((filter == NULL) || matchResult)) { matched = true; } } if (matched) { if (serviceRegistration_isValid(registration)) { service_reference_pt reference = NULL; serviceRegistry_createServiceReference(registry, owner, registration, &reference); arrayList_add(*references, reference); } } } } } hashMapIterator_destroy(iterator); hashMapValues_destroy(registrations); celixThreadMutex_unlock(®istry->mutex); framework_logIfError(logger, status, NULL, "Cannot get service references"); return status; }
celix_status_t serviceRegistry_removeHook(service_registry_pt registry, service_registration_pt registration) { celix_status_t status = CELIX_SUCCESS; char *serviceName = NULL; properties_pt props = NULL; serviceRegistration_getProperties(registration, &props); serviceName = properties_get(props, (char *) OSGI_FRAMEWORK_OBJECTCLASS); if (strcmp(OSGI_FRAMEWORK_LISTENER_HOOK_SERVICE_NAME, serviceName) == 0) { celixThreadMutex_lock(®istry->mutex); arrayList_removeElement(registry->listenerHooks, registration); celixThreadMutex_unlock(®istry->mutex); } return status; }
celix_status_t deviceManager_noDriverFound(device_manager_pt manager, void *service, service_reference_pt reference) { celix_status_t status = CELIX_SUCCESS; service_registration_pt registration = NULL; status = serviceReference_getServiceRegistration(reference, ®istration); if (status == CELIX_SUCCESS) { properties_pt properties = NULL; status = serviceRegistration_getProperties(registration, &properties); if (status == CELIX_SUCCESS) { char *objectClass = properties_get(properties, (char *) OSGI_FRAMEWORK_OBJECTCLASS); if (strcmp(objectClass, OSGI_DEVICEACCESS_DRIVER_SERVICE_NAME) == 0) { device_service_pt device = service; status = device->noDriverFound(device->device); } } } return status; }
static celix_status_t benchmarkRunner_addedService(void * handle, service_reference_pt reference, void * service) { celix_status_t status = CELIX_SUCCESS; struct activator * activator = handle; service_registration_pt registration = NULL; properties_pt properties = NULL; char *serviceName = NULL; serviceReference_getServiceRegistration(reference, ®istration); serviceRegistration_getProperties(registration, &properties); serviceName = properties_get(properties, "objectClass"); if (strcmp(serviceName, BENCHMARK_SERVICE_NAME) == 0) { pthread_mutex_lock(&activator->mutex); activator->benchmark = service; pthread_mutex_unlock(&activator->mutex); } else if (strcmp(serviceName, FREQUENCY_SERVICE_NAME) == 0 ) { pthread_mutex_lock(&activator->mutex); activator->freqService = service; pthread_mutex_unlock(&activator->mutex); } return status; }
celix_status_t consumingDriver_match(void *driverHandler, service_reference_pt reference, int *value) { printf("CONSUMING_DRIVER: match called\n"); int match=0; celix_status_t status = CELIX_SUCCESS; consuming_driver_pt driver = driverHandler; service_registration_pt registration = NULL; properties_pt properties = NULL; status = serviceReference_getServiceRegistration(reference, ®istration); if (status == CELIX_SUCCESS) { status = serviceRegistration_getProperties(registration, &properties); if (status == CELIX_SUCCESS) { char *category = properties_get(properties, OSGI_DEVICEACCESS_DEVICE_CATEGORY); if (strcmp(category, REFINING_DRIVER_DEVICE_CATEGORY) == 0) { match = 10; } } } (*value) = match; return status; }
celix_status_t driverMatcher_getBestMatchInternal(driver_matcher_pt matcher, apr_pool_t *pool, match_pt *match) { celix_status_t status = CELIX_SUCCESS; if (!hashMap_isEmpty(matcher->attributes)) { match_key_t matchKey = NULL; hash_map_iterator_pt iter = hashMapIterator_create(matcher->attributes); while (hashMapIterator_hasNext(iter)) { hash_map_entry_pt entry = hashMapIterator_nextEntry(iter); match_key_t key = hashMapEntry_getKey(entry); if (matchKey == NULL || matchKey->matchValue < key->matchValue) { matchKey = key; } } hashMapIterator_destroy(iter); array_list_pt das = hashMap_get(matcher->attributes, matchKey); service_reference_pt best = NULL; int i; for (i = 0; i < arrayList_size(das); i++) { driver_attributes_pt attributes = arrayList_get(das, i); service_reference_pt reference = NULL; celix_status_t substatus = driverAttributes_getReference(attributes, &reference); if (substatus == CELIX_SUCCESS) { if (best != NULL) { printf("DRIVER_MATCHER: Compare ranking\n"); char *rank1Str, *rank2Str; int rank1, rank2; service_registration_pt registration = NULL; substatus = serviceReference_getServiceRegistration(reference, ®istration); if (substatus == CELIX_SUCCESS) { properties_pt properties = NULL; status = serviceRegistration_getProperties(registration, &properties); if (status == CELIX_SUCCESS) { rank1Str = properties_getWithDefault(properties, (char *) OSGI_FRAMEWORK_SERVICE_RANKING, "0"); rank2Str = properties_getWithDefault(properties, (char *) OSGI_FRAMEWORK_SERVICE_RANKING, "0"); rank1 = atoi(rank1Str); rank2 = atoi(rank2Str); if (rank1 != rank2) { if (rank1 > rank2) { best = reference; } } else { printf("DRIVER_MATCHER: Compare id's\n"); char *id1Str, *id2Str; long id1, id2; id1Str = properties_get(properties, (char *) OSGI_FRAMEWORK_SERVICE_ID); id2Str = properties_get(properties, (char *) OSGI_FRAMEWORK_SERVICE_ID); id1 = atol(id1Str); id2 = atol(id2Str); if (id1 < id2) { best = reference; } } } } } else { best = reference; } } } *match = apr_palloc(pool, sizeof(**match)); if (!*match) { status = CELIX_ENOMEM; } else { (*match)->matchValue = matchKey->matchValue; (*match)->reference = best; } } return status; }
celix_status_t deviceManager_matchAttachDriver(device_manager_pt manager, apr_pool_t *attachPool, driver_loader_pt loader, array_list_pt driverIds, array_list_pt included, array_list_pt excluded, void *service, service_reference_pt reference) { celix_status_t status = CELIX_SUCCESS; array_list_pt references = NULL; int i; for (i = 0; i < arrayList_size(excluded); i++) { void *exclude = arrayList_get(excluded, i); arrayList_removeElement(included, exclude); } for (i = 0; i < arrayList_size(driverIds); i++) { char *id = arrayList_get(driverIds, i); printf("DEVICE_MANAGER: Driver found: %s\n", id); } status = driverLoader_loadDrivers(loader, attachPool, manager->locators, driverIds, &references); if (status == CELIX_SUCCESS) { for (i = 0; i < arrayList_size(references); i++) { service_reference_pt reference = arrayList_get(references, i); driver_attributes_pt attributes = hashMap_get(manager->drivers, reference); if (attributes != NULL) { arrayList_add(included, attributes); } } driver_matcher_pt matcher = NULL; status = driverMatcher_create(attachPool, manager->context, &matcher); if (status == CELIX_SUCCESS) { for (i = 0; i < arrayList_size(included); i++) { driver_attributes_pt attributes = arrayList_get(included, i); int match = 0; celix_status_t substatus = driverAttributes_match(attributes, reference, &match); if (substatus == CELIX_SUCCESS) { printf("DEVICE_MANAGER: Found match: %d\n", match); if (match <= OSGI_DEVICEACCESS_DEVICE_MATCH_NONE) { continue; } driverMatcher_add(matcher, match, attributes); } else { // Ignore } } match_pt match = NULL; status = driverMatcher_getBestMatch(matcher, attachPool, reference, &match); if (status == CELIX_SUCCESS) { if (match == NULL) { status = deviceManager_noDriverFound(manager, service, reference); } else { service_registration_pt registration = NULL; status = serviceReference_getServiceRegistration(match->reference, ®istration); if (status == CELIX_SUCCESS) { properties_pt properties = NULL; status = serviceRegistration_getProperties(registration, &properties); if (status == CELIX_SUCCESS) { char *driverId = properties_get(properties, (char *) OSGI_DEVICEACCESS_DRIVER_ID); driver_attributes_pt finalAttributes = hashMap_get(manager->drivers, match->reference); if (finalAttributes == NULL) { status = deviceManager_noDriverFound(manager, service, reference); } else { char *newDriverId = NULL; status = driverAttributes_attach(finalAttributes, reference, &newDriverId); if (status == CELIX_SUCCESS) { if (newDriverId != NULL) { array_list_pt ids = NULL; arrayList_create(&ids); arrayList_add(ids, newDriverId); arrayList_add(excluded, finalAttributes); status = deviceManager_matchAttachDriver(manager, attachPool, loader, ids, included, excluded, service, reference); } else { // Attached, unload unused drivers status = driverLoader_unloadDrivers(loader, finalAttributes); } } } } } } } } } if (references != NULL) { arrayList_destroy(references); } return status; }
static celix_status_t deviceManager_attachAlgorithm(device_manager_pt manager, service_reference_pt ref, void *service) { celix_status_t status = CELIX_SUCCESS; apr_pool_t *attachPool = NULL; apr_status_t aprStatus = apr_pool_create(&attachPool, manager->pool); if (aprStatus != APR_SUCCESS) { status = CELIX_ILLEGAL_STATE; } else { driver_loader_pt loader = NULL; status = driverLoader_create(attachPool, manager->context, &loader); if (status == CELIX_SUCCESS) { array_list_pt included = NULL; array_list_pt excluded = NULL; array_list_pt driverIds = NULL; hashMap_put(manager->devices, ref, service); status = arrayList_create(&included); if (status == CELIX_SUCCESS) { status = arrayList_create(&excluded); if (status == CELIX_SUCCESS) { service_registration_pt registration = NULL; status = serviceReference_getServiceRegistration(ref, ®istration); if (status == CELIX_SUCCESS) { properties_pt properties = NULL; status = serviceRegistration_getProperties(registration, &properties); if (status == CELIX_SUCCESS) { status = driverLoader_findDrivers(loader, attachPool, manager->locators, properties, &driverIds); if (status == CELIX_SUCCESS) { hash_map_iterator_pt iter = hashMapIterator_create(manager->drivers); while (hashMapIterator_hasNext(iter)) { driver_attributes_pt driverAttributes = hashMapIterator_nextValue(iter); arrayList_add(included, driverAttributes); // Each driver that already is installed can be removed from the list char *id = NULL; celix_status_t substatus = driverAttributes_getDriverId(driverAttributes, &id); if (substatus == CELIX_SUCCESS) { // arrayList_removeElement(driverIds, id); array_list_iterator_pt idsIter = arrayListIterator_create(driverIds); while (arrayListIterator_hasNext(idsIter)) { char *value = arrayListIterator_next(idsIter); if (strcmp(value, id) == 0) { arrayListIterator_remove(idsIter); } } arrayListIterator_destroy(idsIter); } else { // Ignore } } hashMapIterator_destroy(iter); status = deviceManager_matchAttachDriver(manager, attachPool, loader, driverIds, included, excluded, service, ref); arrayList_destroy(driverIds); } } } arrayList_destroy(excluded); } arrayList_destroy(included); } } apr_pool_destroy(attachPool); } return status; }
celix_status_t inspectCommand_printImportedServices(command_pt command, array_list_pt ids, void (*out)(char *), void (*err)(char *)) { celix_status_t status = CELIX_SUCCESS; array_list_pt bundles = NULL; if (arrayList_isEmpty(ids)) { celix_status_t status = bundleContext_getBundles(command->bundleContext, &bundles); } else { unsigned int i; arrayList_create(&bundles); for (i = 0; i < arrayList_size(ids); i++) { char *idStr = (char *) arrayList_get(ids, i); long id = atol(idStr); bundle_pt b = NULL; celix_status_t st = bundleContext_getBundleById(command->bundleContext, id, &b); if (st == CELIX_SUCCESS) { arrayList_add(bundles, b); } else { char line[256]; sprintf(line, "INSPECT: Invalid bundle ID: %ld\n", id); out(line); } } } if (status == CELIX_SUCCESS) { unsigned int i = 0; for (i = 0; i < arrayList_size(bundles); i++) { bundle_pt bundle = (bundle_pt) arrayList_get(bundles, i); if (i > 0) { out("\n"); } if (bundle != NULL) { array_list_pt refs = NULL; if (bundle_getServicesInUse(bundle, &refs) == CELIX_SUCCESS) { char line[256]; module_pt module = NULL; char * name = NULL; status = bundle_getCurrentModule(bundle, &module); if (status == CELIX_SUCCESS) { status = module_getSymbolicName(module, &name); if (status == CELIX_SUCCESS) { sprintf(line, "%s requires services:\n", name); out(line); out("==============\n"); if (refs == NULL || arrayList_size(refs) == 0) { out("Nothing\n"); } else { unsigned int j = 0; for (j = 0; j < arrayList_size(refs); j++) { service_reference_pt ref = (service_reference_pt) arrayList_get(refs, j); service_registration_pt reg = NULL; properties_pt props = NULL; char line[256]; bundle_pt usedBundle = NULL; module_pt usedModule = NULL; char *usedSymbolicName = NULL; long usedBundleId; serviceReference_getBundle(ref, &usedBundle); bundle_getBundleId(usedBundle, &usedBundleId); bundle_getCurrentModule(usedBundle, &usedModule); module_getSymbolicName(usedModule, &usedSymbolicName); sprintf(line, "%s [%ld]\n", usedSymbolicName, usedBundleId); out(line); serviceReference_getServiceRegistration(ref, ®); serviceRegistration_getProperties(reg, &props); hash_map_iterator_pt iter = hashMapIterator_create(props); while (hashMapIterator_hasNext(iter)) { hash_map_entry_pt entry = hashMapIterator_nextEntry(iter); sprintf(line, "%s = %s\n", hashMapEntry_getKey(entry), hashMapEntry_getValue(entry)); out(line); } hashMapIterator_destroy(iter); // objectClass = properties_get(props, (char *) OSGI_FRAMEWORK_OBJECTCLASS); // sprintf(line, "ObjectClass = %s\n", objectClass); if ((j + 1) < arrayList_size(refs)) { out("----\n"); } } } } } } } } } return status; }