Saturday, 23 June 2007

Holidays

I have three weeks!!!. One week to travel, and two weeks to be with my friends, go to the beach and whatever I want.
Goodbye work!!!

Sunday, 10 June 2007

My Web 2.0 favorite apps

One thing I like to do is test Web 2.0 applications. Ajaxian is in my favorite feeds. But there are three applications that change my habits in the Web.

  • Delicious. All that things that could be interesting I save in delicious. And my network is one of best source of really interesting things.
  • Google Reader. Previously I used to read my feeds in Thunderbird at home and work, and I had to mark as a read many many feeds in both sites. Google Reader it's very useful with two clicks on firefox I have a new feed, and I share my starred items.
  • Last.fm. Usually, I listen many music at work, and last.fm help me to find new groups, and share with my friends. My profile tells me who are my favorite groups now. The Killers and Coldplay, are winning.

Saturday, 2 June 2007

JSR 181 Web Service

In the last post I have used simple Web Service to test Spring-WS. Now I'm going to explain the implementation of this service.
The service is based in XFire and JSR 181 annotations.

First, the Book class, that represent the model.

package net.dahernan.model;

public class Book {

protected String title;
protected String isbn;
protected String author;
protected Double price;

// ... getters and setters
}


Next, the BookService class (yeah a class not an interface).

package net.dahernan.service;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

import net.dahernan.model.Book;

@WebService
public class BookService {

protected Book getMockBook(){
Book book = new Book();
book.setAuthor("Matt Raible");
book.setTitle("Spring Live");
book.setIsbn("12345");
book.setPrice(12.22);
return book;
}

protected List getMockBooks(){
List result = new ArrayList();
Book book = new Book();
book.setAuthor("Matt Raible");
book.setTitle("Spring Live");
book.setIsbn("12345");
book.setPrice(12.22);
result.add(book);

book = new Book();
book.setAuthor("Martin Fowler");
book.setTitle("Planning Extreme Programming");
book.setIsbn("12346");
book.setPrice(18.00);
result.add(book);
return result;
}

@WebMethod
@WebResult(name="Book")
public Book findBook(@WebParam(name="isbn")String isbn) {
Book book = this.getMockBook();
book.setIsbn(isbn);
return book;
}

@WebMethod
@WebResult(name="Books")
public List getBooks() {
return getMockBooks();
}

}


The class is annotated with @WebService. The first two methods are Mocks, and "getBooks" and "findBooks" are the methods exposed in service.

It's good practice annotate the result with @WebResult and the parameters with @WebParams to obtain an human readable WSDL.

Finally, the services.xml to configure my POJO.

<beans>
<service xmlns="http://xfire.codehaus.org/config/1.0">
<name>BookService</name>
<namespace>http://dahernan.net/BookService</namespace>
<serviceClass>net.dahernan.service.BookService</serviceClass>
<serviceFactory>#jsr181ServiceFactory</serviceFactory>
</service>

<bean id="config"
class="org.codehaus.xfire.aegis.type.Configuration">
<property name="defaultExtensibleElements" value="false" />
<property name="defaultExtensibleAttributes" value="false" />
<property name="defaultNillable" value="false" />
<property name="defaultMinOccurs" value="1" />
</bean>

<bean name="jsr181ServiceFactory"
class="org.codehaus.xfire.annotations.AnnotationServiceFactory">
<constructor-arg ref="xfire.transportManager" index="0" />
<constructor-arg ref="config" index="1"
type="org.codehaus.xfire.aegis.type.Configuration" />
</bean>

</beans>


The generated WSDL looks like a contract-first WSDL ;)

Sunday, 27 May 2007

Web Service client with Spring-WS and DuckTyping

This week I have tested Spring-WS, I'm interesting in Xpath support and I have done a simple client of BookService.
The BookService is a test of JSR 181 with XFire (perhaps I'll explain the implementation in other post).

One example of SOAP request/response:

SOAP Request:

<ns1:findBook xmlns:ns1="http://dahernan.net/BookService">
<ns1:isbn>12567</ns1:isbn>
</ns1:findBook>


SOAP Response:

<ns1:findBookResponse xmlns:ns1="http://dahernan.net/BookService">
<ns1:Book>
<author xmlns="http://model.dahernan.net">Matt Raible</author>
<isbn xmlns="http://model.dahernan.net">12567</isbn>
<price xmlns="http://model.dahernan.net">12.22</price>
<title xmlns="http://model.dahernan.net">Spring Live</title>
</ns1:Book>
</ns1:findBookResponse>


So, let's see the Spring-WS Client:

public class BookServiceClient {

private static final String MESSAGE =
"<ns1:findbook ns1="\"http://dahernan.net/BookService\"">" +
"<ns1:isbn>12567</ns1:isbn>" +
"</ns1:findbook>";

private WebServiceTemplate webServiceTemplate;

private XPathExpression xpathExp;

// ... getters and setters

public String getAuthor() {
StreamSource source = new StreamSource(new StringReader(MESSAGE));
DOMResult result = new DOMResult();
webServiceTemplate.sendAndReceive(source, result);
String author = xpathExp.evaluateAsString(result.getNode());

return author;
}


It's based in WebServiceTemplate (I like the Spring templates :), and XPathExpresion, the request is constant.
The response, first I build a DOM with response and next I evaluate the XPath Expresion.

And the Spring wiring.

<bean id="bookServiceClient" class="net.dahernan.example.BookServiceClient">
<property name="webServiceTemplate" ref="webServiceTemplate"/>
<property name="xpathExp" ref="xpathExp"/>
</bean>

<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<property name="defaultUri" value="http://localhost:8080/xfire_test/services/BookService"/>
<property name="messageSender">
<bean class="org.springframework.ws.transport.http.HttpUrlConnectionMessageSender"/>
</property>
</bean>

<bean id="xpathExp" class="org.springframework.xml.xpath.XPathExpressionFactoryBean">
<property name="expression" value="/ns1:findBookResponse/ns1:Book/dh:author"/>
<property name="namespaces">
<props>
<prop key="ns1">http://dahernan.net/BookService</prop>
<prop key="dh">http://model.dahernan.net</prop>
</props>
</property>
</bean>


The last step, is do a small test.

public class BookServiceClientTest extends AbstractDependencyInjectionSpringContextTests{

protected static Logger logger = Logger.getLogger(BookServiceClientTest.class);

protected String[] getConfigLocations() {
return new String [] {"ws-beans.xml"};
}

public void testClient(){
BookServiceClient client = (BookServiceClient) this.applicationContext.getBean("bookServiceClient");
String author = client.getAuthor();
logger.info("Author: " + author);
}

}


All, without stubs, It looks like great.

Wednesday, 16 May 2007

Things that I'd like to test

Now I'm testing Mule, because I'm interesting in ESB (it's fantastic I hope spend some time writing a post about Mule). And when I'm tired, I read about Scrum and Agile.

There are a lot of things that I'd like to test but I don't have enough time. Here a bunch of things:
My delicious network and my feeds make that the list grows. I have to spend some time in an idea that make me rich.

Sunday, 6 May 2007

Arid Framework or Simplifying Spring XML

Chris Richardson (author of Pojos in Action) in his blog tell us about Simplifying Spring XML with Arid Framework. This is a very interesting idea, and it's on the same line as actual trends about "Convention over Configuration".

This approach to the problem, it's based in autowire and custom namespace in Spring 2.0 XML.

I don't like especially autowire by type, it's useful for test proposes, but when you have two classes that implements the same interface, you must disabled. I see beginners developers crash with this, many times in test classes.

Another very useful thing about classic Spring XML, it's that you have a mirror of the architecture. You can use Spring IDE or another tool to see the real architecture, not an obsolete UML diagram. With annotations or conventions it's more difficult to see the architecture.

In my humble opinion, with expert developers Arid framework and other solutions about Convention over Configuration can be fantastic, but with beginners the best approach is the classic style.

Thursday, 19 April 2007

First entry

This is my new blog, nowadays my old blog is for archive . I open this blog for two reasons, the first one, and the most important, is to improve my english, I have a poor english and I think this blog can help me.

And the second reason is because I still have something to tell about Java, Maven, Spring, SOA, Agile and me.