celix_status_t managedServiceTracker_add()

in misc/experimental/bundles/config_admin/service/private/src/managed_service_tracker.c [304:399]


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
        }
    }
}