restlet/restlet-framework-java — pull request #765 by Raniz85 (2013-07-09 10:43:40+00:00)
Title: Changing the Finder to use a factory for creating resources to allow control over object instantiation

I make heavy use of Guice for dependency injection in our projects and I had previously implemented this in a way similar to how the guice extension is implemented.

When I started using Authorizer and Authenticator, however, everything fell apart since I can't control how the Authorizer and Authenticator creates their finders without overriding those methods in every subclass. This mean that resources set after an Authorizer or Authenticator caused errors since their constructors relied on dependency injection in the constructors.

I needed a different solution and the best one seemed to be to allow modification of how the Finder instantiates new instances of resources.

I added an ObjectFactory interface with a default implementation that works exactly as the old Finder. The object factory is stored in the Context and the Finder fetches it from there whenever a new instance is required. I had to modify ChildContext to defer to it's parent if an explict object factory hadn't been set on the child context, otherwise child contexts created before the object factory is set would not inherit the custom object factory.

The changes required to implement this are light and previous functionality is preserved while still allowing users of Restlet to take control of object instantiation and either handle it themselves or passing control over to an external framework such as Guice by providing their own implementation of ObjectFactory.

Here's an example on how I use this:
```java
@SuppressWarnings("unchecked")
@Override
public Router createInboundRoot()
{
// Change the object factory to the Guice implementation
getContext().setObjectFactory(injector.get(GuiceObjectFactory.class));

Router router = new Router(getContext());

AddressAuthorizer authorizer = new AddressAuthorizer.Builder()
.denyAll()
.allow("127.0.0.1/24")
.build();

// This is very important, otherwise Guice injecton won't work
// for resources protected by the authorizer
authorizer.setContext(router.getContext());

authorizer.setNext(HelloWorldResource.class);
router.attach("/", authorizer);

return router;
}
```