deadbolt

Deadbolt 2.5.0, unable to instantiate custom SubjectPresentHandler extending AbstractDeadboltHandler


I am using deadbolt 2.5.0 and I have created custom SubjectPresentHandler as below:

public class SubjectPresentHandler extends AbstractDeadboltHandler
{
    public SubjectPresentHandler(ExecutionContextProvider ecProvider) {
        super(ecProvider);
    }
    // other required methods
}

And, I also have :

@Singleton
public class CustomDeadboltHandlerCache implements HandlerCache
{
    private final DeadboltHandler defaultHandler = new  SubjectPresentHandler();
   // other required code
}

Now the problem that I am facing here is I cannot instantiate SubjectPresentHandler using its default contructor. I get an error as: "The constructor SubjectPresentHandler is undefined". Now when I add default constructor in SubjectPresentHandler as below:

public SubjectPresentHandler() {
   super();
}

I get an error as: The constructor AbstractDeadboltHandler is undefined. If I try removing the paramaterized constructor in SubjectPresentHandler then I get error message as

"Implicit super constructor AbstractDeadboltHandler() is undefined for default constructor. Must define an explicit constructor".

I am not sure how can I resolve this, thus seeking solution regarding this issue.


Solution

  • The constructor of SubjectPresentHandler takes an ExecutionContextProvider as a parameter. The easiest way to do this is to inject one and have the creation of the handler done via Guice.

    The ExecutionContextProvider is provided by DeadboltModule - you can see this here.

    @Singleton
    public class SubjectPresentHandler extends AbstractDeadboltHandler
    {
        @Inject
        public SubjectPresentHandler(ExecutionContextProvider ecProvider) {
            super(ecProvider);
        }
        // other required methods
    }
    

    You can also inject the handler into the handler cache:

    @Singleton
    public class CustomDeadboltHandlerCache implements HandlerCache
    {
        private final DeadboltHandler defaultHandler;
    
        @Inject
        public CustomDeadboltHandlerCache(final DeadboltHandler defaultHandler) {
            this.defaultHandler = defaultHandler;
        }
       // other required code
    }
    

    If you have multiple handlers, take a look at the documentation for how to handle this.