You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Kimberly Begley <ki...@gmail.com> on 2008/04/02 13:30:50 UTC

where to put config file in a webapp

Hi,
I have a config file for a web application - currently the absolute path is
specified in a java class that's method's are read by a servlet to get the
info in the config file - I want to deploy the webapp on a remote server so
I'd like to remove the absolute path to the file but can't seem to figure
out where to put it so that the class can see it.
Currently it is in WEB-INF and the java file that references it is in
WEB-INF/src/java which is compiled using the -d ../../classes flag so the
resulting class ends up in WEB-INF/classes/tester/test (being in the
tester.test package) - I've tried different combinations and read online
that I could put the entry in the web.xml file? but haven't been able to
find what the syntax would be for that or how I would access it.
I'm using tomcat5 on a linux box.
Can anyone point me in the right direction?
Thanks

Re: where to put config file in a webapp

Posted by David Smith <dn...@cornell.edu>.
Caldarale, Charles R wrote:
>> From: Kimberly Begley [mailto:kimberly.lewis@gmail.com] 
>> Subject: where to put config file in a webapp
>>
>> Currently it is in WEB-INF
>>     
>
> Normally, such a properties or config file would be placed in
> WEB-INF/classes, and accessed via ServletContext.getResourceAsStream().
>
>   
If using ServletContext.getResourceAsStream(), the file can be anywhere 
in the webapp.  If using Class.getResourceAsStream(), then it has to be 
in WEB-INF/classes or in a jar in WEB-INF/lib.

>> I read online that I could put the entry in the web.xml file?
>>     
>
> See section 4 of the Servlet Spec, in particular the
> ServletContext.getInitParameter() API, then look at the Tomcat doc for
> where to set them:
> http://tomcat.apache.org/tomcat-5.5-doc/config/context.html#Context%20Pa
> rameters
>
>  - Chuck
>
>
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
> MATERIAL and is thus for use only by the intended recipient. If you
> received this in error, please contact the sender and delete the e-mail
> and its attachments from all computers.
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>   


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: where to put config file in a webapp

Posted by Ingmar Lötzsch <il...@asci-systemhaus.de>.
I forgot to change line 113:

old version
Object[][] contents = resources.toArray(new Object[0][0]);

new version
Object[][] contents = resources.toArray(emptyObjectArray);

Ingmar

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: where to put config file in a webapp

Posted by Ingmar Lötzsch <il...@asci-systemhaus.de>.
You also can use ResourceBundle (if you need only Strings) or
ListResourceBundle.

1. ResourceBundle

Save your configuration in com.x.y.z.MyConfig.properties (or in the
default package)
In your Java class type

ResourceBundle bundle = ResourceBundle.getBundle("com.x.y.z.MyConfig");
(= ResourceBundle.getBundle("MyConfig") in case of the default package;
String value1 = bundle.getString("key1");

2. ListResourceBundle

You have to overwrite the getContents() method.

I wrote the following class CommonListResourceBundle that can be
parameterized with the name of the bundle and therefore reused (Excuse
the german comments):

package com.asci.common.util;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.ListResourceBundle;
import java.util.ResourceBundle;

/**
 * Die Klasse stellt die in der Ressourcendatei mit dem durch den
Parameter bundleName übergebenen
 * Namen enthaltenen Inhalte als Objekte zur Verfügung. Die in der
Ressourcendatei abgelegten Werte
 * müssen ein durch Komma abgetrenntes Präfix haben, das den zu
erzeugenden Typ angibt.
 * Unterstützt werden nur Typen aus dem JDK, und zwar
 * string -> java.lang.String
 * int -> java.lang.Integer
 * long -> java.lang.Long
 * double -> java.lang.Double
 * color -> java.awt.Color
 * font -> java.awt.Font
 * dimension -> java.awt.Dimension
 * date -> java.sql.Date
 *
 * Für Boolean wird einfach "true" oder "false" angegeben.
 *
 * Beispiele:
 *
 * param1=int,1
 * param2=string,de_DE
 * param3=true
 * param4=color,255,255,255
 */
public class CommonListResourceBundle extends ListResourceBundle
{
	protected final static Object[][] emptyObjectArray = new Object[0][0];
	
	protected String bundleName;
	
	public CommonListResourceBundle(String bundleName)
	{
		this.bundleName = bundleName;
	}
	
	/**
	 * Die Methode liefert abgeleiteten Klassen die Standardtypen, so dass
dort nur noch die
	 * speziellen Objekte angehangen werden müssen.
	 * @return eine Liste aller Inhalte, die Standardtypen entsprechen.
	 */
	protected List<Object[]> getContentsAsList()
	{
		ArrayList<Object[]> resources = new ArrayList<Object[]>();
		ResourceBundle bundle = ResourceBundle.getBundle(this.bundleName);
		Enumeration<String> keys = bundle.getKeys();
		while (keys.hasMoreElements())
		{
			Object value = null;
			String key = keys.nextElement();
			String valueString = bundle.getString(key);
			String[] tokens = valueString.split(",");
			String type = tokens[0];
			if (type.equals("string"))
			{
				value = tokens[1];
			}
			else if (type.equals("int"))
			{
				value = Integer.valueOf(tokens[1]);
			}
			else if (type.equals("long"))
			{
				value = Long.valueOf(tokens[1]);
			}
			else if (type.equals("double"))
			{
				value = Double.valueOf(tokens[1]);
			}
			else if (type.equals("true"))
			{
				value = Boolean.TRUE;
			}
			else if (type.equals("false"))
			{
				value = Boolean.FALSE;
			}
			else if (type.equals("date"))
			{
				value = Date.valueOf(tokens[1]);
			}
			else if (type.equals("color"))
			{
				value = createColor(tokens);
			}
			else if (type.equals("dimension"))
			{
				value = createDimension(tokens);
			}
			else if (type.equals("font"))
			{
				value = createFont(tokens);
			}
			resources.add(new Object[]{key, value});
		}
		return resources;
	}
	
	@Override
	protected Object[][] getContents()
	{
		List<Object[]> resources = getContentsAsList();
		Object[][] contents = resources.toArray(new Object[0][0]);
		
		// Der folgende Block ist zum Testen aus ListRessourceBundle kopiert.
//		HashMap temp = new HashMap(contents.length);
//		for (int i = 0; i < contents.length; ++i)
//		{
//			// key must be non-null String, value must be non-null
//			String key = (String) contents[i][0];
//			Object value = contents[i][1];
//			if (key == null || value == null)
//			{
//				throw new NullPointerException();
//			}
//			temp.put(key, value);
//		}
		return contents;
	}

	private Color createColor(String[] tokens)
	{
		int r = Integer.parseInt(tokens[1]);
		int g = Integer.parseInt(tokens[2]);
		int b = Integer.parseInt(tokens[3]);
		Color color = new Color(r, g, b);
		return color;
	}

	private Dimension createDimension(String[] tokens)
	{
		int width = Integer.parseInt(tokens[1]);
		int height = Integer.parseInt(tokens[2]);
		Dimension dimension = new Dimension(width, height);
		return dimension;
	}

	private Font createFont(String[] tokens)
	{
		String schriftart = tokens[1];
		int schrifttyp = Integer.parseInt(tokens[2]);
		int schriftgroesse = Integer.parseInt(tokens[3]);
		Font font = new Font(schriftart, schrifttyp, schriftgroesse);
		return font;
	}
}


Ingmar

Kimberly Begley schrieb:
> very cool - yes sorry I never seem to give enough detailed info - thanks for
> reading my mind! you have my wants described very clearly!
> I will give it a go - thanks!!
> 
> On Wed, Apr 2, 2008 at 10:08 PM, Peter Crowther <Pe...@melandra.com>
> wrote:
> 
>>> From: Kimberly Begley [mailto:kimberly.lewis@gmail.com]
>>> Great thanks - it's not actually in a servlet - just a java
>>> class of methods
>>> so I guess I could pull it out of the java class and put it
>>> into the servlet
>>> that is calling the method - if that makes sense - I was just
>>> hoping to avoid that.
>> Can we be clear about what "it" is here?  Lots of pronouns, no clarity
>> :-).
>>
>> If I read you correctly:
>>
>> 1. You have a Java class that reads a config file, presently from a
>> hard-coded location;
>>
>> 2. Other code within your webapp invokes methods on the Java class that
>> reads the config file in order to read configuration information;
>>
>> 3. You want the Java class to be sensitive to the webapp's location, so
>> that the class can read the file from a location within the webapp;
>>
>> 4. You don't want to put webapp-specific code into the Java class that
>> reads the config file.
>>
>> While you can't *quite* do both 3 and 4, the following approach might be
>> helpful:
>>
>> - Amend the class that reads the config file so that it can accept a
>> stream, possibly in a constructor or static method call - depends whether
>> you're instantiating the class.  Read the config data, close the stream -
>> you don't want to hold a file stream open for longer than you have to!
>>
>> - In your servlet code, use Chuck's init parameter approach to obtain the
>> path to the config file.  Open a stream using getResourceAsStream() and hand
>> that stream to the class that reads the config file.
>>
>> - If you still want your class that reads the config file to be usable in
>> non-webapps, re-code so that if the class hasn't been passed a stream by the
>> time the first config variable is requested, it loads from a default
>> location.
>>
>> This isolates your class for reading the config file from any servlet
>> dependencies (they're external) at the expense of a little more complexity.
>>
>>                - Peter
>>
>> ---------------------------------------------------------------------
>> To start a new topic, e-mail: users@tomcat.apache.org
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
> 
> 


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: where to put config file in a webapp

Posted by Kimberly Begley <ki...@gmail.com>.
very cool - yes sorry I never seem to give enough detailed info - thanks for
reading my mind! you have my wants described very clearly!
I will give it a go - thanks!!

On Wed, Apr 2, 2008 at 10:08 PM, Peter Crowther <Pe...@melandra.com>
wrote:

> > From: Kimberly Begley [mailto:kimberly.lewis@gmail.com]
> > Great thanks - it's not actually in a servlet - just a java
> > class of methods
> > so I guess I could pull it out of the java class and put it
> > into the servlet
> > that is calling the method - if that makes sense - I was just
> > hoping to avoid that.
>
> Can we be clear about what "it" is here?  Lots of pronouns, no clarity
> :-).
>
> If I read you correctly:
>
> 1. You have a Java class that reads a config file, presently from a
> hard-coded location;
>
> 2. Other code within your webapp invokes methods on the Java class that
> reads the config file in order to read configuration information;
>
> 3. You want the Java class to be sensitive to the webapp's location, so
> that the class can read the file from a location within the webapp;
>
> 4. You don't want to put webapp-specific code into the Java class that
> reads the config file.
>
> While you can't *quite* do both 3 and 4, the following approach might be
> helpful:
>
> - Amend the class that reads the config file so that it can accept a
> stream, possibly in a constructor or static method call - depends whether
> you're instantiating the class.  Read the config data, close the stream -
> you don't want to hold a file stream open for longer than you have to!
>
> - In your servlet code, use Chuck's init parameter approach to obtain the
> path to the config file.  Open a stream using getResourceAsStream() and hand
> that stream to the class that reads the config file.
>
> - If you still want your class that reads the config file to be usable in
> non-webapps, re-code so that if the class hasn't been passed a stream by the
> time the first config variable is requested, it loads from a default
> location.
>
> This isolates your class for reading the config file from any servlet
> dependencies (they're external) at the expense of a little more complexity.
>
>                - Peter
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>


-- 
Kimberly Begley

RE: where to put config file in a webapp

Posted by Peter Crowther <Pe...@melandra.com>.
> From: Kimberly Begley [mailto:kimberly.lewis@gmail.com]
> Great thanks - it's not actually in a servlet - just a java
> class of methods
> so I guess I could pull it out of the java class and put it
> into the servlet
> that is calling the method - if that makes sense - I was just
> hoping to avoid that.

Can we be clear about what "it" is here?  Lots of pronouns, no clarity :-).

If I read you correctly:

1. You have a Java class that reads a config file, presently from a hard-coded location;

2. Other code within your webapp invokes methods on the Java class that reads the config file in order to read configuration information;

3. You want the Java class to be sensitive to the webapp's location, so that the class can read the file from a location within the webapp;

4. You don't want to put webapp-specific code into the Java class that reads the config file.

While you can't *quite* do both 3 and 4, the following approach might be helpful:

- Amend the class that reads the config file so that it can accept a stream, possibly in a constructor or static method call - depends whether you're instantiating the class.  Read the config data, close the stream - you don't want to hold a file stream open for longer than you have to!

- In your servlet code, use Chuck's init parameter approach to obtain the path to the config file.  Open a stream using getResourceAsStream() and hand that stream to the class that reads the config file.

- If you still want your class that reads the config file to be usable in non-webapps, re-code so that if the class hasn't been passed a stream by the time the first config variable is requested, it loads from a default location.

This isolates your class for reading the config file from any servlet dependencies (they're external) at the expense of a little more complexity.

                - Peter

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: where to put config file in a webapp

Posted by Kimberly Begley <ki...@gmail.com>.
Great thanks - it's not actually in a servlet - just a java class of methods
so I guess I could pull it out of the java class and put it into the servlet
that is calling the method - if that makes sense - I was just hoping to
avoid that.
Thanks for the advice - much appreciated.


On Wed, Apr 2, 2008 at 9:47 PM, Caldarale, Charles R <
Chuck.Caldarale@unisys.com> wrote:

> > From: Kimberly Begley [mailto:kimberly.lewis@gmail.com]
> > Subject: where to put config file in a webapp
> >
> > Currently it is in WEB-INF
>
> Normally, such a properties or config file would be placed in
> WEB-INF/classes, and accessed via ServletContext.getResourceAsStream().
>
> > I read online that I could put the entry in the web.xml file?
>
> See section 4 of the Servlet Spec, in particular the
> ServletContext.getInitParameter() API, then look at the Tomcat doc for
> where to set them:
> http://tomcat.apache.org/tomcat-5.5-doc/config/context.html#Context%20Pa
> rameters
>
>  - Chuck
>
>
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
> MATERIAL and is thus for use only by the intended recipient. If you
> received this in error, please contact the sender and delete the e-mail
> and its attachments from all computers.
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>


-- 
Kimberly Begley

RE: where to put config file in a webapp

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Kimberly Begley [mailto:kimberly.lewis@gmail.com] 
> Subject: where to put config file in a webapp
> 
> Currently it is in WEB-INF

Normally, such a properties or config file would be placed in
WEB-INF/classes, and accessed via ServletContext.getResourceAsStream().

> I read online that I could put the entry in the web.xml file?

See section 4 of the Servlet Spec, in particular the
ServletContext.getInitParameter() API, then look at the Tomcat doc for
where to set them:
http://tomcat.apache.org/tomcat-5.5-doc/config/context.html#Context%20Pa
rameters

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e-mail
and its attachments from all computers.

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org