Hi folks,

here is the code snippet I use with a Struts Plugin to initialize Torque. I tested 
with a Tomcat and Bea Weblogic


/**
 * Tries to load the Torque configuration file using a file or as a resource via
 * classloader
 * 
 * @param servletContext Used to determine the absolute path
 * @param fileName The name of the configuration file to use
 * @return The Torque configuration file
 * @throws IOException Failed to load the Configuration
 */
private Configuration getTorqueConfiguration( ServletContext servletContext, String 
fileName )
    throws Exception
{
    // Do some diagnostic output

    this.getLogger().info( "Running on the following servlet container : " + 
servletContext.getServerInfo() );

    // Handle a missing configuration

    if( (fileName == null) || (fileName.length() == 0) )
    {
        this.getLogger().info( "There was no value for the TORQUE config file defined" 
);           
        fileName = "/WEB-INF/conf/Torque.properties";
    }

    // Try to load the config file as file from an exploded WAR or
    // from the classpath

    String tempFileName = servletContext.getRealPath(fileName);

    this.getLogger().info( "Using the following configuration file : " + tempFileName 
);

    if( ( tempFileName != null) )
    {
        File tempFile = new File( tempFileName );

        if( tempFile.exists() )
        {
            this.getLogger().info( "Try to load the following file using the file 
system : " + tempFile.getAbsolutePath() );
            return new PropertiesConfiguration( tempFile.getAbsolutePath() );       
        }
    }

    this.getLogger().info( "Try to load the following resource using a class loader: " 
+ fileName );
    return new ClassPropertiesConfiguration( this.getClass(), fileName );              
 
}


-----Original Message-----
From: Gary Shea [mailto:[EMAIL PROTECTED]
Sent: Wednesday, January 28, 2004 10:17 PM
To: Apache Torque Users List
Subject: RE: Web Applications


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 ([EMAIL PROTECTED]) 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:[EMAIL PROTECTED]
> 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]
> ������ҡ��졼�����ե��å��塦����ѥ�
> [EMAIL PROTECTED]
> Tel:03-5436-9980 Fax:03-5436-9126
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to