You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by James Dekker <ja...@gmail.com> on 2007/01/16 23:32:42 UTC

Configurable Errors

Hello there,

I am creating a configurable errors file which gets loaded as a
properties file from an init servlet:

import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

public class ErrorInitServlet extends HttpServlet {

        static Properties errorProps = new Properties();

        public void init() throws ServletException {
            Properties props = new Properties();
            try {
                props.load(this.getClass().getClassLoader().getResourceAsStream(
                        "/error.properties"));
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }

       public static Properties getErrorProperties() {
		return errorProps;
	}
}

The error.properties file looks like this:

error.required.field=Required %s
error.invalid.entry=Invalid %s
error.unknown.entry=Unknown %s

I created an ActionErrors.java file:

package org.coffeebreak.wrapper;

import java.util.Formatter;
import java.util.ResourceBundle;

/**
 * @author jdekker
 */
public class ActionError {

    private final ResourceBundle m_resource;

    public ActionError() {
    	 m_resource = ResourceBundle.getBundle("error.properties");
    }

    public String getMessage(String id, Object... parameters) {
        String value = m_resource.getString(id);
        if (null != value) {
            StringBuilder builder = new StringBuilder();
            Formatter f = new Formatter(builder);
            f.format(value, parameters);
            f.flush();
            return builder.toString();
        }
        return value;
    }
}

I had my ant build script move the error.properties to:
TOMCAT_HOME/WEB-INF/classes/org/coffeebreak/wrapper/

Now, when I invoke this class, through a client, this is the error that I get:

INFO: Deploying web application archive coffeebreak.war
2007-01-16 13:55:41,753 WARN
[org.coffeebreak.model.AttributeBeanXmlConfigHelper] - commons
digester rules location:
file:/C:/DevTools/tomcat/jakarta-tomcat-5.5.9/webapps/coffeebreak/WEB-INF/classes/org/coffeebreak/model/attribute-rules.xml
2007-01-16 13:55:42,128 WARN
[org.coffeebreak.config.XmlConfigInitServlet] - Finished parsing the
attribute XML config file.
2007-01-16 13:55:42,128 WARN
[org.coffeebreak.config.LoadErrorProperties] - Loaded error.properties
file.
2007-01-16 13:56:16,421 ERROR
[org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/coffeebreak].[CoffeebreakAppServlet]]
- Servlet.service() for servlet CoffeebreakAppServlet threw exception
java.util.MissingResourceException: Can't find bundle for base name
/error.properties, locale en_US
	at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:836)
	at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:805)
	at java.util.ResourceBundle.getBundle(ResourceBundle.java:549)
	at org.coffeebreak.wrapper.ActionError.<init>(ActionError.java:21)
	at org.coffeebreak.views.EditUserPane.validateFields(EditUserPane.java:159)
	at org.coffeebreak.views.EditUserPane.processSave(EditUserPane.java:148)
	at org.coffeebreak.views.EditUserPane.actionPerformed(EditUserPane.java:141)

Why is ActionError having trouble finding the error.properties file?
Is there a way to use my InitServlet's getter to set the resource
bundle? Am I going about this the wrong way?

Sincerely,

James

---------------------------------------------------------------------
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: Configurable Errors

Posted by Robert Harper <ro...@iat-cti.com>.
In my servlet, I load a properties file that is in my [context_path]/classes
directory with the following line of code

m_resource = ResourceBundle.getBundle( "ctimpact", Locale.getDefault() );

This works fine for me. You could try removing the ".properties" part of the
file name. The API may be adding it by default. Also maybe move your
properties file to the classes directory.

I hope that helps.

Robert S. Harper
Senior Engineer
Information Access Technology, Inc.
1100 East 6600 South, Suite 300
Salt Lake City Utah USA 84121-7411
(801)265-8800 Ext. 255 
FAX (801)265-8880
 

This e-mail is intended only for the addressee and may contain confidential
and/or privileged information. Any review, retransmission, or action taken
upon this information by persons other than the intended recipient is
prohibited by law. If you received this communication in error, please
contact us immediately at 801-265-8800. Although this e-mail and any
attachments are believed to be free of any virus or other defect, it is the
responsibility of the recipient to ensure that anything received or opened
is virus free. No responsibility is accepted by IAT for any loss or damage
in the event that such a virus or defect exists.

-----Original Message-----
From: James Dekker [mailto:james.dekker@gmail.com] 
Sent: Tuesday, January 16, 2007 3:53 PM
To: Tomcat Users List
Subject: Re: Configurable Errors

Dear Mr. Harper,

Thank you for the response!

I set the path inside the ActionError constructor as follows:

public ActionError() {
        m_resource = ResourceBundle.getBundle("/error.properties");
}

I thought the "/" meant the root directory inside
CATALINA_HOME/WEB-INF/classes/.

Since, my error.properties file is going inside the
CATALINA_HOME/WEB-INF/classes/org/coffeebreak/wrapper/ directory via
my Ant build script, I rewrote the code inside the constructor as:

public ActionError() {
        // I removed the slash
        m_resource = ResourceBundle.getBundle("error.properties");
}

And then it gave me that Missing exception...

What am I possibly doing wrong?

Sincerely,

James Dekker

---------------------------------------------------------------------
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: Configurable Errors

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Martin Gainty [mailto:mgainty@hotmail.com] 
> Subject: Re: Configurable Errors
> 
> ResourceBundle.getResource("errors") will get errors.properties
> language and country is self-explanatory..
> 
> variant is either
> WIN for Windows, MAC for Macintosh, and POSIX for POSIX
> 
> The order to locate the resources properties file on your 
> classpath is ..
>   a.. baseName + "_" + language1 + "_" + country1 + "_" + variant1 
>   b.. baseName + "_" + language1 + "_" + country1 
>   c.. baseName + "_" + language1 
>   d.. baseName + "_" + language2 + "_" + country2 + "_" + variant2 
>   e.. baseName + "_" + language2 + "_" + country2 
>   f.. baseName + "_" + language2 
>   g.. baseName 
> 
>   (of course .properties is appended at end of the construct)

Examples of the use of the above (without the platform variants) can be
found within Tomcat itself.  The various language-specific .properties
files are packaged in jars in common/i18n; these are accessed through
various StringManager classes such as:

apache-tomcat-5.5.20-src/container/catalina/src/share/org/apache/naming/
StringManager.java

 - 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


Re: Configurable Errors

Posted by Martin Gainty <mg...@hotmail.com>.
ResourceBundle.getResource("errors") will get errors.properties
language and country is self-explanatory..

variant is either
WIN for Windows, MAC for Macintosh, and POSIX for POSIX

The order to locate the resources properties file on your classpath is ..
  a.. baseName + "_" + language1 + "_" + country1 + "_" + variant1 
  b.. baseName + "_" + language1 + "_" + country1 
  c.. baseName + "_" + language1 
  d.. baseName + "_" + language2 + "_" + country2 + "_" + variant2 
  e.. baseName + "_" + language2 + "_" + country2 
  f.. baseName + "_" + language2 
  g.. baseName 

  (of course .properties is appended at end of the construct)

  Anyone else?
  M-
--------------------------------------------------------------------------- 
This e-mail message (including attachments, if any) is intended for the use of the individual or entity to which it is addressed and may contain information that is privileged, proprietary , confidential and exempt from disclosure. If you are not the intended recipient, you are notified that any dissemination, distribution or copying of this communication is strictly prohibited.
--------------------------------------------------------------------------- 
Le présent message électronique (y compris les pièces qui y sont annexées, le cas échéant) s'adresse au destinataire indiqué et peut contenir des renseignements de caractère privé ou confidentiel. Si vous n'êtes pas le destinataire de ce document, nous vous signalons qu'il est strictement interdit de le diffuser, de le distribuer ou de le reproduire.
----- Original Message ----- 
From: "James Dekker" <ja...@gmail.com>
To: "Tomcat Users List" <us...@tomcat.apache.org>
Sent: Tuesday, January 16, 2007 5:53 PM
Subject: Re: Configurable Errors


> Dear Mr. Harper,
> 
> Thank you for the response!
> 
> I set the path inside the ActionError constructor as follows:
> 
> public ActionError() {
>        m_resource = ResourceBundle.getBundle("/error.properties");
> }
> 
> I thought the "/" meant the root directory inside
> CATALINA_HOME/WEB-INF/classes/.
> 
> Since, my error.properties file is going inside the
> CATALINA_HOME/WEB-INF/classes/org/coffeebreak/wrapper/ directory via
> my Ant build script, I rewrote the code inside the constructor as:
> 
> public ActionError() {
>        // I removed the slash
>        m_resource = ResourceBundle.getBundle("error.properties");
> }
> 
> And then it gave me that Missing exception...
> 
> What am I possibly doing wrong?
> 
> Sincerely,
> 
> James Dekker
> 
> ---------------------------------------------------------------------
> 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: Configurable Errors

Posted by James Dekker <ja...@gmail.com>.
Dear Mr. Harper,

Thank you for the response!

I set the path inside the ActionError constructor as follows:

public ActionError() {
        m_resource = ResourceBundle.getBundle("/error.properties");
}

I thought the "/" meant the root directory inside
CATALINA_HOME/WEB-INF/classes/.

Since, my error.properties file is going inside the
CATALINA_HOME/WEB-INF/classes/org/coffeebreak/wrapper/ directory via
my Ant build script, I rewrote the code inside the constructor as:

public ActionError() {
        // I removed the slash
        m_resource = ResourceBundle.getBundle("error.properties");
}

And then it gave me that Missing exception...

What am I possibly doing wrong?

Sincerely,

James Dekker

---------------------------------------------------------------------
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: Configurable Errors

Posted by Robert Harper <ro...@iat-cti.com>.
It looks like it is looking for it in the root of either the machine or
CATALINA_HOME. Resolve the path correctly and you'll probably find your
problem. You didn't provide the path so the loader assumes the root.

Robert S. Harper
Senior Engineer
Information Access Technology, Inc.
1100 East 6600 South, Suite 300
Salt Lake City Utah USA 84121-7411
(801)265-8800 Ext. 255 
FAX (801)265-8880
 

This e-mail is intended only for the addressee and may contain confidential
and/or privileged information. Any review, retransmission, or action taken
upon this information by persons other than the intended recipient is
prohibited by law. If you received this communication in error, please
contact us immediately at 801-265-8800. Although this e-mail and any
attachments are believed to be free of any virus or other defect, it is the
responsibility of the recipient to ensure that anything received or opened
is virus free. No responsibility is accepted by IAT for any loss or damage
in the event that such a virus or defect exists.

-----Original Message-----
From: James Dekker [mailto:james.dekker@gmail.com] 
Sent: Tuesday, January 16, 2007 3:33 PM
To: Tomcat Users List
Subject: Configurable Errors

Hello there,

I am creating a configurable errors file which gets loaded as a
properties file from an init servlet:

import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

public class ErrorInitServlet extends HttpServlet {

        static Properties errorProps = new Properties();

        public void init() throws ServletException {
            Properties props = new Properties();
            try {
 
props.load(this.getClass().getClassLoader().getResourceAsStream(
                        "/error.properties"));
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }

       public static Properties getErrorProperties() {
		return errorProps;
	}
}

The error.properties file looks like this:

error.required.field=Required %s
error.invalid.entry=Invalid %s
error.unknown.entry=Unknown %s

I created an ActionErrors.java file:

package org.coffeebreak.wrapper;

import java.util.Formatter;
import java.util.ResourceBundle;

/**
 * @author jdekker
 */
public class ActionError {

    private final ResourceBundle m_resource;

    public ActionError() {
    	 m_resource = ResourceBundle.getBundle("error.properties");
    }

    public String getMessage(String id, Object... parameters) {
        String value = m_resource.getString(id);
        if (null != value) {
            StringBuilder builder = new StringBuilder();
            Formatter f = new Formatter(builder);
            f.format(value, parameters);
            f.flush();
            return builder.toString();
        }
        return value;
    }
}

I had my ant build script move the error.properties to:
TOMCAT_HOME/WEB-INF/classes/org/coffeebreak/wrapper/

Now, when I invoke this class, through a client, this is the error that I
get:

INFO: Deploying web application archive coffeebreak.war
2007-01-16 13:55:41,753 WARN
[org.coffeebreak.model.AttributeBeanXmlConfigHelper] - commons
digester rules location:
file:/C:/DevTools/tomcat/jakarta-tomcat-5.5.9/webapps/coffeebreak/WEB-INF/cl
asses/org/coffeebreak/model/attribute-rules.xml
2007-01-16 13:55:42,128 WARN
[org.coffeebreak.config.XmlConfigInitServlet] - Finished parsing the
attribute XML config file.
2007-01-16 13:55:42,128 WARN
[org.coffeebreak.config.LoadErrorProperties] - Loaded error.properties
file.
2007-01-16 13:56:16,421 ERROR
[org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/coffeebreak
].[CoffeebreakAppServlet]]
- Servlet.service() for servlet CoffeebreakAppServlet threw exception
java.util.MissingResourceException: Can't find bundle for base name
/error.properties, locale en_US
	at
java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:8
36)
	at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:805)
	at java.util.ResourceBundle.getBundle(ResourceBundle.java:549)
	at org.coffeebreak.wrapper.ActionError.<init>(ActionError.java:21)
	at
org.coffeebreak.views.EditUserPane.validateFields(EditUserPane.java:159)
	at
org.coffeebreak.views.EditUserPane.processSave(EditUserPane.java:148)
	at
org.coffeebreak.views.EditUserPane.actionPerformed(EditUserPane.java:141)

Why is ActionError having trouble finding the error.properties file?
Is there a way to use my InitServlet's getter to set the resource
bundle? Am I going about this the wrong way?

Sincerely,

James

---------------------------------------------------------------------
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: Configurable Errors

Posted by James Dekker <ja...@gmail.com>.
Chris!

You hit the nail on the head!

Removing the .properties extension made it work!

Thank you so much! You rock!

-James

On Jan 16, 2007, at 3:25 PM, Christopher Schultz wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> James,
>
> James Dekker wrote:
>>         m_resource = ResourceBundle.getBundle("error.properties");
>
> When you use ResourceBundle.getBundle, you don't put the ".properties"
> extension on the file. You need to change this line of code to this:
>
> m_resource = ResourceBundle.getBundle("error");
>
> By the way, it looks like you are on your way towards building an
> internationalized component to help manage error messages, probably
> wrapped-up with a validation framework.
>
> Unless it is very important to develop this component in-house,  
> might I
> suggest looking at the jakarta commons validator package
> (http://jakarta.apache.org/commons/validator/)?
>
> I have never used commons-validator alone... I have always used it in
> conjunction with Apache Struts, with which it is very well integrated.
>
> Just a thought.
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.6 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iD8DBQFFrV7t9CaO5/Lv0PARAiHsAJ9wyTOqkQ7Zhj/vwOleb9NTWRk/zwCgpz49
> BzEk86aDio/kxYmdxJgPlNE=
> =t9KI
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> 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: Configurable Errors

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

James,

James Dekker wrote:
>         m_resource = ResourceBundle.getBundle("error.properties");

When you use ResourceBundle.getBundle, you don't put the ".properties"
extension on the file. You need to change this line of code to this:

m_resource = ResourceBundle.getBundle("error");

By the way, it looks like you are on your way towards building an
internationalized component to help manage error messages, probably
wrapped-up with a validation framework.

Unless it is very important to develop this component in-house, might I
suggest looking at the jakarta commons validator package
(http://jakarta.apache.org/commons/validator/)?

I have never used commons-validator alone... I have always used it in
conjunction with Apache Struts, with which it is very well integrated.

Just a thought.
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFrV7t9CaO5/Lv0PARAiHsAJ9wyTOqkQ7Zhj/vwOleb9NTWRk/zwCgpz49
BzEk86aDio/kxYmdxJgPlNE=
=t9KI
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
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