72Miles

Recent News

Archives

June 5, 2008 @ 8:48 pm

Spring: Loading Multiple Contexts


I have already argued that many application contexts are better than a single application context. But how the heck do you load more than one context?

There are a couple of ways to do this.

web.xml

Your first option is to load them all into your Web application context via the ContextConfigLocation element. You’re already going to have your primary applicationContext here, assuming you’re writing a web application. All you need to do is put some white space between the declaration of the next context.

<context-param>
    <param-name>
        contextConfigLocation
    </param-name>
    <param-value>
        applicationContext1.xml
        applicationContext2.xml
    </param-value>
</context-param>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

The above uses carriage returns. Alternatively, yo could just put in a space.

<context-param>
    <param-name>
        contextConfigLocation
    </param-name>
    <param-value>
        applicationContext1.xml applicationContext2.xml
    </param-value>
</context-param>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

applicationContext.xml

Your other option is to just add your primary applicationContext.xml to the web.xml and then use import statements in that primary context.

In applicationContext.xml you might have…

<!-- hibernate configuration and mappings -->
<import resource="applicationContext-hibernate.xml"/>

<!-- ldap -->
<import resource="applicationContext-ldap.xml"/>

<!-- aspects -->
<import resource="applicationContext-aspects.xml"/>

Which Strategy?

I always prefer to load up via web.xml This allows me to keep all contexts isolated from each other. With tests, we can load just the contexts that we need to run those tests. This makes development more modular too as components stay loosely coupled, so that in the future I can extract a package or vertical layer and move it to its own module.

Any benefits to going with the application context import method?

Filed under Software Development, Spring · 2 Comments »

June 5, 2008 @ 7:52 am

Architecture Rules 102

This is the second part in an introduction to Architecture Rules.

The first part, Architecture Rules 101 went over a short, sweet, and simple configuration.

Reminder

So remember, the basics of Architecture Rules is that it is

  • Java.
  • Open Source.
  • Asserts architectural rules via unit tests.
  • You write your rules in XML.
  • Your run your tests with junit, ant, or a continuous integration server.

Source Locations

In this second installment of introductions to Architecture Rules, we’re going to go over source locations which enables the tool to support multi module projects, and allows you to define when a particular source must exist, or may be optional.

By allowing a source to be optional, you can have different developers with different modules of code. So while one developer may have all eight modules of a project, another developer may only need to work on two or three of the modules.

Configuration

Here is an example that uses all of the current features supported by Architecture Rules 2.0.3.

<configuration>

<sources no-packages="exception">
<source not-found="exception">core\target\classes</source>
<source not-found="ignore">web\target\classes</source>
<source not-found="ignore">ws\target\classes</source>
</sources>

</configuration>

The first property that we see is the sources no-packages="exception". This tells Architecture Rule that, after reading all of the defined source directories, if no classes are found, then throw an Exception, more specifically, a NoPackagesFoundException. Here are the configuration options for no-packages:

sources no-packages values
Value Description
exception causes a NoPackagesFoundException when zero classes are found among all of the defined sources
ignore default all test continue although there are no classes to test

The next interesting thing that we come across is <source not-found="ignore"> This configuration causes a SourceNotFoundException when the given path can not be found on the file system. This allows you to ensure that specific modules are present in the test. Alternatively, if you don’t particularly care if some source path is present, you can use the ignore value, which happens to be the default value. In the example above, the core module must be present for the tests to continue. The web and web services (ws) modules are optional.

source not-found values
Value Description
exception causes a SourceNotFoundException when the path does not exist
ignore default all test continue although the classes at the given path will not be included in the test

So, after showing the simplistic configuration in Architecture Rules 101, we have now shown how you can customize your configuration to fit your needs with little work.

In the next Architecture Rules lesson, we’ll demonstrate how to get the Configuration and show how easy it is to provide programmatic configuration instead of or in addition to the XML configuration. Also coming up right after that, we’ll finally introduce the Maven 2 Architecture Rules plugin that Mykola has been working on for the past couple of months.

Filed under Architecture-Rules, Open Source · No Comments »

May 29, 2008 @ 9:40 pm

Exceptional Metadata

Exceptions are an important part of any programming language, especially Java. Java has two types of Exceptions. No, not checked and unchecked, but rather useful and useless. The utility of the Exception is up to you, the developer. What am I talking about? Bear with me, read on, and please share your comments at the end.

Core data

Exceptions are comprised of some sort of primary data, for example, the name or type of Exception, and maybe the exception’s message. Take, for example, a ConnectionTimeoutException.

You, as a developer, have a pretty good idea as to what is going on when you see this at the top of the stack trace. You may be able to read the message, if the developer provided one, and get a little more information about the exception.

/**
* Signals that a timeout occurred while opening the socket.
*/
public class ConnectionTimeoutException extends IOException {

/** Create an instance */
public ConnectionTimeoutException(String message) {
super(message);
}

}

Default Metadata

If you read a stack trace or hack the exception’s API, you can even get some more information about the it. Such as the Class that threw the Exception, or the line number where the Exception occurred. You might be able to review the hierarchy of the class to garner some information. For example, this ConnectionTimeoutException extends from IOException. That tells you something.

The Problem

The problem with this type of Exception is that someone, like a developer, needs to review this information in order to track down the cause of the issue. That means locating the stack trace, and determining the parameters and other configurations that were present at the time of the Exception.

Java, however, and most any other language, provide a means for supplying pertinent metadata when an Exception occurs. They are called properties, getters and setters, or mutators. You, as the designer, just need to include those properties when you design the class.

Example

Consider the ConnectionTimeoutException. Wouldn’t it be nice to know the source and destination URIs of the connection? And what the timeout value was?

/**
* Signals that a timeout occurred while opening the socket.
*/
public class ConnectionTimeoutException extends IOException {

private long timeout;
private String source;
private String destination;

/** Create an instance */
public ConnectionTimeoutException(long timeout, String source, String destination) {

super("failed to connect to " + destination + " within alloted " + timeout + "ms");

this.timeout = timeout;
this.source = source;
this.destination = destination;
}

public long getTimeout();
public String getSource();
public String getDestination();
}

Would you agree that the Exception that knows the timeout value and destination URI is more valuable to you, the developer, than some Exception with an arbitrary message that the designer may or may not have populated with relevant information?

Benefits

The resulting Exception implementation provides a constructor requiring specific values. The developer who consumes this Exception no longer needs to come up with some arbitrary message. He or she just needs to provide the requested parameters, which is known or can be read from some HttpClient implementation.

The developer who is catching and dealing with this ConnectionTimeoutException now has a few more options. Perhaps an error message will be displayed. The developer can write a custom message and use the timeout property to notify the end user what the timeout was. The developer doesn’t need to parse it from a String message property.

If the code throwing the ConnectionTimeoutException is a framework to be used by other developers, the developer has a lot more options to handle this Exception. Perhaps they are writing a GUI for your tool and can now provide their end users with more information.

Metadata in the Real World

I decided to write about this topic after a user of our Architecture Rules (Assert your Architecture!) open source software asked us to enhance our Exception APIs.

Our CyclicRedundancyException simply contained a message that described a package by its name, and then listed all of the packages involved in a cyclic dependency with said package.

...architecturerules.exceptions.CyclicRedundancyException:
cyclic dependencies found:

-- test.com.seventytwomiles.services
|  |
|  |-- test.com.seventytwomiles.model
|  |
|  |-- test.com.seventytwomiles.dao.hibernate
|
|
-- test.com.seventytwomiles.model
|  |
|  |-- test.com.seventytwomiles.services
|
|
-- test.com.seventytwomiles.dao.hibernate
|
|-- test.com.seventytwomiles.services

steven.devijver asked that we provide an API to retrieve a List of the packages involved in the cycle. This way Steven can construct his own message, or wrap the data in his own domain Exception, or whatever else he had in mind. We accommodated him by providing a Map containing a key (package name) and value (List of package names involved in a cyclic dependency) pair. This should be useful when we go to write the Maven 2 plugin report for this tool.

As another example, we inherited a DataFinderException in another project we’re working on. The only constructor for this Exception was a String named message. We introduced a new constructor and two properties — name and value — and getters for each. Now, the code that constructs DataFinderExceptions no longer needs to build some complex message with a StringBuffer before throwing the Exception.

StringBuffer message = new StringBuffer();
message.append("Could not find entity with");
message.append("userId");
message.append(" = ");
message.append(userId);

throw new DataFinderException(message.toString());

became

throw new DataFinderException("userId", userId);

A cleaner execution, fewer lines of code, and useful metadata that can be passed on to the end user, logged, or handled eloquently.

How do you design your Exceptions to provide useful metadata? Can you think of any Exception in the JDK that provide such metadata?

Filed under Architecture, Architecture-Rules, Software Development · 2 Comments »

About

72 Miles Software - open source software, search engine optimization analytics, and software startup information. Software by design. Read More

Categories

Links

Sitemap