celix_status_t driverAttributes_create(service_reference_pt reference, driver_service_pt driver, driver_attributes_pt *attributes) { celix_status_t status = CELIX_SUCCESS; *attributes = calloc(1, sizeof(**attributes)); if (!*attributes) { status = CELIX_ENOMEM; } else { bundle_pt bundle = NULL; bundle_archive_pt bundleArchive = NULL; status = serviceReference_getBundle(reference, &bundle); if (status == CELIX_SUCCESS) { status = bundle_getArchive(bundle, &bundleArchive); if (status == CELIX_SUCCESS) { (*attributes)->reference = reference; (*attributes)->driver = driver; (*attributes)->bundle = bundle; char *location; status = bundleArchive_getLocation(bundleArchive, &location); if (status == CELIX_SUCCESS) { (*attributes)->dynamic = strncmp(location, DRIVER_LOCATION_PREFIX, 4) == 0 ? true : false; } } } } return status; }
celix_status_t deviceManager_getIdleDevices(device_manager_pt manager, array_list_pt *idleDevices) { celix_status_t status = CELIX_SUCCESS; status = arrayList_create(idleDevices); if (status == CELIX_SUCCESS) { hash_map_iterator_pt iter = hashMapIterator_create(manager->devices); while (hashMapIterator_hasNext(iter)) { celix_status_t substatus = CELIX_SUCCESS; service_reference_pt ref = hashMapIterator_nextKey(iter); const char *bsn = NULL; module_pt module = NULL; bundle_pt bundle = NULL; substatus = serviceReference_getBundle(ref, &bundle); if (substatus == CELIX_SUCCESS) { substatus = bundle_getCurrentModule(bundle, &module); if (substatus == CELIX_SUCCESS) { substatus = module_getSymbolicName(module, &bsn); if (substatus == CELIX_SUCCESS) { logHelper_log(manager->loghelper, OSGI_LOGSERVICE_DEBUG, "DEVICE_MANAGER: Check idle device: %s", bsn); array_list_pt bundles = NULL; substatus = serviceReference_getUsingBundles(ref, &bundles); if (substatus == CELIX_SUCCESS) { bool inUse = false; int i; for (i = 0; i < arrayList_size(bundles); i++) { bundle_pt bundle = arrayList_get(bundles, i); bool isDriver; celix_status_t sstatus = deviceManager_isDriverBundle(manager, bundle, &isDriver); if (sstatus == CELIX_SUCCESS) { if (isDriver) { const char *bsn = NULL; module_pt module = NULL; bundle_getCurrentModule(bundle, &module); module_getSymbolicName(module, &bsn); logHelper_log(manager->loghelper, OSGI_LOGSERVICE_DEBUG, "DEVICE_MANAGER: Not idle, used by driver: %s", bsn); inUse = true; break; } } } if (!inUse) { arrayList_add(*idleDevices, ref); } } if(bundles!=NULL){ arrayList_destroy(bundles); } } } } } hashMapIterator_destroy(iter); } return status; }
celix_status_t logService_logSr(log_service_data_pt logger, service_reference_pt reference, log_level_t level, char * message) { celix_status_t status; log_entry_pt entry = NULL; bundle_pt bundle = logger->bundle; bundle_archive_pt archive = NULL; module_pt module = NULL; char *symbolicName = NULL; long bundleId = -1; if (reference != NULL) { serviceReference_getBundle(reference, &bundle); } status = bundle_getArchive(bundle, &archive); if (status == CELIX_SUCCESS) { status = bundleArchive_getId(archive, &bundleId); } if (status == CELIX_SUCCESS) { status = bundle_getCurrentModule(bundle, &module); if (status == CELIX_SUCCESS) { status = module_getSymbolicName(module, &symbolicName); } } logEntry_create(bundleId, symbolicName, reference, level, message, 0, &entry); log_addEntry(logger->log, entry); return CELIX_SUCCESS; }
static celix_status_t topicsub_subscriberTracked(void * handle, service_reference_pt reference, void * service){ celix_status_t status = CELIX_SUCCESS; topic_subscription_pt ts = handle; celixThreadMutex_lock(&ts->ts_lock); if (!hashMap_containsKey(ts->servicesMap, service)) { bundle_pt bundle = NULL; hash_map_pt msgTypes = NULL; serviceReference_getBundle(reference, &bundle); if(ts->serializer != NULL && bundle!=NULL){ ts->serializer->createSerializerMap(ts->serializer->handle,bundle,&msgTypes); if(msgTypes != NULL){ hashMap_put(ts->servicesMap, service, msgTypes); printf("PSA_UDP_MC_TS: New subscriber registered.\n"); } } else{ printf("PSA_UDP_MC_TS: Cannot register new subscriber.\n"); status = CELIX_SERVICE_EXCEPTION; } } celixThreadMutex_unlock(&ts->ts_lock); return status; }
celix_status_t driverLoader_unloadDrivers(driver_loader_pt loader, driver_attributes_pt finalDriver) { celix_status_t status = CELIX_SUCCESS; service_reference_pt finalReference = NULL; if (finalDriver != NULL) { status = driverAttributes_getReference(finalDriver, &finalReference); } if (status == CELIX_SUCCESS) { int i; for (i = 0; i < arrayList_size(loader->loadedDrivers); i++) { service_reference_pt reference = arrayList_get(loader->loadedDrivers, i); bool equal = false; status = serviceReference_equals(reference, finalReference, &equal); if (status == CELIX_SUCCESS && !equal) { bundle_pt bundle = NULL; status = serviceReference_getBundle(reference, &bundle); if (status == CELIX_SUCCESS) { bundle_uninstall(bundle); // Ignore status } } } } return status; }
celix_status_t deviceManager_driverRemoved(void * handle, service_reference_pt ref, void * service) { celix_status_t status = CELIX_SUCCESS; device_manager_pt manager = handle; logHelper_log(manager->loghelper, OSGI_LOGSERVICE_DEBUG, "DEVICE_MANAGER: Remove driver"); hashMap_remove(manager->drivers, ref); array_list_pt idleDevices = NULL; status = deviceManager_getIdleDevices(manager, &idleDevices); if (status == CELIX_SUCCESS) { int i; for (i = 0; i < arrayList_size(idleDevices); i++) { celix_status_t forStatus = CELIX_SUCCESS; service_reference_pt ref = arrayList_get(idleDevices, i); const char *bsn = NULL; bundle_pt bundle = NULL; forStatus = serviceReference_getBundle(ref, &bundle); if (forStatus == CELIX_SUCCESS) { module_pt module = NULL; forStatus = bundle_getCurrentModule(bundle, &module); if (forStatus == CELIX_SUCCESS) { forStatus = module_getSymbolicName(module, &bsn); if (forStatus == CELIX_SUCCESS) { logHelper_log(manager->loghelper, OSGI_LOGSERVICE_DEBUG, "DEVICE_MANAGER: IDLE: %s", bsn); // #TODO attachDriver (idle device) // #TODO this can result in a loop? // Locate and install a driver // Let the match fail, the device is idle // The driver is removed, idle check is performed // Attach is tried again // .. loop .. void *device = hashMap_get(manager->devices, ref); forStatus = deviceManager_attachAlgorithm(manager, ref, device); } } } if (forStatus != CELIX_SUCCESS) { break; //Got error, stop loop and return status } } hash_map_iterator_pt iter = hashMapIterator_create(manager->drivers); while (hashMapIterator_hasNext(iter)) { hashMapIterator_nextValue(iter); // driver_attributes_pt da = hashMapIterator_nextValue(iter); // driverAttributes_tryUninstall(da); } hashMapIterator_destroy(iter); } if (idleDevices != NULL) { arrayList_destroy(idleDevices); } return status; }
celix_status_t logService_logSr(log_service_data_pt logger, service_reference_pt reference, log_level_t level, char * message) { log_entry_pt entry = NULL; bundle_pt bundle = logger->bundle; if (reference != NULL) { serviceReference_getBundle(reference, &bundle); } logEntry_create(bundle, reference, level, message, 0, logger->pool, &entry); log_addEntry(logger->log, entry); return CELIX_SUCCESS; }
celix_status_t managedServiceTracker_remove(managed_service_tracker_pt tracker, service_reference_pt reference, char * pid){ configuration_pt configuration = NULL; bundle_pt bundle = NULL; configurationStore_findConfiguration(tracker->configurationStore, pid, &configuration); if (configuration != NULL) { if (serviceReference_getBundle(reference, &bundle) == CELIX_SUCCESS) { configuration_unbind(configuration->handle, bundle); } } return managedServiceTracker_untrackManagedService(tracker, pid, reference); }
celix_status_t deviceManager_getIdleDevices_exmaple(device_manager_pt manager, apr_pool_t *pool, array_list_pt *idleDevices) { celix_status_t status = CELIX_SUCCESS; status = arrayList_create(idleDevices); if (status == CELIX_SUCCESS) { hash_map_iterator_pt iter = hashMapIterator_create(manager->devices); while (hashMapIterator_hasNext(iter)) { celix_status_t substatus = CELIX_SUCCESS; service_reference_pt ref = hashMapIterator_nextKey(iter); char *bsn = NULL; module_pt module = NULL; bundle_pt bundle = NULL; array_list_pt bundles = NULL; substatus = serviceReference_getBundle(ref, &bundle); substatus = DO_IF_SUCCESS(substatus, bundle_getCurrentModule(bundle, &module)); substatus = DO_IF_SUCCESS(substatus, module_getSymbolicName(module, &bsn)); substatus = DO_IF_SUCCESS(substatus, serviceReference_getUsingBundles(ref, &bundles)); if (substatus == CELIX_SUCCESS) { printf("DEVICE_MANAGER: Check idle device: %s\n", bsn); bool inUse = false; int i; for (i = 0; i < arrayList_size(bundles); i++) { bundle_pt bundle = arrayList_get(bundles, i); bool isDriver; celix_status_t sstatus = deviceManager_isDriverBundle(manager, bundle, &isDriver); if (sstatus == CELIX_SUCCESS) { if (isDriver) { char *bsn = NULL; module_pt module = NULL; bundle_getCurrentModule(bundle, &module); module_getSymbolicName(module, &bsn); printf("DEVICE_MANAGER: Not idle, used by driver: %s\n", bsn); inUse = true; break; } } } if (!inUse) { arrayList_add(*idleDevices, ref); } } } hashMapIterator_destroy(iter); } return status; }
celix_status_t managedServiceTracker_notifyUpdated(managed_service_tracker_pt tracker, configuration_pt configuration) { char *pid; service_reference_pt reference = NULL; bundle_pt bundle = NULL; properties_pt properties = NULL; managed_service_service_pt service = NULL; // (1) config.checkLocked if (configuration_checkLocked(configuration->handle) != CELIX_SUCCESS) { //TODO not yet implemented return CELIX_ILLEGAL_ARGUMENT; } // (2) config.getPid if (configuration_getPid(configuration->handle, &pid) != CELIX_SUCCESS) { return CELIX_ILLEGAL_ARGUMENT; } // (3) reference = getManagedServiceReference(pid) if (managedServiceTracker_getManagedServiceReference(tracker, pid, &reference) != CELIX_SUCCESS || reference == NULL) { printf("[ ERROR ]: Tracker - Notify (NULL Reference Service{PID=%s}) \n", pid); return CELIX_ILLEGAL_ARGUMENT; // Eclipse ignores, but according to Specs, callback is delayed } // (4.1) reference.getBundle if (serviceReference_getBundle(reference, &bundle) != CELIX_SUCCESS || bundle == NULL) { printf("[ ERROR ]: Tracker - Notify (NULL Bundle Service{PID=%s}) \n", pid); return CELIX_ILLEGAL_ARGUMENT; } // (4.2) config.bind(reference.getBundle) bool isBind; if (configuration_bind(configuration->handle, bundle, &isBind) != CELIX_SUCCESS || isBind == false) { printf("[ ERROR ]: Tracker - Notify (Service{PID=%s} Permission Error) \n", pid); return CELIX_ILLEGAL_STATE; } // (5) if (reference != null && config.bind(reference.getBundle())) // (5.1) properties = config.getProperties if (configuration_getProperties(configuration->handle, &properties) != CELIX_SUCCESS) { printf("[ ERROR ]: Tracker - Notify (Service{PID=%s} Wrong Properties) \n", pid); return CELIX_ILLEGAL_ARGUMENT; } // (5.2) modifyConfiguration if (configurationAdminFactory_modifyConfiguration(tracker->configurationAdminfactory, reference, properties) != CELIX_SUCCESS) { return CELIX_ILLEGAL_ARGUMENT; //TODO no yet implemented modifyConfiguration } // (5.3) service = getManagedService(pid) if (managedServiceTracker_getManagedService(tracker, pid, &service) != CELIX_SUCCESS) { printf("[ ERROR ]: Tracker - Notify (NULL Service{PID=%s}) \n", pid); return CELIX_ILLEGAL_ARGUMENT; } // (5.4) asynchUpdate(service,properties) if ((properties == NULL) || (properties != NULL && hashMap_size(properties) == 0)) { return managedServiceTracker_asynchUpdated(tracker, service, NULL); } else { return managedServiceTracker_asynchUpdated(tracker, service, properties); } return CELIX_ILLEGAL_ARGUMENT; }
// org.eclipse.equinox.internal.cm.ManagedServiceTracker celix_status_t managedServiceTracker_add(managed_service_tracker_pt tracker, service_reference_pt reference, char *pid, managed_service_service_pt service) { celix_status_t status; bundle_pt bundle = NULL; const char* bundleLocation; configuration_pt configuration = NULL; properties_pt properties = NULL; configurationStore_findConfiguration(tracker->configurationStore, pid, &configuration); if (configuration == NULL) { if (managedServiceTracker_trackManagedService(tracker, pid, reference, service) == CELIX_SUCCESS) { // TODO : this is new code, it hasn't been tested yet if (serviceReference_getBundle(reference, &bundle) != CELIX_SUCCESS) { return CELIX_ILLEGAL_ARGUMENT; } if (bundle_getBundleLocation(bundle, &bundleLocation) != CELIX_SUCCESS) { return CELIX_ILLEGAL_ARGUMENT; } // (1) creates a new Configuration for the ManagedService if (configurationStore_getConfiguration(tracker->configurationStore, pid, (char*)bundleLocation, &configuration) != CELIX_SUCCESS || configuration == NULL) { return CELIX_ILLEGAL_ARGUMENT; } // (2) bind the Configuration with the ManagedService bool dummy; if ((configuration_bind(configuration->handle, bundle, &dummy) != CELIX_SUCCESS)) { return CELIX_ILLEGAL_ARGUMENT; } // (3) the new Configuration is persisted and visible for other ConfigAdmin instances if (configurationStore_saveConfiguration(tracker->configurationStore, pid, configuration) != CELIX_SUCCESS) { return CELIX_ILLEGAL_STATE; } // End of new code // TODO: It must be considered in case of fail if untrack the ManagedService return managedServiceTracker_asynchUpdated(tracker, service, NULL); } else { return CELIX_ILLEGAL_ARGUMENT; // the service was already tracked } } else { configuration_lock(configuration->handle); if (managedServiceTracker_trackManagedService(tracker, pid, reference, service) == CELIX_SUCCESS) { if (serviceReference_getBundle(reference, &bundle) != CELIX_SUCCESS) { configuration_unlock(configuration->handle); printf("[ERROR ]: Tracker - Add (Service{PID=%s} Reference - getBundle NULL)", pid); return CELIX_ILLEGAL_ARGUMENT; } // TODO configuration.isDeleted ? - with only using one calling bundle OK bool isBind; if ((configuration_bind(configuration->handle, bundle, &isBind) == CELIX_SUCCESS) && (isBind == true)) { // config.bind(bundle) if (configuration_getProperties(configuration->handle, &properties) != CELIX_SUCCESS) { configuration_unlock(configuration->handle); return CELIX_ILLEGAL_ARGUMENT; } if (configurationAdminFactory_modifyConfiguration(tracker->configurationAdminfactory, reference, properties) != CELIX_SUCCESS) { configuration_unlock(configuration->handle); return CELIX_ILLEGAL_ARGUMENT; } status = managedServiceTracker_asynchUpdated(tracker, service, properties); configuration_unlock(configuration->handle); return status; } else { configuration_unlock(configuration->handle); return CELIX_ILLEGAL_STATE; } } else { configuration_unlock(configuration->handle); return CELIX_ILLEGAL_ARGUMENT; // the service was already tracked } } }
celix_status_t inspectCommand_printImportedServices(bundle_context_pt context, array_list_pt ids, FILE *outStream, FILE *errStream) { celix_status_t status = CELIX_SUCCESS; array_list_pt bundles = NULL; if (arrayList_isEmpty(ids)) { status = bundleContext_getBundles(context, &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(context, id, &b); if (st == CELIX_SUCCESS) { arrayList_add(bundles, b); } else { fprintf(outStream, "INSPECT: Invalid bundle ID: %ld\n", id); } } } 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) { fprintf(outStream, "\n"); } if (bundle != NULL) { array_list_pt refs = NULL; if (bundle_getServicesInUse(bundle, &refs) == CELIX_SUCCESS) { 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) { fprintf(outStream, "%s requires services:\n", name); fprintf(outStream, "==============\n"); if (refs == NULL || arrayList_size(refs) == 0) { fprintf(outStream, "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); 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); fprintf(outStream, "%s [%ld]\n", usedSymbolicName, usedBundleId); unsigned int size = 0; char **keys; serviceReference_getPropertyKeys(ref, &keys, &size); for (int k = 0; k < size; k++) { char *key = keys[k]; char *value = NULL; serviceReference_getProperty(ref, key, &value); fprintf(outStream, "%s = %s\n", key, value); } free(keys); // objectClass = properties_get(props, (char *) OSGI_FRAMEWORK_OBJECTCLASS); // sprintf(line, "ObjectClass = %s\n", objectClass); if ((j + 1) < arrayList_size(refs)) { fprintf(outStream, "----\n"); } } } } } } } } } arrayList_destroy(bundles); 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; }