LightGuard's Random Bytes

28 January 2011

Seam Catch Beta1 was released last night! I know it's been a while since I've said anything about Catch and there's been one release between now and the last blog entry, sorry for that. This release is what was scheduled for Alpha4 but we decided to call it a beta and get it setup for inclusion into the Seam 3 bundle beta scheduled shortly. This release is mostly concerned with API changes (the API should now be frozen unless there are bugs found, please see JIRA for more detailed info), but there are couple of features and tweaks that I'd like to highlight:

Better alignment with @Observers

Modification of execution of handlers

Special considerations for SQLExceptions

Better alignment with @Observers

Exception Handlers are becoming more closely aligned with @Observers, while still retaining the enhancements necessary to support this application; with this release the @Handles annotation can be placed on any parameter of the method! Best practice should still be to place the annotation on the first parameter as it reads a little better, but the following code sample will work just fine:

The other two parameters are injection points and will be looked up using the BeanManager. This follows the CDI spec according to section 10.4.

Modification of execution of handlers

See SEAMCATCH-31 and SEAMCATCH-32 are extra detail on this change. In short, there has been some confusion about when exception handlers are executed. If your handlers rely on other handlers in the chain being executed in a particular order this very likely is a breaking change. The exception stack is still unraveled and handlers are run against the actual root cause first and Catch moves up the stack from there. The major change is how the type hierarchy for a given exception is used (also note there was an API change here). The way it works now is that any handlers that handle super types of the exception and are marked as TraversalMode.BREADTH_FIRST will be invoked first, followed by handlers marked with TraversalMode.DEPTH_FIRST starting with the actual type of the exception and working up the type hierarchy. This happens for each exception in the stack, however, a handler will still only be invoked once, unless it calls unmute().

Special considerations for SQLException

For anyone that's ever looked at a stack trace that includes a SQLException and been frustrated at not actually seeing the error from the database, SEAMCATCH-37 is for you. When unwrapping an exception stack, Catch will also unwrap any underlying SQLException instances using the getNextException() method and add that to the stack. Now you can finally see what your database is trying to tell you without having to debug or add your own catch block!

Going forward

Core Catch concepts should now be frozen as far as the API goes. We may add additional functions such as filtering a stack trace or other subsidiary features, but they should not affect the core API as it currently exists in this release. If you have a feature request, please add a JIRA ticket. Happy coding!

08 December 2010

Over the weekend Seam Catch Alpha2 was released! I'm trying to do a two week timebox release cycle for Seam Catch. Alpha 2 contains some minor API changes (some additions, and a couple of renames), a new qualifier and a couple of new objects for better framework integration. The largest of these enhancements is the ability to support ServiceHandlers.

Qualifier changes

The @HandlesExceptions is no longer a qualifier, this was needed to support ServiceHandlers, and is simply a marker anyway. This shouldn't affect users.

We've added a new qualifier @CatchResource to help distinguish resources injected into handlers that are only for Catch use. Here's an example:

It doesn't do much, but this shows that the Logger instance being injected is a special log configured only for Catch. This qualifier can be applied to anything and helps the developer visually see what resources are really being used.

ServiceHandlers

This feature is a great feature that really makes exception handling easy. So far we've seen examples of creating handlers with full method implementations, if you're doing something specialized, but repetitive, such as a JAX-RS error response you end up writing the boiler plate code over and over again just to change a few things (the message and the status code). Now you can do this sort of thing with only writing the boiler plate part once, the rest is all declarative, take a look!

There are three handlers right there, nothing else (save the [ServiceHandler][] implemenation) is needed! These handlers all send an error response for a JAX-RS request. They all change the status code returned and the message. What does it take to implement this? Aside from the new annotation (@SendHttpResponse), there's two other classes that need to be written:

Most of this is building up the error from the annotation, but it's only 26 LoC, and you quite a bit of reuse from them. That's all there is to it! Framework integrations should implement some of these for basic things, but you can further create them for yourself if you have some repetitive action that you can extract in your exception handling practices. Look for another release (Alpha3) in a couple of weeks and a Beta out by the end of the year!

Or at SourceForge. I'm very interested in any feedback that you may have.

What is Catch?

Catch is the exception handling infrastructure in Seam3. I like to refer to it as Next Generation Exception Handling. It really goes beyond what was available in Seam2 and offers a complete solution for Exception Handling. It's built on top of CDI events so the entry point is very minimal and usage is quite easy.

Usage

For those who are familiar with CDI events, creating your own handlers will be very familiar. Handlers are quite similar to CDI Observers, though there are a few differences:

handlers must be contained in a bean marked by @HandlesExceptions

the first parameter in the method must be annotated with @Handles and be an instance of CaughtException&lt;T extends Throwable>

handlers are ordered before they're invoked

Keep those things in mind and the rest is very easy.

Creating a Handler

The docs really cover this very well, but here's a simple handler to get you started:

NOTE: The best way to tie this handler into JSF is by adding a JSF to Catch bridge by creating an ExceptionHandler (this will be added to Catch in the next release, as a separate jar) to fire the ExceptionToCatchEvent.

There's not much there as you can see. We're saying this is a general handler (the CaughtException type is Throwable) and we want it run towards the end of the cause container traversal (precedence = -100). If you're not familiar with JSF, this is will navigate the user to the error page at the end of the exception handling. Of course this would require some extra JSF integration pieces to produce the NavigationHandler such as Seam Faces or Apache CODI, or your own producer. The NavigationHandler is injected via CDI, in fact, any additional parameters past the CaughtException param will be injected for you.
Anything that you need for your exception handling, as long as CDI can create it, can be injected for you.

Entering Catch

Above I mentioned the ExceptionToCatchEvent. This event is all that's needed to start the handling process. Something as simple as

That's really all there is to it! I'm looking forward to any feedback you may have, and bugs if you find them. Bugs should be
filled in JIRA. In the next release look for bridges for JSF, JAX-RS and others, and the ability to filter stack traces!

28 February 2010

I would like to thank those who provided feedback for Part I of this exception handling series.
A special thanks goes out to Dan Allen and Dan Hinojosa for the wonderful constructive feedback.
I'm sure over time these posts will improve and become better.

This entry was originally going to be talking about custom handlers and provide some code; however,
due to family and work factors I have not been able to finish what I wanted for this. Instead we'll be diving
even further into the built-in handlers and providing examples. At the end I'll also give a sneak peek of the
exception handling framework that will make up Part III of this series, hopefully complete in March.
To cut down on the length of this post I've linked to Seam's Fisheye install of the 2.2.0.GA release. With that, let the journey into org.jboss.seam.exception begin!

This class (org.jboss.seam.exception.Exceptions) is a built in Seam class, all you need to do is obtain a reference to it (either via
injection @In("exceptions") Exceptions or Exceptions.getInstance()) and call the getHandlers() method to add your own exception handlers. Typical interaction is done by adding exception handlers via the annotations or pages.xml. Let's begin the exploration with the initialize()
method where all the initial setup and creation of the built-in handlers happen. Handlers are added in the following order:

AnnotationRedirectHandler

AnnotationErrorHandler

Handlers declared in pages.xml

User defined custom handlers (more on this later)

At first
glance, it appears that an old deprecated way of adding exceptions is first
added, then the exception handlers added via page(s).xml; however, this is not the case as
each of those ExceptionHandlers is added to a list, then added at the end. Just before any
handlers that the user specifies with pages.xml or custom handlers the Seam Debug Page is
added.

It's important to know that while in debug (typically only development mode) your
annotated exceptions will take precedence over the Seam Debug Page, and nothing after the
debug page will be triggered (the debug page is a global catch all handler).

The parsing of the XML files happens in the parse() method which does some basic XML parsing
looking for the exception elements. The class and logging level (if configured) are pulled
from the exception element, and a new ConfigRedirectHandler or ConfigErrorHandler is created
(more on these handlers later). If there is no class attribute specified then a catch-all
handler, that handles Exception.class is created.

The private createHandler() method does the work of pulling additional information about the configured
handler such as

ending conversations

redirecting

adding FacesMessages

finding the http error code, etc.

The correct handler is then created and returned to be added into the list.

The only other part of this class that has any importance for the developer/user is the
handle() method, which is really just an iterator over the handlers looking for a handler for
the exception which was thrown. Though there is a catch: Nested exceptions are unwrapped and
handled from the bottom up (though in the Seam Debug Page the exception is recreated so you
may need to scroll to the bottom to actually find the real exception). If a long running
conversation is active then the "org.jboss.seam.handledException" object, which is the
exception object, is added to the conversation context. If logging is configured for the
exception handler a log message is added then two events are raised (regardless of logging):
"org.jboss.seam.exceptionHandled." + cause.getClass().getName() and
org.jboss.seam.exceptionHandled allowing a further user extension point if so desired. If
no handler is found then an event is raised ("org.jboss.seam.exceptionNotHandled") and
the exception is rethrown. Even if this event is observed, (the exception is passed as
a parameter to the observer's method) it will not be able to influence the exception being rethrown.

Any handlers the developer creates and adds to the Exceptions list will be added at the end of
the list (unless the list is manually modified and rearranged, which is allowed).
Depending on the specific
needs of the application this is a great place to add a general catch all handler for the
application (unless a general handler in pages.xml is sufficient). When adding custom
handlers or any handler really, it is important not to create multiple handlers for the same
exception as the first in the list will be called and the list of handlers will terminate
with that handler.

ErrorHandlers (AnnotationErrorHandler and ConfigErrorHandler) are a specific type of ExceptionHandler, they're used to send an HTTP Error
code to the browser, probably more useful when combined with Seam's Web Service or REST integration.
There is not a lot of code to these handlers, in fact, they are only data containers used by the
parent class ErrorHandler which handles sending the error to the browser. However, they are
great examples of reuse for exception handling, and make for good templates to follow in abstracting
logic in custom handlers. The two sub classes hold

The redirect handlers (AnnotationRedirectHandler and ConfigRedirectHandler) are in the same category as the error handlers: there's not much to
them, and most of the interesting code is in the parent class RedirectHandler. The
first bit of code in the handle() method is finding the viewId if none was specified
in the handler instance, but it really isn't related to the actual exception handling. Next is the addition of a
defined FacesMessage, ending the conversation (again, if configured), and finally
the redirect.

This is a great handler (source) along with the associated code in the jboss-seam-debug.jar to fully
understand the potential of exception handlers in Seam. The handler is really
the entry point into the debug system in Seam. Java code wise, there's not much
to it, it's a redirect handler that sets some additional parameters for the redirect.
The whole process of Seam Debug really could be it's own blog entry or series of entries.
It's pretty involved: it includes a PhaseListener, a SerializationProvider, and some
custom Introspection. Because it's such a large, involved piece of code, I'll cover it more
fully in a future post, or perhaps a whole series.

Examples

Annotations

Consistent with the rest of the framework, Seam provides a few Annotations to help with
exception handling, these were briefly explored in Part I. Here are some examples on
how they're applied to application code.

The @ApplicationException annotation is a mirror of javax.ejb.ApplicationException
with additional support for ending a conversation. It is also the entry point Seam uses to configure annotation based exception handlers and must be used if the @HttpError or @Redirect annotations are to be used. In this particular example some of the
defaults are used such as not ending the conversation (with the end attribute) and there
is no redirect happening, but the message (which can also take an EL expression for i18n,
or substitution purposes) will be displayed on the page where the exception occurred.

This next example might be useful with Seam's WebService support, it will return an
HTTP error code 500 (probably not the most useful for the clients, but it's an example, right?).

As noted earlier, these two exception handlers (AnnotationRedirectHandler and
AnnotationErrorHandler) are the first two handlers in the list and will be the first called
to handle the exception, if they are capable, as defined by boolean isHandler(Exception).
The classes use the meta data from the annotations to determine
if the exception can be handled by one of these two classes. Next up is a closer look at
defining the same exception handlers in pages.xml.

Exception handlers can only
be defined in the pages.xml, not in the individual page.xml files,
which in my opinion is a bit of a short coming. There could be times when the same exception should
be handled differently for different pages.

Pages.xml

Because the handlers for pages.xml are roughly the same handlers, different sub classes but they do the same
as the annotation sub classes, the above handlers could be declared via pages.xml with
the following XML:

Pretty simple stuff. These exceptions will follow the annotations in the list (it's worth
noting that each exception that is declared in the XML creates a new instance of the associated
handler, unlike the annotation counterpart). If both were declared in the same application
the annotation ones would be used. Both ways get the job done, and for your own exceptions
it's really just a matter of preference.

The pages.xml declaration really lends itself better
for exceptions that cannot be annotated, such as form Hibernate or JPA, please see Part I for examples.

Part III of this series will demonstrate an exception handler framework that could be plugged into
Seam and will allow chaining for handling (for example send an email or IRC message and create a
bug report for the exception). It should be generic enough to work with Seam, JSF2
(Ed Burns talked about JSF2 Exception handling on his blog), or even the base JRE (yep, 1.5 added Exception handling). It will be hosted on GitHub.

31 January 2010

This three part entry will focus on the mechanisms Seam provides for handling exceptions gracefully -- both out of the box and extensible. An in depth look at where this happens in the Seam code base and user programmable extension points will be covered in Part I. Part II will dive even further and provide examples, and Part III will cover a custom ExceptionHandler implementation.
Exception handling in any large application is something that's typically debated at least once: What does it mean, is it simply logging, what do we show the user, can our framework help etc. etc. All software developers will run into this debate at least once in their career, even if it's as simple as "what should I put in that catch block?"
It's pretty safe to say swallowing exceptions (as a general rule of thumb) is a bad idea. Of course System.out.println(...) or log.error(...) really isn't much better. Typically the user should be informed that something they have done has resulted in an error on the server, and at the very least ask them to try again. We've all seen pages like the following: An error has occurred, please try again. If the error continues, please contact the webmaster / admin / whatever. This is a cop out and typically a sign of poor exception handling. At least the user didn't see that nasty stack trace or the 500 page from the server. If it's being left up to the user to make contact about an error, chances are it won't happen and the development team probably won't hear about it. Enter the debate about handling those exceptions, and being more pro-active about knowing about errors. Covering each of those questions is beyond the scope of this series, but the question about framework help certainly applies!

Built-in Handling

Seam proveds a simple, yet robust solution for integrating your exception handling strategy. Two methods exist out of the box for rudimentary handling: <exception> in pages.xml and annotations directly on application specific exceptions. Dan Allen covers these two options nicely in his book Seam in Action section 3.6, (if you don't already own a copy, go get one. It's a life saver) so these will only be briefly reviewed. A third option exists (and the before mentioned options, as well as the powerful Seam Debug page are all built on top of this mechanism) to allow complete customization of display to the end user and actions on the back-end: ExceptionHandler classes.

pages.xml

In a basic seam-gen application one will find the following stanzas of XML relating to exception handling:

Redirects are the typical body of these XML blocks. FacesMessages can be added to the redirect as well as ending conversations. Typically these are used for exceptions the developer does not create, and therefore cannot be annotated; however, there may be uses for this style of handling with application exceptions. Please note that order does matter. If the catch-all handler is first, none of the other handlers will be used. Each of these entries creates an ExceptionHandler instance which is then added to the chain of handlers, which is covered later in the entry.

Annotations

Seam has three annotations to help with exception handling: @Redirect, @HttpError, and @ApplicationException. Only one of @Redirect or @HttpError may be used on an exception. Both allow for an info level FacesMessage to be added via the message attribute. The @Redirect annotation allows input for a view-id to which the user will be redirected. @HttpError simply returns the specified HTTP Error code. The last annotation, @ApplicationException provides the ability to rollback the transaction and end the existing conversation. These annotations are added directly on the Exception class that is created.
Both of these built-in methods are built on top of two classes in Seam: org.jboss.seam.exception.Exceptions and org.jboss.seam.exception.ExceptionHandler. These two classes and their usage make up Seam's infrastructure for exception handling in an application, and provide customization to the developer.

Extensible Handling

The above two methods work well for simple cases. What if a team wants to be more pro-active about addressing errors? Could the application send the team emails about exceptions, or create bug tickets, or perhaps create a contextual log entry? This is where the exception handling capabilities of Seam really shine, and the answer to the above questions are all yes!

The contract for this class is pretty straight forward. The only methods that need to be implemented are isHandler and handleException. Of course the isHandler implementation is trivial, and based on the needs of the handler the handleException implementation could be fairly simple as well. Take note that ExceptionHandler is a subclass of Navigator. There are methods defined in that class which may be helpful in some exception handlers.

Exceptions

This class is used in the org.jboss.seam.jsf.SeamPhaseListener before and after each phase of the JSF lifecycle and also in the org.jboss.seam.web.ExceptionFilter, wrapping the filter chain, and used again after any redirects that may happen after the filter chain has completed. As Dan Allen described it in Seam in Action: "A try-catch around the [whole request]" (Seam in Action pg. 126).
There's only one method that needs any kind of attention: getHandlers. It returns a list that is used to add or remove ExceptionHandlers as needed. This method should be called in the @Create method, or constructor of any ExceptionHandler. ExceptionHandler classes should also be marked with @BypassInterceptors as there's no need to do bi-jection. A simple POJO, though, will typically suffice.
Exception handling in Seam can be very powerful if used to it's full potential and should be an excellant tool in crafting an exception handler strategy for your application. The next entry in this series will demonstrate a custom exception handler in action.

12 September 2009

Over the past few days I've been working on getting HornetQ (http://www.hornetq.org) to work on Jetty (http://www.mortbay.org/jetty/) -- specifically the embedded Jetty that Gradle (http://www.gradle.org) uses. I would first like to thank those on the HornetQ irc channel (irc://freenode.net:6667/hornetq) for helping me out and offering advice. Specifically I would like to thank Clebert Suconic and Andy Taylor for looking at some of my code and making suggestions that ultimately led me to this solution. It should be noted that the two solutions I will be discussing here are probably not the only roads to success. I'm quite confident the same could be achieved programmatically by setting up HornetQ in a bean that's loaded at application deploy time or similar fashion.

HornetQ setup using jetty-env.xml

Both of my two solutions are using jetty-env.xml to call the needed setup functions for HornetQ. The same could be achieved with jetty.xml if deploying to a standard install of Jetty. More information about these configuration files can be found at http://docs.codehaus.org/display/JETTY/Configuring+Jetty. In short these files act as an XML means of working with Java objects. Objects can be instantiated, methods called, etc. All that's being done here is calling the methods in the HornetQ API to startup HornetQ in Embedded mode. Below is the jetty-env.xml I used:

An instance of JMSServerManager is being created and the needed configuration objects are being created inline in this example. It's very important to make the start call on the JMSServerManager before any other calls on the object to create queues, topics or connection factories are made, otherwise you'll get exceptions in your log saying the server hasn't been started yet. It's also important to make sure the full JNDI names are used. jetty-env.xml is used to bind elements to JNDI for you but because the JMSServerManager will be doing the binding for us I found that the full JNDI name must be used. If programmatic access to the JMSServerManager is needed this is also bound to JNDI for us by Jetty under jms/serverManager -- which doesn't use the full JNDI name because Jetty is handling the binding, a slight bit confusing I know. Work with JMS can now proceed using standard JNDI lookups and JMS code to send and receive messages. All of this code is available at my GitHub repo: http://github.com/LightGuard/Research---Development--JEE-/tree/hornetq feel free to check it out, fork it, whatever :)

It's much smaller this time as most of the configuration has been moved into the hornetq-configuration.xml and hornetq-jms.xml files (both of which are on GitHub). That's really all there is to it! My next steps will be to get logging of hornetq (and hopefully Jetty) using log4j and same log file as the rest of the application.

28 April 2009

Every software project has dependencies: your own resources, your own APIs you've created, third party APIs and projects, etc. We've all had to deal with them at some point in our career. Recently while playing with Gradle (www.gradle.org) I've come to a realization about how they should be handled correctly. In this post, I'll talk about the two most prominent kinds of dependencies, their scopes, and where they fit into an application. I will not be discussing packaging or how the different scopes must be resolved to create a distributable / deployable artifact. Being a Java developer this is seen from within the Java space, but the concepts are universal to all software projects.

Dependency Types

There are basically two types or categories of dependencies: first level and transitive. First level dependencies are those resources that your application directly relies upon. Examples of first level dependencies include the language in which the project is written, entities your project directly uses such as an XML parser, images, and classes from a third party project. Transitive dependencies are dependencies of your first level dependencies. An example from the Java world could be commons-logging which is dependent on some logging implementation, commonly log4j or JDK logging. For commons logging the log implementation is a first level dependency, but for your project it's a transitive dependency. Another example may be an SAX XML parser (or any other XML parsing API). The SAX API is used directly by your project and is therefore a first level dependency, but it requires an implementation, possibly Xerces, which would be a transitive dependency of your project.
This definition of dependencies has really been ingrained in me while using and trying Gradle (a build system [yes, another one] written in Groovy). In the past I've used Maven or Ivy (basically as a Maven alternative, but it does much more in the world of dependencies). Maven introduced me to the concept of transitive dependencies and how they fit into it's life cycle and therefore your project's life cycle. I owe a great deal to Maven for introducing me to the concepts, but Maven has a few blemishes (at least I think so) in the way it handles these different types of dependencies. Maven will by default (I don't think you can change this) include all of the transitive dependencies of the same scope in your classpath, which in my opinion, is incorrect. Here's an example taken from JSFUnit:
pom.xml:

For this project cssparser, nekohtml and xalan have been configured as first level dependencies, but the effective classpath contains their compile (first level) dependencies as well. If the project relies on these libraries it should state them explicitly and not rely on the crutch of having them as transitive dependencies. Ivy usage can fall into the same problem, but this is not the case in Gradle (at least not without changing the default), which I believe is the correct way of handling first level dependencies. With Gradle the compile scoped (more in the next section) is not resolved transitively, so you'll have compile time errors if you have not declared a needed dependency.

Life cycle Scopes and Dependencies

The build life cycle for a software project can be distilled into three phases: compile (if needed), test, and runtime. Test is a little special because it contains two phases itself: testCompile and testRuntime, which are extensions of compile and runtime. So where do the different types of dependencies come into play? Your first level dependencies become your compile dependencies and runtime dependencies are pretty much your compile dependencies with transitive dependencies and a few other things that may be provided for you like container provided dependencies (though, those are arguably transitive dependencies of any third party dependency) such as a messaging provider, an HTTP implementation, transaction support, etc. The test dependencies extend compile and possibly runtime, and add their own dependencies for testing: a testing framework, mocking framework, possibly a slimed down server, and others, which of course would be first level dependencies for your tests.

Summary

To recap, there are two different kinds of dependencies: first level and transitive. First level are dependencies needed to build an run your project. Transitive dependencies are those dependencies of your dependencies. A software build life cycle essentially has three phases: compile, test, run. The compile phase should only use your first level dependencies. Runtime extends compile and is resolved transitively. Test extends both compile and runtime (though at different times) and uses its own dependencies as well. I hope this has been informative and helped others understand the relationship and distinction of dependencies and a software project.