tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ingmar Lötzsch <iloetz...@asci-systemhaus.de>
Subject Re: where to put config file in a webapp
Date Wed, 02 Apr 2008 13:10:03 GMT
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 <Peter.Crowther@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


Mime
View raw message