Re: Global Information Store between resources

Hi,

> Karen Cheng writes:
>  > >  And the in your resources, use:
>  > >
>  > >  Resource r = SharedContext.getSomeResource();
>  > >  r.getValue("some-attribute");
>  > 
>  > Could you please give an example of how one can restore the resource
>  > out of its store in the getSomeResource() method above?   That is,
>  > could you please fill in the code in the method?
> 
> It really depends on a number of things. The simplest way is to assume
> that th resource is in the same resource store as the calling
> resource. In this case:
> 
> public class MyResource ... {
> 
>     public Resource getSomeResource(String name) {
>         // Get the container resource:
>         Resource parent = getParent();
>         // Lookup the shared context resource within that container:
>         if ( ! ( resource instanceof COntainerResource) )
>             throw new Exception();
>         return parent.lookup(name);
>     }
> }
> 
> Then, before using MyResource you should make sure to create the
> shared context resource in the store (ie either when initializing
> MyResource, or by using the /Admin/Editor)
> 
> Anselm.

Using getParent() within getSomeResource() means that this method
can't be made static, and so we'd have to create an instance of
MyResource before we can get the shared context resource, right?

I have a GlobalResource class which registers a number of global 
attributes, and then I have the following ResourceManager class (which
is your MyResource class):

public class ResourceManager extends HTTPResource {
  static GlobalResource gr;

  public ResourceManager() {}

  public synchronized GlobalResource getGlobalResource() {
    if (gr == null) {
      // Get the container resource.
      Resource parent = getParent();
      // Lookup the global resource within that container.
      gr = (GlobalResource) ((DirectoryResource) parent).lookup("globalResource\
");
    }
    return gr;
  }
}

However, when I tried to use it in the constructor of one of my other 
classes, I get a null pointer exception from getGlobalResource():

  public MyNewResource() {
    ResourceManager rm = new ResourceManager();
    GlobalResource gr = rm.getGlobalResource();

    String attr = gr.getAttr();
	:	:	:
  }

Note that I've used Admin/Editor to create "globalResource" under the User
directory, which is where all my other resources were created.

Could you please tell me what's wrong?  Why didn't lookup() load the
global resource when it couldn't find it in the resource store?

Thanks,
Karen

Received on Tuesday, 28 January 1997 03:21:51 UTC