Architecture Declaration: MDA practice

Source: Internet
Author: User

Mikko Kontio, production manager, softify

December 08, 2005

In this article, I applied the learned knowledge about MDA to practice and learned how to use the open source model-driven architecture (MDA) tool to simplify spring-hibernate development.

Recently, several columns provide background knowledge about the MDA technology. In this article, as I introduce how to use an open-source MDA tool called andromda to build simple applications, you will see how MDA works in practice. I will use andromda to build a car management application. Spring serves as the basis for the application user interface, and hibernate serves as the persistence framework. The application server is JBoss.

The purpose of this article is not to provide detailed information about the tools or technologies used, but to provide the experience of applying the MDA technology. See the download section to obtain the complete source code of the sample application. See references to download the software required for running examples of andromda and JBoss.

Start

Andromda uses XML Metadata Exchange (XML Metadata Interchange, XMI) File Format output. Almost all modeling tools are based on platform-specificCartridgeGenerate the source code of the application. For the purpose of this example, I will use cartridge specific to spring and hibernate. For actual MDA modeling, I can use any tool that can export (or save) Standard XMI. Some free or almost free tools are recommended on the andromda homepage, but it does not limit itself to work with them only.

Some MDA tools claim to be able to generate a complete application from a Unified Modeling Language (UML) model, while others focus on eliminating most redundant coding tasks. Andromda belongs to the latter category. In the application, I need to write about 10 lines of code, so we can say that andromda will generate 95% of the Code for me.

See references to download andromda. You will notice that andromda uses Maven (an open source tool similar to ant) to manage installation and general applications. Andromda can also be used with ant, but for the purpose of this example, I suggest using Maven. The open source code tool ensures that everyone can use this example.



Back to Top

Use Cases

The example application contains three use cases: List cars, add cars, and remove cars ). All these use cases are marked as andromda<<FrontEndUseCase>>TypeStereotype)This tells the tool that this use case is related to the end user and should be added to the user interface.<<FrontEndApplication>>The constructor tells andromda that the use case must be active on the first page of the application.

Figure 1. use case diagram of the sample application



Back to Top

Activity diagram

After the use case is created, the next step is to draw an activity diagram for each use case.Activity diagramDescribes the internal situation of each use case. The point is that you must create (and assign) a controller class for each activity diagram. The Controller class is just a common class. Its only task is to forward the call from the UI to the business logic layer (that is, the service of the application ).

Figure 2 shows the activity diagram of the List cars use case. When the user requests the vehicle list, the application gets all the cars in the database. In the figuregetAllCars() / deferIs a reference to the Controller class method. TransitionCollectionPassed to the next state, which displays data on the application interface. After the second status, you can select Add car or remove car or list the cars again.

Figure 2. Activity diagram of list cars use cases

In Figure 3, we can see the activity diagram of the Add car use case, which is a little different. In this figure, the most interesting part is the transition from enter new car to store new car. There is a signal in transition calledaddNewCarIt has three parameters. With this information, andromda can know that the Web interface needs to request these parameters from the user. In the last state of the graph, I use the Controller classcreateCar()Method To pass data to the application's business logic.

Figure 3. Activity diagram of 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.



Back to Top

Class Diagram

Class DiagramRecords constitute all classes of the model. When you view the files generated by the application, you can find more classes and files than you can see in the class diagram. Fortunately, this type of support classes and files can only be considered by architects and programmers who develop cartridge for specific platforms (for example, spring and hibernate cartridge used in the example ). Tools like andromda provide cartridge and are responsible for generating files from them, so you can focus on application modeling.

At the bottom of the graph shown in figure 4CarClass. It adds<<Entity>>Constructor mark, which tells andromda that it is a hibernate entity. Using hibernate cartridge means you don't have to worry about application persistence processing: it is automatically generated.CarsAdded<<Service>>Constructor mark. This means that it is part of the business logic layer. The business logic uses entities to provide services to other layers and classes at the same layer. At the top of the graph is the controller. As you can see, the application requires three controllers to process three use cases respectively.

Figure 4. Class diagram of the sample application



Back to Top

Use andromda

After the application model is designed, you can start to use andromda. For beginners, you can use it to check whether the model is correct. You only need to enter the project root directory and call

maven

If the environment is set correctly, Maven downloads necessary packages from the Internet, generates source code files, and compiles everything. If the model is incorrect, a message is displayed. After starting andromda for the first time, you can change the call

maven -o

In this way, the existing package is used, but occasionally the package is outdated.



Back to Top

Manual encoding

Andromda generates many files of the application, but I still need to do some manual encoding. The four files that need to be manually encoded are as follows:

/core/src/../CarsImpl.java
/web/src/../ListCarsControllerImpl.java
/web/src/../add/AddCarsControllerImpl.java
/web/src/../remove/RemoveCarsControllerImpl.java

In listing 1, I manually encode it to the carsimpl. Java file (which implementsServicesClass ).BoldMark.

Listing 1. Some manual encoding 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 don't need much manual coding. Listing 2 shows what I wrote to the listcarscontroller. Java FilegetAllCars()Content in the method body.

Listing 2. getallcars () method

        try        {        form.setCars(this.getCars().getAllCars());        }        catch (Exception ex)        {            ex.printStackTrace();            throw new RuntimeException(ex);        }

Download the example and view the remaining modifications.

Configure JBoss

I need to slightly modify the JBoss configuration so that it can work with hibernate. First, check whether the server is enabledHSQLDB TCPConnection. Then, Edit[JBOSS_HOME]/server/[SERVER_NAME]/deploy/hsqldb-ds.xmlAnd cancel the comments of the two elements, one of which belongs to the connection:

<connection-url>jdbc:hsqldb:hsql://localhost:1701</connection-url>

Another 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>



Back to Top

Deploy applications

Whether you believe it or not, building an application with andromda is that simple! All I need to do is design and model the application, and a small amount of manual coding. Then I can basically do it. Before you can deploy an example, I need to create a database for it. To keep up with this Part, make sure that JBoss is running on the desktop and the environment settings are correctly configured (that isJBOSS_HOME). Go to the root directory of your project and enter:

maven create-schema

Now, enter the following command in the root directory of your project to deploy the application:

maven deploy

If everything is normal, you will getBUILD SUCCESSFULMessage.

Now you should be able to browse http: // localhost: 8080/cars and use the application.



Back to Top

Application Architecture

It may seem unconventional to discuss the application architecture at the end, but it makes sense for such development. Because I use andromda and its cartridge to generate an application, I should look at the overall effect at the end. If you have not done so, download the sample project and study it now.

The architecture is as simple as the application itself. The/CORE/target/directory contains persistence and business logic classes.CarThe object class is a hibernate object,CarsThe service class is a stateless Session Bean. All necessary files and interfaces are generated. The UI layer can be found in the/web/target/directory. The Java Server Page (JSP) and Cascading Style Sheet (CSS) files and controller classes of the application are all there. The file containing the entire application is located in/APP/target /.

Figure 5 shows what the application looks like in a web browser.

Figure 5. Master screen of the sample application

The sample package contains the UML Model and implementation class of the automotive management application. Therefore, you only need to set environment variables and run andromda for the project to see how it works.



Back to Top

Conclusion

A simple example demonstrates how to use andromda to build a typical web enterprise application. Most of the spring-hibernate application code is generated by spring and hibernate cartridge based on my model. I only manually encode a few lines of code, and the code is not difficult.

Obviously, the more complex the application, the less simple it is to develop. Applications that contain hundreds of classes are not as simple as the ones demonstrated here. On the other hand, such programs can be greatly simplified by using andromda or similar MDA tools. See references to learn more about MDA and andromda.



Back to Top

Download

Description Name Size Download Method
Sample Code Wi-arch19source.zip 3 KB  FTP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.