21 Haziran 2012 Perşembe

Reloadable message bundles with Spring's ReloadableResourceBundleMessageSource

Spring has a very nice class which saves our lives for reloading .properties files which we use in our applications often.

Say we have a bundle named bundle.properties in our application's classpath and there is a property named sample.property:

sample.property = Matrix Reloaded

We will define a ReloadableResourceBundleMessageSource in our application context for this bundle:


        
        


basename property is the bundle file name and the cacheSeconds property is the duration in seconds which the properties are cached. In the example above 60 seconds is the cache duration, after 60 seconds the message source will be refreshed.

Then in our application we can get a property as:

String sampleProperty = messageSource.getMessage("sample.property", null, null);

Our property can also be parametric. The second null parameter in the function above is the object array for parameters defined. In our sample.property we do not have any parameters so the second parameter is null. And the third parameter is the Locale which the property will be read.


20 Nisan 2012 Cuma

Intercepting a Spring bean method with Spring AOP

Spring can give us an ability to intercept a Spring Bean method easily. We can do some job before or after method proceeding. And also get the method return result and process it after the invokation.
Firstly we determine the method of a bean which will be intercepted. For example a Spring bean:

Let's say it has a sample method named sampleMethod. We should create a method interceptor which implements org.aopalliance.intercept.MethodInterceptor:
public class SampleInterceptor implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
         methodInvocation.proceed();
    } 
}
And then let's define it as a bean:

Then a point cut for the sample method which extends org.springframework.aop.support.DynamicMethodMatcherPointcut:
public class SampleMethodPointCut extends DynamicMethodMatcherPointcut {
    private Class clazz;
    private String methodName;

    public SampleMethodPointCut (Class clazz, String methodName) {
        this.clazz = clazz;
        this.methodName = methodName;
    }

    public boolean matches(Method method, Class cls) {
        return (methodName.equals(method.getName()));
    }

    public boolean matches(Method method, Class cls, Object[] args) {
        return true;
    }

    public ClassFilter getClassFilter() {
        return new ClassFilter() {
            public boolean matches(Class cls) {
                return (cls == clazz);
            }
        };
    }
}

      
      

And then we should define our point cut advisor:

       
       

Finally we should define bean name auto proxy creator:
    
        
        
            
                sampleBean
            
        
        
            
                sendMethodSplitterAdvisor
            
        
    
Our sampleMethod of sampleBean will be cut by our sample interceptor :)

21 Mart 2012 Çarşamba

Generating jax-ws classes of an endpoint with wsgen

An example jax-ws end point class:

package com.sample;

@WebService(serviceName = "sampleEndPoint")
public class SampleEndPoint {
    @WebMethod
    public boolean sampleWebMethod(@WebParam(name = "parameter") int parameter){
        return true;
    }
}
After the compilation of this class, we may want to generate request and response classes of this service. wsgen command is used for this purpose:
wsgen -keep -verbose -cp . com.sample.SampleEndPoint 

After the generation is done, we have two Java classes. One is for request and the other is for the response.
Request class is:
@XmlRootElement(name = "sampleWebMethod", namespace = "http://sample.com/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sampleWebMethod", namespace = "http://sample.com/")
public class Trigger {

    @XmlElement(name = "parameter", namespace = "")
    private int parameter;

    public int getParameter() {
        return this.parameter;
    }

    public void setParameter(int parameter) {
        this.parameter= parameter;
    }
}
and the response class is:
@XmlRootElement(name = "sampleWebMethodResponse", namespace = "http://sample.com/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sampleWebMethodResponse", namespace = "http://sample.com/")
public class SampleWebMethodResponse {

    @XmlElement(name = "return", namespace = "")
    private boolean _return;

    public boolean isReturn() {
        return this._return;
    }

    public void setReturn(boolean _return) {
        this._return = _return;
    }

}