Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Apache Tomcat »

Configurable Errors

James Dekker

2007-01-16

Replies:

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@(protected)
To unsubscribe, e-mail: users-unsubscribe@(protected)
For additional commands, e-mail: users-help@(protected)

©2008 junlu.com - Jax Systems, LLC, U.S.A.