@Override
public <V> IInvoker<V> hookInvoker(IMethod<V> method) {
return (arguments, context) -> {
try {
return method.invoke(arguments, context);
} catch(Exception e) {
LOGGER.info(e.getMessage());
}
return null;
};
}
how are arguments passed into the callback returned by hookInvoker. for instance if I want to build a hookInvoker that triggers on data object deletes which DataObjectServiceExtensible constant should I use?
DataObjectServiceExtensible.DATA_OBJECT)
DataObjectServiceExtensible.DELETE_BY_DATAOBJECT
DataObjectServiceExtensible.DELETE_BY_DATAOBJECTS
DataObjectServiceExtensible.DELETE_BY_IDS
or others?
If you’re using all of those delete methods in your app, you would need your hook to tolerate all of the methods that start with the word delete. That means the 3 DELETE constants you posted above; plus there are a few more.
If you want to process the args or use them in some way, you need to be aware of what those methods take. There are delete methods that take a String ID (DataObjectServiceExtensible.ID), multiple IDs (DataObjectServiceExtensible.IDS), a query (DataObjectServiceExtensible.DATA_OBJECT_QUERY), a single DataObject (DataObjectServiceExtensible.DATA_OBJECT), or many DataObjects (DataObjectServiceExtensible.DATA_OBJECTS). So your hook would need to handle all of those cases.
If you just want to process the result, though, the hook is much easier to write, because all DataObjectService delete methods return a Set. The example below shows the start of a hook for all delete methods that gives you access to the deleted objects.
public static class DeleteHook implements ISimpleHook {
@Override
public boolean shouldHook(IMethod<?> method) {
return method.getServiceClass().equals(IDataObjectService.class) && method.getMethodType() == MethodType.DELETE;
}
@SuppressWarnings("unchecked")
@Override
public <V> IInvoker<V> hookInvoker(IMethod<V> method) {
return (args, context) -> {
Set<DataObject> deleted = (Set<DataObject>) method.invoke(args, context);
// Do some action
return (V) deleted;
};
}
}
Also note in this example, that in my shouldHook, I specifically am hooking IDataObjectService methods whose MethodType == DELETE. This will actually cause the hook to be applied to all 10 of the delete methods. We just have to be careful when accessing the args object, because there are 5 different types of arguments we might see (id, ids, query, 1 DataObject, many DataObjects). So we would need to use an if-statement to handle that appropriately.
Thanks Mark. this definitely cleared things up
1 Like