I would recommend against using a singleton for application state - servlets
can be unloaded by their container, and singletons won't be - can lead to a
mess especially if your singleton is in a different jar/classloader from the
servlet.
Richard
----- Original Message -----
From: "Mind Bridge" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, October 22, 2002 8:57 PM
Subject: Re: [Tapestry-developer] Global References
>
> Just to add:
> In addition to what Malcom suggested, you can get the servlet context in
this way as well:
> cycle.getRequestContext().getServlet().getServletContext()
>
> and another approach is to use a singleton class outside the Servlet
hierarchy.
>
> Best regards,
>
> -mb
>
>
>
> Malcolm Edgar <[EMAIL PROTECTED]> wrote:Subclassing the Engine is
a good place for global references. However
> remember the Engine is serialized and stored in the user's HttpSession, so
> you may want to uses transient in some cases, e.g.
>
> public class MyEngine extends SimpleEngine {
>
> private transient PersistenceManager persistenceManager;
>
> public synchronized PersistenceManager getPersistenceManager() {
> if (persistenceManager == null) {
> persistenceManager = new PersistenceManager();
> }
> return persistenceManager;
> }
>
> private static final SimpleDateFormat MY_DATE_FORMAT =
> new SimpleDateFormat("dd MM yyyy");
>
> public DateFormat getDateFormat() {
> return MY_DATE_FORMAT;
> }
> }
>
> Alternatively you can store global objects in the ServletContext. To
access
> the ServletContext override the Engine.service() method:
>
> public class MyEngine extends SimpleEngine {
>
> private static final String PM_KEY = "MyEngine.PersistenceManager";
>
> private transient PersistenceManager persistenceManager;
>
> /** @see IEngine#service(RequestContext) */
> public boolean service(RequestContext context) throws
> ServletException, IOException {
>
> // If not initialized lookup in ServletContext
> if (persistenceManager == null) {
> ServletContext servletContext =
> context.getServlet().getServletContext();
>
> persistenceManager = servletContext.getAttribute(PM_KEY);
>
> // If not found, create and add to ServletContext
> if (persistenceManager == null) {
> persistenceManager = new PersistenceManager();
> servletContext.setAttribute(PM_KEY, persistenceManager);
> }
> }
>
> super.service(context);
> }
>
> public PersistenceManager getPersistenceManager() {
> return persistenceManager;
> }
> }
>
>
> regard Malcolm Edgar
>
> >From: Eric Everman
> >To: [EMAIL PROTECTED]
> >Subject: [Tapestry-developer] Global References
> >Date: Mon, 21 Oct 2002 21:09:22 -0500
> >
> >I'm still coming up to speed on Tapestry and I have a general question:
> >Where can I store a global reference that can be accessed by all Visit
> >objects? I need to store a shared reference to a Hibernate SessionFactory
> >so that all Visit objects can access the persistence layer.
> >
> >Also, is there a good example of the use of ListEdit somewhere (that
> >doesn't involve EJB)?
> >
> >Thanks,
> >
> >Eric Everman
> >
> >
> >
> >-------------------------------------------------------
> >This sf.net emial is sponsored by: Influence the future of Java(TM)
> >technology. Join the Java Community Process(SM) (JCP(SM)) program now.
>
>http://ad.doubleclick.net/clk;4699841;7576298;k?http://www.sun.com/javavote
> >_______________________________________________
> >Tapestry-developer mailing list
> >[EMAIL PROTECTED]
> >https://lists.sourceforge.net/lists/listinfo/tapestry-developer
>
>
> _________________________________________________________________
> Broadband? Dial-up? Get reliable MSN Internet Access.
> http://resourcecenter.msn.com/access/plans/default.asp
>
>
>
> -------------------------------------------------------
> This sf.net emial is sponsored by: Influence the future
> of Java(TM) technology. Join the Java Community
> Process(SM) (JCP(SM)) program now.
>
http://ad.doubleclick.net/clk;4699841;7576298;k?http://www.sun.com/javavote
> _______________________________________________
> Tapestry-developer mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/tapestry-developer
>
>
> ---------------------------------
> Do you Yahoo!?
> Y! Web Hosting - Let the expert host your web site
-------------------------------------------------------
This sf.net emial is sponsored by: Influence the future
of Java(TM) technology. Join the Java Community
Process(SM) (JCP(SM)) program now.
http://ad.doubleclick.net/clk;4699841;7576301;v?http://www.sun.com/javavote
_______________________________________________
Tapestry-developer mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/tapestry-developer