Aspect Oriented Programming: Java EE

CDI is a part of the Java EE farmework which provides us with AOP.

This example is the same one with the spirng aop example post. I will display the behaviour of two beans with the same definition but each one has a different aspect, more specific they will use a different interceptor.

The Entered annotation would be our interceptor binding

package com.gkatzioura;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.interceptor.InterceptorBinding;

@Inherited
@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface Entered {
    
}

The employee interface will be used to create the beans for this project.

package com.gkatzioura;

import javax.ejb.Local;
import javax.ejb.Singleton;

@Local
public interface Employee {
    
    @Entered
    public void enterWorkArea();
    
}

We will have two different kind of advices.
The SupervisorBeforeAdvice

package com.gkatzioura;

import java.io.Serializable;
import java.util.logging.Logger;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

@Entered
@Interceptor
public class SupervisorBeforeAdvice implements Serializable{
    
    private static final Logger LOGGER = Logger.getLogger(SupervisorBeforeAdvice.class.toString());
    
    @AroundInvoke
    protected Object protocolInvocation(final InvocationContext invocationContext) throws Exception {

        if(invocationContext.getMethod().getName().equals("enterWorkArea")) {
            LOGGER.info("Check if everything is ok");
        }
        
        return invocationContext.proceed();
    }
    
}

And the WorkerBeforeAdvice

package com.gkatzioura;

import java.io.Serializable;
import java.util.logging.Logger;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

@Entered
@Interceptor
public class WorkerBeforeAdvice implements Serializable {
    
    private static final Logger LOGGER = Logger.getLogger(WorkerBeforeAdvice.class.toString());
    
    @AroundInvoke
    protected Object protocolInvocation(final InvocationContext invocationContext) throws Exception {
        
        if(invocationContext.getMethod().getName().equals("enterWorkArea")) {
            LOGGER.info("Informing the supervisor that I have arrived");
        }
        
        return invocationContext.proceed();
    }
    
}

Two different beans will implement our employee interface.

The supervisor bean

package com.gkatzioura;

import javax.ejb.Stateless;
import javax.interceptor.InterceptorBinding;
import javax.interceptor.Interceptors;

@Stateless
public class SupervisorEmployee implements Employee{

    @Override
    @Interceptors(SupervisorBeforeAdvice.class)
    public void enterWorkArea() {
    
    }
    
}

And we will have two different kind of beans

Supervisor

package com.gkatzioura;

import javax.ejb.Stateless;
import javax.interceptor.Interceptors;

@Stateless
public class SupervisorEmployee implements Employee{

    @Override
    @Interceptors(SupervisorBeforeAdvice.class)
    public void enterWorkArea() {
    
    }
    
}

And worker

package com.gkatzioura;

import javax.ejb.Stateless;
import javax.interceptor.Interceptors;

@Stateless
public class WorkerEmployee implements Employee{

    @Override
    @Interceptors(WorkerBeforeAdvice.class)
    public void enterWorkArea() {
    }
    
}

I will use JAX-RS to cal those beans

Application configuration

package com.gkatzioura.jersey;

import javax.ws.rs.core.Application;

@javax.ws.rs.ApplicationPath("rest")
public class ApplicationConfig extends Application {

}

And calling the two beans from different endpoints

package com.gkatzioura.jersey;


import com.gkatzioura.Employee;
import com.gkatzioura.SupervisorBeforeAdvice;
import com.gkatzioura.WorkerBeforeAdvice;
import javax.ejb.EJB;
import javax.interceptor.Interceptors;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/")
public class AspectEndpoint {
    
    @EJB(beanName = "WorkerEmployee")
    Employee worker;
    
    @EJB(beanName = "SupervisorEmployee")
    Employee supervisor;
    
    @GET
    @Path("/worker/enter")
    public String workerEnter() {
        worker.enterWorkArea();
        return "worker entered";
    }
    
    @GET
    @Path("/supervisor/enter")
    public String supervisorEnter() {
        supervisor.enterWorkArea();
        return "supervisor entered";
    }
    
}

Aspect Oriented Programming: Spring

One of the most powerfull tools spring provides us with is aspect orientation.
Aspect oriented programming helps us to continue development for special use cases, without breaking our modularity.

This is part of a series of blog posts on aspect orientation using different tools.
The first one is about aspect orientation on Spring.

In this example I will display the behaviour of two beans from the same definition but each one has a different aspect.

The employe class will be used to create the beans for this project.

package com.gkatzioura.spring.employees;

public class Employee {

    public void enterWorkArea() {

    }

    public void leaveWorkArea() {

    }

}

However considering a company, probably there are more than one types of employes.
For our project we will use the aspect of the supervisor employee and the aspect of the worker employee.

The worker employee before entering the work area should inform the supervisor employee that he has arrived.
The supervisor employee before entering the area should check that everything is ok.

package com.gkatzioura.spring.employees;

import org.apache.log4j.Logger;
import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class WorkerBeforeAdvice implements MethodBeforeAdvice {

    private static final Logger LOGGER = Logger.getLogger(WorkerBeforeAdvice.class);

    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {

        if (method.getName().equals("enterWorkArea")) {
            System.out.println("Informing the supervisor that I have arrived");
        }

    }

}
package com.gkatzioura.spring.employees;

import org.apache.log4j.Logger;
import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class SupervisorBeforeAdvice implements MethodBeforeAdvice {

    private static final Logger LOGGER = Logger.getLogger(SupervisorBeforeAdvice.class);

    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        
        if(method.getName().equals("enterWorkArea")) {
            System.out.println("Check if everything is ok");
        }
    }
}

The advices defined above would have to be wired on our employee bean according to the aspect needs.
For the worker bean we will use the WorkerBeforeAdvice before entering the factory.
For the employee bean we wil use the SupervisorBeforeAdvice before leaving the factory

On your META-INF create a spring directory and inside the spring directory create the employeeapects.xml file. This file would wire the employee bean with the behaviours described above.
The first ProxyFactory Bean will be used to create employee beans wired with the WorkerBeforeAdvice behaviour.
The second ProxyFactory Bean will be used to create employee beans wired with the SupervisorBeforeAdvice behaviour.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="employee" class="com.gkatzioura.spring.employees.Employee"/>

    <bean id="workerBeforeBean" class="com.gkatzioura.spring.employees.WorkerBeforeAdvice"/>
    <bean id="supervisorBeforeBean" class="com.gkatzioura.spring.employees.SupervisorBeforeAdvice"/>

    <bean id="workerProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="employee"/>
        <property name="interceptorNames">
            <list>
                <value>workerBeforeBean</value>
            </list>
        </property>
    </bean>

    <bean id="superVisorProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="employee"/>
        <property name="interceptorNames">
            <list>
                <value>supervisorBeforeBean</value>
            </list>
        </property>
    </bean>

</beans>

On your dispatcher-servlet.xml you need to import employeeapects.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.gkatzioura.spring"/>

    <import resource="classpath:/META-INF/spring/employeeapects.xml"/>
    <mvc:annotation-driven/>
</beans>

We will create a controller in order to create the beans.

package com.gkatzioura.spring.controllers;

import com.gkatzioura.spring.employees.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class AspectController {

    @Autowired
    @Qualifier("workerProxy")
    Employee workEmployee;

    @Autowired
    @Qualifier("superVisorProxy")
    Employee superVisorEmployee;

    @RequestMapping(value = "/")
    @ResponseBody
    public String index() {

        return "Check other endpoints";
    }

    @RequestMapping(value = "/worker/enter")
    @ResponseBody
    public String workerEnter() {
        workEmployee.enterWorkArea();
        return "Worker entered the factory";
    }

    @RequestMapping(value = "/supervisor/enter")
    @ResponseBody
    public String superVisortEnter() {
        superVisorEmployee.enterWorkArea();
        return "Supervisor entered the factory";
    }

}

You should hit the endpoint /worker/enter and /supervisor/enter.
You will notice that the workerEmployee and superVisorEmployee have a different reaction once calling the enterWorkArea function.

By hitting the /worker/enter endpoint, the string “Informing the supervisor that I have arrived” is printed on the console.
By hitting the /supervisor/enter endpoint, the string “Check if everything is ok” is printed on the console.

Make objects with node.js

Node.js is definitely awesome, especially on an i/o heavy application.
Being a Java developer, my first take was on making a class.
ResultExtractor.js would be the file used to define a module.

function ResultExtractor(wordToMatch) {
    this.wordToMatch = wordToMatch
}

ResultExtractor.prototype.filterRecords = function(jsonResults) {
    var filteredRecords = []

    for(var i=0;i<jsonResults.length;i++) {
        var jsonResult = jsonResults[i]
        if(this.recordContains(jsonResult))
            filteredRecords.push(jsonResult)
    }

    return filteredRecords
}

ResultExtractor.prototype.recordContains = function(jsonResult) {
    return jsonResult['word'].indexOf(this.wordToMatch)!=-1;
}

module.exports = ResultExtractor

On your other file that imports the module you can use the ResultExtractor class


var ResultExtractor = require('./ResultExtractor')

var resultExtractor = new ResultExtractor('whatever')
var testData = [{'word':'whatever'},{'word':'where'},{'word':'when'}]
var filteredData = resultExtractor.filterRecords(testData)

console.log(filteredData)

That’s it!!! More post will follow, node.js seems really promising.

Database refactoring with Liquibase

Database changes are painful. Every time you have to do it, there is a mess of alter table statements and sql scripts.
What I really liked about Liquibase is its maven plugin.
Consider the scenario where each colleague has a database for testing purposes on his local pc.
Once you have pushed to the repository your Liquibase configuration files It is a matter of a maven command in order for your
colleagues to be on the same page with you.

Add inside your pom add the plugin configuration

<build>
	<resources>
		<resource>
			<directory>src/main/resources</directory>
			<filtering>true</filtering>
		</resource>
	</resources>
	<plugins>
		<plugin>
			<groupId>org.liquibase</groupId>
			<artifactId>liquibase-maven-plugin</artifactId>
			<version>3.0.5</version>
			<configuration>
				<driver>{your driver}</driver>
				<url>jdbc:mysql://127.0.0.1:3306/test</url>
				<username>test</username>
				<password>test</password>
				<changeLogFile>database/refactor/db.changelog.master.xml</changeLogFile>
			</configuration>
			<executions>
				<execution>
					<phase>process-resources</phase>
					<goals>
						<goal>update</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

Our changelog file would be place on the resources

src/main/resources/database/refactor/db.changelog.master.xml

We can include other changelog files inside one

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
		xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog db.changelog-3.1.xsd">
	<include file="db.changelog-1.0.xml" relativeToChangelogFile="true" />
	<include file="db.changelog-2.0.xml" relativeToChangelogFile="true" />
	<include file="db.changelog-3.0.xml" relativeToChangelogFile="true" />
	<include file="db.changelog-4.0.xml" relativeToChangelogFile="true" />
	<include file="db.changelog-5.0.xml" relativeToChangelogFile="true" />
</databaseChangeLog>

db.changelog-1.0.xml creates a table

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog 
		xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog db.changelog-3.1.xsd">
	<changeSet id="1" author="change1">
		<createTable tableName="supervisor">
			<column name="username" type="varchar(50)" />
			<column name="password" type="varchar(50)"/>
			<column name="securePassword" type="varchar(50)"/>
		</createTable>
	</changeSet>
</databaseChangeLog>

db.changelog-2.0.xml creates another table

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
		xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog db.changelog-3.1.xsd">
	<changeSet id="2" author="change2">
		<createTable tableName="employee">
			<column name="username" type="varchar(50)"/>
			<column name="password" type="varchar(50)"/>
		</createTable>
	</changeSet>
</databaseChangeLog>

db.changelog-3.0.xml makes an insert

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
		xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog db.changelog-3.1.xsd">
	<changeSet id="3" author="change3">
		
		<insert tableName="employee">
			<column name="username" value="emmanouil"/>
			<column name="password" value="whatever"/>
		</insert>

	</changeSet>
</databaseChangeLog>

db.changelog-4.0.xml is a showcase of insert but with a precondition

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog db.changelog-3.1.xsd">
<changeSet id="4" author="change4">
	<preConditions>
		<sqlCheck expectedResult="0">select count(*) from supervisor</sqlCheck>
	</preConditions>
	<insert tableName="supervisor">
		<column name="username" value="james"/>
		<column name="password" value="test"/>
	</insert>
</changeSet>
</databaseChangeLog>

db.changelog-5.0.xml adds a unique constraint

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
		xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog db.changelog-3.1.xsd">
	<changeSet id="5" author="change5">
		<addUniqueConstraint tableName="supervisor" columnNames="username"/>
	</changeSet>
</databaseChangeLog>

In order to run just issue

mvn resources:resources liquibase:update

Liquibase has some great features. If you do iterative development and you do a lot of refactoring it is great for the collaboration among the team members.

Connecting to JMX through Jython

Jython is great when you want a dynamically typed language based on the JVM.

Also comes really in handy when you want to write small monitoring scripts based on JMX.This is an examble on how to call a function from a MBean through Jython using JMX.

jmxaction.py

from javax.management.remote import JMXConnector
from javax.management.remote import JMXConnectorFactory
from javax.management.remote import JMXServiceURL
from javax.management import MBeanServerConnection
from javax.management import MBeanInfo
from javax.management import ObjectName
from java.lang import String

from jarray import array
import sys   

if __name__=='__main__':
        
        if len(sys.argv)> 5:
                serverUrl = sys.argv[1]
                username = sys.argv[2]
                password = sys.argv[3] 
                beanName = sys.argv[4]
                action = sys.argv[5]
        else:
                sys.exit(-1)
        credentials = array([username,password],String)
        environment = {JMXConnector.CREDENTIALS:credentials}

        jmxServiceUrl = JMXServiceURL('service:jmx:rmi:///jndi/rmi://'+serverUrl+':9999/jmxrmi');
        jmxConnector = JMXConnectorFactory.connect(jmxServiceUrl,environment);
        mBeanServerConnection = jmxConnector.getMBeanServerConnection()
        objectName = ObjectName(beanName);
        mBeanServerConnection.invoke(objectName,action,None,None)
        jmxConnector.close()

By calling the script

jython jmxaction.py {ip} {jmx user} {jmx password} {mbean name} {action}

You can invoke the action of the mbean specified.

JDBC basic actions.

This is a basic set of database actions when using jdbc.

Issuing queries with prepared statements

/**
 * Will fetch only one result
 */
public String justQuery() throws SQLException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = dataSource.getConnection();
            preparedStatement = connection.prepareStatement("SELECT a FROM abc");
            ResultSet resultSet = preparedStatement.executeQuery();
            String a = null;
            if(resultSet.next()) {
                a = resultSet.getString("a");
            }
            resultSet.close();
            return a;
        } finally {
            //Close the statements
            if(preparedStatement!=null) preparedStatement.close();
            //Relase the connection resource back to the pool
            if(connection!=null) connection.close();
        }
        
    }

Fetching more the one results.

/**
 * Fetching many results
 */
public List<Long> justQuery() throws SQLException {
        
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = dataSource.getConnection();
            preparedStatement = connection.prepareStatement("SELECT id FROM abc");
            ResultSet resultSet = preparedStatement.executeQuery();
            List<Long> ids = new ArrayList<>();
            if(resultSet.next()) {
                Long id = resultSet.getLong("id");
                ids.add(id);
            }
            resultSet.close();
            return ids;
        } finally {
            //Close the statements
            if(preparedStatement!=null) preparedStatement.close();
            //Relase the connection resource back to the pool
            if(connection!=null) connection.close();
        }
    }

Calling stored procedures with callable statements

public long callProcedure(String something) throws SQLException {
        
        Connection connection = null;
        CallableStatement callableStatement = null;
        try {
            connection = dataSource.getConnection();
            callableStatement = connection.prepareCall("CALL doSomething(?,?)");
            callableStatement.setString(1, something);
            callableStatement.registerOutParameter(2, Types.NUMERIC);
            callableStatement.execute();
            Long lastInsertId = callableStatement.getLong("resultvar");
            return lastInsertId;
        } finally {
            if(callableStatement!=null) callableStatement.close();
            //Release back to the connection pool
            if(connection!=null) connection.close();
        }   
    }

Batch processing

public void doBatchOpeation(List<Long> userIds) throws SQLException {
        Connection connection = null;
        Statement batchStmt = null;
        try {
            connection = DatasourceProvider.getConnection();
            batchStmt = connection.createStatement();
            for(Long userId:userIds) {
                batchStmt.addBatch("UPDATE user SET active=true WHERE userid="+userId);
            }
            batchStmt.executeBatch();
        } finally {
            if(batchStmt!=null) batchStmt.close();
            if(connection!=null) connection.close();
        }
    }

And last but not least transactions!!!

public void doTransaction(Long userId,BigDecimal ammount) throws SQLException{

        Connection connection = null;
        PreparedStatement insertStatemt = null;
        PreparedStatement updateStatement = null;
        
        try {
            connection = dataSource.getConnection();
            connection.setAutoCommit(false);

            insertStatemt = connection.prepareStatement("INSERT INTO transfers (USERID,AMMOUNT) VALUES (?,?)");
            insertStatemt.setLong(1, userId);
            insertStatemt.setBigDecimal(2, ammount);
            insertStatemt.executeUpdate();

            updateStatement = connection.prepareStatement("UPDATE user SET tnum=tnum+1 WHERE userid=?");
            updateStatement.setLong(1, userId);
            updateStatement.executeUpdate();
            
            //Don't forget to commit
            connection.commit();
            
        } catch (SQLException e) {
            if(connection!=null) {
                connection.rollback();
            } else throw e;
        } finally {
            if(insertStatemt!=null) insertStatemt.close();
            if(updateStatement!=null) updateStatement.close();
            if(connection!=null) {
                //Setting autocommit true before sending connection back to the pool
                connection.setAutoCommit(true);
                //And release the connection back to the pool
                connection.close();
            }
        }
    }

All in all using jdbc has a lot of boilerplate and also you have to be carefull. While many other frameworks like spring provide tools such as a transaction manager or jdbc template, when using jdbc you have to do everything on your own. You are responsible for everything, for example releasing the connections back to the pool or setting the connection back to autocommit mode.

Java Tools and Proxies

Most of us work in offices where a proxy server setup.
So our build process, documentation, searches everything goes through the proxy server.

Proxy settings can be set on bot Eclipse and NetBeans.

On eclipse on preferences you choose General>Network Connections.
You set active provider on manual and you edit the http and https properties (proxy server, username,password). Also keep in mind to hit clear for the socks proxy entry.
After that you just need a restart.

Screen Shot 2014-05-11 at 11.46.24 PM

On maven where you have to download your dependencies you can set up your proxy settings there too.

You can just edit your ~/.m2/settings.xml

and inside the settings tag add

  <proxies>
    <proxy>
        <active>true</active>
        <protocol>http</protocol>
        <host>yourhost</host>
        <port>yourport</port>
        <username>username</username>
        <password>password</password>
        <nonProxyHosts>localhost,127.0.0.1</nonProxyHosts>
    </proxy>
  </proxies>