Hi Sean,
Thanks for detailing your requirements with concrete code. I think you can
already cover your need for application level state (shared by all Resources
and Restlets) by leveraging Context's attributes:
@Override
public Restlet createRoot()
{
// Initialize shared state
getContext().getAttributes().put("myVar1", myVar1);
getContext().getAttributes().put("myVar2", myVar2);
// ...
}
Then, in your Resource you can do
getContext().getAttributes().get("myVar1"). Does that help you?
Best regards,
Jerome
> -----Message d'origine-----
> De : news [mailto:[EMAIL PROTECTED] De la part de Sean Landis
> Envoyé : jeudi 25 janvier 2007 20:09
> À : [email protected]
> Objet : Re: Newbie question on the Resource changes in RC3
>
> Hi Jerome,
>
> Jerome Louvel <contact <at> noelios.com> writes:
> >
> > Hi all,
> >
> > Thanks for exploring this recurring issue. I realize that
> the current
> > approach isn't flexible enough in many case. So, I have
> restored the default
> > constructor and added a new 'init(Context, Request,
> Response) method that
> > gets invoke by the other constructor.
>
> This clearly adds much needed flexibility. Thanks.
>
> > Also, the Finder class will now look for a default
> constructor if the normal
> > one isn't available and will call 'init' after the
> instantiation in this
> > case. This should make everyone happy.
>
> I can't decide if I'm happy based on the description of the
> change. I am
> unclear how this helps my issue. I have application level state the
> resource requires.
> I don't see how the above changes resolve that issue.
>
> One solution would be to add Finder(Context context,
> ResourceFactory factory).
>
> interface ResourceFactory {
> public Resource createResource(Class<? extends Resource>
> targetClass);
> }
>
> That would allow me to say something like:
>
> public class MyApplication extends Application implements
> ResourceFactory {
> private State state = ...
> ...
>
> public Resource createResource(Class<? extends Resource>
> targetClass) {
> // Create the targetClass and initialize it with my state.
> ...
> return r;
> }
>
> Then in Finder, the factory gets called to create an instance
> that I can
> initialize. Then finder calls Resource.init(...).
>
> I'm not sure if this is the best solution but hopefully it makes clear
> the capability I'm interested in.
>
> Sean