package net.peierls.restlet.example;

import net.peierls.restlet.util.*;

import com.google.inject.*;
import static com.google.inject.name.Names.named;
import org.restlet.*;

public class FirstStepsApplication extends Application implements Module {

    public FirstStepsApplication(Context parentContext) {
        super(parentContext);
    }

    private boolean EXPLICIT_INJECTOR = false;

    @Override public synchronized Restlet createRoot() {
        FinderFactory factory = EXPLICIT_INJECTOR ? // Two ways to do this...

            // (1) Explicit Injector creation:
            RestletGuice.createInjector(this).getInstance(FinderFactory.class) :

            // (2) Use a special module that is also a FinderFactory and
            //     that automatically creates the Injector when needed:
            new FinderFactoryModule(this);

        Finder finder = factory.finderFor(Key.get(Handler.class, HelloWorld.class));

        Router router = new Router(getContext());
        router.attachDefault(finder);
        return router;
    }

    public void configure(Binder binder) {
        binder.bind(Handler.class)
            .annotatedWith(HelloWorld.class)
            .to(HelloWorldResource.class);

        binder.bindConstant()
            .annotatedWith(named(HelloWorldResource.HELLO_MSG))
            .to("Hello, Restlet-Guice!");
    }
}
