You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Sébastien LABEY <se...@gmail.com> on 2006/12/13 23:47:17 UTC

[S2] loading another configuration file

Hi all,

I'd like to load a file containing the parameters to connect to DB at the
application startup. In the S2 documentation I read about the "key
initialization parameters" (http://struts.apache.org/2.x/docs/webxml.html)
and so I add the 2 lines <init-param> to my web.xml like this :
<filter>
        <filter-name>action2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
        <init-param>
                <param-name>config</param-name>
                <param-value>appConfig.xml</param-value>
        </init-param>
        <init-param>
                <param-name>configProviders</param-name>
                <param-value>
app.core.configuration.ApplicationConfigurationProvider</param-value>
        </init-param>
</filter>

The appConfig.xml is the file where I have my configuration, it is supposed
to be at the root (/WEB-INF/), and the
app.core.configuration.ApplicationConfigurationProvider is the class
implementing ConfigurationProvider interface as said in the doc.
In the init() method of the Provider, I just add "System.out.println("--Init
plugin begin--");" but it never appears in the log, and I cannot see
anything neither about the xml file nor the Provider.

Maybe, I'm totally wrong and there is a better way to load the file where I
configure my DB connection?

I'm a little confused, so any help would be appreciated.

Thanks in advance.

Sebastien

Re: [S2] loading another configuration file

Posted by Sébastien LABEY <se...@gmail.com>.
Hi Don,

Until now, I configured the datasource through the <resource> tag in the
context.xml file (in the META-INF directory of my app) and through the
web.xml with the <resource-ref> tag like this :

context.xml :
<Resource     name="jdbc/myDatabase"
                     auth="Container"
                     type="javax.sql.DataSource"
                     username="user"
                     password="password"
                     driverClassName="org.postgresql.Driver"
                     url="jdbc:postgresql://localhost:5432/test"
                     maxActive="8" maxIdle="4"/>

web.xml :
<resource-ref>
              <description/>
              <res-ref-name>jdbc/myDatabase</res-ref-name>
              <res-type>javax.sql.DataSource</res-type>
              <res-auth>Container</res-auth>
</resource-ref>

Then I was able to get connections by getting an access to JNDI with
something like :
DataSource myDS = new
InitialContext().lookup("java:comp/env/jdbc/myDatabase");

The thing that I don't like in this way of configuring my DB access is that
the name of the resource "myDatabase" is hardcoded. So in fact I would like
to specify the name of the resource in a file (web.xml ?) instead of having
it hard coded. But, I'm wondering if this is the right way of configuring?
Could you explain me how to do this the right way?
Thanks very much

Sebastien



On 12/14/06, Don Brown <do...@gmail.com> wrote:
>
> One word, eh? ServletContextListener.  Use a ServletContextListener to
> load your properties files and pass the name to it via servlet context
> parameter.  There are ways to use Struts classes to initialize and
> load the data, but I wouldn't recommend it when there are solutions
> that are just as effective, but don't rely on Struts.
>
> See also http://www.onjava.com/pub/a/onjava/2001/04/12/listeners.html
> or google for ServletContextListener
>
> BTW, this is the approach Spring, MyFaces, and I imagine many other
> frameworks and applications use to load their data on startup.
>
> Don
>
> On 12/13/06, Sébastien LABEY <se...@gmail.com> wrote:
> > Thank you Don,
> >
> > In fact, I don't want to have the name of my properties file to be hard
> > coded... So I thought about the "key initialization parameters" that
> allowed
> > me to set the name of the file and the Java class to load it.
> > Do you think it is a right way to do it, or maybe should I think about a
> > Listener like in the MailReader app to load the database?
> > In one word, what's the best way to do it?
> >
> > Thanks
> >
> > On 12/13/06, Don Brown <do...@gmail.com> wrote:
> > >
> > > If you just need to load a few properties, you shouldn't have to touch
> > > Struts at all.  Just create a file, say,
> > > WEB-INF/classes/db.properties, then load it in your Java code via:
> > >
> > > Properties props = new Properties();
> > > props.load(this.getClass
> > > ().getClassLoader().getResourceAsStream("/db.properties");
> > >
> > > and away you go...
> > >
> > > Don
> > >
> > > On 12/13/06, Sébastien LABEY <se...@gmail.com> wrote:
> > > > Hi all,
> > > >
> > > > I'd like to load a file containing the parameters to connect to DB
> at
> > > the
> > > > application startup. In the S2 documentation I read about the "key
> > > > initialization parameters" (
> > > http://struts.apache.org/2.x/docs/webxml.html)
> > > > and so I add the 2 lines <init-param> to my web.xml like this :
> > > > <filter>
> > > >         <filter-name>action2</filter-name>
> > > >         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher
> > > > </filter-class>
> > > >         <init-param>
> > > >                 <param-name>config</param-name>
> > > >                 <param-value>appConfig.xml</param-value>
> > > >         </init-param>
> > > >         <init-param>
> > > >                 <param-name>configProviders</param-name>
> > > >                 <param-value>
> > > > app.core.configuration.ApplicationConfigurationProvider
> </param-value>
> > > >         </init-param>
> > > > </filter>
> > > >
> > > > The appConfig.xml is the file where I have my configuration, it is
> > > supposed
> > > > to be at the root (/WEB-INF/), and the
> > > > app.core.configuration.ApplicationConfigurationProvider is the class
> > > > implementing ConfigurationProvider interface as said in the doc.
> > > > In the init() method of the Provider, I just add "System.out.println
> > > ("--Init
> > > > plugin begin--");" but it never appears in the log, and I cannot see
> > > > anything neither about the xml file nor the Provider.
> > > >
> > > > Maybe, I'm totally wrong and there is a better way to load the file
> > > where I
> > > > configure my DB connection?
> > > >
> > > > I'm a little confused, so any help would be appreciated.
> > > >
> > > > Thanks in advance.
> > > >
> > > > Sebastien
> > > >
> > > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > > For additional commands, e-mail: user-help@struts.apache.org
> > >
> > >
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: [S2] loading another configuration file

Posted by Don Brown <do...@gmail.com>.
One word, eh? ServletContextListener.  Use a ServletContextListener to
load your properties files and pass the name to it via servlet context
parameter.  There are ways to use Struts classes to initialize and
load the data, but I wouldn't recommend it when there are solutions
that are just as effective, but don't rely on Struts.

See also http://www.onjava.com/pub/a/onjava/2001/04/12/listeners.html
or google for ServletContextListener

BTW, this is the approach Spring, MyFaces, and I imagine many other
frameworks and applications use to load their data on startup.

Don

On 12/13/06, Sébastien LABEY <se...@gmail.com> wrote:
> Thank you Don,
>
> In fact, I don't want to have the name of my properties file to be hard
> coded... So I thought about the "key initialization parameters" that allowed
> me to set the name of the file and the Java class to load it.
> Do you think it is a right way to do it, or maybe should I think about a
> Listener like in the MailReader app to load the database?
> In one word, what's the best way to do it?
>
> Thanks
>
> On 12/13/06, Don Brown <do...@gmail.com> wrote:
> >
> > If you just need to load a few properties, you shouldn't have to touch
> > Struts at all.  Just create a file, say,
> > WEB-INF/classes/db.properties, then load it in your Java code via:
> >
> > Properties props = new Properties();
> > props.load(this.getClass
> > ().getClassLoader().getResourceAsStream("/db.properties");
> >
> > and away you go...
> >
> > Don
> >
> > On 12/13/06, Sébastien LABEY <se...@gmail.com> wrote:
> > > Hi all,
> > >
> > > I'd like to load a file containing the parameters to connect to DB at
> > the
> > > application startup. In the S2 documentation I read about the "key
> > > initialization parameters" (
> > http://struts.apache.org/2.x/docs/webxml.html)
> > > and so I add the 2 lines <init-param> to my web.xml like this :
> > > <filter>
> > >         <filter-name>action2</filter-name>
> > >         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher
> > > </filter-class>
> > >         <init-param>
> > >                 <param-name>config</param-name>
> > >                 <param-value>appConfig.xml</param-value>
> > >         </init-param>
> > >         <init-param>
> > >                 <param-name>configProviders</param-name>
> > >                 <param-value>
> > > app.core.configuration.ApplicationConfigurationProvider</param-value>
> > >         </init-param>
> > > </filter>
> > >
> > > The appConfig.xml is the file where I have my configuration, it is
> > supposed
> > > to be at the root (/WEB-INF/), and the
> > > app.core.configuration.ApplicationConfigurationProvider is the class
> > > implementing ConfigurationProvider interface as said in the doc.
> > > In the init() method of the Provider, I just add "System.out.println
> > ("--Init
> > > plugin begin--");" but it never appears in the log, and I cannot see
> > > anything neither about the xml file nor the Provider.
> > >
> > > Maybe, I'm totally wrong and there is a better way to load the file
> > where I
> > > configure my DB connection?
> > >
> > > I'm a little confused, so any help would be appreciated.
> > >
> > > Thanks in advance.
> > >
> > > Sebastien
> > >
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>
>

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


Re: [S2] loading another configuration file

Posted by Sébastien LABEY <se...@gmail.com>.
Thank you Don,

In fact, I don't want to have the name of my properties file to be hard
coded... So I thought about the "key initialization parameters" that allowed
me to set the name of the file and the Java class to load it.
Do you think it is a right way to do it, or maybe should I think about a
Listener like in the MailReader app to load the database?
In one word, what's the best way to do it?

Thanks

On 12/13/06, Don Brown <do...@gmail.com> wrote:
>
> If you just need to load a few properties, you shouldn't have to touch
> Struts at all.  Just create a file, say,
> WEB-INF/classes/db.properties, then load it in your Java code via:
>
> Properties props = new Properties();
> props.load(this.getClass
> ().getClassLoader().getResourceAsStream("/db.properties");
>
> and away you go...
>
> Don
>
> On 12/13/06, Sébastien LABEY <se...@gmail.com> wrote:
> > Hi all,
> >
> > I'd like to load a file containing the parameters to connect to DB at
> the
> > application startup. In the S2 documentation I read about the "key
> > initialization parameters" (
> http://struts.apache.org/2.x/docs/webxml.html)
> > and so I add the 2 lines <init-param> to my web.xml like this :
> > <filter>
> >         <filter-name>action2</filter-name>
> >         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher
> > </filter-class>
> >         <init-param>
> >                 <param-name>config</param-name>
> >                 <param-value>appConfig.xml</param-value>
> >         </init-param>
> >         <init-param>
> >                 <param-name>configProviders</param-name>
> >                 <param-value>
> > app.core.configuration.ApplicationConfigurationProvider</param-value>
> >         </init-param>
> > </filter>
> >
> > The appConfig.xml is the file where I have my configuration, it is
> supposed
> > to be at the root (/WEB-INF/), and the
> > app.core.configuration.ApplicationConfigurationProvider is the class
> > implementing ConfigurationProvider interface as said in the doc.
> > In the init() method of the Provider, I just add "System.out.println
> ("--Init
> > plugin begin--");" but it never appears in the log, and I cannot see
> > anything neither about the xml file nor the Provider.
> >
> > Maybe, I'm totally wrong and there is a better way to load the file
> where I
> > configure my DB connection?
> >
> > I'm a little confused, so any help would be appreciated.
> >
> > Thanks in advance.
> >
> > Sebastien
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: [S2] loading another configuration file

Posted by Don Brown <do...@gmail.com>.
If you just need to load a few properties, you shouldn't have to touch
Struts at all.  Just create a file, say,
WEB-INF/classes/db.properties, then load it in your Java code via:

Properties props = new Properties();
props.load(this.getClass().getClassLoader().getResourceAsStream("/db.properties");

and away you go...

Don

On 12/13/06, Sébastien LABEY <se...@gmail.com> wrote:
> Hi all,
>
> I'd like to load a file containing the parameters to connect to DB at the
> application startup. In the S2 documentation I read about the "key
> initialization parameters" (http://struts.apache.org/2.x/docs/webxml.html)
> and so I add the 2 lines <init-param> to my web.xml like this :
> <filter>
>         <filter-name>action2</filter-name>
>         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher
> </filter-class>
>         <init-param>
>                 <param-name>config</param-name>
>                 <param-value>appConfig.xml</param-value>
>         </init-param>
>         <init-param>
>                 <param-name>configProviders</param-name>
>                 <param-value>
> app.core.configuration.ApplicationConfigurationProvider</param-value>
>         </init-param>
> </filter>
>
> The appConfig.xml is the file where I have my configuration, it is supposed
> to be at the root (/WEB-INF/), and the
> app.core.configuration.ApplicationConfigurationProvider is the class
> implementing ConfigurationProvider interface as said in the doc.
> In the init() method of the Provider, I just add "System.out.println("--Init
> plugin begin--");" but it never appears in the log, and I cannot see
> anything neither about the xml file nor the Provider.
>
> Maybe, I'm totally wrong and there is a better way to load the file where I
> configure my DB connection?
>
> I'm a little confused, so any help would be appreciated.
>
> Thanks in advance.
>
> Sebastien
>
>

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