You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Elad Messing <El...@sphera.com> on 2004/03/01 15:01:46 UTC

RE: Reading a file in a "BaseEngine" subclass

On the same note - When using the global object (a simple POJO) how can
I get the path to the classpath / context ? According to my tests, when
opening a file , the root path ("/") is really the root path of the
machine. I also tried
ClassLoader.getSystemResourceAsStream("com/sphera/rd..../file.xml")
which didn't do the trick..

Any ideas ?

Thanks

-----Original Message-----
From: Howard M. Lewis Ship [mailto:hlship@comcast.net] 
Sent: Friday, February 27, 2004 1:04 AM
To: 'Tapestry users'
Subject: RE: Reading a file in a "BaseEngine" subclass

>From a general efficiency point of view, you should only serialize the
bare minimum. If you can reconstruct data rather than serialize it, you
should.  Store the id, not the object.  In your case, you can assume
that the same WAR is deployed throughout the cluster.

Additionally, in your case, the engine may not be the right location for
this data, since it is fixed and global ... perhaps the Global object is
the right place.  Your custom BaseEngine subclass can invoke a method on
your custom Global object to allow it to read the XML file into memory.

--
Howard M. Lewis Ship
Independent J2EE / Open-Source Java Consultant Creator, Tapestry: Java
Web Components http://howardlewisship.com


> -----Original Message-----
> From: Elad Messing [mailto:EladM@sphera.com] 
> Sent: Thursday, February 26, 2004 4:00 PM
> To: Tapestry users
> Subject: RE: Reading a file in a "BaseEngine" subclass
> 
> 
>  Well - the whole idea was not to read the file on each request - but
> store it in the engine, and let it sarialized/deserialized 
> the data for
> me (it's a DOM document object). Am I missing the right place for it ?
> What do you mean "You should restore the data" ? Each time I 
> request it
> I should check if its not null, and re-read it then ?
> 
> -----Original Message-----
> From: Howard M. Lewis Ship [mailto:hlship@comcast.net] 
> Sent: Thursday, February 26, 2004 6:54 PM
> To: 'Tapestry users'
> Subject: RE: Reading a file in a "BaseEngine" subclass
> 
> There's a setupForRequest() method you can override for these 
> purposes. 
> 
> http://jakarta.apache.org/tapestry/doc/api/org/apache/tapestry
> /engine/Ab
> stractEngine.html#setupForRe
> quest(org.apache.tapestry.request.RequestContext)
> 
> The engine's lifecycle can be complicated, because it is stored in the
> HttpSession and may be serialized and deserialized about a cluster.
> Whatever data you read from this file should be stored in a transient
> field. You should be prepared to restore that data (in a threadsafe
> manner) on any request.
> 
> --
> Howard M. Lewis Ship
> Independent J2EE / Open-Source Java Consultant Creator, Tapestry: Java
> Web Components http://howardlewisship.com
> 
> 
> > -----Original Message-----
> > From: Elad Messing [mailto:EladM@sphera.com] 
> > Sent: Thursday, February 26, 2004 11:43 AM
> > To: Tapestry users
> > Subject: Reading a file in a "BaseEngine" subclass
> > 
> > 
> > Hi all
> > 	In order to support a GUI "navigation tree" in my App, I wish to
> > read an XML file - that holds the App structure, and then use it in
> > every page.
> > 	So I figured the best way to do it is read it in my "BaseEngine"
> > subclass' constructor, and store it there.
> > 
> > 	Now - the XML file sits in sub-dir in the context dir, and I
> > cant figure out how to get its full path (in order to read 
> it) in the
> > "BaseEngine" scope. I tried "getServletPath" and 
> > "getContextPath" - but
> > they are both null.
> > 
> > 	I saw that the cycle class has
> > "getRequestContext().getServlet().getServletContext()" from 
> > which I can
> > get a "ContextResourceLocation". How can I get this - in the
> > "BaseEngine" scope ?
> > 
> > 
> > Thanks
> > 
> > Elad Messing
> > 
> > Sphera Tech. LTD
> >  
> <http://www.sphera.com/demosite/index.htm>
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re[2]: Reading a file in a "BaseEngine" subclass

Posted by Denis Ponomarev <oz...@romsat.ua>.
EM> On the same note - When using the global object (a simple POJO) how can
EM> I get the path to the classpath / context ? According to my tests, when
EM> opening a file , the root path ("/") is really the root path of the
EM> machine. I also tried
EM> ClassLoader.getSystemResourceAsStream("com/sphera/rd..../file.xml")
EM> which didn't do the trick..

EM> Any ideas ?

Try this:

InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(file);
if (stream == null) {
        stream = getClass().getResourceAsStream(file);
}




---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


RE: Reading a file in a "BaseEngine" subclass

Posted by Petter Måhlén <pe...@elevance.se>.
I use RequestContext.createSession().getServletContext() to get at the
ServletContext. From that, I can use the getResourceAsStream() method. That
is part of the servlet stuff, not the class loader, maybe that's a way that
you could also use. The RequestContext is a parameter to the createGlobal
method, by the way.

/ Petter

> -----Original Message-----
> From: Elad Messing [mailto:EladM@sphera.com] 
> Sent: den 1 mars 2004 15:02
> To: Tapestry users
> Subject: RE: Reading a file in a "BaseEngine" subclass
> 
> 
> On the same note - When using the global object (a simple 
> POJO) how can
> I get the path to the classpath / context ? According to my 
> tests, when
> opening a file , the root path ("/") is really the root path of the
> machine. I also tried
> ClassLoader.getSystemResourceAsStream("com/sphera/rd..../file.xml")
> which didn't do the trick..
> 
> Any ideas ?
> 
> Thanks
> 
> -----Original Message-----
> From: Howard M. Lewis Ship [mailto:hlship@comcast.net] 
> Sent: Friday, February 27, 2004 1:04 AM
> To: 'Tapestry users'
> Subject: RE: Reading a file in a "BaseEngine" subclass
> 
> From a general efficiency point of view, you should only serialize the
> bare minimum. If you can reconstruct data rather than 
> serialize it, you
> should.  Store the id, not the object.  In your case, you can assume
> that the same WAR is deployed throughout the cluster.
> 
> Additionally, in your case, the engine may not be the right 
> location for
> this data, since it is fixed and global ... perhaps the 
> Global object is
> the right place.  Your custom BaseEngine subclass can invoke 
> a method on
> your custom Global object to allow it to read the XML file 
> into memory.
> 
> --
> Howard M. Lewis Ship
> Independent J2EE / Open-Source Java Consultant Creator, Tapestry: Java
> Web Components http://howardlewisship.com
> 
> 
> > -----Original Message-----
> > From: Elad Messing [mailto:EladM@sphera.com] 
> > Sent: Thursday, February 26, 2004 4:00 PM
> > To: Tapestry users
> > Subject: RE: Reading a file in a "BaseEngine" subclass
> > 
> > 
> >  Well - the whole idea was not to read the file on each 
> request - but
> > store it in the engine, and let it sarialized/deserialized 
> > the data for
> > me (it's a DOM document object). Am I missing the right 
> place for it ?
> > What do you mean "You should restore the data" ? Each time I 
> > request it
> > I should check if its not null, and re-read it then ?
> > 
> > -----Original Message-----
> > From: Howard M. Lewis Ship [mailto:hlship@comcast.net] 
> > Sent: Thursday, February 26, 2004 6:54 PM
> > To: 'Tapestry users'
> > Subject: RE: Reading a file in a "BaseEngine" subclass
> > 
> > There's a setupForRequest() method you can override for these 
> > purposes. 
> > 
> > http://jakarta.apache.org/tapestry/doc/api/org/apache/tapestry
> > /engine/Ab
> > stractEngine.html#setupForRe
> > quest(org.apache.tapestry.request.RequestContext)
> > 
> > The engine's lifecycle can be complicated, because it is 
> stored in the
> > HttpSession and may be serialized and deserialized about a cluster.
> > Whatever data you read from this file should be stored in a 
> transient
> > field. You should be prepared to restore that data (in a threadsafe
> > manner) on any request.
> > 
> > --
> > Howard M. Lewis Ship
> > Independent J2EE / Open-Source Java Consultant Creator, 
> Tapestry: Java
> > Web Components http://howardlewisship.com
> > 
> > 
> > > -----Original Message-----
> > > From: Elad Messing [mailto:EladM@sphera.com] 
> > > Sent: Thursday, February 26, 2004 11:43 AM
> > > To: Tapestry users
> > > Subject: Reading a file in a "BaseEngine" subclass
> > > 
> > > 
> > > Hi all
> > > 	In order to support a GUI "navigation tree" in my App, I wish to
> > > read an XML file - that holds the App structure, and then 
> use it in
> > > every page.
> > > 	So I figured the best way to do it is read it in my "BaseEngine"
> > > subclass' constructor, and store it there.
> > > 
> > > 	Now - the XML file sits in sub-dir in the context dir, and I
> > > cant figure out how to get its full path (in order to read 
> > it) in the
> > > "BaseEngine" scope. I tried "getServletPath" and 
> > > "getContextPath" - but
> > > they are both null.
> > > 
> > > 	I saw that the cycle class has
> > > "getRequestContext().getServlet().getServletContext()" from 
> > > which I can
> > > get a "ContextResourceLocation". How can I get this - in the
> > > "BaseEngine" scope ?
> > > 
> > > 
> > > Thanks
> > > 
> > > Elad Messing
> > > 
> > > Sphera Tech. LTD
> > >  
> > <http://www.sphera.com/demosite/index.htm>
> > 
> > 
> > 
> > 
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: 
> tapestry-user-help@jakarta.apache.org
> > 
> > 
> > 
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: 
> tapestry-user-help@jakarta.apache.org
> > 
> > 
> > 
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: 
> tapestry-user-help@jakarta.apache.org
> > 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org