Kevin's Blog

IT, Scala, Java, Web, Ubuntu

Tag: Spring (page 2 of 3)

ROO So Cool!!! -03-

Continued from
http://blog.lckymn.com/2009/05/17/roo-so-cool-02/

Now, let’s look at more interesting one that is the ROO generated source code!

As you can see, there are many AspectJ aspects in the com.springsource.vote.domain package. So I’ll firstly try to look at the Choice class which is not an aspect.

Domain object - Choice

Domain object - Choice


Wow! Is that it? Without the annotations, it consists of only two lines of code in the class. Just two variable declarations.

So where are the others?

Check out the Choice_Roo_Entity aspect.

Persistent Object (Aspect)

Persistent Object (Aspect)


It appears to be a persistent object class with a persistence manager which means it includes data access logic. This sounds like normal Active Record yet if you closer look at the code, it uses AspectJ’s inter-type declaration to add more attributes and behaviours to the domain object Choice.

    @javax.persistence.Id    
    @javax.persistence.GeneratedValue(strategy = javax.persistence.GenerationType.AUTO)    
    @javax.persistence.Column(name = "id")    
    private java.lang.Long Choice.id;

This code above is declaring long type id variable to the type Choice to use as an identifier for OR mapping. Because it is declared as a private variable, it is impossible to access it inside the code of Choice but possible only inside this Choice_Roo_Entity aspect.

What about the Choice_Roo_JavaBean.

JavaBean (Aspect)

JavaBean (Aspect)


It has all the getters and setters for the Choice class and again inter-type declaration is used for adding these accessor methods. So an instance of Choice type cannot only behave as a persistent object but also as a JavaBean object. If you look into the jsp file, you can see it is used just like the methods belong to Choice class.
In order to access the variables inside Choice object, the Choice_Roo_JavaBean is defined as privileged aspect.

privileged aspect Choice_Roo_JavaBean {
    
    public java.lang.String Choice.getNamingChoice() {    
        return this.namingChoice;        
    }    
    
    public void Choice.setNamingChoice(java.lang.String namingChoice) {    
        this.namingChoice = namingChoice;        
    }    
    
    public java.lang.String Choice.getDescription() {    
        return this.description;        
    }    
    
    public void Choice.setDescription(java.lang.String description) {    
        this.description = description;        
    }    
    
}

So as shown above, Choice_Roo_JavaBean is a privileged aspect and therefore it can access namingChoice and description variables which are declared in Choice.

So what is it? It is a mixin. Mixin is a class which provides a functionality to a subclass yet mixin itself is not supposed to stand alone. So a class can collect its functionality by inheriting from one or more mixins through multiple inheritance. However, as we all know, Java does not support multiple inheritance. So to use mixins in Java world, inter-type declarations in AspectJ can be used.

privileged aspect Choice_Roo_Entity {
    
	(...)
    
    @javax.persistence.Id    
    @javax.persistence.GeneratedValue(strategy = javax.persistence.GenerationType.AUTO)    
    @javax.persistence.Column(name = "id")    
    private java.lang.Long Choice.id;    
    
    (...)
    
    public java.lang.Long Choice.getId() {    
        return this.id;        
    }    
}

As seen in the aspect code above, a long type variable is added to the type Choice and a getId() method which returns a long type value that is the value in the variable, id, just added by the aspect. So an instance of Choice type can have getId() method even though it does not exist in the Choice class. Therefore, as mentioned, Choice type can have characteristics of both persistent object and JavaBean.

AspectJ is also used to declare Choice type as a configurable type.

Make Choice @Configurable

Make Choice @Configurable


This code reveals that Choice type has @org.springframework.beans.factory.annotation.Configurable annotation.

privileged aspect Choice_Roo_Configurable {
    
    declare @type: Choice: @org.springframework.beans.factory.annotation.Configurable;    
    
}

Next:
http://blog.lckymn.com/2009/05/17/roo-so-cool-04/

ROO So Cool!!! -02-

Continued from
http://blog.lckymn.com/2009/05/17/roo-so-cool-01/

Let’s run this project to see what it looks like.

Run ROO Generated Application on Server

Run ROO Generated Application on Server


Right click on the project -> Select the Run As option on the menu -> Select the Run on Server.

Choose Server

Choose Server


Select Choose an existing server -> Select any Servlet Container or JEE Server on which you would like ROO generated application to run (If you do not have any available ones, install set it up first). -> Click the Next button.

Click Finish to Run

Click Finish to Run


Make sure vote project appears on the right-hand side and Click the Finish button.

ROO Generated Application is Running.

ROO Generated Application is Running.


Now it’s running.

I opened Firefox 3 to test it instead of using the built-in browser of Eclipse.
Let’s create a new choice.

Create New Choice (Firefox3)

Create New Choice (Firefox3)


Click the Create New Choice menu.

Create New Choice Form

Create New Choice Form


It displays the Create New Choice form.

Wait a minute! Is that a JavaScript (JS) error that Firebug complains?

dojo JS Toolkit Error

dojo JS Toolkit Error


Yes, it is but it looks like an error in dojo JS toolkit which Spring JS uses. Anyway, despite the error, it works fine.

Fill in the Form -> Save
Quickly fill in the form. -> Click the Save button.

New Choice Created

New Choice Created


The entered data are added and a new Choice is created.

Next:
http://blog.lckymn.com/2009/05/17/roo-so-cool-03/

ROO So Cool!!! -01-

The end of the last month Spring Source released Spring ROO in alpha stage. My friend, Dr. Stefan Schmidt, who in fact used to teach me J2EE technology at UTS when I was a UTS student, has been working for Spring Source and is now involved in ROO development. Although ROO sounds really like Australian, it signifies Real Object-Oriented (ROO). After the release of the alpha version, Dr. Schmidt posted an introductory tutorial of ROO on his blog. He explained well so I could easily try this new mind-blowing technology. :)

Let’s start a journey!

-Check your maven and JVM versions.

$ mvn --version
Maven version: 2.0.9
Java version: 1.6.0_13
OS name: "linux" version: "2.6.28-11-generic" arch: "amd64" Family: "unix"

– I’m using Ubuntu Linux 9.04 Jaunty Jackalope desktop edition 64bit and installed Sun JDK & Maven2 from its repository.

-Extract spring-roo-1.0.0.A2.zip file to /somepath/roo-1.0.0.A2
-Add the environment variable ROO_HOME with the path of Roo home in the .bashrc file.
-Add $ROO_HOME/bin to PATH in the .bashrc file.
e.g.)

export ROO_HOME=/opt/lib/spring/roo-1.0.0.A2
PATH=${PATH}:$ROO_HOME/bin

export PATH

For Eclipse,
-Extract sts-roo-integration-1.0.0.A2.zip file to the eclipse/dropins folder to use Spring ROO integration.
This copying to the dropins directory only works for Eclipse Ganymede.

-Install ROO packages in the maven repository.

mvn install:install-file -DgroupId=org.springframework.roo \
  -DartifactId=roo-annotations -Dversion=1.0.0.A2 -Dpackaging=jar \
  -Dfile=$ROO_HOME/dist/roo-annotations-1.0.0.A2.jar

-Result

$ mvn install:install-file -DgroupId=org.springframework.roo \
       -DartifactId=roo-annotations -Dversion=1.0.0.A2 -Dpackaging=jar \
       -Dfile=$ROO_HOME/dist/roo-annotations-1.0.0.A2.jar
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'install'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [install:install-file] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] [install:install-file]
[INFO] Installing /opt/lib/spring/roo-1.0.0.A2/dist/roo-annotations-1.0.0.A2.jar to /home/blade2/.m2/repository/org/springframework/roo/roo-annotations/1.0.0.A2/roo-annotations-1.0.0.A2.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Tue May 05 22:09:23 EST 2009
[INFO] Final Memory: 7M/165M
[INFO] ------------------------------------------------------------------------

-Now starts ROO console!

$ mkdir roo_test
$ cd roo_test/
$ roo.sh
ROO Console

ROO Console

-Create a project directory for a ROO project.

Instead of following the script that Dr. Schmidt provided, I just used vote.roo which can be found in the samples directory. Well no special reasons, it just looks simpler. That’s it. :) If you are seeking more interesting one, you’d better try the one on the blog of Dr. Schmidt and then try your own one. ^O^

-In order to import the ROO project, just created, to Eclipse (STS), run maven2’s eclipse plug-in.

$ mvn eclipse:eclipse
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'eclipse'.
[INFO] ------------------------------------------------------------------------
[INFO] Building vote
[INFO]    task-segment: [eclipse:eclipse]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing eclipse:eclipse
Downloading: 
... lots of dependencies including spring framework 3.0.0.M2 ...

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7 minutes 26 seconds
[INFO] Finished at: Tue May 05 22:32:18 EST 2009
[INFO] Final Memory: 31M/353M
[INFO] ------------------------------------------------------------------------

Well, although it says

Sources for some artifacts are not available.
Javadoc for some artifacts is not available.

it is not that important unless I really want to see the missing source code and Javadoc of the artifacts listed with the message.

If you want to get it, add -DdownloadSources=true -DdownloadJavadocs=true options and run maven again.

$ mvn eclipse:eclipse -DdownloadSources=true  -DdownloadJavadocs=true

I’m not sure if the source is available though. The source code is not available at the moment though. I was wrong. The source code is available. Check this out. http://forum.springsource.org/showthread.php?t=71985

-Now run Eclipse. Since ROO is a part of Spring Framework, I’m using SpringSource Tool Suite (STS) for this practice.

SpringSource Tool Suite

SpringSource Tool Suite

Well, before I go further, here are what I am using.
*SpringSource Tool Suite (STS) 2.0.0

  • Apart from all the Spring related plug-ins that STS comes with, it also comes with several good plug-ins such as AspectJ Development Tools (AJDT), PMD for Eclipse (Java source code analyser) and EclEmma (Code Coverage Tool) which are what I usually install when I use pure Eclipse JEE.

*Eclipse Plug-ins

  • Subclipse
  • Maven Integration for Eclipse (M2Eclipse)
  • Aptana
  • JDepend (Java source code analyser)
  • Copy Fully Qualified Class Name Plugin
  • Resource Bundle Editor
  • and of course Spring ROO Integration for Eclipse (I heard a new STS will include it).

However, what are really required for this ROO practice are Eclipse JEE, Spring IDE, AJDT, M2Eclipse and Spring ROO Integration. Or you can simply use STS and install M2Eclipse and Spring ROO Integration. If you use Eclipse JEE Ganymede and install Spring IDE, it will automatically install AJDT as a dependency of Spring IDE since Ganymede has a smart software & add-ons update manager. :)

(Good News: A free version of STS is released. Check this out
SpringSource Tool Suite now free).

Import ROO project

Import ROO project


Right click on the project explorer -> Select Import

Existing Projects into Workspace

Existing Projects into Workspace


Expand the General entry -> Select Existing Projects into Workspace -> Click the Next button.

Select Project to Import

Select Project to Import


Browse the roo project directory created by roo. In my case it’s roo_test -> Click the Finish button.
(NOTE: Because I created the roo project directory inside the workspace of Eclipse (STS), I don’t need to check Copy projects into workspace option.)

Turn AJDT's Weaving Service on

Turn AJDT's Weaving Service on


If Eclipse asks to turn on weaving service function of AJDT, Click the Yes button.

Restart STS (Eclipse)

Restart STS (Eclipse)


Restart Eclipse.

Dependency Problems

Dependency Problems


Eclipse is restarted and there are errors in the project as the require dependencies are not in the project directory.

So now, let M2Eclipse resolves this dependency problem. That’s why I’m using this plug-in. I want to focus on real problems in software development rather than focusing on providing dependency libraries for development.

Let M2Eclipse Resolves Dependencies

Let M2Eclipse Resolves Dependencies


Right click on the project -> Select the Maven option on the menu -> Select the Enable Dependency Management.
Wait until it does its job.

Dependency Problems Solved

Dependency Problems Solved


As shown here, Maven resolved it so all the required dependencies are there.

Next:
http://blog.lckymn.com/2009/05/17/roo-so-cool-02/

Older posts Newer posts

© 2018 Kevin's Blog

Theme by Anders NorenUp ↑

%d bloggers like this: