Operations like service registration and modifying service properties may require some kind of hooking mechanism. This allows applications with appropriate permissions to reject these operations by forcing the framework to throw an exception.
The decision to allow or reject has to be based on arguments such as service object, objectclass(es), properties, etc.
Thus to intercept the service registration and to protect the service properties update, the following solutions are provided:
Using registration hooks
If an application wants to use a service registration hook, it must provide an implementation and register it as an OSGi service.
The registering method will be called each time a service is being registered, unless another hook had already rejected the operation.
import com.prosyst.mbs.framework.hooks.service.RegisterHook;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import java.util.Dictionary;
import java.security.AccessControlContext;
class RegisterHookImpl implements RegisterHook {
void register(BundleContext context) {
context.registerService(RegisterHook.class, this, null);
}
public void registering(Bundle provider, String[] objectClasses, Object serviceObject, Dictionary<String, ?> props, AccessControlContext context) {
// TODO - perform some checks here
// throw new IllegalArgumentException("Operation not allowed"); - service will not be registered in this case
}
}
Using modification hooks
If an application wants to use a service modification hook, it must provide an implementation and register it as an OSGi service.
The modifying method will be called each time a service properties are being modified, unless another hook had already rejected the operation.
import com.prosyst.mbs.framework.hooks.service.ModifyHook;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import java.util.Dictionary;
class ModifyHookImpl implements ModifyHook {
void register(BundleContext context) {
context.registerService(ModifyHook.class, this, null);
}
public void modifying(ServiceReference<?> reference, Dictionary<String, ?> newProps) {
// TODO - perform some checks here
// throw new IllegalArgumentException("Operation not allowed"); - service properties will not be replaced with newProps in this case
}
}