Beispiel #1
0
celix_status_t serviceRegistry_getService(service_registry_pt registry, bundle_pt bundle, service_reference_pt reference, void **service) {
	celix_status_t status = CELIX_SUCCESS;
	service_registration_pt registration = NULL;
	*service = NULL;
	usage_count_pt usage = NULL;
	serviceReference_getServiceRegistration(reference, &registration);
	
	celixThreadMutex_lock(&registry->mutex);

	if (serviceRegistration_isValid(registration)) {
		status = serviceRegistry_getUsageCount(registry, bundle, reference, &usage);
		if (usage == NULL) {
			status = serviceRegistry_addUsageCount(registry, bundle, reference, &usage);
		}
		usage->count++;
		*service = usage->service;
	}
	celixThreadMutex_unlock(&registry->mutex);

	if ((usage != NULL) && (*service == NULL)) {
		status = serviceRegistration_getService(registration, bundle, service);
	}
	celixThreadMutex_lock(&registry->mutex);
	if ((!serviceRegistration_isValid(registration)) || (*service == NULL)) {
		serviceRegistry_flushUsageCount(registry, bundle, reference);
	} else {
		usage->service = *service;
	}
	celixThreadMutex_unlock(&registry->mutex);

	return status;
}
Beispiel #2
0
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, &registration);
					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;
}
Beispiel #3
0
celix_status_t serviceRegistry_getServiceReferencesForRegistration(service_registry_pt registry, service_registration_pt registration, array_list_pt *references) {
    celix_status_t status = CELIX_SUCCESS;

    hash_map_values_pt referenceValues = NULL;
    hash_map_iterator_pt iterator = NULL;

    arrayList_create(references);

    celixThreadMutex_lock(&registry->referencesMapMutex);
    referenceValues = hashMapValues_create(registry->serviceReferences);
    iterator = hashMapValues_iterator(referenceValues);
    while (hashMapIterator_hasNext(iterator)) {
        array_list_pt refs = (array_list_pt) hashMapIterator_nextValue(iterator);
        unsigned int refIdx;
        for (refIdx = 0; (refs != NULL) && refIdx < arrayList_size(refs); refIdx++) {
            service_registration_pt reg = NULL;
            service_reference_pt reference = (service_reference_pt) arrayList_get(refs, refIdx);
            bool valid = false;
            serviceRefernce_isValid(reference, &valid);
            if (valid) {
                serviceReference_getServiceRegistration(reference, &reg);
                if (reg == registration) {
                    arrayList_add(*references, reference);
                }
            }
        }
    }
    hashMapIterator_destroy(iterator);
    hashMapValues_destroy(referenceValues);

    celixThreadMutex_unlock(&registry->referencesMapMutex);

    return status;
}
Beispiel #4
0
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, &registration);
	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, &registration);
    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;
}
Beispiel #6
0
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, &registration);
	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;
}
Beispiel #7
0
celix_status_t serviceRegistry_ungetService(service_registry_pt registry, bundle_pt bundle, service_reference_pt reference, bool *result) {
	celix_status_t status = CELIX_SUCCESS;
	service_registration_pt registration = NULL;
	usage_count_pt usage = NULL;
	serviceReference_getServiceRegistration(reference, &registration);

	celixThreadMutex_lock(&registry->mutex);

	status = serviceRegistry_getUsageCount(registry, bundle, reference, &usage);
	if (usage == NULL) {
		celixThreadMutex_unlock(&registry->mutex);
		if (result) {
			*result = false;
		}
		return CELIX_SUCCESS;
	}

	if (usage->count == 1) {
		serviceRegistration_ungetService(registration, bundle, &usage->service);
	}

	usage->count--;

	if ((!serviceRegistration_isValid(registration)) || (usage->count <= 0)) {
		usage->service = NULL;
		serviceRegistry_flushUsageCount(registry, bundle, reference);
	}

	celixThreadMutex_unlock(&registry->mutex);

	if (result) {
		*result = true;
	}

	return status;
}
Beispiel #8
0
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, &registration);
					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;
}
Beispiel #9
0
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, &registration);
					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;
}
Beispiel #10
0
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, &registration);
					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;
}
Beispiel #11
0
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, &reg);
                                    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;
}