You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Brian Goetz <br...@quiotix.com> on 2000/10/20 00:18:20 UTC
Re: Patch: eliminating requirement of an external .properties
file.
> > I'm fiddling around with using velocity outside of the context of
> > servlets and I wanted to feed it it's properties directly.
> > Runtime.init( String propertiesFilename ) still works but now
> > Runtime.init( Properties properties ) is also supported:
I use the following module for managing properties, which is flexible and
which does what you are looking for. It allows you to load properties out
of a properties file which is found through the class loader, and also get
them from the system properties, and also specify reasonable defaults in
the code.
Example:
Application name = velocity
Application prefix = org.apache.velocity
PropUtil.getProperty("velocity", "prop", "org.apache.velocity", "foo")
will look for prop in the following order:
- "org.apache.velocity.prop" in the system properties
- "org.apache.velocity.prop" in "velocity.properties", loaded from
somewhere on the class path (properties file is cached after first read)
- "prop" in "velocity.properties"
- if not found in any of above, return the default (foo)
This allows you to specify all properties through either system properties
or application-specific properties file, and also to override properties
from the properties file with system properties. And you can abbreviate
the properties (leave off the application prefix) in the properties file
where it won't be ambiguous.
Also, because you can specify a default value in the code for each
property, you can ensure that the code will at least not explode if it
can't find any of its properties.
More detail in the JavaDoc.
BSD licensed. Contributed freely to Velocity.
/*
* Copyright (c) 2000, Quiotix Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither name of Quiotix Corporation nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.quiotix.util;
import java.util.Properties;
import java.io.*;
import com.sun.java.util.collections.*;
import org.webmacro.servlet.HandlerException;
/**
* PropUtil
*
* A utility class for managing configuration properties for applications.
* It allows applications to retrieve their properties from a properties file
* or be specified on the command line when the java interpreter is started.
* Also contains static utility messages for parsing and converting types
* and providing default values of input properties.
*/
public class PropUtil {
private static class ApplicationInfo {
public String name;
public Properties properties;
};
private static final ClassLoader classLoader =
PropUtil.class.getClassLoader();
/**
* Parses the input property string as an integer, and returns the
* value, returning the default value if the specified string is null,
* or doesn't parse to an integer.
* @param s The string to parse
* @param defaultValue The default value to use in the event of an error
*/
public static int defaultInt(String s, int defaultValue) {
if (s == null)
return defaultValue;
else try {
return Integer.parseInt(s);
}
catch (NumberFormatException e) {
};
return defaultValue;
};
/**
* Returns the default value if the specified string is null, or empty.
* @param s The input string
* @param defaultValue The default value to use if the input string is null
* or empty
*/
public static String defaultString(String s, String defaultValue) {
if (s == null || s.trim().equals(""))
return defaultValue;
else
return s;
};
/**
* Parses the input property string as an boolean, and returns the
* value, returning the default value if the specified string is null,
* or doesn't parse to a boolean.
* @param s The string to parse
* @param defaultValue The default value to use in the event of an error
*/
public static boolean defaultBoolean(String s, boolean defaultValue) {
if (s == null)
return defaultValue;
else try {
return Boolean.valueOf(s).booleanValue();
}
catch (Exception e) {
};
return defaultValue;
};
private static HashMap applicationMap;
private static synchronized ApplicationInfo
getApplicationInfo(String application) {
ApplicationInfo ai;
if (applicationMap == null)
applicationMap = new HashMap();
if (applicationMap.containsKey(application))
ai = (ApplicationInfo) applicationMap.get(application);
else {
InputStream is=null;
ai = new ApplicationInfo();
ai.name = application;
ai.properties = new Properties();
try {
is = classLoader.getResourceAsStream("/" + application
+ ".properties");
if (is == null)
is = classLoader.getResourceAsStream(application
+ ".properties");
if (is != null) {
ai.properties.load(is);
is.close();
}
}
catch (IOException e) { };
applicationMap.put(application, ai);
};
return ai;
}
/**
* Retrieves a property relative to a specified application.
* It searches both the applications property file and the system
* properties. The properties file is only read once per application,
* and then cached.
*
* Example:
* Let application = "myapp", propName = "fileDir",
appPrefix="com.foo.bar"
*
* It will try to resolve the property in the following order:
* Search the system properties for com.foo.bar.fileDir
* Search file myapp.properties (in the class path) for a property
* named com.for.bar.fileDir
* Search file myapp.properties (in the class path) for a property
* named fileDir
* Return the default value
*
* @param application The name of the application, which will be used
* to search for the application's properties file, and also be used to
* optimize future calls (since the property file will only be read once.)
* @param propName The name of the property to look for
* @param appPrefix A prefix to prepend to the property name when looking
* for the property in the system properties. This allows you to use
* the short (or long) names in the application-specific properties file
* but prevents ambiguity when using the system properties.
* @param defaultValue The value to return if the property cannot be found
* in any of the other ways
*/
public static String getProperty(String application,
String propName,
String appPrefix,
String defaultValue) {
ApplicationInfo ai;
String fullPropName;
String value;
ai = getApplicationInfo(application);
fullPropName = (appPrefix != null) ? appPrefix + "." + propName :
propName;
value = System.getProperty(fullPropName);
if (value == null) value = ai.properties.getProperty(fullPropName);
if (value == null) value = ai.properties.getProperty(propName);
if (value == null) value = defaultValue;
return value;
}
}
--
Brian Goetz
Quiotix Corporation
brian@quiotix.com Tel: 650-843-1300 Fax: 650-324-8032
http://www.quiotix.com
Re: Patch: eliminating requirement of an external .properties
file.
Posted by Jon Stevens <jo...@latchkey.com>.
on 10/19/2000 5:41 PM, "Brian Goetz" <br...@quiotix.com> wrote:
> Does anyone else find it odd that using the ASF license is not sufficient
> to contribute code to an ASF project?
Hi Brian,
It doesn't matter. The ASF needs to be the sole owner of the code that is
donated to the ASF. There is a BIG difference between code used as a library
and code that is donated.
I'm not making up the rules here, I'm just following them. If you have a
problem with that, then feel free to bring it up with board@apache.org or
members@apache.org.
thanks,
-jon
--
http://scarab.tigris.org/ | http://noodle.tigris.org/
http://java.apache.org/ | http://java.apache.org/turbine/
http://www.working-dogs.com/ | http://jakarta.apache.org/velocity/
http://www.collab.net/ | http://www.sourcexchange.com/
Re: Patch: eliminating requirement of an external .properties
file.
Posted by Brian Goetz <br...@quiotix.com>.
>Sorry, we can't accept this. It must be copyright to the ASF.
Does anyone else find it odd that using the ASF license is not sufficient
to contribute code to an ASF project?
--
Brian Goetz
Quiotix Corporation
brian@quiotix.com Tel: 650-843-1300 Fax: 650-324-8032
http://www.quiotix.com
Re: Patch: eliminating requirement of an external .properties
file.
Posted by Brian Goetz <br...@quiotix.com>.
>Sigh, that is what I said in the first place. Let me quote again:
>
>"It must be copyright to the ASF."
I think its pretty clear that
License
... or contain a copyright and license that allows redistribution under the
same conditions as the Apache License.
doesn't require it be copyrighted to ASF. In fact, the existence of this
alternate clause suggests that ASF envisions contribution of any
compatibly-licensed code to Apache projects.
It says ALLOWS REDISTRIBUTION UNDER THE SAME CONDITIONS, which is not the
same as "must be identical to." This is to say, it cannot place additional
conditions on the redistribution. It may contain fewer conditions, such as
code placed into the public domain. Or a license that is identical to the
Apache license, except with a copyright to another source, and eliminating
paragraphs 2 and 3 (so as not to place an additional notice requirement)
should be enough.
>However, it will still be up to someone to actually check the code in *as
>well as integrate it into the system*. If no one picks up that ball, then
>don't expect to see it show up because you simply copy/pasted a bunch of
>code, you didn't actually integrate it.
Of course.
I offered some useful code, which addressed an issue that has been brought
up on the list. If you want to use it, that's fine, that's why I posted
it. If you don't want to use it, I won't be offended.
--
Brian Goetz
Quiotix Corporation
brian@quiotix.com Tel: 650-843-1300 Fax: 650-324-8032
http://www.quiotix.com
Re: Patch: eliminating requirement of an external .properties
file.
Posted by Jon Stevens <jo...@latchkey.com>.
on 10/19/2000 5:59 PM, "Brian Goetz" <br...@quiotix.com> wrote:
> I just checked. I don't think you are correct.
Huh? Keep reading...
> My reading of the second clause ("or contains...") is that, if I took the
> Apache license, substituted "Copyright Quiotix" for "Copyright Apache", and
> contributed it, that would make it acceptable. I am willing to do this.
Sigh, that is what I said in the first place. Let me quote again:
"It must be copyright to the ASF."
> Jon, is there any other reason why you don't want me to contribute this
> code to the project?
Wow, I feel like I'm repeating myself. I already specified the reasons for
not accepting it.
However, it will still be up to someone to actually check the code in *as
well as integrate it into the system*. If no one picks up that ball, then
don't expect to see it show up because you simply copy/pasted a bunch of
code, you didn't actually integrate it.
-jon
--
http://scarab.tigris.org/ | http://noodle.tigris.org/
http://java.apache.org/ | http://java.apache.org/turbine/
http://www.working-dogs.com/ | http://jakarta.apache.org/velocity/
http://www.collab.net/ | http://www.sourcexchange.com/
Re: Patch: eliminating requirement of an external .properties
file.
Posted by Brian Goetz <br...@quiotix.com>.
>Sorry, we can't accept this. It must be copyright to the ASF.
...
>I'm not making up the rules here, I'm just following them. If you have a
>problem with that, then feel free to bring it up with board@apache.org or
>members@apache.org.
I just checked. I don't think you are correct.
At http://jakarta.apache.org/guidelines/source.html, which claims to be the
governing rules for code ownership in Jakarta projects, it says:
License
All source code committed to the Project's repositories must be covered by
the Apache License or contain a copyright and license that allows
redistribution under the same conditions as the Apache License.
My reading of the second clause ("or contains...") is that, if I took the
Apache license, substituted "Copyright Quiotix" for "Copyright Apache", and
contributed it, that would make it acceptable. I am willing to do this.
Jon, is there any other reason why you don't want me to contribute this
code to the project?
--
Brian Goetz
Quiotix Corporation
brian@quiotix.com Tel: 650-843-1300 Fax: 650-324-8032
http://www.quiotix.com
Re: Patch: eliminating requirement of an external .properties file.
Posted by Jason van Zyl <jv...@periapt.com>.
Cool! Thanks Brian!
jvz.
On Thu, 19 Oct 2000, Brian Goetz wrote:
>
> > > I'm fiddling around with using velocity outside of the context of
> > > servlets and I wanted to feed it it's properties directly.
> > > Runtime.init( String propertiesFilename ) still works but now
> > > Runtime.init( Properties properties ) is also supported:
>
> I use the following module for managing properties, which is flexible and
> which does what you are looking for. It allows you to load properties out
> of a properties file which is found through the class loader, and also get
> them from the system properties, and also specify reasonable defaults in
> the code.
>
> Example:
> Application name = velocity
> Application prefix = org.apache.velocity
>
> PropUtil.getProperty("velocity", "prop", "org.apache.velocity", "foo")
>
> will look for prop in the following order:
>
> - "org.apache.velocity.prop" in the system properties
> - "org.apache.velocity.prop" in "velocity.properties", loaded from
> somewhere on the class path (properties file is cached after first read)
> - "prop" in "velocity.properties"
> - if not found in any of above, return the default (foo)
>
> This allows you to specify all properties through either system properties
> or application-specific properties file, and also to override properties
> from the properties file with system properties. And you can abbreviate
> the properties (leave off the application prefix) in the properties file
> where it won't be ambiguous.
>
> Also, because you can specify a default value in the code for each
> property, you can ensure that the code will at least not explode if it
> can't find any of its properties.
>
> More detail in the JavaDoc.
>
> BSD licensed. Contributed freely to Velocity.
>
>
> /*
> * Copyright (c) 2000, Quiotix Corporation
> * All rights reserved.
> *
> * Redistribution and use in source and binary forms, with or without
> * modification, are permitted provided that the following conditions are
> * met:
> *
> * Redistributions of source code must retain the above copyright
> * notice, this list of conditions and the following disclaimer.
> *
> * Redistributions in binary form must reproduce the above copyright
> * notice, this list of conditions and the following disclaimer in the
> * documentation and/or other materials provided with the distribution.
> *
> * Neither name of Quiotix Corporation nor the names of its
> * contributors may be used to endorse or promote products derived from
> * this software without specific prior written permission.
> *
> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
> * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
> * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
> * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
> * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
> * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
> * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
> * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
> * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
> * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
> * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
> */
>
> package com.quiotix.util;
>
> import java.util.Properties;
> import java.io.*;
> import com.sun.java.util.collections.*;
> import org.webmacro.servlet.HandlerException;
>
>
> /**
> * PropUtil
> *
> * A utility class for managing configuration properties for applications.
> * It allows applications to retrieve their properties from a properties file
> * or be specified on the command line when the java interpreter is started.
> * Also contains static utility messages for parsing and converting types
> * and providing default values of input properties.
> */
>
> public class PropUtil {
>
> private static class ApplicationInfo {
> public String name;
> public Properties properties;
> };
>
> private static final ClassLoader classLoader =
> PropUtil.class.getClassLoader();
>
> /**
> * Parses the input property string as an integer, and returns the
> * value, returning the default value if the specified string is null,
> * or doesn't parse to an integer.
> * @param s The string to parse
> * @param defaultValue The default value to use in the event of an error
> */
> public static int defaultInt(String s, int defaultValue) {
> if (s == null)
> return defaultValue;
> else try {
> return Integer.parseInt(s);
> }
> catch (NumberFormatException e) {
> };
>
> return defaultValue;
> };
>
> /**
> * Returns the default value if the specified string is null, or empty.
> * @param s The input string
> * @param defaultValue The default value to use if the input string is null
> * or empty
> */
> public static String defaultString(String s, String defaultValue) {
> if (s == null || s.trim().equals(""))
> return defaultValue;
> else
> return s;
> };
>
> /**
> * Parses the input property string as an boolean, and returns the
> * value, returning the default value if the specified string is null,
> * or doesn't parse to a boolean.
> * @param s The string to parse
> * @param defaultValue The default value to use in the event of an error
> */
> public static boolean defaultBoolean(String s, boolean defaultValue) {
> if (s == null)
> return defaultValue;
> else try {
> return Boolean.valueOf(s).booleanValue();
> }
> catch (Exception e) {
> };
>
> return defaultValue;
> };
>
> private static HashMap applicationMap;
>
> private static synchronized ApplicationInfo
> getApplicationInfo(String application) {
> ApplicationInfo ai;
>
> if (applicationMap == null)
> applicationMap = new HashMap();
>
> if (applicationMap.containsKey(application))
> ai = (ApplicationInfo) applicationMap.get(application);
> else {
> InputStream is=null;
>
> ai = new ApplicationInfo();
> ai.name = application;
> ai.properties = new Properties();
> try {
> is = classLoader.getResourceAsStream("/" + application
> + ".properties");
> if (is == null)
> is = classLoader.getResourceAsStream(application
> + ".properties");
> if (is != null) {
> ai.properties.load(is);
> is.close();
> }
> }
> catch (IOException e) { };
> applicationMap.put(application, ai);
> };
>
> return ai;
> }
>
>
> /**
> * Retrieves a property relative to a specified application.
> * It searches both the applications property file and the system
> * properties. The properties file is only read once per application,
> * and then cached.
> *
> * Example:
> * Let application = "myapp", propName = "fileDir",
> appPrefix="com.foo.bar"
> *
> * It will try to resolve the property in the following order:
> * Search the system properties for com.foo.bar.fileDir
> * Search file myapp.properties (in the class path) for a property
> * named com.for.bar.fileDir
> * Search file myapp.properties (in the class path) for a property
> * named fileDir
> * Return the default value
> *
> * @param application The name of the application, which will be used
> * to search for the application's properties file, and also be used to
> * optimize future calls (since the property file will only be read once.)
> * @param propName The name of the property to look for
> * @param appPrefix A prefix to prepend to the property name when looking
> * for the property in the system properties. This allows you to use
> * the short (or long) names in the application-specific properties file
> * but prevents ambiguity when using the system properties.
> * @param defaultValue The value to return if the property cannot be found
> * in any of the other ways
> */
> public static String getProperty(String application,
> String propName,
> String appPrefix,
> String defaultValue) {
> ApplicationInfo ai;
> String fullPropName;
> String value;
>
> ai = getApplicationInfo(application);
>
> fullPropName = (appPrefix != null) ? appPrefix + "." + propName :
> propName;
> value = System.getProperty(fullPropName);
> if (value == null) value = ai.properties.getProperty(fullPropName);
> if (value == null) value = ai.properties.getProperty(propName);
> if (value == null) value = defaultValue;
>
> return value;
> }
> }
>
>
>
>
> --
> Brian Goetz
> Quiotix Corporation
> brian@quiotix.com Tel: 650-843-1300 Fax: 650-324-8032
>
> http://www.quiotix.com
>
>
Re: Patch: eliminating requirement of an external .propertiesfile.
Posted by Kasper Nielsen <ne...@kav.dk>.
what the difference compared to lets say org.apache.velocity.io.fastwriter?
- Kasper
----- Original Message -----
From: "Jon Stevens" <jo...@latchkey.com>
To: <ve...@jakarta.apache.org>
Sent: Friday, October 20, 2000 2:26 AM
Subject: Re: Patch: eliminating requirement of an external .propertiesfile.
> on 10/19/2000 3:18 PM, "Brian Goetz" <br...@quiotix.com> wrote:
>
> > * Copyright (c) 2000, Quiotix Corporation
> > * All rights reserved.
> > *
>
> Sorry, we can't accept this. It must be copyright to the ASF.
>
> -jon
>
Re: Patch: eliminating requirement of an external .properties
file.
Posted by Jon Stevens <jo...@latchkey.com>.
on 10/19/2000 3:18 PM, "Brian Goetz" <br...@quiotix.com> wrote:
> * Copyright (c) 2000, Quiotix Corporation
> * All rights reserved.
> *
Sorry, we can't accept this. It must be copyright to the ASF.
-jon
--
http://scarab.tigris.org/ | http://noodle.tigris.org/
http://java.apache.org/ | http://java.apache.org/turbine/
http://www.working-dogs.com/ | http://jakarta.apache.org/velocity/
http://www.collab.net/ | http://www.sourcexchange.com/