Over a million developers have joined DZone.

Generating a J2EE Stack Using AndroMDA

AndroMDA is a framework for code generation based on UML modeling. This article uses AndroMDA to build a complete stack of a basic traditional J2EE application.

· Java Zone

Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience, brought to you in partnership with JetBrains

AndroMDA is a framework for code generation based on UML modeling.

In this article I use AndroMDA to build a complete stack of (JPA, Spring, CXF Apache web service) as a basic traditional J2EE application.

Generated j2ee stack

When Can You Actually Use AndroMDA?

Project Technologies Are Clear

If you know the technologies that you will use in your project and you have all the cartridges, then you can use AndroMDA for the implementation. For instance, if you need to connect to middleware or an ESB, you have to implement the cartridge for generating the integration/DAO layers before you start the project.

Long-Term Investment

I think the code generation approach is a choice for long investment in the software industry; if you are going to make a software company with fewer resources and you already developed all the cartridges you need for the technologies you are going to use, then in you can go for androMDA.

Fast Kickoff

You can use code generation for starting up a new software project; it gives a fast project kickoff by generating the nonfunctional requirements (J2EE layers, DB Schemas, auditing and logging, Spring, security configurations, etc.). Then development can go forward by creating the Impl classes and adding the business logic until the project is complete.

Modeling Is Required

AndroMDA is still a choice for code generation, especially if the modeling is one of your deliverables. Creating software for process automation/workflow is a good example of code generated by modeling using UML activity diagrams.

Software Automation

Code generation is good for software automation to speed up development. If you have experienced developers in AndroMDA cartridge development, you can make a business by delivering software automation to your customers.

If you have an existing J2EE traditional application (Spring, Hibernate, and REST) already developed, or if you have a legacy system and you want to speed up the development lifecycle and enhance code quality, you can use AndroMDA by building custom cartridges.

I think we have to admit that the importance of code generation has been reduced by raising declarative programming, but still we can say "I can model and generate anything..."

MDA and MDD Benefits:

- Enforces architecture

- Focus on business goals instead of technology

- Increase in Quality of the application

- Cost effective

– Leverage open source

Using Spring and Hibernate are traditional technologies for building a Java stack; AndroMDA is a good choice but still complex for code generating based on UML.

In this application we use UML 1.4 and Magic Draw 9.5 for UML modeling, and AndroMDA 3.4.

The interface of this application is a REST web service and the backend is a MySQL DB.

WebService Cartridge:

In this demo you need to use a web service cartridge in GitHub, as it fixes a minor error. You have to use a modified web service cartridge from here:


Environment Setup

1-Install JDK

Install JDK 1.7 and make sure it is in JAVA_HOME

2-Install Maven

Make sure you have the maven in the path C:\apache-maven-3.2.3

Make sure M2 and MAVEN_HOME are in system variables

3-Install Nexus

Copy the nexus folder in D:\ and run the Nexus Jetty server

nexus start

Make sure you have the Admin privilege and the Java install is JDK 1.7

4-Install MagicDraw 9.5

For modeling and design we use UML 1.4 and MagicDraw 9.5.

You can download it from http://cc.embarcadero.com/item/23512

5-Install MySQL

Install the MySQL DB and workbench from the source folder.

The default DB name and schema is “pag”; username/password: root/root and port 3306

6-Install Eclipse

Copy the eclipse from the source directory.

To import the project right click on the left panel of the eclipse and click import.

Use import existing ant project, the project will be imported in Eclipse.

(All the edit you have to do in the code is in Impl classes).

7-Install Apache 7

Copy the Apache 7 from source, too, because it has extra jars in the lib folder for the PAG and extract in C:\apache-tomcat-7.0.55

8-Install REST Client

Install the Firefox REST client plugin.

Modeling and Code Generation Cycle

Image title

All the logic is in Impl classes, and Impl classes are generated once, and then is used to add modified code.

Model Design and Code Generation

Project Structure

/PAG OUBL J2EE project
     |-- pom.xml
     +-- /mda
     |     |
     |     |
     |     +-- pom.xml
     |     |        contains the AndroMDA dependencies and configuration (cartridges, translation-libraries, etc)
     |     +-- /src
     |     |        additional sources such as merge-mappings can be
     |     +-- /src/main/config/andromda.xml
     |              configures AndroMDA and its components, most
     |              importantly the cartridges which are listed in
     |              their own namespace; global settings are done in the
     |              'default' namespace
     +-- /common
     |     |
     |     |    The COMMON module collects those resources and classes
     |     |    that are shared between the other modules.
     |     |
     |     +-- pom.xml
     |     |        lists common dependencies
     |     +-- /target
     |              shared resources and java classes are generated here,
     |              such as value objects and exceptions
     +-- /core
     |     |
     |     |    The CORE module collects those resources and classes
     |     |    that use the Spring framework, optionally making
     |     |    use of Hibernate and/or EJB under the hood.
     |     |
     |     +-- pom.xml
     |     |        
     |     +-- /src/main/java
     |     |        Spring classes that need manual implementation are
     |     |        generated here, they will not be overwritten upon
     |     |        regeneration
     |     +-- /target
     +-- /webservices
     |     |
     |     |
     |     +-- pom.xml
     |     |        lists WebApp dependencies
     |     +-- /src/main/java
     |     |        controller implementations and editable resource bundles
     |     |        will be generated here,
     |     +-- /target
     |              a deployable war is bundled here

To start MagicDraw 9.5, you need to import all of the UML profile library of AndroMDA 3.4. You will find these artifacts in the Maven repository under \org\andromda\profiles\uml14

Open the model file under \mda\src\main\uml\rest-webservice.xml

Image title

Design Notes

All Class names start with Uppercase.

Thename of the web service class is the same as the base URL as in tag value andromda_REST_path

Add Your Logic in Impl Classes

For each change in the model, check the Impl Classes and add your logic.

In this example you have 3 classes you have to put your code in.

GetAllItemsImpl.java represents the logic implementation of web service method

public static ItemVO[] getAllItemsTransformResponse() {
     ServiceLocator locator= ServiceLocator.instance();
     return locator.getItemService().getAllItems();

ItemServiceImpl.java has the handle method to implement the logic of the service layer.

protected  ItemVO[] handleGetAllItems() throws Exception {
    return this.getItemDao().getAllItems();

and ItemDaoImpl.java represents the logic of the DAO layer.

protected ItemVO[] handleGetAllItems()
    return  this.toItemVOArray(this.loadAll());

Also DAO layer has transformation methods to transform the entity objects to DTO objects

Clean and Build

Run the following Maven commands to skip the tests:

mvn clean 
mvn install –Dmaven.test.skip=true

Generating the DB Schema

Generate the DB Tables using the following command, which removes existing tables and creates new ones.

mvn -f core/pom.xml org.andromda.maven.plugins:andromdapp-maven-plugin:schema ,create

Create Dummy Data

INSERT INTO `item` VALUES (1,'T-Section','1234','description 1','1'),(2,'I-Section','123','description 2','1');

Deploying on Apache 7

Copy the war file from D:\PAG\PAG-OUBL\webservice\target and place it in the app Directory in Tomcat: C:\apache-tomcat-7.0.55\webapps

It will be self-extracted and the deployed folder will be created.

Using the REST Client

Test the URL using the GET method and header type JSON.


The results will be the following:


Image title

Source code is available in https://github.com/ayman-elgharabawy/J2ee-fullstack-generated

Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.


Published at DZone with permission of Ayman Elgharabawy. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}