Registering a new Adapter to the HttpAutomationClient (BUG?)

Hello everyone,

I'm having an issue while registering a new adapter to the HttpAutomationClient.

The particularity is that my adapter extends the Nuxeo's DocumentService adapter.

My issue is when I call the session.getAdapter(DocumentServiceEnhanced.class) method, I randomly get some ClassCastException because the method returns the DocumentService and not my DocumentServiceEnhanced.

By randomly I mean :
When I start my webApp, I try a call. If it fails, I restart my webApp cross-fingering next start will be ok.

After some investigations, I figured out why :

In one hand, in the org.nuxeo.ecm.automation.client.AdapterManager class, all factories are stored in a HashSet. This makes the iteration of factories stable at runtime, but not the same at every start of the app.

In the other hand, the factoryAccept(AdapterFactory factory, Class adapterType) method returns clazz.isAssignableFrom(adapterType) to check if the factory can accept the asked type.
But if the AdapterManager iterate on the DocumentServiceFactory before my DocumentServiceEnhancedFactory, clazz.isAssignableFrom(adapterType) returns true, and I get my ClassCastException. Then I have to restart my webApp, hoping the order will be inverted at the initialisation / registration of my factory.

But all of it makes me question my code, I mean you know, am I wrong by extending DocumentService ? I really like it, really accessible for developpers, still considering it as a DAO layer, not implementing the business yet, just convinience like make a folder with a title, create a document with a blob attached, all that stuff calling a single service on the business Layer.

Do you, Nuxeo experts, think I should change my approach ?

Some of my DocumentServiceEnhanced :

import org.nuxeo.ecm.automation.client.adapters.DocumentService;

public class DocumentServiceEnhanced extends DocumentService {
    public DocumentServiceEnhanced(Session session) {
        super(session);
    }
}

My Factory :

import org.nuxeo.ecm.automation.client.AdapterFactory;

public class DocumentServiceEnhancedFactory implements AdapterFactory<DocumentServiceEnhanced> {
    @Override
    public DocumentServiceEnhanced getAdapter(Session session,
                        Class<DocumentServiceEnhanced> clazz) {
        return new DocumentServiceEnhanced(session);
    }
}

How I wan't it to be :

private void makeDocumentServiceEnhanced() {
    automationClient.registerAdapter(new DocumentServiceEnhancedFactory());
}

protected DocumentServiceEnhanced getDocumentServiceEnhanced() {
    return automationClient.getAdapter(session, DocumentServiceEnhanced.class);
}

How I do it to avoid the bug :

private void makeDocumentServiceEnhanced() {
    // DO NOTHING
}

protected DocumentServiceEnhanced getDocumentServiceEnhanced() {
    return new DocumentServiceEnhanced(session);
}

Thanks in advance for your time !
Best regards,
Nicolas

P.S. English is not my native language, plz don't grammar nazi me ^^

0 votes

0 answers

826 views

ANSWER