<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   >
<channel>
    <title>Gridshore software engineering weblog - Springframework</title>
    <link>http://www.gridshore.nl/blog/</link>
    <description>This weblog is about everything that has to do with software engineering. It will focus on items like Springframework, acegi, hibernate and other tools to help creating good software</description>
    <dc:language>en</dc:language>
    <generator>Serendipity 1.2.1 - http://www.s9y.org/</generator>
    <pubDate>Tue, 29 Jan 2008 10:27:03 GMT</pubDate>

    <image>
        <url>http://www.gridshore.nl/blog/templates/gridshore/img/s9y_banner_small.png</url>
        <title>RSS: Gridshore software engineering weblog - Springframework - This weblog is about everything that has to do with software engineering. It will focus on items like Springframework, acegi, hibernate and other tools to help creating good software</title>
        <link>http://www.gridshore.nl/blog/</link>
        <width>100</width>
        <height>21</height>
    </image>

<item>
    <title>Last entry ...</title>
    <link>http://www.gridshore.nl/blog/index.php?/archives/74-Last-entry-....html</link>
            <category>acegi</category>
            <category>ajax</category>
            <category>Announcements</category>
            <category>Books</category>
            <category>gridshore</category>
            <category>Java</category>
            <category>News</category>
            <category>spring-osgi</category>
            <category>Springframework</category>
            <category>Technology</category>
            <category>Webservices</category>
            <category>Websites&amp;products</category>
    
    <comments>http://www.gridshore.nl/blog/index.php?/archives/74-Last-entry-....html#comments</comments>
    <wfw:comment>http://www.gridshore.nl/blog/wfwcomment.php?cid=74</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://www.gridshore.nl/blog/rss.php?version=2.0&amp;type=comments&amp;cid=74</wfw:commentRss>
    

    <author>nospam@example.com (Jettro Coenradie)</author>
    <content:encoded>
    Welcome to the last entry of this blog. No I am not quitting, I am just moving. I just installed a new blog software framework. I was starting to dislike the way to interact and I wanted something that contained more out of the box. I am in the middle of moving to wordpress software. There is a very good reason to do this. I had found a great tool on the mac called MarsEdit. And again this could not be used with serendipity. I hope I did make a good choice. Since there is a lot of information in the old blog, I did not take this blog offline. It will stay to exist, but I will not make changes there anymore.&lt;br /&gt;
&lt;br /&gt;
I hope to see you at the new version of the blog (check the page at http://www.gridshore.nl). You can attach you feedreader to the following url: feed://www.gridshore.nl/feed/ 
    </content:encoded>

    <pubDate>Sat, 19 Jan 2008 20:58:56 +0100</pubDate>
    <guid isPermaLink="false">http://www.gridshore.nl/blog/index.php?/archives/74-guid.html</guid>
    
</item>
<item>
    <title>Creating an application with maven2, jpa, springframework and intellij</title>
    <link>http://www.gridshore.nl/blog/index.php?/archives/72-Creating-an-application-with-maven2,-jpa,-springframework-and-intellij.html</link>
            <category>Java</category>
            <category>Springframework</category>
    
    <comments>http://www.gridshore.nl/blog/index.php?/archives/72-Creating-an-application-with-maven2,-jpa,-springframework-and-intellij.html#comments</comments>
    <wfw:comment>http://www.gridshore.nl/blog/wfwcomment.php?cid=72</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://www.gridshore.nl/blog/rss.php?version=2.0&amp;type=comments&amp;cid=72</wfw:commentRss>
    

    <author>nospam@example.com (Jettro Coenradie)</author>
    <content:encoded>
    Some time ago I started working with intellij, recently I bought a mac, and now I want to start programming. But where to start? There are some mandatory things, or not? Well lets just start and try to make it as flexible as possible so we can use other frameworks for some layers. For now I will focus on the following technologies:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Springframework&lt;/li&gt;&lt;li&gt;JPA, the hibernate implementation&lt;/li&gt;&lt;li&gt;Java 5 and annotations&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;
&lt;br /&gt;
When creating the sample I started of programming in Intellij. Even with the excellent integration of JPA and Hibernate I still had a lot op problems creating the sample. Damn this is not easy. So what do you do when you are stuck? You start reading, I already had some books that could help me out. Still I like the Java Persistence with Hibernate book the best. The similarities between hibernate and JPA enabled me to quicker learn the basics. So I started reading, I came to the conclusion I had or wanted to start using annotations. It struck me how well Intellij was helping me again.&lt;br /&gt;
&lt;br /&gt;
Just a few tips when developing with intellij, maven, jpa, spring, etc.&lt;br /&gt;
&lt;br /&gt;
Create a new project based on an external model, click next and choose maven. Click next again and browse to the folter containing the master pom file. Click next and you have the option to select the project. The finish will create your new project with all modules present.&lt;br /&gt;
&lt;br /&gt;
&lt;!-- s9ymdb:40 --&gt;&lt;img width=&#039;500&#039; height=&#039;302&#039; style=&quot;border: 0px; padding-left: 5px; padding-right: 5px;&quot; src=&quot;http://www.gridshore.nl/blog/uploads/jpaspring/createnewintellijproject.jpg&quot; alt=&quot;&quot; /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;!-- s9ymdb:44 --&gt;&lt;img width=&#039;500&#039; height=&#039;302&#039; style=&quot;border: 0px; padding-left: 5px; padding-right: 5px;&quot; src=&quot;http://www.gridshore.nl/blog/uploads/jpaspring/createnewintellijproject2.jpg&quot; alt=&quot;&quot; /&gt;&lt;br /&gt;
&lt;br /&gt;
By default the spring and jpa facets are not present. You need to add them to the modules. Open the module settings (right mouse click and select menu item) and add the plus sign for adding a facet. The following screen shows the result of adding the spring facet and creating a new File Set. Another cool thing is that you can create a datasource to a database and connect this to a persistent unit. Columns and tables are then checked when creating your annotations.&lt;br /&gt;
&lt;br /&gt;
&lt;!-- s9ymdb:43 --&gt;&lt;img width=&#039;600&#039; height=&#039;266&#039; style=&quot;border: 0px; padding-left: 5px; padding-right: 5px;&quot; src=&quot;http://www.gridshore.nl/blog/uploads/jpaspring/addfacettomodule.jpg&quot; alt=&quot;&quot; /&gt;&lt;br /&gt;
&lt;br /&gt;
I like the intellij way of creating new projects, but still I am a maven adept, so I wanted to have it all running with maven. You can find the sample online again. Beware, it is work in progress. I am adding a front end and more logic the coming weeks. Check:&lt;br /&gt;
&lt;a href=&quot;http://gridshore.googlecode.com/svn/trunk/RaffleApp/&quot;  title=&quot;sources&quot;&gt;http://gridshore.googlecode.com/svn/trunk/RaffleApp/&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
If you want to start with jpa, spring and maven I can recommend the following archetype:&lt;br /&gt;
&lt;a href=&quot;http://www.rateyourwriting.com/downloads/jpa-hibernate-archetype.tar.gz&quot;  title=&quot;maven archetype&quot;&gt;http://www.rateyourwriting.com/downloads/jpa-hibernate-archetype.tar.gz&lt;/a&gt;&lt;br /&gt;
I did not try it out myself, but it looks very good. It is create by Chris Maki (&lt;a href=&quot;http://www.jroller.com/cmaki/category/JPA+101&quot;  title=&quot;blog&quot;&gt;http://www.jroller.com/cmaki/category/JPA+101&lt;/a&gt;)&lt;br /&gt;
&lt;br /&gt;
In the reference manual of the springframework I found a sentence that for new applications they prefer not to use the JpaDaoSuppor class. There is another way based on the special bean PersistenceAnnotationBeanPostProcessor. Using this bean you can use annotations like :  @Entity, @PersistenceContext from within the spring container.&lt;br /&gt;
&lt;br /&gt;
Back to my problem, for some reason my tests using the AbstractJpaTests did not work with the separate jars for domain and data access (dao). I started by combining them in one module. But I was not satisfied. So I went back to my friend Google. I typed in a query for information about using modules with jpa and springframework. To my surprise I got back a lot of articles about the debate between using DAOs or not. Yes we are back to the  discussion about DDD or Domain Driven Design. After reading a lot of posts I guess the following sentence found on a blog item from Craig Wickesser but written by Adam Bien:&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;em&gt;I would say: it depends. It depends how complex your application really is.&lt;/em&gt;&lt;/strong&gt; &lt;br /&gt;
&lt;br /&gt;
Now back to the original problem, how to design a application using JPA?&lt;br /&gt;
&lt;br /&gt;
Some of the choices you have to make:&lt;ul&gt;&lt;li&gt;Use annotations or XML or both?&lt;/li&gt;&lt;li&gt;Use JpaTemplate or annotations and (org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor)&lt;/li&gt;&lt;li&gt;Create one or multiple modules&lt;br /&gt;
Use a DAO layer or step into the DDD world and use the Persistence class.&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;
&lt;br /&gt;
In the end I did manage to get back to my so well known structure of a DAO layer and domain objects. What I had to do? We will check the persistence.xml file later on. I had to add the specific classes in this file.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Every problem needs a sample&lt;/strong&gt;&lt;br /&gt;
Let me first explain a bit about the example that I am going to use. For a technology evening at my company I had some books that I could give away to the people present. Since it was an evening about the Google Web Toolkit I created a nice front end application using a file as storage to a Raffle application. Check the following application if you want to see it:&lt;br /&gt;
&lt;a href=&quot;http://gridshore.googlecode.com/svn/trunk/Raffle/&quot;  title=&quot;google web toolkit sample&quot;&gt;http://gridshore.googlecode.com/svn/trunk/Raffle/&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
I decided this file thing was not really optimal, so I wanted to create a database backed application. What to use? Well lets try it out JPA, hibernate started to get boring and I wanted to move on. So what is in the raffle applications domain model&lt;br /&gt;
&lt;br /&gt;
&lt;!-- s9ymdb:45 --&gt;&lt;img width=&#039;646&#039; height=&#039;364&#039; style=&quot;border: 0px; padding-left: 5px; padding-right: 5px;&quot; src=&quot;http://www.gridshore.nl/blog/uploads/jpaspring/domainclassdiagram.jpeg&quot; alt=&quot;&quot; /&gt;&lt;br /&gt;
&lt;br /&gt;
Some rules:&lt;ul&gt;&lt;li&gt;A raffle has multiple prices and multiple participants.&lt;/li&gt;&lt;li&gt;A price has a winner&lt;/li&gt;&lt;li&gt;A winner is a participant, or has a participant and a price.&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;
&lt;br /&gt;
We begin with the project setup using maven:&lt;br /&gt;
First the dependencies that you need in your pom:&lt;br /&gt;
spring-aop, spring-dao, spring-jpa, spring-hibernate3, hibernate-entitymanager, c3p0, mysql-connector-java, concurrent, spring-mock&lt;br /&gt;
&lt;br /&gt;
Thats about it, put that in the pom.xml, create the structure and do a mvn clean install, or ...&lt;br /&gt;
&lt;br /&gt;
Fire up intelij, create a new project based on this pom.xml and you can do the same things as I showed before.&lt;br /&gt;
&lt;br /&gt;
Well begin with the domain objects:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
@MappedSuperclass&lt;br /&gt;
public class BaseDomain {&lt;br /&gt;
    @Id&lt;br /&gt;
    @GeneratedValue&lt;br /&gt;
    private Long id;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
@Entity&lt;br /&gt;
@Table (name = &quot;participants&quot;)&lt;br /&gt;
public class Participant extends BaseDomain {&lt;br /&gt;
    private String name;&lt;br /&gt;
    @ManyToOne&lt;br /&gt;
    @JoinColumn(name = &quot;raffle_id&quot;)&lt;br /&gt;
    private Raffle raffle;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
@Entity&lt;br /&gt;
@Table(name = &quot;prices&quot;)&lt;br /&gt;
public class Price extends BaseDomain {&lt;br /&gt;
    private String title;&lt;br /&gt;
    private String description;&lt;br /&gt;
    @ManyToOne&lt;br /&gt;
    @JoinColumn(name = &quot;raffle_id&quot;)&lt;br /&gt;
    private Raffle raffle;&lt;br /&gt;
    @OneToOne (mappedBy = &quot;price&quot;)&lt;br /&gt;
    private Winner winner;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
@Entity&lt;br /&gt;
@Table (name = &quot;winners&quot;)&lt;br /&gt;
public class Winner extends BaseDomain {&lt;br /&gt;
    @OneToOne&lt;br /&gt;
    @JoinColumn(name = &quot;price_id&quot;)&lt;br /&gt;
    private Price price;&lt;br /&gt;
    @ManyToOne&lt;br /&gt;
    @JoinColumn(name = &quot;participant_id&quot;)&lt;br /&gt;
    private Participant participant;&lt;br /&gt;
}&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
Next step is to create a DAO interface that does not have anything to do with JPA, I use the interface like this:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
public interface BaseDao&lt;T extends BaseDomain&gt; {&lt;br /&gt;
    T save(T entity);&lt;br /&gt;
    T loadByExample(T entity);&lt;br /&gt;
    T loadById(Long entityId);&lt;br /&gt;
    List&lt;T&gt; loadAll();&lt;br /&gt;
    List&lt;T&gt; loadByFilter(T entityFilter);&lt;br /&gt;
}&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
For now I will not focus on the implementation, I am not a JPA expert yet &lt;img src=&quot;http://www.gridshore.nl/blog/templates/gridshore/img/emoticons/smile.png&quot; alt=&quot;:-)&quot; style=&quot;display: inline; vertical-align: bottom;&quot; class=&quot;emoticon&quot; /&gt;. One thing I want to mention is the EntityManager. Using a special annotation we make sure the EntityManager is injected by the container, the spring container in our case.&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
public abstract class BaseDaoJpa&lt;T extends BaseDomain&gt; implements BaseDao&lt;T&gt; {&lt;br /&gt;
    @PersistenceContext&lt;br /&gt;
    private EntityManager entityManager;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
Next step is to configure the spring beans:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
&amp;lt;bean id=&quot;raffleDao&quot; class=&quot;nl.gridshore.samples.raffle.dao.jpa.RaffleDaoJpa&quot;/&gt;&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;bean id=&quot;entityManagerFactory&quot; class=&quot;org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean&quot;&gt;&lt;br /&gt;
        &amp;lt;property name=&quot;persistenceUnitName&quot; value=&quot;raffle&quot;/&gt;&lt;br /&gt;
        &amp;lt;property name=&quot;jpaVendorAdapter&quot;&gt;&lt;br /&gt;
            &amp;lt;bean class=&quot;org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter&quot;&gt;&lt;br /&gt;
                &amp;lt;property name=&quot;databasePlatform&quot; value=&quot;${hibernate.sql.dialect}&quot;/&gt;&lt;br /&gt;
            &amp;lt;/bean&gt;&lt;br /&gt;
        &amp;lt;/property&gt;&lt;br /&gt;
        &amp;lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;/&gt;&lt;br /&gt;
    &amp;lt;/bean&gt;&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;bean id=&quot;transactionManager&quot; class=&quot;org.springframework.orm.jpa.JpaTransactionManager&quot;&gt;&lt;br /&gt;
        &amp;lt;property name=&quot;entityManagerFactory&quot; ref=&quot;entityManagerFactory&quot;/&gt;&lt;br /&gt;
    &amp;lt;/bean&gt;&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;bean class=&quot;org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor&quot;/&gt;&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;bean id=&quot;dataSource&quot; class=&quot;org.springframework.jdbc.datasource.DriverManagerDataSource&quot;&gt;&lt;br /&gt;
        &amp;lt;property name=&quot;driverClassName&quot; value=&quot;${jdbc.driverclass}&quot;/&gt;&lt;br /&gt;
        &amp;lt;property name=&quot;url&quot; value=&quot;${jdbc.url}&quot;/&gt;&lt;br /&gt;
        &amp;lt;property name=&quot;username&quot; value=&quot;${jdbc.username}&quot;/&gt;&lt;br /&gt;
        &amp;lt;property name=&quot;password&quot; value=&quot;${jdbc.password}&quot;/&gt;&lt;br /&gt;
    &amp;lt;/bean&gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
Finally we need to configure the the entity manager, this is done with a special file named persistence.xml which is displayed beneath.&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
&amp;lt;persistence xmlns=&quot;http://java.sun.com/xml/ns/persistence&quot; version=&quot;1.0&quot;&gt;&lt;br /&gt;
    &amp;lt;persistence-unit name=&quot;raffle&quot; transaction-type=&quot;RESOURCE_LOCAL&quot;&gt;&lt;br /&gt;
        &amp;lt;provider&gt;org.hibernate.ejb.HibernatePersistence&amp;lt;/provider&gt;&lt;br /&gt;
        &amp;lt;class&gt;nl.gridshore.samples.raffle.domain.BaseDomain&amp;lt;/class&gt;&lt;br /&gt;
        &amp;lt;class&gt;nl.gridshore.samples.raffle.domain.Raffle&amp;lt;/class&gt;&lt;br /&gt;
        &amp;lt;class&gt;nl.gridshore.samples.raffle.domain.Price&amp;lt;/class&gt;&lt;br /&gt;
        &amp;lt;class&gt;nl.gridshore.samples.raffle.domain.Participant&amp;lt;/class&gt;&lt;br /&gt;
        &amp;lt;class&gt;nl.gridshore.samples.raffle.domain.Winner&amp;lt;/class&gt;&lt;br /&gt;
        &amp;lt;properties&gt;&lt;br /&gt;
            &amp;lt;property name=&quot;hibernate.archive.autodetection&quot; value=&quot;class&quot;/&gt;&lt;br /&gt;
            &amp;lt;property name=&quot;hibernate.show_sql&quot; value=&quot;true&quot;/&gt;&lt;br /&gt;
            &amp;lt;property name=&quot;hibernate.format_sql&quot; value=&quot;true&quot;/&gt;&lt;br /&gt;
            &amp;lt;property name=&quot;hibernate.dialect&quot; value=&quot;org.hibernate.dialect.MySQL5Dialect&quot;/&gt;&lt;br /&gt;
        &amp;lt;/properties&gt;&lt;br /&gt;
    &amp;lt;/persistence-unit&gt;    &lt;br /&gt;
&amp;lt;/persistence&gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
That is enough to start using the application. However, there is one thing I forget. What about testing this application. Spring comes with a special class for testing called AbstractJpaTests. This is class had familiar functionality, auto dependency injection and rolling back a transaction after the test.&lt;br /&gt;
&lt;br /&gt;
Using this test you still need a database. For the data access tests I am using a combination of dbunit, springframework and hsqldb. Most important part of the test class is the injection of the dao:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
public class RaffleDaoJpaTest extends AbstractJpaTests {&lt;br /&gt;
    private RaffleDaoJpa raffleDao;&lt;br /&gt;
&lt;br /&gt;
    public void setRaffleDao(RaffleDaoJpa raffleDao) {&lt;br /&gt;
        this.raffleDao = raffleDao;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
	...&lt;br /&gt;
&lt;br /&gt;
    protected String[] getConfigLocations() {&lt;br /&gt;
        return new String[] {&quot;classpath:test-dao-spring.xml&quot;,&quot;classpath:dao-config.xml&quot;};&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
The following beans are in the special test spring config file together with the datasource for the test database.&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
    &amp;lt;bean id=&quot;dbUnitInsertOperation&quot; class=&quot;org.springframework.beans.factory.config.MethodInvokingFactoryBean&quot;&lt;br /&gt;
            depends-on=&quot;transactionManager&quot;&gt;&lt;br /&gt;
        &amp;lt;property name=&quot;targetObject&quot;&gt;&lt;br /&gt;
            &amp;lt;bean class=&quot;org.dbunit.operation.RefreshOperation&quot; /&gt;&lt;br /&gt;
        &amp;lt;/property&gt;&lt;br /&gt;
        &amp;lt;property name=&quot;targetMethod&quot; value=&quot;execute&quot; /&gt;&lt;br /&gt;
        &amp;lt;property name=&quot;arguments&quot;&gt;&lt;br /&gt;
            &amp;lt;list&gt;&lt;br /&gt;
                &amp;lt;ref bean=&quot;dbUnitConnection&quot; /&gt;&lt;br /&gt;
                &amp;lt;bean id=&quot;dbUnitDataSet&quot; class=&quot;org.dbunit.dataset.xml.FlatXmlDataSet&quot;&gt;&lt;br /&gt;
                    &amp;lt;constructor-arg&gt;&lt;br /&gt;
                        &amp;lt;bean id=&quot;refDataFile&quot; factory-bean=&quot;referenceData&quot; factory-method=&quot;getInputStream&quot; /&gt;&lt;br /&gt;
                    &amp;lt;/constructor-arg&gt;&lt;br /&gt;
                &amp;lt;/bean&gt;&lt;br /&gt;
            &amp;lt;/list&gt;&lt;br /&gt;
        &amp;lt;/property&gt;&lt;br /&gt;
    &amp;lt;/bean&gt;&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;bean id=&quot;referenceData&quot; class=&quot;org.springframework.core.io.ClassPathResource&quot;&gt;&lt;br /&gt;
        &amp;lt;constructor-arg value=&quot;testdata.xml&quot;/&gt;&lt;br /&gt;
    &amp;lt;/bean&gt;&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;bean id=&quot;dbUnitConnection&quot;	class=&quot;util.CustomDatabaseConnection&quot;&gt;&lt;br /&gt;
        &amp;lt;constructor-arg&gt;&lt;br /&gt;
            &amp;lt;bean id=&quot;connection&quot; factory-bean=&quot;dataSource&quot;	factory-method=&quot;getConnection&quot; /&gt;&lt;br /&gt;
        &amp;lt;/constructor-arg&gt;&lt;br /&gt;
    &amp;lt;/bean&gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
That is about it, again, check the source code, everything is runnable by maven2 and tested on tomcat 5.5. If you want to read more, I used the following resources while creating learning about jpa and the other topics.&lt;br /&gt;
&lt;br /&gt;
Links:&lt;br /&gt;
&lt;a href=&quot;http://www.infoq.com/news/2007/09/jpa-dao&quot; &gt;http://www.infoq.com/news/2007/09/jpa-dao&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://debasishg.blogspot.com/&quot; &gt;http://debasishg.blogspot.com/&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://www.vitarara.org/cms/struts_2_cookbook/integration_testing&quot; &gt;http://www.vitarara.org/cms/struts_2_cookbook/integration_testing&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://dev2dev.bea.com/pub/a/2006/03/jpa-spring-medrec.html?page=6&quot; &gt;http://dev2dev.bea.com/pub/a/2006/03/jpa-spring-medrec.html?page=6&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://www.jroller.com/cmaki/entry/domain_model_revisited&quot; &gt;http://www.jroller.com/cmaki/entry/domain_model_revisited&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 
    </content:encoded>

    <pubDate>Wed, 21 Nov 2007 19:49:46 +0100</pubDate>
    <guid isPermaLink="false">http://www.gridshore.nl/blog/index.php?/archives/72-guid.html</guid>
    
</item>
<item>
    <title>Loading properties using JNDI within the springframework deployed on tomcat</title>
    <link>http://www.gridshore.nl/blog/index.php?/archives/71-Loading-properties-using-JNDI-within-the-springframework-deployed-on-tomcat.html</link>
            <category>Java</category>
            <category>Springframework</category>
    
    <comments>http://www.gridshore.nl/blog/index.php?/archives/71-Loading-properties-using-JNDI-within-the-springframework-deployed-on-tomcat.html#comments</comments>
    <wfw:comment>http://www.gridshore.nl/blog/wfwcomment.php?cid=71</wfw:comment>

    <slash:comments>1</slash:comments>
    <wfw:commentRss>http://www.gridshore.nl/blog/rss.php?version=2.0&amp;type=comments&amp;cid=71</wfw:commentRss>
    

    <author>nospam@example.com (Jettro Coenradie)</author>
    <content:encoded>
    Hai All,&lt;br /&gt;
finally a new technical item. It is a short item, but I have been struggling to get this to work. Therefore I want to share this so other can use it to configure there properties in a flexible way.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Problem:&lt;/strong&gt;&lt;br /&gt;
Properties like database url&#039;s, userid&#039;s and pasword&#039;s are different in your test environment and your production environment. There are other items you might want to configure.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br /&gt;
You can make complicated deployment scripts that get the right property files for the right environment. But you need to create different packages for different environments. I think a more elegant solution is to use the JNDI parameters to load the specific environmental values. If there are a lot a good way might be to store them in a database and only configure the database access parameters using JNDI.&lt;br /&gt;
&lt;br /&gt;
The following solution is based on two posts in different forums:&lt;br /&gt;
&lt;a href=&quot;http://forum.java.sun.com/thread.jspa?threadID=647327&amp;messageID=3810991&quot; &gt;http://forum.java.sun.com/thread.jspa?threadID=647327&amp;messageID=3810991&lt;/a&gt; - gives details about the configuration of tomcat.&lt;br /&gt;
&lt;a href=&quot;http://forum.springframework.org/showthread.php?t=30991&quot; &gt;http://forum.springframework.org/showthread.php?t=30991&lt;/a&gt; - gives details about the spring configuration&lt;br /&gt;
&lt;br /&gt;
Now the code, we start with the tomcat configuration. The server.xml file contains all the actual values connected to the exposed JNDI parameter names.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;server.xml&lt;/strong&gt;&lt;br /&gt;
&lt;pre style=&quot;width:600px&quot;&gt;&lt;br /&gt;
  &amp;lt;!-- Global JNDI resources --&gt;&lt;br /&gt;
  &amp;lt;GlobalNamingResources&gt;&lt;br /&gt;
    &amp;lt;!-- Test entry for demonstration purposes --&gt;&lt;br /&gt;
    &amp;lt;Environment name=&quot;jdbc/username&quot; value=&quot;myUser&quot; type=&quot;java.lang.String&quot;/&gt;&lt;br /&gt;
    &amp;lt;Environment name=&quot;jdbc/password&quot; value=&quot;myPwd&quot; type=&quot;java.lang.String&quot;/&gt;&lt;br /&gt;
    &amp;lt;Environment name=&quot;jdbc/url&quot; value=&quot;jdbc:mysql://servername/schema&quot; type=&quot;java.lang.String&quot;/&gt;&lt;br /&gt;
    &amp;lt;Environment name=&quot;jdbc/drivername&quot; value=&quot;com.mysql.jdbc.Driver&quot; type=&quot;java.lang.String&quot;/&gt;&lt;br /&gt;
  &amp;lt;/GlobalNamingResources&gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
We need to tell the application the names of the parameters that are available. We must add the following items to the web configuration file.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;web.xml&lt;/strong&gt;&lt;br /&gt;
&lt;pre style=&quot;width:600px&quot;&gt;&lt;br /&gt;
&amp;lt;resource-env-ref&gt;&lt;br /&gt;
    &amp;lt;resource-env-ref-name&gt;jdbc/username&lt;/resource-env-ref-name&gt;&lt;br /&gt;
    &amp;lt;resource-env-ref-type&gt;java.lang.String&lt;/resource-env-ref-type&gt;&lt;br /&gt;
&amp;lt;/resource-env-ref&gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
Of course you need to do this for all four parameters. Then the tricky part. This is the one I missed while getting this to work. You need to add a tomcat specific deployment configuration with the following lines&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;META-INF/context.xml&lt;/strong&gt;&lt;br /&gt;
&lt;pre  style=&quot;width:600px&quot; &gt;&lt;br /&gt;
&amp;lt;Context path=&quot;/webapp&quot;&gt;&lt;br /&gt;
    &amp;lt;ResourceLink name=&quot;jdbc/username&quot; global=&quot;jdbc/username&quot; type=&quot;java.lang.String&quot;/&gt;&lt;br /&gt;
    &amp;lt;ResourceLink name=&quot;jdbc/password&quot; global=&quot;jdbc/password&quot; type=&quot;java.lang.String&quot;/&gt;&lt;br /&gt;
    &amp;lt;ResourceLink name=&quot;jdbc/url&quot; global=&quot;jdbc/url&quot; type=&quot;java.lang.String&quot;/&gt;&lt;br /&gt;
    &amp;lt;ResourceLink name=&quot;jdbc/drivername&quot; global=&quot;jdbc/drivername&quot; type=&quot;java.lang.String&quot;/&gt;&lt;br /&gt;
&amp;lt;/Context&gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
Thats is all we need to do to make these values available to the spring beans. So the last part is the spring configuration. We are going to create a PropertyPlaceHolderConfigurer. That way you can access the properties just like properties from a file.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;spring-config.xml&lt;/strong&gt;&lt;br /&gt;
&lt;pre  style=&quot;width:600px&quot; &gt;&lt;br /&gt;
    &amp;lt;bean id=&quot;databaseprops&quot; class=&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot;&gt;&lt;br /&gt;
        &amp;lt;property name=&quot;properties&quot;&gt;&lt;br /&gt;
            &amp;lt;bean class=&quot;java.util.Properties&quot;&gt;&lt;br /&gt;
                &amp;lt;constructor-arg&gt;&lt;br /&gt;
                    &amp;lt;map&gt;&lt;br /&gt;
                        &amp;lt;entry key=&quot;database.username&quot;&gt;&lt;br /&gt;
                            &amp;lt;jee:jndi-lookup jndi-name=&quot;java:comp/env/jdbc/username&quot;/&gt;&lt;br /&gt;
                        &amp;lt;/entry&gt;&lt;br /&gt;
                        &amp;lt;entry key=&quot;database.password&quot;&gt;&lt;br /&gt;
                            &amp;lt;jee:jndi-lookup jndi-name=&quot;java:comp/env/jdbc/password&quot;/&gt;&lt;br /&gt;
                        &amp;lt;/entry&gt;&lt;br /&gt;
                        &amp;lt;entry key=&quot;database.url&quot;&gt;&lt;br /&gt;
                            &amp;lt;jee:jndi-lookup jndi-name=&quot;java:comp/env/jdbc/url&quot;/&gt;&lt;br /&gt;
                        &amp;lt;/entry&gt;&lt;br /&gt;
                        &amp;lt;entry key=&quot;database.drivername&quot;&gt;&lt;br /&gt;
                            &amp;lt;jee:jndi-lookup jndi-name=&quot;java:comp/env/jdbc/drivername&quot;/&gt;&lt;br /&gt;
                        &amp;lt;/entry&gt;&lt;br /&gt;
                    &amp;lt;/map&gt;&lt;br /&gt;
                &amp;lt;/constructor-arg&gt;&lt;br /&gt;
            &amp;lt;/bean&gt;&lt;br /&gt;
        &amp;lt;/property&gt;&lt;br /&gt;
    &amp;lt;/bean&gt;&lt;br /&gt;
    &amp;lt;bean id=&quot;sampleConfigBean&quot; class=&quot;nl.gridshore.samples.config.SampleConfiguredBean&quot;&gt;&lt;br /&gt;
        &amp;lt;property name=&quot;username&quot; value=&quot;${database.username}&quot;/&gt;&lt;br /&gt;
        &amp;lt;property name=&quot;password&quot; value=&quot;${database.password}&quot;/&gt;&lt;br /&gt;
        &amp;lt;property name=&quot;url&quot; value=&quot;${database.url}&quot;/&gt;&lt;br /&gt;
        &amp;lt;property name=&quot;drivername&quot; value=&quot;${database.drivername}&quot;/&gt;&lt;br /&gt;
    &amp;lt;/bean&gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
This made my deployment life a lot easier, hope you will find some good use of it. Thanks again to contributers on the forum for making the different parts clear to me.&lt;br /&gt;
&lt;br /&gt;
Till next time, which is going to be a piece about JPA.&lt;br /&gt;
&lt;br /&gt;
greetz Jettro Coenradie 
    </content:encoded>

    <pubDate>Fri, 16 Nov 2007 23:32:54 +0100</pubDate>
    <guid isPermaLink="false">http://www.gridshore.nl/blog/index.php?/archives/71-guid.html</guid>
    
</item>
<item>
    <title>Using Spring-ws for creating a webservice</title>
    <link>http://www.gridshore.nl/blog/index.php?/archives/66-Using-Spring-ws-for-creating-a-webservice.html</link>
            <category>Springframework</category>
            <category>Webservices</category>
    
    <comments>http://www.gridshore.nl/blog/index.php?/archives/66-Using-Spring-ws-for-creating-a-webservice.html#comments</comments>
    <wfw:comment>http://www.gridshore.nl/blog/wfwcomment.php?cid=66</wfw:comment>

    <slash:comments>2</slash:comments>
    <wfw:commentRss>http://www.gridshore.nl/blog/rss.php?version=2.0&amp;type=comments&amp;cid=66</wfw:commentRss>
    

    <author>nospam@example.com (Jettro Coenradie)</author>
    <content:encoded>
    My previous post was about creating a client with the spring-ws project. This dis take me some time to grasp. But without a published webservice a client is of no much use. Therefore I also had a look at the server side. To my surprise this was even easier than expected. I did have some requirements for the sample:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;wsdl generation&lt;/li&gt;&lt;li&gt;marshalling/unmarshalling using jaxb2&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;
That does not sound to bad does it? Well try to do it with axis and you&#039;ll be surprised. If you know a little bid about spring-webmvc and the use of the dispatcher, you know you need one configuration file for spring. For the webservice I had to provide the spring config file and one class, yes really, just one class. I used maven to generate the jaxb stuff and this is wat I did:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
&lt;strong&gt;web.xml&lt;/strong&gt;&lt;br /&gt;
&amp;lt;servlet&gt;&lt;br /&gt;
  &amp;lt;servlet-name&gt;spring-ws&amp;#160;/servlet-name&gt;&lt;br /&gt;
  &amp;lt;servlet-class&gt;org.springframework.ws.transport.http.MessageDispatcherServlet&amp;lt;/servlet-class&gt;&lt;br /&gt;
&amp;lt;/servlet&gt;&lt;br /&gt;
&amp;lt;servlet-mapping&gt;&lt;br /&gt;
  &amp;lt;servlet-name&gt;spring-ws&amp;#160;/servlet-name&gt;&lt;br /&gt;
  &amp;lt;url-pattern&gt;/*&amp;#160;/url-pattern&gt;&lt;br /&gt;
&amp;lt;/servlet-mapping&gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
The spring-ws framework provides a special servlet that receives the requests. No the really hard part, the spring configuration for the endpoint:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
&amp;lt;bean class=&quot;org.springframework.ws.server.endpoint.mapping.SimpleMethodEndpointMapping&quot;&gt;&lt;br /&gt;
  &amp;lt;property name=&quot;endpoints&quot; ref=&quot;congressRegistrationEndpoint&quot;/&gt;&lt;br /&gt;
&amp;lt;/bean&gt;&lt;br /&gt;
&amp;lt;bean id=&quot;congressRegistrationEndpoint&quot;  class=&quot;nl.gridshore.samples.springws.server.JaxbMarshallingMethodEndpoint&quot;&gt;&lt;br /&gt;
  &amp;lt;property name=&quot;marshaller&quot; ref=&quot;marshaller&quot;/&gt;&lt;br /&gt;
  &amp;lt;property name=&quot;unmarshaller&quot; ref=&quot;unmarshaller&quot;/&gt;&lt;br /&gt;
&amp;lt;/bean&gt;&lt;br /&gt;
&amp;lt;bean id=&quot;marshaller&quot; class=&quot;org.springframework.oxm.jaxb.Jaxb2Marshaller&quot;&gt;&lt;br /&gt;
  &amp;lt;property name=&quot;contextPath&quot; value=&quot;nl.gridshore.samples.jaxb&quot;/&gt;&lt;br /&gt;
&amp;lt;/bean&gt;&lt;br /&gt;
&amp;lt;bean id=&quot;unmarshaller&quot; class=&quot;org.springframework.oxm.jaxb.Jaxb2Marshaller&quot;&gt;&lt;br /&gt;
  &amp;lt;property name=&quot;contextPath&quot; value=&quot;nl.gridshore.samples.jaxb&quot;/&gt;&lt;br /&gt;
&amp;lt;/bean&gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
First check the endpoint mapping, this class &lt;em&gt;SimpleMethodEndpointMapping&lt;/em&gt; is found by the dispatcher to map all incoming requests to endpoints. In our case we map to our only endpoint called the congressRegistrationEndpoint. This class has a special super class that needs a marshaller and unmarshaller. Other than that it needs nothing. By convention the name of the Request is used by the super class to know which method to call. In our case we have a CongressRegistrationRequest. Therefore we need to implement to method handleCongressRegistrationRequest as you can see in the following coded block.&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
public CongressRegistrationResponse handleCongressRegistrationRequest(CongressRegistrationRequest request) {&lt;br /&gt;
  ObjectFactory objectFactory = new ObjectFactory();&lt;br /&gt;
  CongressRegistrationResponse response = objectFactory.createCongressRegistrationResponse();&lt;br /&gt;
  response.setRegistrationCode(&quot;code333&quot;);&lt;br /&gt;
  return response;&lt;br /&gt;
}&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
In this code the argument and the return value are JAXB objects. I created the most easy method, do nothing with the request and always return the same response. Bu as you can see the code itself is very clean. I do not need to do a lot of conding. I think this is probably one of the easiest implementations I have seen that works in java 1.4 as well.&lt;br /&gt;
&lt;br /&gt;
That is it for one of the requirements. Now I also wanted a solution for generating the wsdl. In the end this is only some springframework configuration. You also need an xsd, but you have already used this to generate the jaxb classes with maven. So we can reuse it. Check the following configuration&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
&amp;lt;bean id=&quot;service&quot; class=&quot;org.springframework.ws.wsdl.wsdl11.DynamicWsdl11Definition&quot;&gt;&lt;br /&gt;
  &amp;lt;property name=&quot;builder&quot;&gt;&lt;br /&gt;
    &amp;lt;bean class=&quot;org.springframework.ws.wsdl.wsdl11.builder.XsdBasedSoap11Wsdl4jDefinitionBuilder&quot;&gt;&lt;br /&gt;
      &amp;lt;property name=&quot;schema&quot; value=&quot;/WEB-INF/classes/congressregistration.xsd&quot;/&gt;&lt;br /&gt;
      &amp;lt;property name=&quot;portTypeName&quot; value=&quot;services&quot;/&gt;&lt;br /&gt;
      &amp;lt;property name=&quot;locationUri&quot; value=&quot;http://localhost:8080/spring-ws-sample-server/&quot;/&gt;&lt;br /&gt;
      &amp;lt;property name=&quot;targetNamespace&quot; value=&quot;http://www.gridshore.nl/samples/definitions&quot;/&gt;&lt;br /&gt;
    &amp;lt;/bean&gt;&lt;br /&gt;
  &amp;lt;/property&gt;&lt;br /&gt;
&amp;lt;/bean&gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
So you need the location of the xsd, the name of the bean is used to find the wsdl. The locationUri locates the endpoint and the targetNamespace is the namespace used in the xsd. By entering the following url you can find the wsdl.&lt;br /&gt;
http://localhost:8080/spring-ws-sample-server/service.wsdl&lt;br /&gt;
&lt;br /&gt;
You can check the code at the &lt;a href=&quot;http://gridshore.googlecode.com/svn/trunk/spring-ws-sample/spring-ws-sample-server/&quot;&gt;google code project&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
I know it is pretty short, but with the code you should be able to understand it.&lt;br /&gt;
&lt;br /&gt;
greetz Jettro 
    </content:encoded>

    <pubDate>Wed, 04 Jul 2007 20:33:52 +0200</pubDate>
    <guid isPermaLink="false">http://www.gridshore.nl/blog/index.php?/archives/66-guid.html</guid>
    
</item>
<item>
    <title>Creating a webservice client using Spring-ws and maven2</title>
    <link>http://www.gridshore.nl/blog/index.php?/archives/65-Creating-a-webservice-client-using-Spring-ws-and-maven2.html</link>
            <category>Springframework</category>
            <category>Webservices</category>
    
    <comments>http://www.gridshore.nl/blog/index.php?/archives/65-Creating-a-webservice-client-using-Spring-ws-and-maven2.html#comments</comments>
    <wfw:comment>http://www.gridshore.nl/blog/wfwcomment.php?cid=65</wfw:comment>

    <slash:comments>14</slash:comments>
    <wfw:commentRss>http://www.gridshore.nl/blog/rss.php?version=2.0&amp;type=comments&amp;cid=65</wfw:commentRss>
    

    <author>nospam@example.com (Jettro Coenradie)</author>
    <content:encoded>
    Last week I attended the &lt;a href=&quot;http://www.springone.com&quot;&gt;SpringOne Congress&lt;/a&gt; in Antwerp. Just one or two days before the congress I wanted to create a proof of concept for a client connecting to a webservice. I have been using the proxy way (spring-remote), but I did not really like the way this works. Therefore I was looking for something else. I stumbled upon the  &lt;a href=&quot;http://static.springframework.org/spring-ws/site/&quot;&gt;Spring Webservices&lt;/a&gt; project. I did try this out before, but not for clients. I started working and it did not look very complicated. I must admit I needed the help from Arjen Poutsma himself to make it work, but now I have a very light weight solution. I like the approach very much, therefore I will briefly describe the solution I have created. You can of course find the sources online as well. Check the &lt;a href=&quot;http://gridshore.googlecode.com/svn/trunk/spring-ws-sample/&quot;&gt;google code subversion repo for gridshore&lt;/a&gt;.&lt;br /&gt;
&lt;h2&gt;Overview of application&lt;/h2&gt;&lt;ul&gt;&lt;li&gt;Build application using maven2&lt;/li&gt;&lt;br /&gt;
&lt;li&gt;Web application build using spring webmvc&lt;/li&gt;&lt;br /&gt;
&lt;li&gt;Webservice client created using spring-ws&lt;/li&gt;&lt;br /&gt;
&lt;li&gt;Mapping between objects and xml using jaxb2 and spring-oxm&lt;/li&gt;&lt;br /&gt;
&lt;/ul&gt;&lt;br /&gt;
&lt;h2&gt;Maven 2 for building&lt;/h2&gt;This time not much details about the maven2 part. There are some things interesting though. Most important part is the creation of the jaxb generated classes using the &lt;a href=&quot;https://maven-jaxb2-plugin.dev.java.net/&quot;&gt;maven2 jaxb2 plugin&lt;/a&gt;.&lt;br /&gt;
&lt;h2&gt;Using spring-webmv for web project&lt;/h2&gt;I do not think it is really interesting to talk a long time about this. There can be found a lot (better) resources online. I used the spring webmvc project to make the client i18n compatible, used the form controllers from spring to help with the error messages and validation stuff. For the webservices part the most interesting thing starts in the file &lt;strong&gt;service-applicationcontext.xml&lt;/strong&gt;. This spring configurtion file contains the webservice client, the converters for jaxb (using generics to abstract the technique marshalling technique).&lt;br /&gt;
&lt;h2&gt;Webservice client created using spring-ws&lt;/h2&gt;Let&#039;s start by having a look at the configuration:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
&amp;lt;bean id=&quot;congressRegistrationDAO&quot;&lt;br /&gt;
    class=&quot;nl.gridshore.samples.springws.integration.wsclient.impl.CongressRegistrationGateway&quot;&gt;&lt;br /&gt;
  &amp;lt;property name=&quot;defaultUri&quot; value=&quot;${cr.endpoint}&quot; /&gt;&lt;br /&gt;
  &amp;lt;property name=&quot;marshaller&quot; ref=&quot;marshaller&quot;/&gt;&lt;br /&gt;
  &amp;lt;property name=&quot;unmarshaller&quot; ref=&quot;unmarshaller&quot;/&gt;&lt;br /&gt;
  &amp;lt;constructor-arg index=&quot;0&quot; ref=&quot;congressRegistrationRequestConverter&quot;/&gt;&lt;br /&gt;
  &amp;lt;constructor-arg index=&quot;1&quot; ref=&quot;congressRegistrationResponseConverter&quot;/&gt;&lt;br /&gt;
&amp;lt;/bean&gt;&lt;br /&gt;
&amp;lt;bean id=&quot;marshaller&quot; class=&quot;org.springframework.oxm.jaxb.Jaxb2Marshaller&quot;&gt;&lt;br /&gt;
  &amp;lt;property name=&quot;contextPath&quot; value=&quot;nl.gridshore.samples.jaxb&quot;/&gt;&lt;br /&gt;
&amp;lt;/bean&gt;&lt;br /&gt;
&amp;lt;bean id=&quot;unmarshaller&quot; class=&quot;org.springframework.oxm.jaxb.Jaxb2Marshaller&quot;&gt;&lt;br /&gt;
  &amp;lt;property name=&quot;contextPath&quot; value=&quot;nl.gridshore.samples.jaxb&quot;/&gt;&lt;br /&gt;
&amp;lt;/bean&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/pre&gt;This configured class is the class that knows how to connect to the webservice. It uses a few values and classes to do it&#039;s work. The defaultUri is used to find the endpoint to connect to (we obtain it from a property file). Since we use JAXB2 to marshall and unmarshall the request we need the marshaller and unmarshaller. These (un)marshallers are provided by spring-ws as well. Actually they are provided by spring-oxm. The interface for the marshaller is the same as for the unmarshaller. The only thing you need to provide is the package where the jaxb files are generatd in. The other two references are converters used to convert the application specific components to JAXB2 components. More on this later as well. So the most important class is the Gateway class. Let&#039;s have a look at that now.&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
package nl.gridshore.samples.springws.integration.wsclient.impl;&lt;br /&gt;
&lt;br /&gt;
import nl.gridshore.samples.springws.domain.RegistrationDetails;&lt;br /&gt;
import nl.gridshore.samples.springws.integration.converter.Converter;&lt;br /&gt;
import nl.gridshore.samples.springws.integration.wsclient.CongressRegistrationDAO;&lt;br /&gt;
&lt;br /&gt;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;&lt;br /&gt;
import org.springframework.ws.soap.client.core.SoapActionCallback;&lt;br /&gt;
&lt;br /&gt;
public class CongressRegistrationGateway extends WebServiceGatewaySupport implements CongressRegistrationDAO {&lt;br /&gt;
  private final Converter&lt;Object,RegistrationDetails&gt; requestConverter;&lt;br /&gt;
  private final Converter&lt;String,Object&gt; responseConverter;&lt;br /&gt;
	&lt;br /&gt;
  public CongressRegistrationGateway(Converter&lt;Object,RegistrationDetails&gt; requestConverter,  &lt;br /&gt;
      Converter&lt;String,Object&gt; responseConverter) {&lt;br /&gt;
    this.requestConverter = requestConverter;&lt;br /&gt;
    this.responseConverter = responseConverter;&lt;br /&gt;
  }&lt;br /&gt;
	&lt;br /&gt;
  public String registerForCongress(final RegistrationDetails registrationDetails) {&lt;br /&gt;
    Object request = requestConverter.convert(registrationDetails);&lt;br /&gt;
    &lt;strong&gt;Object response = getWebServiceTemplate().marshalSendAndReceive(request);&lt;/strong&gt;&lt;br /&gt;
    return responseConverter.convert(response);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
This is it, really. I show you the complete class, but in the end the most important works takes place in one line. That&#039;s the one in bold. Here we use the special webServiceTemplate class and ask it to marshall our request object and send it as a soap message to the configured endpoint. Ofcourse you can do a lot more, but for most situations this is all it takes. Do not forget to extend the &lt;strong&gt;WebServiceGatewaySupport&lt;/strong&gt; class. &lt;br /&gt;
&lt;h2&gt;Mapping between objects and xml using jaxb2 and spring-oxm&lt;/h2&gt;Not much to tell here as well. I keep saying this, but that&#039;s because it is true. We have configured the marshaller and the unmarshaller which are provided by spring-oxm. We also call the right method from the template adapter called marshalSendAndReceive. I did create an interface and some implementations that enable the gateway to be ignorand to the type of marshalling and unmarshalling we use. This is my first try to do something with generics. There can be a better way, if you know one, please let me know as well &lt;img src=&quot;http://www.gridshore.nl/blog/templates/gridshore/img/emoticons/smile.png&quot; alt=&quot;:-)&quot; style=&quot;display: inline; vertical-align: bottom;&quot; class=&quot;emoticon&quot; /&gt;. Let&#039;s start with the generic interface.&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
public interface Converter&lt;E,T&gt; {&lt;br /&gt;
  public E convert(T toBeConverted);&lt;br /&gt;
}&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
So we have an interface specifying we have a object as a parameter and an object as a return type. Then there is a method called convert that uses these generic types. At the moment I have two concrete implementations for creating the JAXB request and handling the JAXB Response. One of them is presented below.&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
public class CongressRegistrationResponseConverter implements Converter&lt;String, CongressRegistrationResponse&gt; {&lt;br /&gt;
  public String convert(CongressRegistrationResponse toBeConverted) {&lt;br /&gt;
    String response = toBeConverted.getRegistrationCode();&lt;br /&gt;
      return response;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
As you can see we now have a concrete implementation for the generic interface. The returned object is a String and the parameter is a JAXB object called CongressRegistrationResponse. Now look back at the gateway code, check that there is no jaxb reference in that code.&lt;br /&gt;
&lt;br /&gt;
Again check out the code at &lt;a href=&quot;http://gridshore.googlecode.com/svn/trunk/spring-ws-sample/&quot;&gt;google code subversion repo for gridshore&lt;/a&gt;. You can already find the code for a server implementation there as well. The next blog will deal with the server. I am also working on a sample about the Rule engine called Drools. so stay tuned. 
    </content:encoded>

    <pubDate>Sat, 30 Jun 2007 21:25:00 +0200</pubDate>
    <guid isPermaLink="false">http://www.gridshore.nl/blog/index.php?/archives/65-guid.html</guid>
    
</item>
<item>
    <title>Spring osgi - an evaluation using maven 2 and the special spring osgi archetype</title>
    <link>http://www.gridshore.nl/blog/index.php?/archives/59-Spring-osgi-an-evaluation-using-maven-2-and-the-special-spring-osgi-archetype.html</link>
            <category>spring-osgi</category>
            <category>Springframework</category>
    
    <comments>http://www.gridshore.nl/blog/index.php?/archives/59-Spring-osgi-an-evaluation-using-maven-2-and-the-special-spring-osgi-archetype.html#comments</comments>
    <wfw:comment>http://www.gridshore.nl/blog/wfwcomment.php?cid=59</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://www.gridshore.nl/blog/rss.php?version=2.0&amp;type=comments&amp;cid=59</wfw:commentRss>
    

    <author>nospam@example.com (Jettro Coenradie)</author>
    <content:encoded>
    A few months a go I was reading the website of the springframework. One article subject supprised me. I had never heard of it, OSGi. Curious enough to want to know what it was about I opened the article and started reading. The more I read the more I was certain, this is going to be the next big thing. Then in December I attended &lt;a href=&quot;http://www.thespringexperience.com&quot; &gt;The SpringExperience in Miami&lt;/a&gt; and of course there was one presentation I had to attend. It turned out I was not the only one interested in spring-OSGi. Adrian Colyer gave a very interesting presentation and almost the complete interface21 team was there. They all think OSGi is going to be the next big thing. If you do a google on spring osgi you will get a lot of hits. Of course none of these is as technical as mine &lt;img src=&quot;http://www.gridshore.nl/blog/templates/gridshore/img/emoticons/smile.png&quot; alt=&quot;:-)&quot; style=&quot;display: inline; vertical-align: bottom;&quot; class=&quot;emoticon&quot; /&gt;&lt;br /&gt;
&lt;br /&gt;
If you want to read more about the event look at &lt;a href=&quot;http://accenturetechnologysolutions.blogspot.com/index.html&quot; &gt;this blog&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Now a few weeks later I had finally found some time to experiment, and of course I want to share what I have learned. This blog item is a step by step approach of creating your own spring-osgi bundle. Later I will add some more complicated bundles with references, listeners and maybe a webserver.&lt;br /&gt;
&lt;br /&gt;
So, how do we start? Well first download all code and install it using maven. Hmm, maybe I should tell you first that you need to install maven 2 and subversion. So, please install subversion and maven2. Then checkout all sources:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
svn checkout https://svn.sourceforge.net/svnroot/springframework/spring-osgi/trunk spring-osgi&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
Than step into the root folder and do a mvn install. You can ofcourse have a look at the samples that come with spring-osgi. Please go to the &lt;a href=&quot;http://www.springframework.org/osgi&quot; &gt;spring-osgi website&lt;/a&gt; and see the tutorials there. We arfe going to create a new bundle using the maven archetype.&lt;br /&gt;
&lt;br /&gt;
If you want to have a look at the complete source code, do a checkout from the following google code website or &lt;a href=&quot;http://gridshore.googlecode.com/svn/trunk/SpringOsgiSample/&quot; &gt;browse it online&lt;/a&gt;&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
svn checkout http://gridshore.googlecode.com/svn/trunk/SpringOsgiSample gridshore&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;strong&gt;Using the archetype&lt;/strong&gt;&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
mvn archetype:create \&lt;br /&gt;
  -DarchetypeGroupId=org.springframework.osgi \&lt;br /&gt;
  -DarchetypeArtifactId=spring-osgi-bundle-archetype \&lt;br /&gt;
  -DarchetypeVersion=1.0-SNAPSHOT \&lt;br /&gt;
  -DgroupId=nl.gridshore.samples.springosgi \&lt;br /&gt;
  -DartifactId=bookreview-service \&lt;br /&gt;
  -Dversion=1.0-SNAPSHOT \&lt;br /&gt;
  -DremoteRepositories=http://static.springframework.org/maven2-snapshots&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
Have a look at the generated pom.xml file and check mine. There are some things I altered that are important.&lt;br /&gt;
- I added the scope - test to the dependencies that did not have it&lt;br /&gt;
- I added the following dependencies : jcl104-over-slf4j.osgi, slf4j-log4j-full and log4j.osgi&lt;br /&gt;
- removed the dependecy for commons-logging&lt;br /&gt;
Check the pom.xml file of the provided samples by spring-osgi, that is where I got it from.&lt;br /&gt;
&lt;br /&gt;
The archetype also provides a .project and .classpath file for eclipse. I had problems when I did a mvn eclipse:clean eclipse:eclipse. Some lines are not added that were in place when the files were provided by the archetype.&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
    &amp;lt;buildCommand&gt;&lt;br /&gt;
      &amp;lt;name&gt;org.eclipse.pde.ManifestBuilder&amp;lt;/name&gt;&lt;br /&gt;
    &amp;lt;/buildCommand&gt;&lt;br /&gt;
    &amp;lt;buildCommand&gt;&lt;br /&gt;
      &amp;lt;name&gt;org.eclipse.pde.SchemaBuilder&amp;lt;/name&gt;&lt;br /&gt;
    &amp;lt;/buildCommand&gt;&lt;br /&gt;
    &amp;lt;nature&gt;org.eclipse.pde.PluginNature&amp;lt;/nature&gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
Not sure whether these are absolutely necessary. I do know a bundle within eclipse is a plugin, therefore you need the plugin nature.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Start coding&lt;/strong&gt;&lt;br /&gt;
The framework of the project is in place and should compile (mvn install). Now it is time to start creating the bundle. We&#039;ll start by creating the BookReviewService, the unit test and the implementation of this service. This is pure java code, check the sources if you like. Now add a bean that we can expose as a service to the spring context file. There are two context files out of the box. You can find them in the src/main/resources/META-INF/spring folder. &lt;br /&gt;
bundle-context.xml : contains the normal spring configuration&lt;br /&gt;
bundle-context-osgi.xml : contains all beans that use the osgi namespace&lt;br /&gt;
&lt;br /&gt;
We have created the spring bean myBookReviewService:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
&amp;lt;bean name=&quot;myBookReviewService&quot; &lt;br /&gt;
    class=&quot;nl.gridshore.samples.springosgi.impl.BookReviewServiceImpl&quot; /&gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
Then we expose this bean as a special osgi service&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
&amp;lt;osgi:service id=&quot;bookReviewServiceOSGi&quot; ref=&quot;myBookReviewService&quot;&lt;br /&gt;
    interface=&quot;nl.gridshore.samples.springosgi.BookReviewService&quot; /&gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
And now we are done, no not really. First we need to create an integration test. We use the special super class &lt;u&gt;AbstractDependencyInjectionSpringContextTests&lt;/u&gt;. This test uses the spring config file to configure and start an application context. From the context we obtain our bean and test it to see if it works.&lt;br /&gt;
&lt;strong&gt;Alter the manifest file&lt;/strong&gt;&lt;br /&gt;
Next step is to alter the manifest file, you can find this file in the  META-INF folder at the same level of the src folder. You need to add an export package and the Bundle-classpath&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
Bundle-Version: 1.0&lt;br /&gt;
Bundle-SymbolicName: nl.gridshore.samples.springosgi.bookreviewservice&lt;br /&gt;
Bundle-Name: nl.gridshore.samples.springosgi.bookreview-service&lt;br /&gt;
&lt;strong&gt;Export-Package: nl.gridshore.samples.springosgi&lt;br /&gt;
Bundle-ClassPath: .,&lt;br /&gt;
 target/classes/&lt;br /&gt;
&lt;/strong&gt;&lt;/pre&gt;&lt;br /&gt;
Now we are ready to package our bundle. By using one of the tutorials from the spring-osgi website you can use eclipse/equinox to install and start this bundle. There is however not much to see. Wouldn&#039;t it be nice to be able to test within an exsiting osgi container whether your hard labor was wurth it? You can, the next steps will describe how you can create an in container test.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Creating the in container test&lt;/strong&gt;&lt;br /&gt;
Again the framework provides a number of super classes that makes life a lot easier. I am not going to explain in detail what these classes are doing. Still I think one thing is important to know. Your test projects is being transformed into a bundle at runtime with a reference to the bundle under test. But lets start with the creation of the normal jar file that will contain our test.&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
mvn archetype:create \&lt;br /&gt;
  -DgroupId=nl.gridshore.samples.springosgi \&lt;br /&gt;
  -DartifactId=bookreview-service-integration-test \&lt;br /&gt;
  -Dversion=1.0-SNAPSHOT \&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
Alter the pom, it should use the parent and have a lot of dependencies. There are two I would like to mention is special:&lt;br /&gt;
bookreview-service : this is our own project that we are gonna test&lt;br /&gt;
org.springframework.osgi.test : utilit classes for our own test class&lt;br /&gt;
&lt;br /&gt;
The manifest file for the bundle that is created on the fly comes from the test/resources directory and looks like this.&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
Manifest-Version: 1.0&lt;br /&gt;
Bundle-Name: simple-service-integration-tests&lt;br /&gt;
Bundle-SymbolicName: nl.gridshore.samples.springosgi.test&lt;br /&gt;
Bundle-Activator: org.springframework.osgi.test.JUnitTestActivator&lt;br /&gt;
Import-Package: junit.framework,&lt;br /&gt;
 org.osgi.framework;specification-version=&quot;1.3.0&quot;,&lt;br /&gt;
 org.springframework.core.io;specification-version=&quot;2.1.0&quot;,&lt;br /&gt;
 org.springframework.osgi.test,&lt;br /&gt;
 org.springframework.osgi.test.runner,&lt;br /&gt;
 nl.gridshore.samples.springosgi&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
Now we can create the test, use the &lt;u&gt;ConfigurableBundleCreatorTests&lt;/u&gt; as a super class and implement the method to locate your MANIFEST.MF file. You also must implement a method that defines all the dependencies that can be obtained from your local maven repository. Finally you need to add your test methods.&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
public void testOSGiStartedOk() {&lt;br /&gt;
   BundleContext bundleContext = getBundleContext();&lt;br /&gt;
   assertNotNull(bundleContext);&lt;br /&gt;
}&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
This test checks if the context for the created bundle can be obtained.&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
public void testSimpleServiceExported() {&lt;br /&gt;
  waitOnContextCreation(&quot;nl.gridshore.samples.springosgi.bookreviewservice&quot;);&lt;br /&gt;
  BundleContext context = getBundleContext();&lt;br /&gt;
  ServiceReference ref = context.getServiceReference(BookReviewService.class.getName());&lt;br /&gt;
  assertNotNull(&quot;Service Reference is null&quot;, ref);&lt;br /&gt;
  try {&lt;br /&gt;
    BookReviewService bookReviewService = (BookReviewService) context.getService(ref);&lt;br /&gt;
    assertNotNull(&quot;Cannot find the service&quot;, bookReviewService);&lt;br /&gt;
    List bookreviews = bookReviewService.findBookReviewByKeyword(&quot;habits&quot;); &lt;br /&gt;
    assertEquals(1, bookreviews.size());&lt;br /&gt;
  } finally {&lt;br /&gt;
    context.ungetService(ref);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
This test first checks if the osgi service can be obtained from the bundle context. Then the obtained service is used and the result is checked. In my case we are looking for a book and if all is well we should be able to find exactly one.&lt;br /&gt;
&lt;br /&gt;
Now do a mvn install on the highest level and you should have a tested spring osgi module&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br /&gt;
That is about it, I hope this will help some of you, do not hesitate to ask questions if something is not clear. You can find a lot of information on the spring osgi website, there is a google group and google lists about thousands of hits.  
    </content:encoded>

    <pubDate>Wed, 03 Jan 2007 21:20:43 +0100</pubDate>
    <guid isPermaLink="false">http://www.gridshore.nl/blog/index.php?/archives/59-guid.html</guid>
    
</item>
<item>
    <title>Creating a maven archetype for acegi and springframework</title>
    <link>http://www.gridshore.nl/blog/index.php?/archives/55-Creating-a-maven-archetype-for-acegi-and-springframework.html</link>
            <category>acegi</category>
            <category>Springframework</category>
    
    <comments>http://www.gridshore.nl/blog/index.php?/archives/55-Creating-a-maven-archetype-for-acegi-and-springframework.html#comments</comments>
    <wfw:comment>http://www.gridshore.nl/blog/wfwcomment.php?cid=55</wfw:comment>

    <slash:comments>4</slash:comments>
    <wfw:commentRss>http://www.gridshore.nl/blog/rss.php?version=2.0&amp;type=comments&amp;cid=55</wfw:commentRss>
    

    <author>nospam@example.com (Jettro Coenradie)</author>
    <content:encoded>
    I have created a lot of small applications that include spring and acegi. Most of the time I copy most of the resources from another project. Some time ago I learned about the maven archetypes. An easy way to create the project structure. That was the time I decided that I wanted to have my acegi springframework archetype. Last weeks I found some time to create this archetype. You can download the files here:&lt;br /&gt;
&lt;a href=&quot;http://code.google.com/p/gridshore/&quot;  title=&quot;http://code.google.com/p/gridshore/&quot;&gt;http://code.google.com/p/gridshore/&lt;/a&gt;&lt;br /&gt;
And the generated maven site here:&lt;br /&gt;
&lt;a href=&quot;http://www.gridshore.nl/projects/springframeworkarchetype/index.html&quot;  title=&quot;http://www.gridshore.nl/projects/springframeworkarchetype/index.html&quot;&gt;http://www.gridshore.nl/projects/springframeworkarchetype/index.html&lt;/a&gt;&lt;br /&gt;
Ofcourse there is also a maven website about creating an archetype, this is not a very extensive website. In this blog item I will explain more about the archetype I created.&lt;br /&gt;
&lt;br /&gt;
First thing to know, well surprise, the archetype is just a maven project. You can start you new archetype by using a &lt;a href=&quot;http://maven.apache.org/plugins/maven-archetype-plugin/create-mojo.html&quot;  title=&quot;http://maven.apache.org/plugins/maven-archetype-plugin/create-mojo.html&quot;&gt;maven archetype&lt;/a&gt;:&lt;br /&gt;
mvn archetype:create &lt;br /&gt;
	-DgroupId=nl.gridshore.samples.archetype &lt;br /&gt;
	-DartifactId=SpringframeworkArchetype &lt;br /&gt;
	-DarchetypeArtifactId=maven-archetype-quickstart&lt;br /&gt;
Have a look at the pom for the other properties; scm, distributionManagement, etc.&lt;br /&gt;
&lt;br /&gt;
The project itself contains an src\main\resources\META-INF\maven\archetype.xml file that defines all the resources that need to be copied.&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
&amp;lt;archetype&gt;&lt;br /&gt;
  &amp;lt;id&gt;SpringframeworkArchetype&amp;lt;/id&gt;&lt;br /&gt;
  &amp;lt;sources&gt;&lt;br /&gt;
    &amp;lt;source&gt;src/main/java/App.java&amp;lt;/source&gt;&lt;br /&gt;
  &amp;lt;/sources&gt;&lt;br /&gt;
  &amp;lt;testSources&gt;&lt;br /&gt;
    &amp;lt;source&gt;src/test/java/AppTest.java&amp;lt;/source&gt;&lt;br /&gt;
  &amp;lt;/testSources&gt;&lt;br /&gt;
  &amp;lt;resources&gt;&lt;br /&gt;
      &amp;lt;resource&gt;src/main/webapp/index.jsp&amp;lt;/resource&gt;&lt;br /&gt;
      &amp;lt;resource&gt;src/main/webapp/WEB-INF/web.xml&amp;lt;/resource&gt;&lt;br /&gt;
      &amp;lt;resource&gt;src/main/webapp/WEB-INF/action-servlet.xml&amp;lt;/resource&gt;&lt;br /&gt;
      &amp;lt;resource&gt;src/main/webapp/WEB-INF/jsps/welcome.jsp&amp;lt;/resource&gt;&lt;br /&gt;
      &amp;lt;resource&gt;src/main/webapp/WEB-INF/jsps/adminwelcome.jsp&amp;lt;/resource&gt;&lt;br /&gt;
      &amp;lt;resource&gt;src/main/webapp/WEB-INF/jsps/accessdenied.jsp&amp;lt;/resource&gt;&lt;br /&gt;
      &amp;lt;resource&gt;src/main/webapp/WEB-INF/jsps/login.jsp&amp;lt;/resource&gt;&lt;br /&gt;
      &amp;lt;resource&gt;src/main/webapp/WEB-INF/jsps/loginerror.jsp&amp;lt;/resource&gt;&lt;br /&gt;
      &amp;lt;resource&gt;src/main/resources/security-springConfig.xml&amp;lt;/resource&gt;&lt;br /&gt;
      &amp;lt;resource&gt;src/main/resources/views.properties&amp;lt;/resource&gt;&lt;br /&gt;
      &amp;lt;resource&gt;src/main/resources/errormessages.properties&amp;lt;/resource&gt;&lt;br /&gt;
      &amp;lt;resource&gt;src/main/resources/sitetext.properties&amp;lt;/resource&gt;&lt;br /&gt;
      &amp;lt;resource&gt;src/main/resources/log4j.dtd&amp;lt;/resource&gt;&lt;br /&gt;
      &amp;lt;resource&gt;src/main/resources/log4j.xml&amp;lt;/resource&gt;&lt;br /&gt;
  &amp;lt;/resources&gt;&lt;br /&gt;
&amp;lt;/archetype&gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
The structure is pretty easy, you can find all the resources under the directory:&lt;br /&gt;
src\main\resources\archetype-resources&lt;br /&gt;
&lt;br /&gt;
You can use the following parameters in your file as placeholders:&lt;br /&gt;
&lt;a href=&quot;http://maven.apache.org/plugins/maven-archetype-plugin/create-mojo.html&quot;  title=&quot;http://maven.apache.org/plugins/maven-archetype-plugin/create-mojo.html&quot;&gt;http://maven.apache.org/plugins/maven-archetype-plugin/create-mojo.html&lt;/a&gt;. Have a look at the src\main\resources\archetype-resources\pom.xml for an example:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
  &amp;lt;groupId&gt;$groupId&amp;lt;/groupId&gt;&lt;br /&gt;
  &amp;lt;artifactId&gt;$artifactId&amp;lt;/artifactId&gt;&lt;br /&gt;
  &amp;lt;version&gt;$version&amp;lt;/version&gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Hope that helps you on your way to your own archetype. If you just want to use mine:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
mvn install&lt;br /&gt;
mvn archetype:create&lt;br /&gt;
	-DgroupId=your.groupid&lt;br /&gt;
	-DartifactId=your.artifactId&lt;br /&gt;
	-DarchetypeArtifactId=SpringframeworkArchetype&lt;br /&gt;
	-DarchetypeGroupId=nl.gridshore.samples.archetype&lt;br /&gt;
	-DarchetypeVersion=1.0.0-SNAPSHOT&lt;br /&gt;
&lt;/pre&gt; 
    </content:encoded>

    <pubDate>Thu, 31 Aug 2006 20:51:15 +0200</pubDate>
    <guid isPermaLink="false">http://www.gridshore.nl/blog/index.php?/archives/55-guid.html</guid>
    
</item>
<item>
    <title>Providing a webservice with Spring and axis</title>
    <link>http://www.gridshore.nl/blog/index.php?/archives/49-Providing-a-webservice-with-Spring-and-axis.html</link>
            <category>Springframework</category>
            <category>Webservices</category>
    
    <comments>http://www.gridshore.nl/blog/index.php?/archives/49-Providing-a-webservice-with-Spring-and-axis.html#comments</comments>
    <wfw:comment>http://www.gridshore.nl/blog/wfwcomment.php?cid=49</wfw:comment>

    <slash:comments>1</slash:comments>
    <wfw:commentRss>http://www.gridshore.nl/blog/rss.php?version=2.0&amp;type=comments&amp;cid=49</wfw:commentRss>
    

    <author>nospam@example.com (Jettro Coenradie)</author>
    <content:encoded>
    Finally I found some time to start experimenting with a webservice. I know I am not the first to write about creating your own webservice, still I think this is worth reading. I am writing a step by step creation of a webservice via maven2, axis 1.4 and spring 2.0RC1. Ofcourse you can use older versions, but I want to be ready for the future and keep on using this sample. For now I am working with a HelloWorld sample. I will make it more complicated when needed to show interesting things.&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.gridshore.nl/blog/uploads/WebservicePoc.zip&quot; title=&quot;WebservicePoc.zip&quot; target=&quot;_blank&quot;&gt;Download the sources here&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Step 1 &lt;/u&gt;- Configuring your maven project environment&lt;/b&gt;&lt;br /&gt;
We create a new directory to store all projects : ~/WebservicePoc&lt;br /&gt;
Within this directory use the archetype of maven to create a structure for a web project:&lt;br /&gt;
The groupid is used within the create pom. The artifactId is used as project id and to create the folder to store all files in. The archetypeArtifactId defines the type of structure to create, in our case a web project.&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
mvn archetype:create &lt;br /&gt;
          -DgroupId=com.coenradie.webservicespoc &lt;br /&gt;
          -DartifactId=webservice-jaxrpc-style &lt;br /&gt;
          -DarchetypeArtifactId=maven-archetype-webapp&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
Now it is time to create the pom in the root project directory. Beware if you create this pom first, the archetype:create won&#039;t work. Execute the command in a temporary directory and copy the files into the sub-directory of the module we are creating.&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
...&lt;br /&gt;
	&amp;lt;groupId&gt;com.coenradie.webservicespoc&amp;lt;/groupId&gt;&lt;br /&gt;
	&amp;lt;version&gt;1.0-SNAPSHOT&amp;lt;/version&gt;&lt;br /&gt;
	&amp;lt;artifactId&gt;WebservicePoc&amp;lt;/artifactId&gt;&lt;br /&gt;
	&amp;lt;packaging&gt;pom&amp;lt;/packaging&gt;&lt;br /&gt;
	&amp;lt;name&gt;Webservice proof of concept&amp;lt;/name&gt;&lt;br /&gt;
	&amp;lt;inceptionYear&gt;2006&amp;lt;/inceptionYear&gt;&lt;br /&gt;
	&amp;lt;url&gt;http://localhost:8080/webservicepoc&amp;lt;/url&gt;&lt;br /&gt;
...&lt;br /&gt;
	&amp;lt;modules&gt;&lt;br /&gt;
		&amp;lt;module&gt;webservice-jaxrpc-style&amp;lt;/module&gt;&lt;br /&gt;
	&amp;lt;/modules&gt;&lt;br /&gt;
...&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
For a complete look at the pom, have a look at the downloadable source code.&lt;br /&gt;
&lt;br /&gt;
We also need to make some adjustments to the generated pom of the webservice-jaxrpc-style module. Again have a look at the sources. Time to try the first compile. Execute the following command in the root of the project:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
~/WebservicePoc/mvn install&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Step 2 &lt;/u&gt; - Decide upon the libraries that we are going to use&lt;/b&gt;&lt;br /&gt;
For the application we are going to create we are going to use some libraries. We do not want to code ourselves to much. We are going to use the spring library and axis. Add these to the module pom.&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
  &amp;lt;dependencies&gt;&lt;br /&gt;
    &amp;lt;dependency&gt;&lt;br /&gt;
      &amp;lt;groupId&gt;org.springframework&amp;lt;/groupId&gt;&lt;br /&gt;
      &amp;lt;artifactId&gt;spring&amp;lt;/artifactId&gt;&lt;br /&gt;
      &amp;lt;version&gt;2.0-RC1&amp;lt;/version&gt;&lt;br /&gt;
    &amp;lt;/dependency&gt;&lt;br /&gt;
    &amp;lt;dependency&gt;&lt;br /&gt;
      &amp;lt;groupId&gt;axis&amp;lt;/groupId&gt;&lt;br /&gt;
      &amp;lt;artifactId&gt;axis&amp;lt;/artifactId&gt;&lt;br /&gt;
      &amp;lt;version&gt;1.4&amp;lt;/version&gt;&lt;br /&gt;
    &amp;lt;/dependency&gt;&lt;br /&gt;
    &amp;lt;dependency&gt;&lt;br /&gt;
      &amp;lt;groupId&gt;axis&amp;lt;/groupId&gt;&lt;br /&gt;
      &amp;lt;artifactId&gt;axis-jaxrpc&amp;lt;/artifactId&gt;&lt;br /&gt;
      &amp;lt;version&gt;1.4&amp;lt;/version&gt;&lt;br /&gt;
    &amp;lt;/dependency&gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
You will notice a problem with spring and maven 2 at the moment of writing. Maven provides the solution in the error message, we need to install the maven library manually. There aresome other options, but for now this is the easiest one. First download spring version 2.0 RC1, than issue the following command:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
mvn install:install-file &lt;br /&gt;
          -DgroupId=org.springframework &lt;br /&gt;
          -DartifactId=spring &lt;br /&gt;
          -Dversion=2.0-RC1 &lt;br /&gt;
          -Dpackaging=jar&lt;br /&gt;
          -Dfile=%PATH_TO_JAR%/spring.jar&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Step 3&lt;/u&gt; - Time to get the eclipse wtp to work&lt;/b&gt;&lt;br /&gt;
I want to deploy the webservice via tomcat and ofcourse I want a nice environment for debugging, deploying, and developing. I like to work with spring WebTools project, there is a pretty good integration with maven from the maven perspective. Lets use that. Fire up your eclipse (with wtp) and create a workspace. Do not create this workspace inside our projects, do it on another part of the disk. After creating this workspace, add the maven repository as a variable by using the following command:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
mvn eclipse:add-maven-repo -Declipse.workspace=%PATH_TO_WORKSPACE%/WebservicePoc&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
Now let maven generate the eclipse project and classpath files, issue the following command:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
mvn -Dwtpversion=1.0 eclipse:eclipse&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
You are ready to import the project into eclipse, &quot;&lt;i&gt;File &gt; Import &gt; Existing projects into workspace&lt;/i&gt;&quot;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;step 4&lt;/u&gt; - Lets publish a webservice&lt;/b&gt;&lt;br /&gt;
The webservice is deployed within a web container. Both axis and the spring container run in a servlet. There is a connection between the two via the servlet endpoint which will be discussed in a while. These servlets are configured in the &lt;i&gt;web.xml&lt;/i&gt; file. The template for the file is generated by maven, lets add the servlets, servlet mappings and a spring specific parameter.&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
	&amp;lt;context-param&gt;&lt;br /&gt;
		&amp;lt;param-name&gt;contextConfigLocation&amp;lt;/param-name&gt;&lt;br /&gt;
		&amp;lt;param-value&gt;&lt;br /&gt;
			classpath:/applicationContext.xml&lt;br /&gt;
		&amp;lt;/param-value&gt;&lt;br /&gt;
	&amp;lt;/context-param&gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
This parameter specifies the locations where spring can find the configuration files for the container. The &lt;i&gt;applicationContext.xml&lt;/i&gt; contains the definition of the beans in the Business service domain. There is another configuration file for spring thats needs to be present. This is the default spring config file based on the name of the servlet that configures the spring container. In our case this file does not contain any beans, but it must be available. It must be called &lt;i&gt;action-servlet.xml&lt;/i&gt; since we have given the spring dispatcher servlet the name action. The file should reside next to the &lt;i&gt;web.xml&lt;/i&gt; in the WEB-INF folder. &lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
	&amp;lt;servlet&gt;&lt;br /&gt;
		&amp;lt;servlet-name&gt;action&amp;lt;/servlet-name&gt;&lt;br /&gt;
		&amp;lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&amp;lt;/servlet-class&gt;&lt;br /&gt;
		&amp;lt;load-on-startup&gt;1&amp;lt;/load-on-startup&gt;&lt;br /&gt;
	&amp;lt;/servlet&gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;servlet&gt;&lt;br /&gt;
		&amp;lt;servlet-name&gt;axis&amp;lt;/servlet-name&gt;&lt;br /&gt;
		&amp;lt;servlet-class&gt;org.apache.axis.transport.http.AxisServlet&amp;lt;/servlet-class&gt;&lt;br /&gt;
		&amp;lt;load-on-startup&gt;2&amp;lt;/load-on-startup&gt;&lt;br /&gt;
	&amp;lt;/servlet&gt;&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;servlet-mapping&gt;&lt;br /&gt;
        &amp;lt;servlet-name&gt;action&amp;lt;/servlet-name&gt;&lt;br /&gt;
        &amp;lt;url-pattern&gt;*.html&amp;lt;/url-pattern&gt;&lt;br /&gt;
    &amp;lt;/servlet-mapping&gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;servlet-mapping&gt;&lt;br /&gt;
		&amp;lt;servlet-name&gt;axis&amp;lt;/servlet-name&gt;&lt;br /&gt;
		&amp;lt;url-pattern&gt;/services/*&amp;lt;/url-pattern&gt;&lt;br /&gt;
	&amp;lt;/servlet-mapping&gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
Here you can see the spring dispatcher servlet and the axis servlet. Most important to notice is the &lt;b&gt;load-on-startup&lt;/b&gt; parameter that tells the order of loading the servlets. The axis servlet connects to the loaded spring context via the special endpoint. Therefore the spring servlet needs to be loaded first. You do not need the dispatcher servlet if you plan to expose only webservices. If you also want to include spring mvc or the like, you do need the dispatcher servlet.&lt;br /&gt;
&lt;br /&gt;
Next task is to configure the axis webservice, this is done via the &lt;i&gt;server-config.wsdd&lt;/i&gt; file.&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
	&amp;lt;service name=&quot;JaxrpcwebserviceProvider&quot; provider=&quot;java:RPC&quot;&gt;&lt;br /&gt;
		&amp;lt;parameter name=&quot;className&quot;&lt;br /&gt;
			value=&quot;com.coenradie.webservicepoc.server.jaxrpcstyle.ServiceProvider&quot;/&gt;&lt;br /&gt;
		&amp;lt;parameter name=&quot;allowedMethods&quot; value=&quot;*&quot;/&gt;&lt;br /&gt;
		&amp;lt;beanMapping qname=&quot;webservicepoc:MessageBean&quot; xmlns:webservicepoc=&quot;urn:MessageBean&quot; &lt;br /&gt;
			languageSpecificType=&quot;java:com.coenradie.webservicepoc.server.jaxrpcstyle.MessageBean&quot;/&gt;&lt;br /&gt;
	&amp;lt;/service&gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
We are creating a jaxrpc style webservice with the name &lt;i&gt;JaxrpcwebserviceProvider&lt;/i&gt;. Axis should redirect all calls to this service to the endpoint of type ServiceProvider. This is a subclass of a special spring implementation of the servletendpoint, this class is will be discussed later on. As you can see from the wsdd file, all public methods are exposed via the webservice. As a transport object we use the MessageBean.&lt;br /&gt;
&lt;br /&gt;
Now all configuration is in place, time to do some real coding. We need to implement the following classes and interfaces.&lt;br /&gt;
MessageService and MessageServiceImpl - Business service interface and implementation&lt;br /&gt;
MessageBean - Object returned via the jaxrpc webservice&lt;br /&gt;
RemoteMessageService - Remote interface that is implemented by the service endpoint and therefore implemented by the webservice.&lt;br /&gt;
ServiceProvider - the special webservice endpoint that contains access to the laoded spring context. The endpoint contains the method that is called via axis.&lt;pre&gt;&lt;br /&gt;
public interface MessageService {&lt;br /&gt;
	String getMessage();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class MessageServiceImpl implements MessageService {&lt;br /&gt;
	public String getMessage() {&lt;br /&gt;
		return &quot;Message from my server&quot;;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public interface RemoteMessageService extends Remote {&lt;br /&gt;
	public MessageBean getMessage() throws RemoteException;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class MessageBean implements Serializable {&lt;br /&gt;
	private static final long serialVersionUID = -7014683074252469915L;&lt;br /&gt;
	private String message;&lt;br /&gt;
	private String createDate;&lt;br /&gt;
	&lt;br /&gt;
	public String getCreateDate() {&lt;br /&gt;
		return createDate;&lt;br /&gt;
	}&lt;br /&gt;
	public void setCreateDate(String createDate) {&lt;br /&gt;
		this.createDate = createDate;&lt;br /&gt;
	}&lt;br /&gt;
	public String getMessage() {&lt;br /&gt;
		return message;&lt;br /&gt;
	}&lt;br /&gt;
	public void setMessage(String message) {&lt;br /&gt;
		this.message = message;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
The most important class is the endpoint, this is the class that gets called by axis. The spring superclass takes care of obtaining the spring web context. You can use this context to find spring wired beans. In our case we use it to find the Business service object MessageService.&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
public class ServiceProvider extends ServletEndpointSupport implements RemoteMessageService {&lt;br /&gt;
	private MessageService messageService;&lt;br /&gt;
	&lt;br /&gt;
	protected void onInit() {&lt;br /&gt;
		this.messageService = (MessageService)getWebApplicationContext().getBean(&quot;messageService&quot;);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public MessageBean getMessage() {&lt;br /&gt;
		MessageBean messageBean = new MessageBean();&lt;br /&gt;
		messageBean.setMessage(messageService.getMessage());&lt;br /&gt;
		messageBean.setCreateDate(new Date().toString());&lt;br /&gt;
		&lt;br /&gt;
		return messageBean;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
Time to fire up the server. You can use the run on server with a right mouse click on the project within eclipse wtp. Popup a browser and go to the following url:&lt;br /&gt;
http://localhost:8080/webservice-jaxrpc-style/services&lt;br /&gt;
That should give you an overview of all deployed services, probably only the JaxrpcwebserviceProvider.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;i&gt;Step 5&lt;/i&gt; - Using xml spy to send a message via soap to the webservice&lt;/b&gt;&lt;br /&gt;
The webservice is deployed, you can have a look at the wsdl file, nice. Now we want to call the webservice. We can write a special java client. There is however an easier way with xmlspy. Since this blog item is all about exposing the webservice and not about creating a client, I will use xmlspy.&lt;br /&gt;
&lt;br /&gt;
Fire up xmlspy and use the menu: SOAP &gt; Create new soap request and enter the next url in the pop-up box:&lt;br /&gt;
http://localhost:8080/webservice-jaxrpc-style/services/JaxrpcwebserviceProvider?wsdl&lt;br /&gt;
After clicking oke, choose the getMessage() operating name, the following request is generated:&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
&amp;lt;SOAP-ENV:Envelope xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot; xmlns:SOAP-ENC=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;&lt;br /&gt;
	&amp;lt;SOAP-ENV:Body&gt;&lt;br /&gt;
		&amp;lt;m:getMessage xmlns:m=&quot;http://jaxrpcstyle.server.webservicepoc.coenradie.com&quot; SOAP-ENV:encodingStyle=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot;/&gt;&lt;br /&gt;
	&amp;lt;/SOAP-ENV:Body&gt;&lt;br /&gt;
&amp;lt;/SOAP-ENV:Envelope&gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
Then we can send the request over soap via xmlspy: SOAP &gt; Send request to server&lt;br /&gt;
&lt;pre&gt;&lt;br /&gt;
&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;br /&gt;
&amp;lt;soapenv:Envelope xmlns:soapenv=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot; xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&gt;&lt;br /&gt;
	&amp;lt;soapenv:Body&gt;&lt;br /&gt;
		&amp;lt;ns1:getMessageResponse soapenv:encodingStyle=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot; xmlns:ns1=&quot;http://jaxrpcstyle.server.webservicepoc.coenradie.com&quot;&gt;&lt;br /&gt;
			&amp;lt;getMessageReturn href=&quot;#id0&quot;/&gt;&lt;br /&gt;
		&amp;lt;/ns1:getMessageResponse&gt;&lt;br /&gt;
		&amp;lt;multiRef id=&quot;id0&quot; soapenc:root=&quot;0&quot; soapenv:encodingStyle=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot; xsi:type=&quot;ns2:MessageBean&quot; xmlns:soapenc=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot; xmlns:ns2=&quot;urn:MessageBean&quot;&gt;&lt;br /&gt;
			&amp;lt;createDate xsi:type=&quot;soapenc:string&quot;&gt;Mon Jun 26 09:48:31 CEST 2006&amp;lt;/createDate&gt;&lt;br /&gt;
			&amp;lt;message xsi:type=&quot;soapenc:string&quot;&gt;Message from my server&amp;lt;/message&gt;&lt;br /&gt;
		&amp;lt;/multiRef&gt;&lt;br /&gt;
	&amp;lt;/soapenv:Body&gt;&lt;br /&gt;
&amp;lt;/soapenv:Envelope&gt;&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
That concludes this article, I learned a lot while writing this item, hope you learned something while reading. The coming weeks I want to write about creating a client, doing document style webservices, using spring webservices component and security. Stay tuned. 
    </content:encoded>

    <pubDate>Sun, 25 Jun 2006 22:00:00 +0200</pubDate>
    <guid isPermaLink="false">http://www.gridshore.nl/blog/index.php?/archives/49-guid.html</guid>
    
</item>
<item>
    <title>Using springframework to send an email with streaming attachment</title>
    <link>http://www.gridshore.nl/blog/index.php?/archives/36-Using-springframework-to-send-an-email-with-streaming-attachment.html</link>
            <category>Springframework</category>
            <category>Technology</category>
    
    <comments>http://www.gridshore.nl/blog/index.php?/archives/36-Using-springframework-to-send-an-email-with-streaming-attachment.html#comments</comments>
    <wfw:comment>http://www.gridshore.nl/blog/wfwcomment.php?cid=36</wfw:comment>

    <slash:comments>5</slash:comments>
    <wfw:commentRss>http://www.gridshore.nl/blog/rss.php?version=2.0&amp;type=comments&amp;cid=36</wfw:commentRss>
    

    <author>nospam@example.com (Jettro Coenradie)</author>
    <content:encoded>
    For a customer of mine I had another fine problem to resolve. He must send an email with a csv file as an attachment to his transport partner. The content for the csv file come from the database. Creating a string that has the contents for the file is easy, it would also be fairly easy to create a file, store it on the file system and add the file as an attachment. There is however one problem; I do not need the file after it has been attached to the email and send. &lt;br /&gt;
&lt;br /&gt;
I started lookin at the api&#039;s of springframework related to email. It is not that hard to find some code that looks like the following to create the email to be sent:&lt;br /&gt;
&lt;br /&gt;
1. MimeMessage message = getJavaMailSender().createMimeMessage();&lt;br /&gt;
2. MimeMessageHelper helper = new MimeMessageHelper(message,true);&lt;br /&gt;
3. helper.setFrom(&quot;from@gridshore.nl&quot;);&lt;br /&gt;
4. helper.setTo(&quot;to@gridshore.nl&quot;);&lt;br /&gt;
5. helper.setText(&quot;Text of the email&quot;);&lt;br /&gt;
6. helper.setSubject(&quot;attachment mail sample&quot;);&lt;br /&gt;
&lt;br /&gt;
Now for the fun part, how can we attach a generated csv file to the email without storing it on th efile system first.&lt;br /&gt;
&lt;br /&gt;
7. InputStream attach = new ByteArrayInputStream(mailMessage.getBytes());&lt;br /&gt;
8. StreamAttachmentDataSource datasource = new StreamAttachmentDataSource(attach,&quot;attachment&quot;, &quot;txt/plain&quot;);&lt;br /&gt;
9. helper.addAttachment(attachmentName,datasource);&lt;br /&gt;
10.getJavaMailSender().send(message);&lt;br /&gt;
&lt;br /&gt;
The fun part is in line 8, you must create a custom implementation for the &lt;b&gt;org.springframework.core.io.AbstractResource&lt;/b&gt;. This resource must convert an inputstream to an attachment. The code for this class looks like this:&lt;br /&gt;
&lt;br /&gt;
public class StreamAttachmentDataSource extends AbstractResource {&lt;br /&gt;
&amp;#160;	private final static Log logger = LogFactory.getLog(StreamAttachmentDataSource.class);&lt;br /&gt;
&amp;#160;	private ByteArrayOutputStream outputStream;&lt;br /&gt;
&amp;#160;	private String name;&lt;br /&gt;
&amp;#160;	private String contentType;&lt;br /&gt;
	&lt;br /&gt;
&amp;#160;	public StreamAttachmentDataSource(InputStream inputStream, String name,&lt;br /&gt;
&amp;#160;			String contentType) {&lt;br /&gt;
&amp;#160;		this.outputStream = new ByteArrayOutputStream();&lt;br /&gt;
&amp;#160;		this.name = name;&lt;br /&gt;
&amp;#160;		this.contentType = contentType;&lt;br /&gt;
		&lt;br /&gt;
&amp;#160;		int read;&lt;br /&gt;
&amp;#160;		byte[] buffer = new byte[256];&lt;br /&gt;
&amp;#160;        try {&lt;br /&gt;
&amp;#160;			while((read = inputStream.read(buffer)) != -1) {&lt;br /&gt;
&amp;#160;				getOutputStream().write(buffer, 0, read);&lt;br /&gt;
&amp;#160;			}&lt;br /&gt;
&amp;#160;		} catch (IOException e) {&lt;br /&gt;
&amp;#160;			logger.error(&quot;Cannot create inputstream for mail attachment&quot;);&lt;br /&gt;
&amp;#160;			throw new AddAttachmentMailException(&quot;error.technical.mail.attachment&quot;);&lt;br /&gt;
&amp;#160;		}&lt;br /&gt;
&amp;#160;	}&lt;br /&gt;
	&lt;br /&gt;
&amp;#160;	public String getDescription() {&lt;br /&gt;
&amp;#160;		return &quot;Stream resource used for attachments&quot;;&lt;br /&gt;
&amp;#160;	}&lt;br /&gt;
&lt;br /&gt;
&amp;#160;	public InputStream getInputStream() throws IOException {&lt;br /&gt;
&amp;#160;		return new ByteArrayInputStream(this.outputStream.toByteArray());&lt;br /&gt;
&amp;#160;	}&lt;br /&gt;
&lt;br /&gt;
&amp;#160;	public String getContentType() {&lt;br /&gt;
&amp;#160;		return contentType;&lt;br /&gt;
&amp;#160;	}&lt;br /&gt;
&lt;br /&gt;
&amp;#160;	public String getName() {&lt;br /&gt;
&amp;#160;		return name;&lt;br /&gt;
&amp;#160;	}&lt;br /&gt;
&lt;br /&gt;
&amp;#160;	public ByteArrayOutputStream getOutputStream() {&lt;br /&gt;
&amp;#160;		return outputStream;&lt;br /&gt;
&amp;#160;	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The hard part is not in the code, it is in finding the right code &lt;img src=&quot;http://www.gridshore.nl/blog/templates/gridshore/img/emoticons/smile.png&quot; alt=&quot;:-)&quot; style=&quot;display: inline; vertical-align: bottom;&quot; class=&quot;emoticon&quot; /&gt;. What makes this a spring sample? I did use a number of utility classes from spring:&lt;br /&gt;
MimeMessageHelper - helps you with creating the mail message, a lot of try catch blocks less than with the standard way.&lt;br /&gt;
AbstractResource - This is a convenience class for creating your custom resources&lt;br /&gt;
JavaMailSender - Used to send messages that cannot be send as a SimpleMailMessage, implementation of th MailSender interface. 
    </content:encoded>

    <pubDate>Tue, 22 Nov 2005 22:46:42 +0100</pubDate>
    <guid isPermaLink="false">http://www.gridshore.nl/blog/index.php?/archives/36-guid.html</guid>
    
</item>
<item>
    <title>Building backoffice system with spring, hibernate, acegi, sitemesh, iText and other frameworks</title>
    <link>http://www.gridshore.nl/blog/index.php?/archives/6-Building-backoffice-system-with-spring,-hibernate,-acegi,-sitemesh,-iText-and-other-frameworks.html</link>
            <category>Springframework</category>
    
    <comments>http://www.gridshore.nl/blog/index.php?/archives/6-Building-backoffice-system-with-spring,-hibernate,-acegi,-sitemesh,-iText-and-other-frameworks.html#comments</comments>
    <wfw:comment>http://www.gridshore.nl/blog/wfwcomment.php?cid=6</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://www.gridshore.nl/blog/rss.php?version=2.0&amp;type=comments&amp;cid=6</wfw:commentRss>
    

    <author>nospam@example.com (Jettro Coenradie)</author>
    <content:encoded>
    I am working on an application for a friend of mine. Boy am I learning a lot. The coming weeks I want to share some stuff that I have learned. For now I will just give you a few of the frameworks that I have used.&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
&lt;li&gt;&lt;a href=&quot;http://www.springframework.org/&quot;&gt;Springframework&lt;/a&gt; for the foundation&lt;/li&gt;&lt;br /&gt;
&lt;li&gt;&lt;a href=&quot;http://www.hibernate.org&quot;&gt;Hibernate&lt;/a&gt; for persistence&lt;/li&gt;&lt;br /&gt;
&lt;li&gt;&lt;a href=&quot;http://www.hsqldb.org&quot;&gt;Hsqldb&lt;/a&gt; for the database during testing and mysql on the server&lt;/li&gt;&lt;br /&gt;
&lt;li&gt;&lt;a href=&quot;http://acegisecurity.sourceforge.net/&quot;&gt;Acegi&lt;/a&gt; for security&lt;/li&gt;&lt;br /&gt;
&lt;li&gt;&lt;a href=&quot;http://www.opensymphony.com/sitemesh/&quot;&gt;Sitemesh&lt;/a&gt; for templating&lt;/li&gt;&lt;br /&gt;
&lt;li&gt;&lt;a href=&quot;http://www.lowagie.com/iText/&quot;&gt;iText&lt;/a&gt; for pdf generation&lt;/li&gt;&lt;br /&gt;
&lt;li&gt;&lt;a href=&quot;http://ant.apache.org/&quot;&gt;Jakarta ant&lt;/a&gt; for building the application war file&lt;/li&gt;&lt;br /&gt;
&lt;li&gt;&lt;a href=&quot;http://jakarta.apache.org/tomcat/index.html&quot;&gt;Tomcat&lt;/a&gt; for deployment (windows for development and linux for deployment&lt;/li&gt;&lt;br /&gt;
&lt;li&gt;&lt;a href=&quot;http://www.eclipse.org&quot;&gt;Eclipse&lt;/a&gt; and &lt;a href=&quot;http://www.myeclipseide.com/&quot;&gt;MyEclipseIDE&lt;/a&gt; for development&lt;/li&gt;&lt;br /&gt;
&lt;li&gt;&lt;a href=&quot;http://jotm.objectweb.org/&quot;&gt;Jotm&lt;/a&gt; for transactions over multiple datasources&lt;/li&gt;&lt;br /&gt;
&lt;li&gt;&lt;a href=&quot;http://xapool.experlog.com/&quot;&gt;XaPool&lt;/a&gt; for connection pooling&lt;/li&gt;&lt;br /&gt;
&lt;/ul&gt;&lt;br /&gt;
&lt;br /&gt;
I want to thank Matt Raible for his Book SpringLive and his light version of AppFuse, &#039;Equinox&#039;. This has helped me a lot. I think it is also time for a review of his book.&lt;br /&gt;
&lt;br /&gt;
Lets start with discussing the environment. I use my laptop to develop the application. For development I use Jdk5, eclipse 3.1 and I have bought the plugin myeclipseide. This plugin helps me with the style sheets, java script, but most of all, with creating the descriptor files and the domain layer for hibernate. You connect via jdbc to mysql and hsqldb. This is done via the database plugin of myeclipseide. Then you select the tables you want to work with and generate the complete domain layer and *.hbm.xml files. There are abstract classes for the generated files and subclasses that contain your custom code. You can use the myeclipseide for creating the framework of your application, and you can also use equinox. Actually I used a cobination. I did not like the way equinox configures tests. For some reason I could not get this to work in eclipse as well as for ant. I prefer a seperate folder for all test files, including resources. Disadvantage is the duplication of config files, but that is my problem. Again use Myeclipseide for running your tomcat web server (version 5.5.9). Ofcourse you can use the ant file to start/stop and deploy your application in tomcat. I prefer myeclipse. I do use ant to generate a war file for the deployment server. I also use ant to startup the hsqldb database and for  running the create and fill sql script. This makes it easy to unit test your application. But more about this later.&lt;br /&gt;
&lt;br /&gt;
What are some of the issues that I need to resolve for the application?&lt;br /&gt;
&lt;ul&gt;&lt;br /&gt;
&lt;li&gt;Transactions over multiple datasources&lt;/li&gt;&lt;br /&gt;
&lt;li&gt;Synchronisation from webdatabase and new domain model in the backoffice&lt;/li&gt;&lt;br /&gt;
&lt;li&gt;Generation of multiple documents, invoice, order approval, packing list, etc&lt;/li&gt;&lt;br /&gt;
&lt;li&gt;Authentication and authorization, use of multiple roles in the application&lt;/li&gt;&lt;br /&gt;
&lt;/ul&gt;&lt;br /&gt;
 
    </content:encoded>

    <pubDate>Tue, 06 Sep 2005 22:58:00 +0200</pubDate>
    <guid isPermaLink="false">http://www.gridshore.nl/blog/index.php?/archives/6-guid.html</guid>
    
</item>

</channel>
</rss>