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 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 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 getSpecifiedBundles(bundle_context_pt context, array_list_pt bundleNames, array_list_pt retrievedBundles) { celix_status_t status; array_list_pt bundles = NULL; status = bundleContext_getBundles(context, &bundles); if (status == CELIX_SUCCESS) { unsigned int size = arrayList_size(bundles); unsigned int i; for (i = 0; i < size; i++) { module_pt module = NULL; const char *name = NULL; bundle_pt bundle = (bundle_pt) arrayList_get(bundles, i); status = bundle_getCurrentModule(bundle, &module); if (status == CELIX_SUCCESS) { status = module_getSymbolicName(module, &name); } if (status == CELIX_SUCCESS) { printf("FOUND %s\n", name); array_list_iterator_pt iter = arrayListIterator_create(bundleNames); while(arrayListIterator_hasNext(iter)) { char* bundleName = (char*) arrayListIterator_next(iter); if ((strcmp(name, bundleName) == 0)) { bundle_archive_pt bundleArchive = NULL; long bundleId = -1; status = bundle_getArchive(bundle, &bundleArchive); if (status == CELIX_SUCCESS) { status = bundleArchive_getId(bundleArchive, &bundleId); } if (status == CELIX_SUCCESS) { arrayList_add(retrievedBundles, (void*) bundleId); break; } } } arrayListIterator_destroy(iter); } } arrayList_destroy(bundles); } 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 deploymentPackage_getBundle(deployment_package_pt package, char *name, bundle_pt *bundle) { if (hashMap_containsKey(package->nameToBundleInfo, name)) { array_list_pt bundles = NULL; bundleContext_getBundles(package->context, &bundles); int i; for (i = 0; i < arrayList_size(bundles); i++) { bundle_pt ibundle = arrayList_get(bundles, i); module_pt module = NULL; bundle_getCurrentModule(ibundle, &module); char *bsn = NULL; module_getSymbolicName(module, &bsn); if (strcmp(bsn, name) == 0) { *bundle = ibundle; break; } } } return CELIX_SUCCESS; }
celix_status_t logEntry_create(bundle_pt bundle, service_reference_pt reference, log_level_t level, char *message, int errorCode, log_entry_pt *entry) { celix_status_t status = CELIX_SUCCESS; *entry = malloc(sizeof(**entry)); if (*entry == NULL) { status = CELIX_ENOMEM; } else { (*entry)->level = level; (*entry)->message = strdup(message); (*entry)->errorCode = errorCode; (*entry)->time = time(NULL); (*entry)->bundleSymbolicName = NULL; (*entry)->bundleId = 0; } if (status == CELIX_SUCCESS) { status = bundle_getBundleId(bundle, &(*entry)->bundleId); } if (status == CELIX_SUCCESS) { module_pt module = NULL; status = bundle_getCurrentModule(bundle, &module); if (status == CELIX_SUCCESS) { char *symbolicName = NULL; status = module_getSymbolicName(module, &symbolicName); if (status == CELIX_SUCCESS) { (*entry)->bundleSymbolicName = strdup(symbolicName); } } } return status; }
static celix_status_t stopStartPermutation(bundle_context_pt context, long* permutation, int size) { celix_status_t status = CELIX_SUCCESS; int y = 0; printf("Test stop/start permutation: "); for (y = 0; (y < size) && (status == CELIX_SUCCESS); y++) { bundle_pt bundle = NULL; status = bundleContext_getBundleById(context, permutation[y], &bundle); if (status == CELIX_SUCCESS) { module_pt module = NULL; const char *name = NULL; status = bundle_getCurrentModule(bundle, &module); if (status == CELIX_SUCCESS) { status = module_getSymbolicName(module, &name); printf("%s (%ld) ", name, permutation[y]); } } } printf("\n"); // stop all bundles if (status == CELIX_SUCCESS) { for (y = 0; (y < size) && (status == CELIX_SUCCESS); y++) { bundle_pt bundle = NULL; status = bundleContext_getBundleById(context, permutation[y], &bundle); if (status == CELIX_SUCCESS) { printf("stop bundle: %ld\n", permutation[y]); status = bundle_stop(bundle); } } } // verify stop state if (status == CELIX_SUCCESS) { for (y = 0; (y < size) && (status == CELIX_SUCCESS); y++) { bundle_pt bundle = NULL; status = bundleContext_getBundleById(context, permutation[y], &bundle); if (status == CELIX_SUCCESS) { bundle_state_e state; status = bundle_getState(bundle, &state); if (state != OSGI_FRAMEWORK_BUNDLE_RESOLVED) { printf("bundle %ld has state %d (should be %d) \n", permutation[y], state, OSGI_FRAMEWORK_BUNDLE_RESOLVED); status = CELIX_ILLEGAL_STATE; } } } } // start all bundles if (status == CELIX_SUCCESS) { for (y = 0; (y < size) && (status == CELIX_SUCCESS); y++) { bundle_pt bundle = NULL; status = bundleContext_getBundleById(context, permutation[y], &bundle); if (status == CELIX_SUCCESS) { printf("start bundle: %ld\n", permutation[y]); status = bundle_start(bundle); } } } // verify started state if (status == CELIX_SUCCESS) { for (y = 0; (y < size) && (status == CELIX_SUCCESS); y++) { bundle_pt bundle = NULL; status = bundleContext_getBundleById(context, permutation[y], &bundle); if (status == CELIX_SUCCESS) { bundle_state_e state; status = bundle_getState(bundle, &state); if (state != OSGI_FRAMEWORK_BUNDLE_ACTIVE) { printf("bundle %ld has state %d (should be %d) \n", permutation[y], state, OSGI_FRAMEWORK_BUNDLE_ACTIVE); status = CELIX_ILLEGAL_STATE; } } } } return status; }
celix_status_t inspectCommand_printExportedServices(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_getRegisteredServices(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 provides 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); 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); } // objectClass = properties_get(props, (char *) OSGI_FRAMEWORK_OBJECTCLASS); // sprintf(line, "ObjectClass = %s\n", objectClass); if ((j + 1) < arrayList_size(refs)) { fprintf(outStream, "----\n"); } } } } } } } } } if (bundles != NULL) { arrayList_destroy(bundles); } return status; }
celix_status_t psCommand_execute(void *_ptr, char *command_line_str, FILE *out_ptr, FILE *err_ptr) { celix_status_t status = CELIX_SUCCESS; bundle_context_pt context_ptr = _ptr; array_list_pt bundles_ptr = NULL; bool show_location = false; bool show_symbolic_name = false; bool show_update_location = false; char *message_str = "Name"; if (!context_ptr || !command_line_str || !out_ptr || !err_ptr) { status = CELIX_ILLEGAL_ARGUMENT; } if (status == CELIX_SUCCESS) { status = bundleContext_getBundles(context_ptr, &bundles_ptr); } if (status == CELIX_SUCCESS) { char *sub_str = NULL; char *save_ptr = NULL; strtok_r(command_line_str, OSGI_SHELL_COMMAND_SEPARATOR, &save_ptr); sub_str = strtok_r(NULL, OSGI_SHELL_COMMAND_SEPARATOR, &save_ptr); while (sub_str != NULL) { if (strcmp(sub_str, "-l") == 0) { show_location = true; message_str = "Location"; } else if (strcmp(sub_str, "-s") == 0) { show_symbolic_name = true; message_str = "Symbolic name"; } else if (strcmp(sub_str, "-u") == 0) { show_update_location = true; message_str = "Update location"; } sub_str = strtok_r(NULL, OSGI_SHELL_COMMAND_SEPARATOR, &save_ptr); } fprintf(out_ptr, " %-5s %-12s %s\n", "ID", "State", message_str); unsigned int size = arrayList_size(bundles_ptr); bundle_pt bundles_array_ptr[size]; for (unsigned int i = 0; i < size; i++) { bundles_array_ptr[i] = arrayList_get(bundles_ptr, i); } for (unsigned int i = 0; i < size - 1; i++) { for (unsigned int j = i + 1; j < size; j++) { bundle_pt first_ptr = bundles_array_ptr[i]; bundle_pt second_ptr = bundles_array_ptr[j]; bundle_archive_pt first_archive_ptr = NULL; bundle_archive_pt second_archive_ptr = NULL; long first_id; long second_id; bundle_getArchive(first_ptr, &first_archive_ptr); bundle_getArchive(second_ptr, &second_archive_ptr); bundleArchive_getId(first_archive_ptr, &first_id); bundleArchive_getId(second_archive_ptr, &second_id); if (first_id > second_id) { bundle_pt temp_ptr = bundles_array_ptr[i]; bundles_array_ptr[i] = bundles_array_ptr[j]; bundles_array_ptr[j] = temp_ptr; } } } for (unsigned int i = 0; i < size - 1; i++) { celix_status_t sub_status; bundle_pt bundle_ptr = bundles_array_ptr[i]; bundle_archive_pt archive_ptr = NULL; long id = 0; bundle_state_e state = OSGI_FRAMEWORK_BUNDLE_UNKNOWN; char *state_str = NULL; module_pt module_ptr = NULL; char *name_str = NULL; sub_status = bundle_getArchive(bundle_ptr, &archive_ptr); if (sub_status == CELIX_SUCCESS) { sub_status = bundleArchive_getId(archive_ptr, &id); } if (sub_status == CELIX_SUCCESS) { sub_status = bundle_getState(bundle_ptr, &state); } if (sub_status == CELIX_SUCCESS) { state_str = psCommand_stateString(state); sub_status = bundle_getCurrentModule(bundle_ptr, &module_ptr); } if (sub_status == CELIX_SUCCESS) { sub_status = module_getSymbolicName(module_ptr, &name_str); } if (sub_status == CELIX_SUCCESS) { if (show_location) { sub_status = bundleArchive_getLocation(archive_ptr, &name_str); } else if (show_symbolic_name) { // do nothing } else if (show_update_location) { sub_status = bundleArchive_getLocation(archive_ptr, &name_str); } } if (sub_status == CELIX_SUCCESS) { fprintf(out_ptr, " %-5ld %-12s %s\n", id, state_str, name_str); } if (sub_status != CELIX_SUCCESS) { status = sub_status; break; } } arrayList_destroy(bundles_ptr); } return status; }
celix_status_t bundle_createModule(bundle_pt bundle, module_pt *module) { celix_status_t status = CELIX_SUCCESS; bundle_archive_pt archive = NULL; bundle_revision_pt revision = NULL; manifest_pt headerMap = NULL; status = CELIX_DO_IF(status, bundle_getArchive(bundle, &archive)); status = CELIX_DO_IF(status, bundleArchive_getCurrentRevision(archive, &revision)); status = CELIX_DO_IF(status, bundleRevision_getManifest(revision, &headerMap)); if (status == CELIX_SUCCESS) { long bundleId; status = bundleArchive_getId(bundle->archive, &bundleId); if (status == CELIX_SUCCESS) { int revision = 0; char moduleId[512]; char *mId; snprintf(moduleId, sizeof(moduleId), "%ld.%d", bundleId, revision); mId = strdup(moduleId); *module = module_create(headerMap, mId, bundle); free(mId); if (*module != NULL) { version_pt bundleVersion = module_getVersion(*module); char * symName = NULL; status = module_getSymbolicName(*module, &symName); if (status == CELIX_SUCCESS) { array_list_pt bundles = framework_getBundles(bundle->framework); unsigned int i; for (i = 0; i < arrayList_size(bundles); i++) { bundle_pt check = (bundle_pt) arrayList_get(bundles, i); long id; if (bundleArchive_getId(check->archive, &id) == CELIX_SUCCESS) { if (id != bundleId) { module_pt mod = NULL; char * sym = NULL; version_pt version; int cmp; status = bundle_getCurrentModule(check, &mod); status = module_getSymbolicName(mod, &sym); version = module_getVersion(mod); version_compareTo(bundleVersion, version, &cmp); if ((symName != NULL) && (sym != NULL) && !strcmp(symName, sym) && !cmp) { char *versionString = NULL; version_toString(version, &versionString); printf("Bundle symbolic name and version are not unique: %s:%s\n", sym, versionString); free(versionString); status = CELIX_BUNDLE_EXCEPTION; break; } } } } arrayList_destroy(bundles); } } } } framework_logIfError(bundle->framework->logger, status, NULL, "Failed to create module"); 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; }