-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Catching XML Errors
PostPosted: Mon Oct 27, 2003 11:00 am 
Beginner
Beginner

Joined: Fri Oct 24, 2003 4:28 pm
Posts: 20
I'm working on a JUnit test case to run all the mapping files thru Configuration to verify they are correct. We have a lot of errors sneaking thru the build.

One issue I'm having is that I'm not getting the exceptions when the XML in the mapping file is bad. It shows up in the log but does not pass the exception out from Configuration.addInputStream(). I'd like to have this exception get passed back up so I know if parsing failed

Any ideas how I can get this to work?

Also, I still can't figure out how to get those JCS error messages out of my log file. Can anyone point me to info on that?

Thanks,
John


Top
 Profile  
 
 Post subject: One solution
PostPosted: Mon Oct 27, 2003 12:36 pm 
Beginner
Beginner

Joined: Fri Oct 24, 2003 4:28 pm
Posts: 20
Being the impatient guy that I am, I came up with the following hack for both issues by adding a filter to log4j. Let me know if anyone has a better idea:

Code:
/** holds parsing exceptions */
private List parsingExceptions = new ArrayList();

/** custom log4j filter to remove JCS messages and watch for XML exceptions */
private Filter filter = new Filter() {
  public int decide(LoggingEvent event) {
    LocationInfo info = event.getLocationInformation();
    String className = info.getClassName();
    if (CompositeCacheConfigurator.class.getName().equals(className)
    && "Could not instantiate auxFactory named \"DC\".".equals(event.getMessage())
    && "359".equals(info.getLineNumber())) {
      return Filter.DENY;
    }
    if (net.sf.hibernate.util.XMLHelper.ErrorLogger.class.getName().equals(className)) {
      parsingExceptions.add(event.getThrowableInformation().getThrowable());
    }
    return Filter.ACCEPT;
  }
};

/**
* Configure the session factory
* @throws Exception
*/
public void configure() throws Exception {
      
  if (sessionFactory != null) return;
      
  Logger logger = Logger.getRootLogger();
  for (Enumeration e = logger.getAllAppenders(); e.hasMoreElements(); ) {
    ((Appender) e.nextElement()).addFilter(filter);
  }

  net.sf.hibernate.cfg.Configuration cfg = new net.sf.hibernate.cfg.Configuration();      
      
  List classNames = Configuration.getInstance().getMappedClasses();
  for (Iterator i = classNames.iterator(); i.hasNext(); ) {
      cfg.addClass(classForName((String) i.next()));
  }
      
  if (parsingExceptions.size() > 0) {
    throw new Exception("Error occured parsing Hibernate XML files.  See log for more details");
  }
      
  sessionFactory = cfg.buildSessionFactory();

}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 27, 2003 5:37 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Please add an issue to JIRA.

What I will do is have an option or something to make Hibernate throw an exception when validation fails. Have been meaning to do this.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.