Java Mailing List Archive

http://www.junlu.com/

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

FileNotFoundException: When Trying to Use Commons Digester

James Dekker

2007-01-26


Hello there,

I am using the Apache Commons Digester as a way to load up and marshal
XML config files. Am using Tomcat 5.5.9 and JDK 1.5.

I have an init servlet which loads an xml file config file (holding
information to two more XML config files). I get the following
exception at runtime:

  INFO: Deploying web application archive coffeebreak.war
  2007-01-26 17:54:24,530
  DEBUG [org.coffeebreak.config.XmlConfigInitServlet] -
  commons digester rules location:
file:/C:/DevTools/tomcat/jakarta-tomcat-5.5.9/work/
    Catalina/localhost/WEB-INF/classes/org/coffeebreak/config/affiliate-rules.xml
  2007-01-26 17:54:24,935
  DEBUG [org.coffeebreak.config.ConfigHelper] - commons digester
rules location: file:/C:/DevTools/tomcat/jakarta-tomcat-5.5.9/work/
    Catalina/localhost/WEB-INF/classes/org/coffeebreak/config/attribute-rules.xml
  2007-01-26 17:54:24,935
  DEBUG [org.coffeebreak.config.ConfigHelper] - loading config for: Contractor
  java.lang.NullPointerException
   at java.io.FileInputStream.<init>(FileInputStream.java:103)
   at org.coffeebreak.config.ConfigHelper.parse(ConfigHelper.java:43)
   at org.coffeebreak.config.XmlConfigInitServlet.init(XmlConfigInitServlet.java:35)
   at javax.servlet.GenericServlet.init (GenericServlet.java:211)
   at org.apache.catalina.core.StandardWrapper.loadServlet (StandardWrapper.java:1091)
   at org.apache.catalina.core.StandardWrapper.load (StandardWrapper.java:925)
   at org.apache.catalina.core.StandardContext.loadOnStartup (StandardContext.java:3857)
   at org.apache.catalina.core.StandardContext.start (StandardContext.java:4118)

My XMLConfigServlet:

  public class XmlConfigInitServlet extends HttpServlet {

   List<AffiliateBean> affiliateList;
   
   public void init() throws ServletException {
     String affiliateFile = getInitParameter("xml-config-file");
     File xmlConfigFile = new File(affiliateFile);
            if (!xmlConfigFile.exists()) {
         
Logger.getLogger(this.getClass()).debug(affiliateFile + "   not
found " + xmlConfigFile.getAbsolutePath());
            }
     try {
       affiliateList = parse(xmlConfigFile);
       for (AffiliateBean bean : affiliateList) {
         ConfigHelper.parse(bean.getName(), bean.getFileName());
       }
     } catch (Throwable t) {
       t.printStackTrace();
     }
   }
   
   public List<AffiliateBean> parse(File xmlConfigFile) throws
IOException, SAXException {
     URL rules = this.getClass().getResource("affiliate-rules.xml");
     Logger.getLogger(this.getClass()).debug("commons digester rules
location: "+rules);
     Digester digester = null;
     try {
       digester = DigesterLoader.createDigester(rules);
     } catch (Throwable e) {
       e.printStackTrace();
       e.getCause().printStackTrace();
     }

     // Push empty list onto Digester's Stack
     List<AffiliateBean> affiliateList = new ArrayList<AffiliateBean>();
            digester.push(affiliateList);

     // Parse the XML document
     InputStream input = new FileInputStream(xmlConfigFile);
     digester.parse(input);
     return affiliateList;
   }
  }

ConfigHelper (This is where the exception actually occurs, look for my
comment, in order, to pinpoint the actual line of code):

  public class ConfigHelper {
   
   private static List<AttributeConfig> attributeConfigs = new
ArrayList<AttributeConfig>();
       private static Map<String, AttributeConfig>
attributeConfigMap = new HashMap<String, AttributeConfig>();
   
   public static void parse(String name, File xmlConfigFile) throws
IOException, SAXException {
     URL rules = ConfigHelper.class.getResource("attribute-rules.xml");
     Logger.getLogger(ConfigHelper.class).debug("commons digester
rules location: "+rules);
     Digester digester = null;
     Logger.getLogger(ConfigHelper.class).debug("loading config for: "+name);
     try {
       digester = DigesterLoader.createDigester(rules);
     } catch (Throwable e) {
       e.printStackTrace();
       e.getCause().printStackTrace();
     }

     // Push empty list onto Digester's Stack
     List<AttributeBean> attributeConfigList = new ArrayList<AttributeBean>();
            digester.push(attributeConfigList);

     // Parse the XML document
     InputStream input = new FileInputStream(xmlConfigFile); // <--
This is where the FileNotFound Exception is occurring.
     digester.parse(input);
     
            AttributeConfig config = new AttributeConfig(name,
attributeConfigs.size(), attributeConfigList);
        attributeConfigs.add(config);
            attributeConfigMap.put(name, config);

Logger.getLogger(ConfigHelper.class).debug("attributes: " +
attributeConfigList);
   }
   
   public static AttributeConfig getConfig(int i) {
     return attributeConfigs.get(i);
   }

       public static AttributeConfig getConfigByName(String name) {
            return attributeConfigMap.get(name);
       }

       public static AttributeConfig getDefaultConfig() {
            return attributeConfigs.get(0);
       }

       public static List<AttributeConfig> getConfigs() {
            return attributeConfigs;
       }
  }

Here's the affiliates-rules file:

  <digester-rules>
   <pattern value="affiliates/affiliate">
     <object-create-rule classname="org.coffeebreak.config.AffiliateBean"/>
     <set-next-rule methodname="add" paramtype="java.lang.Object"/>
     <set-properties-rule/>
     <bean-property-setter-rule pattern="name"/>
     <bean-property-setter-rule pattern="fileName"/>
   </pattern>
  </digester-rules>

AffiliateBean:

  public class AffiliateBean {
   
   private String name;

   private File fileName;

   public String getName() {
     return name;
   }
   
   public void setName(String name) {
     this.name = name;
   }
   
   public File getFileName() {
     return fileName;
   }
   
   public void setFileName(File file) {
     this.fileName = file;
   }
  }

The actual affiliates-config file itself:

  <affiliates>
   <affiliate>
     <name>Contractor</name>
     <file>/temp/contractor-config.xml</file>
   </affiliate>
   <affiliate>
     <name>Employee</name>
     <file>/temp/employee-config.xml</file>
   </affiliate>
  </affiliates>

Here's my web.xml file:

  <web-app>  
      <servlet>
          <servlet-name>xml-config-init</servlet-name>
          <servlet-class>
             org.coffeebreak.config.XmlConfigInitServlet
          </servlet-class>
          <init-param>
             <param-name>xml-config-file</param-name>
            <param-value>/temp/affiliate-config.xml</param-value>
          </init-param>
         <load-on-startup>2</load-on-startup>
     </servlet>
  </web-app>

Why is it failing inside ConfigHelper? For some odd reason, it can't
seem to find the contractor-config.xml file's location from the
affiliates-config.xml file.

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.