Simple call to a Web Service with Excel

December 17, 2008 Leave a comment
I've made a basic test calling a web service using excel vba.
The xml data sent by the service is the following :
<Toto>
  <Titi val='2'/>
</Toto>

Looking on the net for some examples I've found this first one :

Public Sub wsCall_test1()

Dim objIE As Object
Dim Anymore As Boolean
Dim Found As Boolean

Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = False
objIE.Navigate "http://localhost:8182/"
Do While objIE.Busy: DoEvents: Loop
Do While objIE.readystate <> 4: DoEvents: Loop

MsgBox objIE.document.documentElement.innerText

Dim docXML As String
docXML = objIE.document.documentElement.innerText

Dim doc As DOMDocument
Set doc = New DOMDocument
MsgBox doc.loadXML(docXML)

MsgBox doc.selectSingleNode("Toto/Titi").Attributes(0).Text

End Sub

Finally it has turn out that it can be simplified to this :

</pre>
Public Sub wscall_test2()

Dim doc As DOMDocument
Set doc = New DOMDocument

MsgBox doc.Load("http://localhost:8182/")

MsgBox doc.XML

MsgBox doc.selectSingleNode("Toto/Titi").Attributes(0).Text

End Sub

In both test the msgbox used to output information are the following :

excelvba1

excelvba2excelvba3

Categories: Uncategorized Tags: , ,

Synchronizing threads

December 10, 2008 Leave a comment

To synchronize thread, use the monitoring methods.

Warning : the monitoring methods are available on all the java classes in any conditions albeit calling these method outside synchronied code will throw an exception. The following example describes it :

public static void main(String[] args) throws InterruptedException {
  Object lock = new Object();

  synchronized (lock) {
    System.out.println("calling a monitor method inside a synchronized block");
    lock.wait(1000);
  }

  System.out.println("calling a monitor method outside a synchronized block");
  lock.wait(1000);
}

This will lead to the following output :

calling a monitor method inside a synchronized block
calling a monitor method outside a synchronized block
Exception in thread “main” java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at TestThreadMonitor.main(TestThreadMonitor.java:15)

Categories: Uncategorized Tags: ,

Thread basics

December 10, 2008 Leave a comment

First of all the thread life cycle :

Thread Life Cycle

Thread Life Cycle

(Source reference: http://www.witscale.com/scjp_studynotes/chapter8.html)

There is two wayq to create a thread an run it :

Directly by extending the Thread class:

public class MyThread extends Thread {
  @Override
  public void run() {
  // do some stuff here
  }
}

To call it somewhere in you java code :

new MyThread().start();

Or by implementing the Runnable interface:

public class MyRunnable implements Runnable {
  Public void run() {
  // do some stuff here
  }
}

To call it somewhere in you java code :

new Thread(new MyRunnable()).start();

The best practice is to use the Runnable interface, this allows you to extends a business class or other if needed and its offers a better separation (than being stuck with the Thread class).

Categories: Uncategorized Tags: ,

Retreiving annotation data using introspection and dynamic proxy

December 10, 2008 Leave a comment

Just trying a simple test to retreive the annotation data during runtime using introspection.

Here’s the classes created for the test:

annotationsandproxy

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
    String value();
}

public class MyProxy implements InvocationHandler {

    private Object impl;

    private MyProxy(Object impl) {
        this.impl = impl;
    }

    public static Object newInstance(Object impl, Class[] interfaces) {
        return Proxy.newProxyInstance(impl.getClass().getClassLoader(), interfaces, new MyProxy(impl));
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // get the implementation object method on which the annotation has been setted
        Method implObjectMethod = impl.getClass().getMethod(method.getName(), method.getParameterTypes());

        // print the annotation in output
        Annotation[] annotations = implObjectMethod.getAnnotations();
        for (int i=0;i

Beans Binding (JSR 295) with POJO and AOP

December 2, 2008 11 comments

(download the net beans project : BeansBindingTest.zip.ppt : rename to .zip before opening)

Beans binding (JSR 295) is about keeping two properties in sync. To take a Simple example, let say that you have a simple text field (the view) in a Swing form and that you want it to be kept in sync with a POJO (the data model).

In a small first part I show how this can be done the standard way and in a second part how we can enhanced it we some simple AOP.

Part 1 : the standard way

Here the simple user form that we will create and bind with our data model :

beansbindingjrs295

And here the PersonVO.java POJO to be used as data model :


public class PersonVO {

  private String name;
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

public class PersonVOBindingEnabled extends PersonVO {
  protected PropertyChangeSupport props = new PropertyChangeSupport(this);
  public void addPropertyChangeListener( PropertyChangeListener l) {
    System.out.println("addPropertyChangeListener");
    props.addPropertyChangeListener(l);
  }

  public void removePropertyChangeListener( PropertyChangeListener l) {
    System.out.println("removePropertyChangeListener");
    props.removePropertyChangeListener(l);
  }

  public void setName(String newName) {
    String oldValue = getName();
    super.setName();
    props.firePropertyChange("name", oldName, newName);
  }
}

To bind the name attribute with the JTextField we use the Beans binding API :

JTextField nameTextField =…

PersonVO aPersonVO = new PersonVO();

aPersonVO.setName(“Brian”);

…

BeanProperty nameProperty = BeanProperty.create("name");

Binding b = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, aPersonVO, nameProperty, nameTextField, BeanProperty.create("text"));

And that’s it !

If you change the name attribute on the aPersonVO instance it will be sync in the JTextField and vice versa.

Yes but …

Having to extend the PersonVO so that it gets the PropertyChangeSupport is really annoying so it could be great if we could find a way to avoid it.

Part 2 : Using AOP

I have used AspectJ to automatically find my VO objects and add them all the necessary stuff for us :

package beansbindingtest.aspects;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import org.apache.commons.beanutils.PropertyUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;

public @Aspect class PropertyChangeListenerAspect {
  public interface ChangeListener {

  public void addPropertyChangeListener( PropertyChangeListener l);
  public void removePropertyChangeListener( PropertyChangeListener l);
  public String getPropertyName(String setterMethodName);
  public PropertyChangeSupport getPropertyChangeSupport();
}

public static class ChangeListenerImpl implements ChangeListener {

  protected PropertyChangeSupport props = new PropertyChangeSupport(this);

  public void addPropertyChangeListener( PropertyChangeListener l) {
    System.out.println("addPropertyChangeListener");
    props.addPropertyChangeListener(l);
  }

  public void removePropertyChangeListener( PropertyChangeListener l) {
    System.out.println("removePropertyChangeListener");
    props.removePropertyChangeListener(l);
  }

  public String getPropertyName(String setterMethodName) {
    setterMethodName = setterMethodName.substring(3);
    String firstLetter = setterMethodName.substring(0, 1);
    setterMethodName = firstLetter.toLowerCase() + setterMethodName.substring(1);
    return setterMethodName;
  }

  public PropertyChangeSupport getPropertyChangeSupport() {
    return props;
  }
}

  @DeclareParents(value="beansbindingtest.datamodel.*",defaultImpl=ChangeListenerImpl.class)
  private ChangeListener implInterface;

  @Around("execution(void *..*VO.set*(..))")
  public void interceptSetterAndFirePropertyChange(ProceedingJoinPoint pjp) throws Throwable {
    System.out.println("before " + pjp.getSignature().getName());
    String setterMethodName = pjp.getSignature().getName().substring(3);
    String firstLetter = setterMethodName.substring(0, 1);
    String propertyName = firstLetter.toLowerCase() + setterMethodName.substring(1);
    Object oldValue = null;
    try {
      oldValue = PropertyUtils.getProperty(pjp.getTarget(), propertyName);
    } catch (Exception e) {
      e.printStackTrace();
    }
    Object newValue = pjp.getArgs()[0];

    pjp.proceed();

    System.out.println("Firing event : " + propertyName + "; oldValue = " + oldValue + "; newValue = " + newValue);
    PropertyChangeEvent pe = new PropertyChangeEvent(pjp.getTarget(), propertyName, oldValue, newValue);
    ((ChangeListener)pjp.getTarget()).getPropertyChangeSupport().firePropertyChange(pe);
    System.out.println("after " + pjp.getSignature().getName());
  }

}

I have used the following jars :

  • aspectjrt-1.5.4.jar
  • aspectjweaver-1.5.4.jar
  • commons-beanutils-1.8.0.jar
  • commons-logging-1.1.1.jar

aop.xml has to be copied in the META-INF directory of your source.

And you need to add these args to the JVM when starting the application :

-javaagent:[set your path here]/aspectjweaver-1.5.4.jar

What is your opinion about using AOP for bean binding ?

Categories: java Tags: , , ,