All reasonable advice, but if you're going to be doing this a lot, I highly
recommend using a dependency injection framework rather than explicit
singletons. I use Guice, but many people have reported great things of
Spring.

If you need to have your resource injected with dependencies that might be
singletons (and if you're willing to use Guice), you can use the code and
ideas that I've contributed to the Guice extension for Restlet. See
http://wiki.restlet.org/developers/257-restlet/284-restlet.html for more
information. This will let you write things like this:

  public MyServerResource extends ServerResource {
      @Inject public MyServerResource(DbSessionManager sessionMgr) {
          this.sessionMgr = sessionMgr;
      }
      // ... service methods here using sessionMgr ...
      private final DbSessionManager sessionMgr;
  }

This gives you flexibility for later changing the scope of the session
manager.

--tim


On Sun, Jan 2, 2011 at 8:34 PM, Fabian Mandelbaum <[email protected]>wrote:

> You are welcomed Anthony. Here you have some 'general' singleton
> coding that works 'everywhere' in Java:
>
> public class MySingleton {
>
>  private static class MySingletonHolder {
>    private static final MySingleton INSTANCE = new MySingleton();
>  }
>
>  private MySingleton() {
>    // You'll do all initialization stuff, for example DB pools, here
>  }
>
>  public static MySingleton getInstance() {
>    return MySingletonHolder.INSTANCE;
>  }
>
>  // Rest of the needed methods here (for example, to access the DB pools)
>
> }
>
> This coding uses William Pugh's "initialization on demand holder
> idiom" (no need for volatile/synchronized)
>
> Good luck.
>
> On Sun, Jan 2, 2011 at 8:06 AM, Anthony <[email protected]> wrote:
> > Fabian,
> >
> > Being determined to accomplish the task via one of the methods I outlined
> in original post I completely neglected to consider this approach.
> >
> > I'll give the Singleton a go as time is slipping through my fingers.
> >
> > Thanks Fabian!!!
> >
> > ------------------------------------------------------
> >
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2695430
> >
>
>
>
> --
> Fabián Mandelbaum
> IS Engineer
>
> ------------------------------------------------------
>
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2695561
>

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2695565

Reply via email to