tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From JensStu...@quidinfo.it
Subject Re: Re[2]: (argh!) RE: Location of application-specific properties files
Date Thu, 08 Aug 2002 08:23:48 GMT

Obviously, i did NOT try your examples - i just tried to answer the
question of Bob (who seems to be satisfied, he does not ask any more),
trying to clear things. Essentially our two answers are the same: usa a
classloader - the confusion was about, where to put the file to be found by
the classloader.

Regards,

Jens Stutte




                                                                                         
           
                    Jacob Kjome                                                          
           
                    <hoju@visi.co        To:     "Tomcat Users List"                  
              
                    m>                   <tomcat-user@jakarta.apache.org>, JensStutte@quidinfo.it
   
                                         cc:                                             
           
                    07/08/2002           Subject:     Re[2]: (argh!) RE: Location of     
           
                    20.19                application-specific properties files           
           
                    Please                                                               
           
                    respond to                                                           
           
                    "Tomcat Users                                                        
           
                    List"                                                                
           
                                                                                         
           
                                                                                         
           




Hello JensStutte,

Ahhh.... I think you must have misunderstood what I was saying.  I am
*not* saying that you would put a class inside WEB-INF.  I'm saying
that if you had servlet specific configuration info, you'd be better
off putting the configuration file inside WEB-INF and accessing it
with the servlet specific
load(getServletContext().getResourceAsStream
("/WEB-INF/myprops.properties")).

I only pointed that out to make the distinction between loading the
properties off a file path and loading it off a class loader like the
next 3 examples would do.  The 3rd example finding the class in any
of the classloaders provided by Tomcat (except the container specific
classloader) or, if the app isn't running in Tomcat, any classloaders
available to it.

So, to reiterate, the 3 examples following the talk about the
getServletContext().... example are *totally* independent of servlets.


Did you even try them?  I guarantee they work.

Jake


Wednesday, August 07, 2002, 2:02:57 AM, you wrote:


Jqi> I did never intend, that you have to include the WEB-INF or classes
part
Jqi> into the path. The servlet spec says, that the folder
"WEB-INF/classes"
Jqi> will be mounted into the application classpath as well as every jar in
Jqi> "WEB-INF/lib". So a properties file named MyProp.properties placed in
the
Jqi> "WEB-INF/classes" folder (or in any of the mounted jars) will be found
from
Jqi> inside your application with the simple path "MyProp" or if you use
Jqi> getResourceAsStream() with "MyProp.properties". No folders, no magic,
no
Jqi> servlet dependency. It is the same process like finding a class - and
a
Jqi> class should never reside directly in the "WEB-INF" folder.

Jqi> Regards,

Jqi> Jens Stutte




Jqi>                     Jacob Kjome

Jqi>                     <hoju@visi.co        To:     "Tomcat Users List"

Jqi>                     m>
<tomcat-user@jakarta.apache.org>
Jqi>                                          cc:

Jqi>                     07/08/2002           Subject:     Re: (argh!) RE:
Location of
Jqi>                     05.49                application-specific
properties files
Jqi>                     Please

Jqi>                     respond to

Jqi>                     "Tomcat Users

Jqi>                     List"








Jqi> Do you literally have the filename as
Jqi> "/WEB-INF/classes/myprops.properties"?

Jqi> The only thing that knows about that is the ServletContext.  If you
were to

Jqi> use that method you'd be better off storing your config file in
Jqi> "WEB-INF" and doing:

Jqi> load(getServletContext().getResourceAsStream
Jqi> ("/WEB-INF/myprops.properties"))


Jqi> Since you want to avoid this so that your app is not servlet specific,
you
Jqi> can do the following....

Jqi> load(CurrentClass.class.getResourceAsStream("/myprops.properties"));

Jqi> or

Jqi> load(CurrentClass.class.getClassloader().getResourceAsStream
Jqi> ("myprops.properties"));

Jqi> or

Jqi> load(Thread.currentThread().getContextClassLoader
().getResourceAsStream
Jqi> ("myprops.properties"));


Jqi> Notice in all the cases where you are looking in the
classpath/classloader,

Jqi> you do not reference "WEB-INF" or the "classes" directory within
Jqi> WEB-INF.  The classloader doesn't know about those.  The
ServletContext, on

Jqi> the other hand, knows about everything inside the root of the current
Jqi> webapp.

Jqi> That should solve things.

Jqi> Jake

Jqi> At 09:22 PM 8/6/2002 -0400, you wrote:
>>Jens,
>>
>>Thanks for the reply.  I understand what you're saying, and I wish it was
>>working for me, but it doesn't.  The ONLY way I've been able to get this
Jqi> to
>>work is by using the servlet context method as mentioned in a previous
Jqi> post,
>>but that's NOT the way I want this to ultimately work.
>>
>>I want to make it as generic as possible without having to rely on
passing
>>in a servlet context to my class.  For example, what if I need access to
>>that file in an application that's not using a servlet?
>>
>>If I have the filename as "/WEB-INF/classes/myprops.properties", I have
>>tried this in one of four different ways so far:
>>
>>1. load(new FileInputStream(filename));
>>2.
>>load(Thread.currentThread().getContextClassLoader
Jqi> ().getResourceAsStream(file
>>name));
>>3. load(this.getClassLoader().getResourceAsStream(filename));
>>4. load(getServletContext().getResourceAsStream(filename));
>>
>>All of them fail for not finding the file except #4, but that's not the
>>optimal solution as mentioned previously.
>>
>>As an aside, I pulled the "java.class.path" from the System properties to
>>see where it's pointing, and all it has in it is
>>"<CATALINA_HOME>\bin\bootstrap.jar" with no other paths included.
>>
>>Thanks,
>>Bob
>>
>>
>>-----Original Message-----
>>From: JensStutte@quidinfo.it [mailto:JensStutte@quidinfo.it]
>>Sent: Tuesday, August 06, 2002 9:56 AM
>>To: Tomcat Users List
>>Subject: Re: Location of application-specific properties files
>>
>>You must load your properties through some Classloader that is able to
>>locate your properties file.  For example you can use the class
>>PropertyResourceBundle for a real properties file or if you want to
access
>>some own file format, just call the Thread.getCurrentThread
>>().getContextClassLoader().getResourceAsStream(...) function. Then the
>>WEB-INF/classes folder or some of your application specific .jar files in
>>WEB-INF/lib (if any) is the right place for it (and as far as i know, it
Jqi> is
>>the only standard way, which assures you to find a specific file on every
>>standard J2EE server).
>>
>>
>>
>>--
>>To unsubscribe, e-mail:   <
Jqi> mailto:tomcat-user-unsubscribe@jakarta.apache.org>
>>For additional commands, e-mail: <
Jqi> mailto:tomcat-user-help@jakarta.apache.org>





Jqi> --
Jqi> To unsubscribe, e-mail:   <
mailto:tomcat-user-unsubscribe@jakarta.apache.org>
Jqi> For additional commands, e-mail: <
mailto:tomcat-user-help@jakarta.apache.org>



--
Best regards,
 Jacob                            mailto:hoju@visi.com


--
To unsubscribe, e-mail:   <
mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <
mailto:tomcat-user-help@jakarta.apache.org>






--
To unsubscribe, e-mail:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-user-help@jakarta.apache.org>


Mime
View raw message