Thursday, October 13, 2011

Building AspectJ aspects using Ant


To develop Aspects using AspectJ you can follow my Blog.
After implementing there comes the work of automating the build process. Since, we use Hudson for  build, it can be configured to run an Ant script.

Tuesday, October 11, 2011

Aspect Oriented Programming (AOP) using AspectJ

Cross cutting concerns is set of behaviours needed by the conceptual section s of the program. An example of the cross cutting concerns is Logging.
AOP separates the cross cutting concerns from the functional requirements hence increasing the modularity of the code and making it easier to maintain and decreasing the complexity.
AspectJ is the aspect oriented extension for the Java Programming Language.
Following are the terminologies used in AOP
1.        Aspect - General feature we want to apply globally to your application (ex. Logging , transaction Management)
2.       Advice - A chunk of code that is invoked during program execution
3.       Joinpoint - A single location in the code where an advice should be executed  
4.        Pointcut - A pointcut is a set of many joinpoints where an advice should be executed
5.        Targets/Target Objects - The objects you want to apply an aspect or set of aspects to
6.       Introduction - This is the ability to add methods to an object
Different Types of advices in AspectJ are:
1.       Before advice:  Before advice executes before the execution of the advised join point. The  syntax of this advice is  before(args) : pointcut_expression {}
2.       After advice:  After advice executes after the execution of a join point. The  syntax of this advice is  after(args) returning() : pointcut_expression {}
3.       Around advice:  Around advice surrounds join points i.e. it can execute before and after the join points execution. The syntax of this advice is  around(args) : pointcut_expression {}. Responsibility when using an around advice is calling   the join point by using the proceed() method.
4.       After Throwing: This advice will be called when an advised join point throws an exception. The  syntax of this advice is  after(args) throwing() : pointcut_expression {}
 Eclipse IDE provides the AspectJ development tools plugin (available in Eclipse MarketPlace) .

Wednesday, October 5, 2011

Xpath = JxPath


Quite often while implementing   SOAP based service we  need to  traverse   xpath.
So, my XSD looks like this;
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="myorg.xml.com" targetNamespace="myorg.xml.com">
<element name="employeeDetails">
<complexType >
<sequence>
<element name="employee" type="tns:Employee" minOccurs="1" maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>

    <complexType name="Employee">
    <sequence>
    <element  name="employeeId" type="int" />
    <element name="employeeName" type="string" />
    <element name="department" type="tns:Department"/>
    <element name="EmployeeDesignation" type="tns:Designation"/>
    </sequence>
    </complexType>

    <complexType name="Department">
    <sequence>
    <element  name="departmentId" type="int" />
    <element name="departmentName" type="string" />
    </sequence>
    </complexType>
   
   <simpleType name="Designation">
    <restriction base="string">
    <enumeration value="Associate"/>
    <enumeration value="Software Engineer"/>
    <enumeration value="Senior Software Engineer"/>
    <enumeration value="Manager"/>
    <enumeration value="Executive"/>
   
    </restriction>
    </simpleType>
</schema>

And a sample XML:
<?xml version="1.0" encoding="UTF-8"?>
<tns:employeeDetails xmlns:tns="myorg.xml.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="myorg.xml.com Employee.xsd ">
  <employee>
    <employeeId>230</employeeId>
    <employeeName>Jim</employeeName>
    <department>
      <departmentId>1</departmentId>
      <departmentName>IT</departmentName>
    </department>
    <EmployeeDesignation>Associate</EmployeeDesignation>
  </employee>
 
  <employee>
    <employeeId>231</employeeId>
    <employeeName>Tim</employeeName>
    <department>
      <departmentId>12</departmentId>
      <departmentName>Admin</departmentName>
    </department>
    <EmployeeDesignation>Manager</EmployeeDesignation>
  </employee>
</tns:employeeDetails>

JXPath (http://commons.apache.org/jxpath/)  provides APIs for traversal of graphs of JavaBeans, DOM and other types of objects using the XPath syntax. Both child and attribute axis of a node is mapped to Java Beans Properties.