Ejemplo n.º 1
0
celix_status_t serviceRegistry_create(framework_pt framework, serviceChanged_function_pt serviceChanged, service_registry_pt *registry) {
	celix_status_t status = CELIX_SUCCESS;

	*registry = malloc(sizeof(**registry));
	if (!*registry) {
		status = CELIX_ENOMEM;
	} else {

		(*registry)->serviceChanged = serviceChanged;
		(*registry)->inUseMap = hashMap_create(NULL, NULL, NULL, NULL);
		(*registry)->serviceRegistrations = hashMap_create(NULL, NULL, NULL, NULL);
		(*registry)->framework = framework;
		(*registry)->currentServiceId = 1l;
		(*registry)->serviceReferences = hashMap_create(NULL, NULL, NULL, NULL);;

		arrayList_create(&(*registry)->listenerHooks);

		status = celixThreadMutexAttr_create(&(*registry)->mutexAttr);
		status = CELIX_DO_IF(status, celixThreadMutexAttr_settype(&(*registry)->mutexAttr, CELIX_THREAD_MUTEX_RECURSIVE));
		status = CELIX_DO_IF(status, celixThreadMutex_create(&(*registry)->mutex, &(*registry)->mutexAttr));
		status = CELIX_DO_IF(status, celixThreadMutex_create(&(*registry)->referencesMapMutex, NULL));
	}

	framework_logIfError(logger, status, NULL, "Cannot create service registry");

	return status;
}
Ejemplo n.º 2
0
Archivo: bundle.c Proyecto: jawi/celix
celix_status_t bundle_revise(bundle_pt bundle, char * location, char *inputFile) {
	celix_status_t status = CELIX_SUCCESS;

	bundle_archive_pt archive = NULL;
	status = bundle_getArchive(bundle, &archive);
	if (status == CELIX_SUCCESS) {
		status = bundleArchive_revise(archive, location, inputFile);
		if (status == CELIX_SUCCESS) {
			module_pt module;
			status = bundle_createModule(bundle, &module);
			if (status == CELIX_SUCCESS) {
				status = bundle_addModule(bundle, module);
			} else {
				bool rolledback;
				status = bundleArchive_rollbackRevise(archive, &rolledback);
				if (status == CELIX_SUCCESS) {
					status = CELIX_BUNDLE_EXCEPTION;
				}
			}
		}
	}

	framework_logIfError(bundle->framework->logger, status, NULL, "Failed to revise bundle");

	return status;
}
Ejemplo n.º 3
0
Archivo: bundle.c Proyecto: jawi/celix
celix_status_t bundle_unlock(bundle_pt bundle, bool *unlocked) {
	celix_status_t status = CELIX_SUCCESS;

	bool equals;

	celixThreadMutex_lock(&bundle->lock);

	if (bundle->lockCount == 0) {
		*unlocked = false;
	} else {
		status = thread_equalsSelf(bundle->lockThread, &equals);
		if (status == CELIX_SUCCESS) {
			if ((bundle->lockCount > 0) && !equals) {
				return false;
			}
			bundle->lockCount--;
			if (bundle->lockCount == 0) {
				bundle->lockThread = 0;
			}
			*unlocked = true;
		}
	}

	celixThreadMutex_unlock(&bundle->lock);

	framework_logIfError(bundle->framework->logger, status, NULL, "Failed to unlock bundle");

	return status;
}
Ejemplo n.º 4
0
celix_status_t serviceRegistry_getRegisteredServices(service_registry_pt registry, bundle_pt bundle, array_list_pt *services) {
	celix_status_t status = CELIX_SUCCESS;

	celixThreadMutex_lock(&registry->mutex);
	array_list_pt regs = (array_list_pt) hashMap_get(registry->serviceRegistrations, bundle);
	if (regs != NULL) {
		unsigned int i;
		arrayList_create(services);
		
		for (i = 0; i < arrayList_size(regs); i++) {
			service_registration_pt reg = arrayList_get(regs, i);
			if (serviceRegistration_isValid(reg)) {
				service_reference_pt reference = NULL;
				status = serviceRegistry_createServiceReference(registry, bundle, reg, &reference);
				if (status == CELIX_SUCCESS) {
					arrayList_add(*services, reference);
				}
			}
		}
	}
	celixThreadMutex_unlock(&registry->mutex);

	framework_logIfError(logger, status, NULL, "Cannot get registered services");

	return status;
}
Ejemplo n.º 5
0
celix_status_t requirement_create(hash_map_pt directives, hash_map_pt attributes, requirement_pt *requirement) {
	celix_status_t status = CELIX_SUCCESS;

	*requirement = (requirement_pt) malloc(sizeof(**requirement));
	if (!*requirement) {
		status = CELIX_ENOMEM;
	} else {
		attribute_pt serviceAttribute = NULL;
		attribute_pt versionAttribute = NULL;

		(*requirement)->attributes = attributes;
		(*requirement)->directives = directives;

		serviceAttribute = (attribute_pt) hashMap_get(attributes, "service");
		status = attribute_getValue(serviceAttribute, &(*requirement)->targetName);
		if (status == CELIX_SUCCESS) {
			(*requirement)->versionRange = NULL;
			status = versionRange_createInfiniteVersionRange(&(*requirement)->versionRange);
			if (status == CELIX_SUCCESS) {
				versionAttribute = (attribute_pt) hashMap_get(attributes, "version");
				if (versionAttribute != NULL) {
					char *versionStr = NULL;
					attribute_getValue(versionAttribute, &versionStr);
					(*requirement)->versionRange = NULL;
					status = versionRange_parse(versionStr, &(*requirement)->versionRange);
				}
			}
		}
	}

	framework_logIfError(logger, status, NULL, "Cannot create requirement");

	return status;
}
Ejemplo n.º 6
0
celix_status_t serviceTracker_open(service_tracker_pt tracker) {
	service_listener_pt listener;
	array_list_pt initial = NULL;
	celix_status_t status = CELIX_SUCCESS;
	listener = (service_listener_pt) malloc(sizeof(*listener));
	
	status = bundleContext_getServiceReferences(tracker->context, NULL, tracker->filter, &initial);
	if (status == CELIX_SUCCESS) {
		service_reference_pt initial_reference;
		unsigned int i;

		listener->handle = tracker;
		listener->serviceChanged = (void *) serviceTracker_serviceChanged;
		status = bundleContext_addServiceListener(tracker->context, listener, tracker->filter);
		if (status == CELIX_SUCCESS) {
			tracker->listener = listener;

			for (i = 0; i < arrayList_size(initial); i++) {
				initial_reference = (service_reference_pt) arrayList_get(initial, i);
				serviceTracker_track(tracker, initial_reference, NULL);
			}
			arrayList_clear(initial);
			arrayList_destroy(initial);

			initial = NULL;
		}
	}

	framework_logIfError(logger, status, NULL, "Cannot open tracker");

	return status;
}
Ejemplo n.º 7
0
celix_status_t bundleCache_create(properties_pt configurationMap, framework_logger_pt logger, bundle_cache_pt *bundle_cache) {
    celix_status_t status;
    bundle_cache_pt cache;

	cache = (bundle_cache_pt) malloc((sizeof(*cache)));
    if (cache == NULL) {
        status = CELIX_ENOMEM;
    } else {
		if (configurationMap != NULL && *bundle_cache == NULL) {
            char * cacheDir = properties_get(configurationMap, (char *) OSGI_FRAMEWORK_FRAMEWORK_STORAGE);
			cache->configurationMap = configurationMap;
            if (cacheDir == NULL) {
                cacheDir = ".cache";
            }
            cache->cacheDir = cacheDir;
            cache->logger = logger;

            *bundle_cache = cache;
            status = CELIX_SUCCESS;
        } else {
            status = CELIX_ILLEGAL_ARGUMENT;
        }
    }

    framework_logIfError(cache->logger, status, NULL, "Failed to create bundle cache");

	return status;
}
Ejemplo n.º 8
0
static celix_status_t  serviceTracker_addingService(service_tracker_pt tracker, service_reference_pt reference, void **service) {
	celix_status_t status = CELIX_SUCCESS;

    if (tracker->customizer != NULL) {
    	void *handle = NULL;
		adding_callback_pt function = NULL;

		status =  serviceTrackerCustomizer_getHandle(tracker->customizer, &handle);
		if (status == CELIX_SUCCESS) {
			status = serviceTrackerCustomizer_getAddingFunction(tracker->customizer, &function);
			if (status == CELIX_SUCCESS) {
				if (function != NULL) {
					status = function(handle, reference, service);
				} else {
                    status = bundleContext_getService(tracker->context, reference, service);
				}
			}
		}
	} else {
		status = bundleContext_getService(tracker->context, reference, service);
	}

    framework_logIfError(logger, status, NULL, "Cannot handle addingService");

    return status;
}
Ejemplo n.º 9
0
Archivo: bundle.c Proyecto: jawi/celix
celix_status_t bundle_getRegisteredServices(bundle_pt bundle, array_list_pt *list) {
	celix_status_t status = CELIX_SUCCESS;

	status = fw_getBundleRegisteredServices(bundle->framework, bundle, list);

	framework_logIfError(bundle->framework->logger, status, NULL, "Failed to get registered services");

	return status;
}
Ejemplo n.º 10
0
Archivo: bundle.c Proyecto: jawi/celix
celix_status_t bundle_getServicesInUse(bundle_pt bundle, array_list_pt *list) {
	celix_status_t status = CELIX_SUCCESS;

	status = fw_getBundleServicesInUse(bundle->framework, bundle, list);

	framework_logIfError(bundle->framework->logger, status, NULL, "Failed to get in use services");

	return status;
}
Ejemplo n.º 11
0
static celix_status_t serviceTracker_track(service_tracker_pt tracker, service_reference_pt reference, service_event_pt event) {
	celix_status_t status = CELIX_SUCCESS;

	tracked_pt tracked = NULL;
	int found = -1;
	unsigned int i;
	for (i = 0; i < arrayList_size(tracker->tracked); i++) {
		bool equals;
		tracked = (tracked_pt) arrayList_get(tracker->tracked, i);
		serviceReference_equals(reference, tracked->reference, &equals);
		if (equals) {
			found = 0;
			break;
		}
	}

	if (found) {
		void * service = NULL;
		status = serviceTracker_addingService(tracker, reference, &service);
		if (status == CELIX_SUCCESS) {
			if (service != NULL) {
				tracked = (tracked_pt) malloc(sizeof(*tracked));
				tracked->reference = reference;
				tracked->service = service;
				arrayList_add(tracker->tracked, tracked);
				if (tracker->customizer != NULL) {
					void *handle = NULL;
					added_callback_pt function = NULL;

					serviceTrackerCustomizer_getHandle(tracker->customizer, &handle);
					serviceTrackerCustomizer_getAddedFunction(tracker->customizer, &function);
					if (function != NULL) {
						function(handle, reference, service);
					}
				}
			}
		}
	} else {
		if (tracker->customizer != NULL) {
			void *handle = NULL;
			modified_callback_pt function = NULL;

			serviceTrackerCustomizer_getHandle(tracker->customizer, &handle);
			serviceTrackerCustomizer_getModifiedFunction(tracker->customizer, &function);

			if (function != NULL) {
				function(handle, reference, tracked->service);
			}
		}
	}

	framework_logIfError(logger, status, NULL, "Cannot track reference");

	return status;
}
Ejemplo n.º 12
0
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(&registry->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(&registry->mutex);

	framework_logIfError(logger, status, NULL, "Cannot get service references");

	return status;
}
Ejemplo n.º 13
0
celix_status_t bundleRevision_getNumber(bundle_revision_pt revision, long *revisionNr) {
	celix_status_t status = CELIX_SUCCESS;
    if (revision == NULL) {
        status = CELIX_ILLEGAL_ARGUMENT;
    } else {
    	*revisionNr = revision->revisionNr;
    }

    framework_logIfError(logger, status, NULL, "Failed to get revision number");

	return status;
}
Ejemplo n.º 14
0
celix_status_t bundleRevision_getHandles(bundle_revision_pt revision, array_list_pt *handles) {
    celix_status_t status = CELIX_SUCCESS;
    if (revision == NULL) {
        status = CELIX_ILLEGAL_ARGUMENT;
    } else {
        *handles = revision->libraryHandles;
    }

    framework_logIfError(logger, status, NULL, "Failed to get handles");

    return status;
}
Ejemplo n.º 15
0
celix_status_t bundleRevision_getManifest(bundle_revision_pt revision, manifest_pt *manifest) {
	celix_status_t status = CELIX_SUCCESS;
	if (revision == NULL) {
		status = CELIX_ILLEGAL_ARGUMENT;
	} else {
		*manifest = revision->manifest;
	}

	framework_logIfError(logger, status, NULL, "Failed to get manifest");

	return status;
}
Ejemplo n.º 16
0
Archivo: bundle.c Proyecto: jawi/celix
celix_status_t bundle_getBundleId(bundle_pt bundle, long *id) {
	celix_status_t status = CELIX_SUCCESS;
	bundle_archive_pt archive = NULL;
	status = bundle_getArchive(bundle, &archive);
	if (status == CELIX_SUCCESS) {
		status = bundleArchive_getId(archive, id);
	}

	framework_logIfError(bundle->framework->logger, status, NULL, "Failed to get bundle id");

	return status;
}
Ejemplo n.º 17
0
celix_status_t bundleRevision_getLocation(bundle_revision_pt revision, char **location) {
	celix_status_t status = CELIX_SUCCESS;
	if (revision == NULL) {
		status = CELIX_ILLEGAL_ARGUMENT;
	} else {
		*location = revision->location;
	}

	framework_logIfError(logger, status, NULL, "Failed to get revision location");

	return status;
}
Ejemplo n.º 18
0
celix_status_t bundleRevision_getRoot(bundle_revision_pt revision, char **root) {
	celix_status_t status = CELIX_SUCCESS;
	if (revision == NULL) {
		status = CELIX_ILLEGAL_ARGUMENT;
	} else {
		*root = revision->root;
	}

	framework_logIfError(logger, status, NULL, "Failed to get revision root");

	return status;
}
Ejemplo n.º 19
0
celix_status_t bundleContext_getService(bundle_context_pt context, service_reference_pt reference, void **service_instance) {
    celix_status_t status = CELIX_SUCCESS;

    if (context != NULL && reference != NULL && *service_instance == NULL) {
	    status = fw_getService(context->framework, context->bundle, reference, service_instance);
    } else {
        status = CELIX_ILLEGAL_ARGUMENT;
    }

    framework_logIfError(logger, status, NULL, "Failed to get service");

    return status;
}
Ejemplo n.º 20
0
celix_status_t bundleContext_getFramework(bundle_context_pt context, framework_pt *framework) {
	celix_status_t status = CELIX_SUCCESS;

	if (context == NULL) {
		status = CELIX_ILLEGAL_ARGUMENT;
	} else {
		*framework = context->framework;
	}

	framework_logIfError(logger, status, NULL, "Failed to get framework");

	return status;
}
Ejemplo n.º 21
0
celix_status_t bundleContext_getBundle(bundle_context_pt context, bundle_pt *bundle) {
	celix_status_t status = CELIX_SUCCESS;

	if (context == NULL) {
		status = CELIX_ILLEGAL_ARGUMENT;
	} else {
		*bundle = context->bundle;
	}

	framework_logIfError(logger, status, NULL, "Failed to get bundle");

	return status;
}
Ejemplo n.º 22
0
celix_status_t bundleContext_getProperty(bundle_context_pt context, const char *name, char **value) {
	celix_status_t status = CELIX_SUCCESS;

	if (context == NULL || name == NULL || *value != NULL) {
		status = CELIX_ILLEGAL_ARGUMENT;
	} else {
		fw_getProperty(context->framework, name, value);
	}

	framework_logIfError(logger, status, NULL, "Failed to get property [name=%s]", name);

	return status;
}
Ejemplo n.º 23
0
celix_status_t bundleContext_removeFrameworkListener(bundle_context_pt context, framework_listener_pt listener) {
    celix_status_t status = CELIX_SUCCESS;

    if (context != NULL && listener != NULL) {
        fw_removeFrameworkListener(context->framework, context->bundle, listener);
    } else {
        status = CELIX_ILLEGAL_ARGUMENT;
    }

    framework_logIfError(logger, status, NULL, "Failed to remove framework listener");

    return status;
}
Ejemplo n.º 24
0
celix_status_t bundleContext_addServiceListener(bundle_context_pt context, service_listener_pt listener, char * filter) {
    celix_status_t status = CELIX_SUCCESS;

    if (context != NULL && listener != NULL) {
        fw_addServiceListener(context->framework, context->bundle, listener, filter);
    } else {
        status = CELIX_ILLEGAL_ARGUMENT;
    }

    framework_logIfError(logger, status, NULL, "Failed to add service listener");

    return status;
}
Ejemplo n.º 25
0
celix_status_t bundleContext_getBundles(bundle_context_pt context, array_list_pt *bundles) {
	celix_status_t status = CELIX_SUCCESS;

	if (context == NULL || *bundles != NULL) {
		status = CELIX_ILLEGAL_ARGUMENT;
	} else {
		*bundles = framework_getBundles(context->framework);
	}

	framework_logIfError(logger, status, NULL, "Failed to get bundles");

	return status;
}
Ejemplo n.º 26
0
celix_status_t bundleContext_ungetService(bundle_context_pt context, service_reference_pt reference, bool *result) {
    celix_status_t status = CELIX_SUCCESS;

    if (context != NULL && reference != NULL) {
        status = framework_ungetService(context->framework, context->bundle, reference, result);
    } else {
        status = CELIX_ILLEGAL_ARGUMENT;
    }

    framework_logIfError(logger, status, NULL, "Failed to unget service");

    return status;
}
Ejemplo n.º 27
0
celix_status_t bundleContext_getServiceReferences(bundle_context_pt context, const char * serviceName, char * filter, array_list_pt *service_references) {
    celix_status_t status = CELIX_SUCCESS;

    if (context != NULL && *service_references == NULL) {
        fw_getServiceReferences(context->framework, service_references, context->bundle, serviceName, filter);
    } else {
        status = CELIX_ILLEGAL_ARGUMENT;
    }

    framework_logIfError(logger, status, NULL, "Failed to get service references");

	return status;
}
Ejemplo n.º 28
0
celix_status_t manifest_createFromFile(char *filename, manifest_pt *manifest) {
	celix_status_t status;

	status = manifest_create(manifest);

	if (status == CELIX_SUCCESS) {
		manifest_read(*manifest, filename);
	}

	framework_logIfError(logger, status, NULL, "Cannot create manifest from file");

	return status;
}
Ejemplo n.º 29
0
celix_status_t serviceRegistration_getBundle(service_registration_pt registration, bundle_pt *bundle) {
	celix_status_t status = CELIX_SUCCESS;

	if (registration != NULL && *bundle == NULL) {
		*bundle = registration->bundle;
	} else {
		status = CELIX_ILLEGAL_ARGUMENT;
	}

	framework_logIfError(logger, status, NULL, "Cannot get bundle");

	return status;
}
Ejemplo n.º 30
0
celix_status_t serviceRegistration_getServiceName(service_registration_pt registration, char **serviceName) {
	celix_status_t status = CELIX_SUCCESS;

	if (registration != NULL && *serviceName == NULL) {
		*serviceName = registration->className;
	} else {
		status = CELIX_ILLEGAL_ARGUMENT;
	}

	framework_logIfError(logger, status, NULL, "Cannot get service name");

	return status;
}