Tim:

Here is what I'm doing. It bit different than Matt's. The main reason is that I didn't 
want my code to depend on the Servlet API. I have two libraries that should/could be 
used in not web applications, such as web services or command line tools. I created a 
very simple class called Registry. This is just wrapper around a Vector. I made this a 
Singleton by making the constructor private and create Static instance. There are two 
methods getValue(String key) and setValue(String key, String
value).

In my control servlet, I have read in all the configuration values and put them into 
the registry with code like this.

Registry  reg=Registry.getInstance();
reg.setValue("server","ncdcftp");

...

In my code that needs the ftp server, I can now get it via

Registry reg=Registry.getInstance();
reg.getValue("server");

Pros

   * The solution is simple and easy. You can whip this up in a few minutes.
   * No third party libraries needed

Cons

   * You lose your type information
   * You need to know the key.
   * It acts as a global variable
   * Hidden dependencies
   * Scalable?

Some refactoring I've thought about...

I could correct losing the type and key information by using specific field name for 
each configuration element.  Here is my sample using this approach.

Registry  reg=Registry.getInstance();
reg.setFtpServe("ncdcftp");

In my code that needs the ftp server, I can now get it via

Registry reg=Registry.getInstance();
reg.getValue("server");

That they act like a global varible is what concerns me. There is nothing from the 
class, methods or etc to show that this file is needed. I will be in the javadocs, but 
there always the possiblity of change somewhere I don't expect. I have no idea, if 
this is scalable?

I'm curious what people think of this solution, so Tim you may want to wait and see if 
anyone crushes this solution.

Regards,

Jeff Duska


Matt Raible wrote:

> I think it depends on how much you will need to alter this information. What I do in 
>my applications is create an app-config.xml file that lives in WEB-INF or the 
>$user.home directory - then I use Castor to turn marshal those values into a JavaBean 
>called Configuration - which I put in the ServletContext and call when I need it. 
>Another approach is to use context-parameters in your web.xml file. However, once you 
>get a bunch of these, you'll want to move to the XML->JavaBean approach.
>
> HTH,
>
> Matt
>
> On Thursday, December 5, 2002, at 08:08 AM, Tim Reilly wrote:
>
>      Hi there,
>
>      I'm trying to find out what the right place for your own configuration 
>information is.  I'm talking about the kind of stuff you used to just put into a 
>.properties file.  I'm new to Struts, and I'm not sure if there's a better way to do 
>it now.
>
>      Does this kind of configuration information belong in:
>
>      1) A ServletConfig [seems wrong, since with Struts I don't have my own servlet].
>      2) Your own .properties file
>      3) A separate MessageResource file from the one used for your messages?
>      or
>      4) Some other place / method of access I don't yet know about.
>
>      I can't seem to find an answer to this in the struts-mailing list archives or 
>on the web.
>
>      Thanks for your help,
>
>      -Tim

_______________________________________________
MVC-Programmers mailing list
[EMAIL PROTECTED]
http://www.netbean.net/mailman/listinfo/mvc-programmers

Reply via email to