db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gary Shea <s...@gtsdesign.com>
Subject RE: Web Applications
Date Wed, 28 Jan 2004 21:17:29 GMT
Sounds like Herr Goeschl has found a way to get commons configuration to
read the properties file directly.  Until it's published somewhere,
here's the relevant part of the initialization servlet I use.  It's
kinda dumb but doesn't require dealing with class loaders.

I've only used it with Tomcat, but I assume (ulp!) it will work with any
servlet container.

Good luck!

        Gary

public void contextInitialized( ServletContextEvent event ) {

    ServletContext context = event.getServletContext();
    
    context.log("Initializing Torque ...");

    try {
        String fileName = context.getInitParameter("torque-properties");
        if (fileName == null) {
            fileName="WEB-INF/torque.properties";
        }
        String realPath = context.getRealPath(fileName);
        if (realPath != null) {
            context.log("Torque properties  " + realPath);
            Torque.init(realPath);
        } else {
            InputStream stream = context.getResourceAsStream(fileName);
            if (stream == null) {
                context.log(
                    "unable to open torque config file <"
                    + fileName
                    + "> as resource stream"
                );
                return;
            }
            
            /*
             * The Apache configuration project doesn't seem like it
             * will work from inside a servlet.  I'm probably wrong...
             * but anyway, I get the properties the old way, and add the
             * contents of the properties to the Configuration object.
             * Yeah, this is ugly as sin...
             */
            Properties properties = new Properties();
            try {
                properties.load(stream);
            } catch (IOException e) {
                context.log(
                    "Caught IOException loading <" + fileName
                        + "> into a Properties object",
                    e
                );
                return;
            }
            
            Configuration configuration = new PropertiesConfiguration();
            Enumeration names = properties.propertyNames();
            while (names.hasMoreElements()) {
                String name = (String) names.nextElement();
                String value = properties.getProperty(name);
                configuration.addProperty(name, value);
            }
            Torque.init(configuration);
        }
    } catch(Exception e){
       context.log("Caught exception: ", e);
    }
}


[2004-01-28 10:22 +0100] Goschl,Siegfried (Siegfried.Goeschl@drei.com) wrote:

> Finding the Torque.properties file also depends on your application server. In the case
of BEA WebLogic I had the same problem with unexploded WAR files. Therefore I used the class
loading incarnation of commons configuration - I can send you my source code to your private
mail account
> 
> If you have more thing to initialize and manage in as servlet (and have a little bit
of spare time) you should look at a service framework such as Turbine or Avalaon.
> 
> Cheers,
> 
> Siegfried Goeschl
> 
> -----Original Message-----
> From: Hassan Abolhassani [mailto:Hassan.Abolhassani@razorfish.co.jp]
> Sent: Tuesday, January 27, 2004 7:33 AM
> To: Apache Torque Users List
> Subject: Re: Web Applications
> 
> 
> >Hey everybody, st00pid newbie here... I've gone through the torque 
> >tutorial successfully and experimented a bit beyond it but now I'm 
> >trying to use torque in a web application for the first time and have 
> >run into some trouble.  I'm confused about the Torque.properties file 
> >and the init() method.  I've placed the Torque.properties file in the 
> >root of my .war, in WEB-INF/, WEB-INF/classes, WEB-INF/lib, just about 
> >everywhere, but when I call init("Torque.properties") it can't find it. 
> >  Where is it supposed to live and how do I reference it?  
> 
> My solution is to have an initializer servlet. It passes a phycial path
> of the properties file to the init(). Attached is a copy of that servlet
> that might be usefull for you. 
> 
> You need to make it called when the servlet engine is started. To do
> that the following snippet in web.xml of your application will work:
> 
>   <servlet>
>     <servlet-name>initializer</servlet-name>
>     <servlet-class>com.utility.Initializer</servlet-class>
> 
>     <init-param>
>       <param-name>log4j-property-file</param-name>
>       <param-value>WEB-INF/classes/log4j.properties</param-value>
>     </init-param>
> 
>     <init-param>
>       <param-name>torque-property-file</param-name>
>       <param-value>WEB-INF/classes/Torque.properties</param-value>
>     </init-param>
> 
>     <load-on-startup>1</load-on-startup>
>   </servlet>
> 
> Please note that the servlet also initializes log4j, which you may want
> to ignore it.
> 
> >Also, do I 
> >call init() in each class that uses Torque or do I need to somehow only 
> >call it once when the web app is deployed?  Sorry if this is a dumb 
> >question, but I can't find anything in the docs to help me.  Thanks 
> >much.
> 
> You only need to call init() one time.
> 
> --Travis Hanna
> 
> ___
> 
> Razorfish Japan, Inc.
> 
> ¥¢¥Ü¥ë¥Ï¥Ã¥µ¥Ë¡¡¥Ï¥Ã¥µ¥ó   Hassan Abolhassani  [Technology network]
> ³ô¼°²ñ¼Ò¡¡¥ì¡¼¥¶¡¼¥Õ¥£¥Ã¥·¥å¡¦¥¸¥ã¥Ñ¥ó
> Hassan.Abolhassani@razorfish.co.jp
> Tel:03-5436-9980 Fax:03-5436-9126
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
> For additional commands, e-mail: torque-user-help@db.apache.org
> 
> 
> 

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


Mime
View raw message