Level: Intermediate Mikko Kontio (mikko.kontio@softera.fi), Production Manager, Softera
12 Oct 2005 Put what you've learned about MDA into practice this month and see how to simplify Spring-Hibernate development with an open source Model Driven Architecture (MDA) tool.
My last few columns have provided background information on the MDA approach. This month you'll see how MDA works in practice, as I show you how to build a simple application using an open source MDA tool called AndroMDA. I'll use AndroMDA to build an application that manages cars. Spring will serve as the basis for the
application's user interface, and Hibernate will serve as its persistence framework. The application server will be JBoss.
The purpose of this article is not to provide detailed information about any of the tools or technologies used; rather it is to give you the experience of applying the MDA approach. See the Download section for the complete source code for the example application. See Resources to download AndroMDA and JBoss, which you will need to run the example.
Getting started
AndroMDA uses output in the XML Metadata Interchange (XMI) file format of almost any modeling tool to generate application source code based on platform-specific cartridges. For the purpose of this example, I'll be using cartridges for Spring and Hibernate. For the actual MDA modeling I can use any tool that exports (or saves) standard XMI. The AndroMDA homepage recommends certain free or almost-free tools, but it does not specify that it should work with only these tools.
Some MDA tools claim to generate a complete application from a Unified Modeling Language (UML) model, while others focus on removing the most redundant coding tasks. AndroMDA is in the latter category. In the case of the example application, I'll need to write about 10 lines of code, so you could say that AndroMDA will generate about 95 percent of the code for me.
See Resources to download AndroMDA. You'll note that AndroMDA uses Maven (an open source tool similar to Ant) to manage installation and general usage. AndroMDA can also be used with
Ant, but for the purpose of this example, I recommend using Maven. Using open source tools ensures that everyone can follow along with the example.
Use cases
The example application consists of the three use cases shown in Figure 1: List Cars, Add Car, and Remove Car. All of the use cases have been marked with the AndroMDA stereotype of <<FrontEndUseCase>>, which tells the tool that a use case is relevant to end users and should be added to the user interface. The <<FrontEndApplication>> stereotype tells AndroMDA that a use case must be active on the first page of the application.
Figure 1. A use case diagram for the example application
Activity diagrams
After you've established your use cases, the next step is to draw an activity diagram for each one. Activity diagrams describe what is happening inside each use case. The one catch is that you have to create (and assign) a controller class for every activity diagram. The controller class is just a normal class whose only task is to forward the calls from the UI to the business-logic layer (that is, the application services).
Figure 2 is the activity diagram for the List Cars use case. When the user requests a list of cars, the application goes and gets all the cars in its database. The getAllCars() / defer in the first state of the diagram is a reference to the method of the controller class. The transition in the diagram passes the Collection to the next state, which displays the data in application interface. After the second state the user can go to either Add Car or Remove Car, or just list the cars again.
Figure 2. An activity diagram for the List Cars use case
In Figure 3 you can see the activity diagram for the Add Car use case, which is somewhat different. In this diagram, the most interesting part is the transition from Enter New Car to Store New Car. The transition has a signal called addNewCar that has three parameters. Given the signal, AndroMDA knows that the Web interface needs to request these parameters from the user. In the last state of the diagram I use the createCar() method of the controller class to pass the data on to the application's business logic.
Figure 3. An activity diagram for the Add Car use case
The third activity diagram is similar to the second one. See the Download section to download and study the entire model.
Class diagrams
A class diagram documents all the classes that comprise the model. When you look at an application's generated files you'll find a lot more classes and files than you would see in a class diagram. Fortunately, such supporting classes and files are the concern of the architects and programmers who develop platform-specific cartridges (like the Spring and Hibernate ones used for the example). Tools like AndroMDA provide the cartridges and do the work of generating files from them so you can concentrate on application modeling.
At the bottom of the diagram shown in Figure 4 is the Car class. It is marked with the <<Entity>> stereotype, which tells AndroMDA that it is a Hibernate entity. Using the Hibernate cartridge means that you don't have to worry about the application's persistence handling: it is generated automatically. The Cars class is marked with the <<Service>> stereotype. This means that it is part of the business-logic layer, which uses entities to offer services to other layers and classes from the same layer. At the top of the diagram are the controllers. As you can see, the application requires three controllers to handle its three use cases.
Figure 4. A class diagram for the example application
Using AndroMDA
After you've designed the application model, you're ready to start using AndroMDA. For starters, you can use it to check your model for errors. Simply go to the root of the project directory and call
If your environment is set up correctly, Maven will download the necessary packages from the Internet, generate the source code files, and compile everything for you. If your model contains an error, you'll get a message. After the first time you've launched AndroMDA you can switch the call to
which uses the existing packages, although it may occasionally remind you that they are old.
Manual coding
AndroMDA generates many of the files for the application, but I still need to do some manual coding. The four files I need to code manually are as follows:
\core\src\..\CarsImpl.java
\web\src\..\ListCarsControllerImpl.java
\web\src\..\add\AddCarsControllerImpl.java
\web\src\..\remove\RemoveCarsControllerImpl.java
In Listing 1, the lines I've manually coded into the CarsImpl.java file (which implements the Services class) are marked in bold.
Listing 1. Some manual coding is required ...
public class CarsImpl
extends com.dace.cars.CarsBase
{
/**
* @see com.dace.cars.Cars#getAllCars()
*/
protected java.util.Collection handleGetAllCars()
throws java.lang.Exception
{
return this.getCarDao().findAll();
}
/**
* @see com.dace.cars.Cars#removeCar(java.lang.String)
*/
protected void handleRemoveCar(java.lang.String id)
throws java.lang.Exception
{
this.getCarDao().remove(Long.valueOf(id));
}
/**
* @see com.dace.cars.Cars#createCar(java.lang.String, java.lang.String, int)
*/
protected void handleCreateCar(java.lang.String make, java.lang.String model, int year)
throws java.lang.Exception
{
this.getCarDao().create(model, year, make);
}
}
|
As you can see, I did not need to manually code much. Listing 2 shows what I wrote into the ListCarsController.java file as the body of the getAllCars() method.
Listing 2. The getAllCars() method
try
{
form.setCars(this.getCars().getAllCars());
}
catch (Exception ex)
{
ex.printStackTrace();
throw new RuntimeException(ex);
}
|
Download the example to see the rest of the changes for yourself.
Configuring JBoss
I need to make some changes to the JBoss configuration so that it can work with Hibernate. First, I check that the server's HSQLDB TCP connections are enabled. Next, I edit [JBOSS_HOME]/server/[SERVER_NAME]/deploy/hsqldb-ds.xml and uncomment two elements, one for the connection:
<connection-url>jdbc:hsqldb:hsql://localhost:1701</connection-url>
|
and one for the Mbean:
<mbean code="org.jboss.jdbc.HypersonicDatabase"
name="jboss:service=Hypersonic">
<attribute name="Port">1701</attribute>
<attribute name="Silent">true</attribute>
<attribute name="Database">default</attribute>
<attribute name="Trace">false</attribute>
<attribute name="No_system_exit">true</attribute>
</mbean>
|
Deploying the application
Believe it or not, building an application with AndroMDA is just about that simple! All I had to do was design and model the application and do a little bit of manual coding, and I'm almost set to go. Before I can deploy the example, I need to create a database for it. To follow along with this part, make sure that JBoss is running on your desktop and that you've configured your environment settings correctly (that is, JBOSS_HOME). Then go to the root of your project directory and type:
Now you can deploy the application by writing in the root of your project directory
Assuming everything is fine you'll get a BUILD SUCCESSFUL message.
You should now be able to use the application by browsing to http://localhost:8080/cars.
The application architecture
It might seem counterintuitive to discuss the application architecture last, but doing so makes sense with this type of development. Because I used AndroMDA and its cartridges to generate the application, I would see the whole picture come together at the end. If you haven't already done so, download the project directory and study it now.
The architecture, like the application itself, is very simple. The \core\target\ directory contains the persistence and business logic classes. The Car entity class is a Hibernate entity, and the Cars service class is a stateless session bean. All the necessary files and interfaces are generated. The UI layer can be found on the \web\target\ directory. The application's JavaServer Pages (JSP) and Cascading Style Sheet (CSS) files and controller classes are all there. The file that contains the entire application is located at \app\target\.
Figure 5 shows what the application looks like in a Web browser.
Figure 5. The example application's main screen
The example package contains the car management application's UML model and implementation classes, so you only need to set your environment and run AndroMDA on the project to see how it works.
In conclusion
I've walked you through a simple example that demonstrates using AndroMDA to build a typical enterprise application for the Web. Most of the code for the Spring-Hibernate application was generated by Spring and Hibernate cartridges based on my model; I only wrote a few lines of code manually and even those were not difficult.
Obviously, the more complex the application the less simple its development. An application consisting of tens or hundreds of classes would not be so straightforward as the one demonstrated here. On the other hand, it would be greatly simplified by the use of AndroMDA or a similar MDA tool. See Resources to learn more about MDA and AndroMDA.
Download | Description | Name | Size | Download method |
|---|
| Sample code | wi-arch19source.zip | 3 KB | HTTP |
|---|
Resources Learn
Get products and technologies
- "AndroMDA
homepage: Download the MDA tool and cartridges for the example application.
- "JBoss homepage: Download the application server for the example application.
Discuss
About the author  | |  | Mikko Kontio works as a Production Manager for the leading-edge Finnish software company, Softera. He holds a Masters degree in Computer Science and is the author and co-author of several books, the latest being Professional Mobile Java with J2ME, published by IT Press. Mikko can be reached at mikko.kontio@softera.fi. |
Rate this page
|