@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
final XmldbURI moduleUri = args[0].toJavaObject(XmldbURI.class);
final ExistXqueryRegistry xqueryRegistry = ExistXqueryRegistry.getInstance();
final RestXqServiceRegistry registry = RestXqServiceRegistryManager.getRegistry(getContext().getBroker().getBrokerPool());
Sequence result = Sequence.EMPTY_SEQUENCE;
try {
if(isCalledAs(qnRegisterModule.getLocalName())) {
final DocumentImpl module = getContext().getBroker().getResource(moduleUri, Permission.READ);
if(xqueryRegistry.isXquery(module)) {
try {
final List<RestXqService> resourceFunctions = xqueryRegistry.findServices(getContext().getBroker(), module);
xqueryRegistry.registerServices(getContext().getBroker(), resourceFunctions);
result = (NodeValue)org.exist.extensions.exquery.restxq.impl.xquery.RegistryFunctions.serializeRestXqServices(context.getDocumentBuilder(), resourceFunctions).getDocumentElement();
} catch(final ExQueryException exqe) {
LOG.warn(exqe.getMessage(), exqe);
result = Sequence.EMPTY_SEQUENCE;
}
} else {
result = Sequence.EMPTY_SEQUENCE;
}
} else if(isCalledAs(qnDeregisterModule.getLocalName())) {
final DocumentImpl module = getContext().getBroker().getResource(moduleUri, Permission.READ);
if(xqueryRegistry.isXquery(module)) {
final List<RestXqService> deregisteringServices = new ArrayList<RestXqService>();
for(final RestXqService service : registry) {
if(XmldbURI.create(service.getResourceFunction().getXQueryLocation()).equals(moduleUri)) {
deregisteringServices.add(service);
}
}
xqueryRegistry.deregisterServices(getContext().getBroker(), moduleUri);
result = (NodeValue)org.exist.extensions.exquery.restxq.impl.xquery.RegistryFunctions.serializeRestXqServices(context.getDocumentBuilder(), deregisteringServices).getDocumentElement();
} else {
result = Sequence.EMPTY_SEQUENCE;
}
} else if(isCalledAs(qnFindResourceFunctions.getLocalName())) {
final DocumentImpl module = getContext().getBroker().getResource(moduleUri, Permission.READ);
if(xqueryRegistry.isXquery(module)) {
try {
final List<RestXqService> resourceFunctions = xqueryRegistry.findServices(getContext().getBroker(), module);
xqueryRegistry.deregisterServices(getContext().getBroker(), moduleUri);
result = (NodeValue)org.exist.extensions.exquery.restxq.impl.xquery.RegistryFunctions.serializeRestXqServices(context.getDocumentBuilder(), resourceFunctions).getDocumentElement();
} catch(final ExQueryException exqe) {
LOG.warn(exqe.getMessage(), exqe);
result = Sequence.EMPTY_SEQUENCE;
}
} else {
result = Sequence.EMPTY_SEQUENCE;
}
} else if(isCalledAs(qnRegisterResourceFunction.getLocalName())) {
final String resourceFunctionIdentifier = args[1].getStringValue();
final DocumentImpl module = getContext().getBroker().getResource(moduleUri, Permission.READ);
if(xqueryRegistry.isXquery(module)) {
final SignatureDetail signatureDetail = extractSignatureDetail(resourceFunctionIdentifier);
if(signatureDetail != null) {
try {
final RestXqService serviceToRegister = findService(xqueryRegistry.findServices(getContext().getBroker(), module).iterator(), signatureDetail);
if(serviceToRegister != null) {
xqueryRegistry.registerServices(context.getBroker(), Collections.singletonList(serviceToRegister));
result = BooleanValue.TRUE;
} else {
result = BooleanValue.FALSE;
}
} catch(final ExQueryException exqe) {
LOG.warn(exqe.getMessage(), exqe);
result = BooleanValue.FALSE;
}
} else {
result = BooleanValue.FALSE;
}
} else {
result = Sequence.EMPTY_SEQUENCE;
}
} else if(isCalledAs(qnDeregisterResourceFunction.getLocalName())) {
//TODO
final String resourceFunctionIdentifier = args[1].getStringValue();
final SignatureDetail signatureDetail = extractSignatureDetail(resourceFunctionIdentifier);
if(signatureDetail != null) {
final RestXqService serviceToDeregister = findService(xqueryRegistry.registered(context.getBroker()), signatureDetail);
if(serviceToDeregister != null) {
xqueryRegistry.deregisterService(context.getBroker(), serviceToDeregister);
result = BooleanValue.TRUE;
} else {
result = BooleanValue.FALSE;
}
} else {