22/07/2010

GWT is about to add Native JSON function support in version 2.1

GWT 2.1 has reached Milestone 2.

One of the new features in version 2.1 is support for browser's native JSON function in JSONParser class. More...

30/06/2010

Upgrading to Spring 3: "Unable to locate Spring NamespaceHandler" in JAR file built by Maven

After upgrading to Spring 3 your may discover that your application packaged in JAR archive does not start any more throwing an exception like this:

"Unable to locate Spring NamespaceHandler for XML schema namespace[http://www.springframework.org/schema/tx]"

This happens when you include multiple Spring module dependencies in your pom.xml file and use Maven Shade or Assembly plugin to build a single JAR.
Since Spring can does not contain a single distribution jar (org.springframework:spring:jar), Spring namespace handlers, schema mappings and tooling information files are now present in multiple files with names:

  • META-INF/spring.handlers
  • META-INF/spring.schemas
  • META-INF/spring.tooling

In order to make it work in a single shaded JAR you need to merge contents of these files from different jars and place merged files into new JAR. More...

19/11/2009

Mac OS X Applications for Everyday. Keyboard Layout Switchers.


After two years of Mac user experience, I'd like list the applications I use everyday.

I am Russian, so I need a to write the texts in other encoding than ISO. Under windows, there is nice tool called Punto Switcher. For long time there was no such tool for mac, except RuSwitcher by Alexey Proskuryakov. RuSwitcher works only on Tiger and does not work under Leopard.

Hopefully, guys from Yandex has ported PuntoSwitcher to Mac OS X (Leopard & Snow Leopard). It is still a beta but I have no problems using it.

Some of the Punto Switcher's features are:
  • Switching keyboard layout automatically or with a hot key.
  • Fix incorrect layout for selected text.
  • Mac and Windows layout support.

30/01/2009

How to Lookup JBoss MBean Server from Spring (JMX)

If you having problems accessing "mbeanServer" Spring-managed bean under JBoss Application server Read more...

29/01/2009

How to Use Spring from EJB3

This is a short instruction how to inject a spring-managed bean into EJB3 component.

  1. Read SpringFratamework's reference here: http://static.springframework.org/spring/docs/2.5.x/reference/ejb.html#ejb-implementation-ejb3

  2. Place to ejb module's classpath a file beanRefContext.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>

    <bean id="myBeanFactory" class="org.springframework.context.support.ClassPathXmlApplicationContext">
    <constructor-arg value="myApplicationContext.xml"/>
    </bean>

    </beans>

  3. Create application context file named myApplicationContext.xml and define You beans there. Place this file to the ejb module's classpath.

  4. Annotate your Stateless Session Bean:
    @Stateless
    @Interceptors(org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor.class)
    public class MyFacadeBean implements MyFacade {

    @Autowired
    private MySpringComponent component;
    ...
    public void foo() {
    component.foo();//invocation
    }
    }

  5. Deploy and test Your application.

27/01/2009

How To Export Spring Managed Bean To JNDI

Sometimes, it is necessary to export a spring managed bean to JNDI context. Here I want to show how do it.

In spring, there is a bean that provides a similar functionality for exporting to MBean server: MBeanExporter. Unfortunately, there is no standard JNDI bean exporter implementation in spring (current version is 2.5.6) - (Why?).
But it's easy to write it youself:
package com.example.spring.jndi.export;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jndi.JndiTemplate;

public class JndiExporter implements InitializingBean, DisposableBean {

private String jndiName;

private Object bean;

private final JndiTemplate jndiTemplate = new JndiTemplate();

public String getJndiName() {
return jndiName;
}

public void setJndiName(String jndiName) {

this.jndiName = jndiName;
}

public Object getBean() {
return bean;
}

public void setBean(Object bean) {
this.bean = bean;
}


public void afterPropertiesSet() throws Exception {
jndiTemplate.bind(jndiName, bean);
}

public void destroy() throws Exception {
if (bean != null && jndiName != null && bean == jndiTemplate.lookup(jndiName)) {
jndiTemplate.unbind(jndiName);
}
}
}

Add following fragment to spring configuration file:
<bean id="myBean" class="com.example.MyBean"/>

<bean class="com.example.spring.jndi.export.JndiExporter">
<property name="bean" ref="myBean" />
<property name="jndiName" value="MyJNDIName"/>
</bean>
Don't forget to make your bean serializable by implementing java.io.Serializable interface.
Now we can lookup exported bean by adding to the spring config fil:
<jee:jndi-lookup id="myJndiBean" jndi-name="MyJNDIName" proxy-interface="com.example.IMyBean" lookup-on-startup="false"/>

That's all, folks!

02/01/2009

Use BigDecimals for Financial Calculations: One More Example

Recently I was asked about the preferred data type in java application dealing with financial data. I suggest using of BigDecimal and here is the example why. Let's perform following arithmetic operations over double values, double values with strictfp mode enabled and over BigDecimals.

Expression is: "0.999/9 - 0.112"
Expected result is: "-0.001"

Java code:
import java.math.BigDecimal;

public class FloationPointTest {

public strictfp static void testStrictfp() {
System.out.println("strictfp: 0.999/9-0.112 = " + (0.999/9-0.112));
}

public static void main(String ... args) {
System.out.println("double: 0.999/9-0.112 = " + (0.999/9-0.112));
testStrictfp();
System.out.println("BigDecimal: 0.999/9-0.112 = " + (new BigDecimal("0.999").divide(new BigDecimal("9")).subtract(new BigDecimal("0.112"))));
}
}

Program output:
double: 0.999/9-0.112 = -0.0010000000000000009
strictfp: 0.999/9-0.112 = -0.0010000000000000009
BigDecimal: 0.999/9-0.112 = -0.001

We see that only operations with BigDecimal does not lead to the lost of precision. Performing operation over values of type double even in strictfp mode may loose accuracy. It is not acceptable for financial calculations in the days of Global financial crisis.

redirect