Tuesday, September 29, 2009

Designing the Obvious

Adobe MAX2009 is coming up this weekend, and while I won't get the chance to go this year I was reviewing some of the material from years past. One presentation that I have somehow hung onto was one by Robert Hoekman Jr on 'Designing the Obvious' - from his book of the same name.

I never got the book - but I think I will now. As I was reading through the paper, he talks about something we all still come across everyday - form fields with character length limits.

He mentions some of the 'obviously' wrong ways to do this:
1) type in the field, hit submit and the system tells you that you guess wrong and that there were too many characters in the field.
2) There is a 'hint' next to the text field but it lets you type in the wrong number of characters anyways. He also noted that a 'hint' is not instructive to tell someone that spaces are or are not counted.
3) The field just lets you type in characters until it just stops. The 'stops' feedback still leaves us to wonder why the system stopped taking input.

The 'obvious' answer he suggests is to give the user real-time feedback about their input. As his book title suggests - this is so obvious.

I thought I would create a quick component that did just that - gave the user real-time feedback on their input and accounted for either including space or excluding space.

In the example application below, the first input just stops taking input at the maximum character - can you guess what that limit might be? The second input uses a StringValidator - which is a step in the right direction, but does not provide the real-time feedback.

The next two use a simple component I created. The first of those, has a 15 character limit, and spaces count toward that limit. The next one has a 30 character limit and spaces do not count toward the limit.

Getting the component to ignore spaces was trickier than I thought it would be at first - but for those curious you can download the code and see how I did this.

Source can be found by clicking here



Thanks for stopping by. I would love to hear your comments on the 'obvious'.

Saturday, May 23, 2009

Converting a Java/Flex application to Grails/Flex

I have a small reference application to CRUD ( create, read, update and delete ) notes from a back end service using a simple flex application. This reference application was originally written with Spring 2.5, later upgraded to 3, and Hibernate 3.x with MySql 5. As new versions of these technologies come out, I integrate it into this simple reference application. I use it as a sanity check while integrating all of those technologies because the application is not too difficult.

I decided to try to migrate it to a Grails platform to get a sense for how difficult that might be. I am going to share the steps that I took to migrate my Tomcat/Spring3/Hibernate3/MySql5 application to Grails/MySql5.

Since I already had a database schema from the Java implementation I decide to leverage that DDL so that both applications could run against the same backend.

Since I knew that the user interface was a Flex interface, I installed the flex plugin.

grails install-plugin flex
This plugin allows one to expose a Grails service as a Flex BlazeDS service by simply adding: static expose = ['flex-remoting'] to the service.

The 3 methods that the Java service exposed were:
* getAllNotes
* saveOrUpdateNote
* deleteNode

Therefore, to maintain the same Flex UI I needed to expose the same API.

The service ultimately ended up looking like the following:
package com.redpointtech.services
import com.redpointtech.domain.Note
class RedpointNotesService {

boolean transactional = true
static expose = ['flex-remoting']

def getAllNotes() {
def allNotes = Note.findAll()
return allNotes
}

def saveOrUpdateNote(Note note) {
def noteid = note.id
println "NOTE ID TO SAVE: " + noteid
// until I can figure out how to get unsavedValue:0 to
// work I need to do my own unsaved value.
if( noteid == 0 ) {
note.id = null;
}
note.save()
}

def deleteNote(Long noteId) {
def note = Note.findById(nodeId)
note?.delete()
}
}



Take special note of the check for a zero noteid. I could not immediately figure out how to tell grails that the 'unsaved-value' is 0 instead of the default null, so I had to make that programmatic. I am still looking into this, but if you know the answer please leave a comment.

To my pleasant surprise, the service was very small and the only extra work to expose the service to Flex was to add the 'static expose = ['flex-remoting']'. Anyone that has exposed Flex services knows that you have to add a servlet to the web.xml, update the flex-remote.xml, add a Spring factory to get the service. While not hard, certainly more work that added one line to a service. Please note, that I did NOT update the web.xml at all.

Next I turned my attention to the Note domain object. I used the the grails create-domain command to create the Note domain object.

To keep this as minimal as possible to start I did not add any constraints - clearly you would normally.

The Note domain class just keeps track of the authors name, the text of the note and the date the note was created. There is no mapping to a User domain object because I purposefully wanted to keep this reference application very simple.

Below is the Grails domain object:
package com.redpointtech.domain
class Note{

//Long id
String author
String text
Date createDate

static constraints = {
}

static mapping = {
table 'notes'
version false
id unsavedValue: 0
text column: "note_text"
createDate column: "create_date"
}

}



There are a couple of aspects to note:
* I commented out the id property because by convention Grails gives every domain object an id property.
* I had to add a mapping section because I was not able to use the grails default values.

If you look up at the service class again, you will see methods on the Note class like 'findAll' but you do not see them here. This is because GORM, which is Grails' Object Relational Mapping which sits on top of hibernate, creates many of the standard methods for you and dynamically adds them at runtime. This dramatically decreases the number of DAO methods you need to write - and really all but eliminates the DAO layer all together.

At this point, I now I have a service and a domain object that represents the Notes. Because I am using a MySql database and not the supported out of the box HSQL DB, I have a little more configuration to do.

The grails-app/conf/DataSource.groovy file contains the datasource definitions for all of the environments.

Below is the version of DataSource.groovy that I used:
dataSource {
pooled = true
driverClassName = "org.hsqldb.jdbcDriver"
username = "sa"
password = ""
}
hibernate {
cache.use_second_level_cache=true
cache.use_query_cache=true
cache.provider_class='com.opensymphony.oscache.hibernate.OSCacheProvider'
}
// environment specific settings
environments {
development {
dataSource {
pooled = false
driverClassName = "com.mysql.jdbc.Driver"
dbCreate = "update" // one of 'create', 'create-drop','update'
url = "jdbc:mysql://localhost/test"
username = "root"
password = "root"
logSql="true"
dialect = org.hibernate.dialect.MySQL5InnoDBDialect
}
hibernate {
show_sql = true
hibernate.format_sql = true
}
}
hsqldevelopment {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop','update'
url = "jdbc:hsqldb:mem:devDB"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:hsqldb:mem:testDb"
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:hsqldb:file:prodDb;shutdown=true"
}
}
}

You can see in the environments section, I altered the development datasource to use MySql instead of HSQL. This should be pretty standard datasource configuration for MySql.

The last thing I needed to do was drop the mysql JDBC jar into the lib directory - and it was complete.

In the end I went from absolute zero, to a working Grails application that gave me the same functionality as my Tomcat/Spring/Hibernate application in less than an hour. I have to say, I was very impressed with the speed of development in the grails environment.

My company is currently working on a full featured, consumer facing Flex/Grails application and Grails has still not let me down even in this more challenging real world environment.

Every time I use Grails, I am more convinced of its viability as a production ready framework. It probably helps that it is built upon production ready frames of Spring and Hibernate.

Sunday, March 29, 2009

Creating a Grails Plugin for DBUnit

A colleague of mine Eric Weimer introduced me to Grails. I had already used Groovy to a limited degree but not Grails. Eric presented at the Chicago Groovy Users Group and I really wanted to learn more about Grails.

So I bought the book The Definitive Guide to Grails - 2nd Edition which I highly recommend. After getting through nearly all of the book, I really wanted to start working with Grails, so I took the example from the book and extended the Domain model slightly. The area that I wanted to focus on for now was to get a better understanding of GORM, which is the ORM layer that uses Hibernate.

To do this I created a simple Grails application with just Domain classes and I started to create some integration tests that I could use to get a feel for the GORM api.

However, to do this I quickly ended up needing to seed the database with some data so I could execute queries. In my Java bag of tricks, I have used DBUnit and I immediately wanted to leverage this in my Grails work. Normally I would have checked to see if there was already a DBUnit Grails plugin, but I was actually in 'a dead zone' - and did not have internet access. But this was ok - I already had all of the DBUnit code I needed from my Java projects.

What this blog posting is going to show is how I used the information from the Grails book to create my own DBUnit plugin that I can now leverage in my projects. As it turns out, there is already a DBUnit plugin and I will either submit my version or work with the author of that plugin to see about integrating some of my features into his code base. This is my first venture into Grails - so if someone out there sees a better way to tackle this kind of problem please post a comment so we can all benefit.

Step 1 - create a plugin application
grails create-plugin dbunit

This will create what looks like a typical Grails application. The only real difference is a file in the root directory of the project called: DbunitGrailsPlugin.groovy

Step 2 - Update DbunitGrailsPlugin.groovy

Besides the author,title, etc information the only section I changed was the doWithSpring closure. I decided to take the utility class that I already had for my Java development, and create a Spring bean out of it. This Spring bean can then be used in the Grails integration tests. Below is the updated doWithSpring closure.

def doWithSpring = {
dbunitUtil(com.redpointtech.dbunit.DBUnitUtil) { bean ->
bean.scope = "prototype"
}
}


What this says is to create a bean with the name dbunitUtil of the type com.redpointtech.dbunit.DBUnitUtil and set the scope to prototype. Because my DBUnitUtil does keep track of some DBUnit artifacts, I decided to require a new instance to avoid any thread synchronization issues.

Step 3 - Add my Java class DBUnitUtil.java to Grails src
In the root directory of the Grails plugin application, there is a folder called 'src'. In that folder there is another called 'java', and in there I placed the com.redpointtech.dbunit.DBUnitUtil.java class. Since this post is not about the DBUnit part, I am not going to go into the details of that class but it is included in the source distribution at the end of this blog post.

Step 4 - Add DBUnit jars to the lib dir
In the root directory of the Grails plugin application, there is a lib folder and in there I placed the dbunit-2.4.4.jar file.

Step 5 - package the plugin
Because I have not applied for contributor access, I cannot release the plugin so for now I can only package up the plugin and make it available locally. To package the plugin you use the command from the root of the plugin project:

grails package-plugin

This created a file called gails-dbunit-0.1.zip

Thats it - I now have a plugin I can use in any grails application to help support integration testing.

On a side note - Grails supports unit tests and integration tests. Unit tests are supposed to be very lightweight and will not initialize all of the database infrastructure so you have to create integration tests to test the

I created another application that I called gormtest and I used many of the Domain classed from the Album/Song/Artist example from the Grails book.

To use the new plugin you can install a plugin directly from the zip file using the following command:

grails install-plugin [full path to the gails-dbunit-0.1.zip]

Doing this makes a Spring available to my Integration tests. I created an itegration test using the following:

grails create-integration-test com.redpointtech.gtunes.Album

This created a file called AlbumTests.groovy in the test/integration/com/redpointtech/gtunes directory.

An abbreviated version of the AlbumTests.groovy file follows:

1 package com.redpointtech.gtunes
2
3 import grails.test.*
4 import com.redpointtech.dbunit.*
5 import javax.sql.DataSource
6
7 class AlbumTests extends GrailsUnitTestCase {
8 DataSource dataSource
9 DBUnitUtil dbunitUtil
10
11
12 protected void setUp() {
13 super.setUp()
14 dbunitUtil.onSetup(dataSource, "./data/user-testdata.xml, ./data/album-testdata.xml")
15 }
16
17 protected void tearDown() {
18 super.tearDown()
19 dbunitUtil.onTearDown()
20 }
21
22 void testAlbumFind() {
23 def albums = Album.findAllByTitleLike("title1%")
24 assertEquals albums.size, 3
25 albums.each { album ->
26 println "------------------------------------"
27 println "Title: " + album.getTitle()
28 def artist = album.getArtist()
29 println "Artist: " + artist.getName()
30 }
31 }
32}



On lines 8 and 9, we create two instance variables that Grails will automatically inject the beans into - following the naming convention. The first is the dataSource used for the test and the second is the DBUnitUtil bean that is provided by the Plugin we just created.

On line 14, we use the DBUnit plugin bean, to load the database referenced by the dataSource, with the data in the two XML files.

On line 19, we use the dbunitUtil bean again to tear down the data used for the test. I know someone is going to ask about the time it takes to load and unload all of the data for each test and it is a fair concern. My suggestion is to group as many tests in the single test method as is practical, and since I dont think anyone would really want to create dependencies between their tests you probably do want to start with a fresh data load for the next test.

On line 23, we can then start to use the GORM API, against the data that was loaded by the DBUnit plugin.

The last part to take care of is the XML data files. I placed the data files in the grails-app/conf directory in a subdirectory called data

The user-testdata.xml file looks like the following:
<?xml version="1.0" encoding="UTF-8" ?>

<dataset>
<user id="1000" version="1" first_name="user1_fn" last_name="user1_ln" user_name="user1_un" />
<user id="2000" version="1" first_name="user2_fn" last_name="user2_ln" user_name="user2_un" />
<user id="3000" version="1" first_name="user3_fn" last_name="user3_ln" user_name="user3_un" />
<user id="4000" version="1" first_name="user4_fn" last_name="user4_ln" user_name="user4_un" />


</dataset>



That is pretty much all there is to creating a simple DBUnit plugin, that leveraged the Java code I already had to pre-populate a test database with data so I could start to get familiar with the GORM api through the integration tests.

The source code for both the dbunit plugin and the gormtest application can be found here:

DBUnit Grails Plugin

Gormtest Grails App

Sunday, March 15, 2009

How to create a popup window from an ItemEditor

I was recently helping a client that needed to create an item editor that would allow them to update the text field of a data grid, which also rendered an Icon that the user could click to create a comment line. Clicking the comment Icon would bring up a popup window to edit the comments. When the popup window was dismissed, the UI should show the ItemEditor in a state to allow the user to complete the editing of the original DataGrid item.

In their initial implementation they did not use the ItemEditEnd event from the DataGrid, because they noticed that when the popup window was created the ItemEditEnd event would fire and Flex would go through the entire process of ending the editing session. Clearly not what they wanted. The client went through a number of 'workarounds' but they always avoided the ItemEditEnd event.

I think trying to circumvent the cell editing process is almost always a bad idea that leads to code that will break as new releases of the Flex framework come out, and will certainly confuse developers that have to maintain the code. So I set off trying to create a simplified example of two options for them, one of which was a popup window. Since it was sufficiently tricky - I thought there may be others out there that would benefit from this example.

Below is a working example of what I put together. In the top DataGrid, I setup the comment editor to NOT use a popup window and instead just toggle between a textinput to edit the cell data, and a textinput to edit the comment line. Personally I find the popup window awkward to the user experience - but that was the client requirement. The second DataGrid is a simplified version of the required popup window. Notice that it only allows for a single line and takes the return key as the single to end editing the comment.

You can find the code for this example here

Tuesday, February 3, 2009

Flex ItemRenderers - and the case of the random renderering

I was reading through the book 'Flex 3 in Action' and I came across what seemed like a simple ItemRenderer example that I thought would be good show some people because it demonstrated what not to do, the visual consequences and then how to fix it. Except - in my testing it did not fix it.

The purpose of the blog posting is to highlight the issues and how I ultimately solved it - albeit I don't fully understand yet why it was happening. That will take a little more time.

The example was a simple one. There is a data grid with a data provider of objects and if the object has an email address show a button in the grid that the user can press to send the person an email. If the object in the dataprovider has no email address do not show the button.

This example is taken from section 9.2.3, page 190 of Flex 3 In Action.

Below is a working example from that section. In the first data grid, the ItemRenderer was incorrectly implemented just as the authors suggest. That being during creationComplete event we check the data to see if we should render the email button. This is incorrect, because Flex will reuse ItemRenderers and the creationComplete method should only be called once.

In the 'correct' version, we look for the dataChange event and then run the logic again to see if we should render the email button. However, I noticed that if I scrolled the list to the bottom, and then scrolled up via the scroll button one line at a time, the first time Mikey Mouse did not get a button. For some reason, the creationComplete was called for that ItemRenderer - even though the ItemRenderer that was there had its dataChange event called. Or at least that is how it appeared. Why this is happening I am not sure. However, to fix this problem I changed the creationComplete to call the checkEmail function just like dataChange and enhanced the error checking to make sure we cover the case where the ItemRenderer is created before the data property is set.

Now in the third data grid, you can scroll to the bottom, and then back up and see that Mikey Mouse can now still receive email...

Source can be found by clicking here

If anyone knows why the example exercise did not work, please leave a comment.

Saturday, January 31, 2009

Hibernate DAO using Spring and Java Generics

Having created a number of Hibernate DAOs it does not take long to see a pattern emerge which would seem to lend itself to a typed generic DAO. As any good engineer will do, I first set off to see if someone else had already thought about this. It is sometimes easier to stand on the shoulders of giants, and I found one.

The work I am going to present was really inspired by the following blog posting:

http://www.nileshk.com/node/66

But I wanted to change the implementation somewhat, and add more methods that made sense to my problem domain. So what I am going to present is an extension to the above blog posting. The purpose of this is not to suggest that one implementation is better or worse, they are just different and I actually hope that perhaps my implementation will inspire someone else in their work.

The AbstractDao interface looks like the following - with nearly every comment removed for clarity. I hope the method names are suggestive enough:

package com.redpointtech.dao;

import java.util.Collection;
import java.util.List;

/**
* borrowed heavily from: http://www.nileshk.com/node/66
*/
public interface AbstractDao <DomainObject, KeyType> {

void update(DomainObject object);
void refresh(DomainObject object);
void save(DomainObject object);
Long saveOrUpdate(DomainObject object);
void saveAll(Collection<DomainObject> objects);


DomainObject load(KeyType id);
List<DomainObject> findAll();
List<DomainObject> findAllOrderById();
DomainObject findById(KeyType id);
List<DomainObject> findByIds(Collection<KeyType> idList);
List<DomainObject> findAllOrderByProp(String propertyName, Boolean asc);


int deleteAll();
void deleteAll(Collection<DomainObject> objects);
void delete(DomainObject object);
void deleteById(KeyType id);
int deleteByIds(Collection<KeyType> ids);

int count();

}



The Implementation looks as follows:

package com.redpointtech.dao;

import static org.springframework.util.CollectionUtils.isEmpty;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.EntityMode;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.metadata.ClassMetadata;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.util.Assert;

/**
*
* borrowed heavily from: http://www.nileshk.com/node/66
*/
public abstract class AbstractHibernateDaoImpl<T extends Serializable, KeyType extends Serializable>
extends HibernateDaoSupport implements AbstractDao<T, KeyType>{

@SuppressWarnings("unchecked")
protected Class<T> domainClass = getDomainClass();

public void refresh(T t) {
getHibernateTemplate().refresh(t);
}

@SuppressWarnings("unchecked")
public T load(KeyType id) {
return (T) getHibernateTemplate().load(domainClass, id);
}

@SuppressWarnings("unchecked")
public T findById(KeyType id) {
return (T) getHibernateTemplate().get(domainClass, id);
}

@SuppressWarnings("unchecked")
public List<T> findByIds(Collection<KeyType> idList) {
Assert.notNull(idList, "idList parameter cannot be null");
ClassMetadata cmd = getHibernateTemplate().getSessionFactory().getClassMetadata(domainClass);
String idProp = cmd.getIdentifierPropertyName();

if( idList.size() > 0 ) {
Criteria criteria = getHibernateTemplate().getSessionFactory()
.getCurrentSession().createCriteria(domainClass);
criteria.add(Restrictions.in(idProp, idList));

return criteria.list();
} else {
return new ArrayList<T>();
}
}

public void update(T t) {
getHibernateTemplate().update(t);
}

public void save(T t) {
getHibernateTemplate().saveOrUpdate(t);
}

public Long saveOrUpdate(T t) {
getHibernateTemplate().saveOrUpdate(t);
Long id = (Long)getHibernateTemplate().getSessionFactory().getClassMetadata(domainClass).getIdentifier(t, EntityMode.POJO);
return id;
}

public void saveAll(Collection<T> list) {
if(!isEmpty(list)) {
getHibernateTemplate().saveOrUpdateAll(list);
}
}

public void delete(T t) {
getHibernateTemplate().delete(t);
}

@SuppressWarnings("unchecked")
public List<T> findAll() {
return (getHibernateTemplate().find("from " + domainClass.getName()
+ " x"));
}

@SuppressWarnings("unchecked")
public List<T> findAllOrderById() {
ClassMetadata cmd = getHibernateTemplate().getSessionFactory().getClassMetadata(domainClass);
String idProp = cmd.getIdentifierPropertyName();

return findAllOrderByProp(idProp,true);
}

@SuppressWarnings("unchecked")
public List<T> findAllOrderByProp(String propertyName, Boolean asc) {

Criteria criteria = getHibernateTemplate().getSessionFactory()
.getCurrentSession().createCriteria(domainClass);
if( asc ) {
criteria.addOrder(Order.asc(propertyName));
} else {
criteria.addOrder(Order.desc(propertyName));
}


return criteria.list();
}


public void deleteById(KeyType id) {
Object obj = load(id);
getHibernateTemplate().delete(obj);
}

@SuppressWarnings("unchecked")
public int deleteByIds(Collection<KeyType> ids) {
int count = 0;

if(!isEmpty(ids)) {

ClassMetadata cmd = getHibernateTemplate().getSessionFactory().getClassMetadata(domainClass);
String idProp = cmd.getIdentifierPropertyName();

String hqlDelete = "delete " + domainClass.getName() + " x where x." + idProp + " in ( :ids )";

Query deleteQuery = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hqlDelete);
deleteQuery.setParameterList("ids", ids);

count = deleteQuery.executeUpdate();

}
return count;
}

public void deleteAll(Collection<T> objects) {
if(!isEmpty(objects)) {
getHibernateTemplate().deleteAll(objects);
}
}

public int deleteAll() {

String hqlDelete = "delete " + domainClass.getName();
Query deleteQuery = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hqlDelete);

int count = deleteQuery.executeUpdate();
return count;
}

public int count() {
ClassMetadata cmd = getHibernateTemplate().getSessionFactory().getClassMetadata(domainClass);
String idProp = cmd.getIdentifierPropertyName();

Criteria criteria = getHibernateTemplate().getSessionFactory()
.getCurrentSession().createCriteria(domainClass);
criteria.setProjection(Projections.countDistinct(idProp));
Object countValue = criteria.uniqueResult();

return Integer.parseInt(countValue.toString());
}

protected Class getDomainClass() {
if (domainClass == null) {
ParameterizedType thisType = (ParameterizedType) getClass().getGenericSuperclass();
domainClass = (Class) thisType.getActualTypeArguments()[0];
}
return domainClass;
}

}



I incorporated this into my Redpoint Notes reference project and I will show how that looks for the DAO hierarchy.

NotesDao
package com.redpointtech.dao;

import java.util.List;

import com.redpointtech.domain.Note;

public interface NotesDao {

List<Note> getAllNotes();

Long saveOrUpdate(Note note);

}



HibernateNotesDao Interface
package com.redpointtech.dao;

import com.redpointtech.domain.Note;

public interface HibernateNotesDao extends NotesDao,
AbstractDao<Note, Long> {

}



HibernateNotesDaoImpl
package com.redpointtech.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.redpointtech.dao.AbstractHibernateDaoImpl;
import com.redpointtech.dao.HibernateNotesDao;
import com.redpointtech.domain.Note;

@Repository("hibernateNotesDao")
public class HibernateNotesDaoImpl extends AbstractHibernateDaoImpl<Note, Long>
implements HibernateNotesDao {



@Override
public List<Note> getAllNotes() {
List<Note> notes = this.findAll();
return notes;
}


}



As you can see I wrote virtually no code beyond what the abstract generic implementation gives me - and the getAllNotes is really contrived just to show how you would add a specific DAO method.

Using this kind of generic Hibernate DAO has saved us a lot of time and I hope you find this useful as well.

Sunday, January 25, 2009

Spring Blaze Integration 1.0 M1 with Hibernate

I have a reference project that I have been using to stitch the different Spring/Hibernate/Flex/AIR technologies together.

Well today, I wanted to upgrade to Spring3 and look at the new Spring BlazeDS Integration. You can find all of the information about the Spring BlazeDS integration here.

This particular project used as Flex 3 front end, communicating to a Tomcat/Spring2.5/SpringJDBC to MySql database. Today I decided to upgrade the application to use Spring3 and Hibernate 3.3.1 which I did not anticipate being a big project - but I was wrong. Well ok, it was not a big project but it was bigger than I expected it to be.

If you following along with the sample application from the Spring/BlazeDS integration it goes really well.

However when you go to integrate Hibernate and use the OpenSessionInViewFilter things get a little tricky.

Conventional Spring wisdom would have us use the ContextLoaderListener to load the Spring context much like the following:

    <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/main-application-config.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>



However, when I did that the MessageBroker complained that the ServletConfig was null so I decided I would initialize the Spring application context as the Spring BlazeDS Integration suggested. However - when I did that, the OpenSessionInView Filter complained that the web application context was not initialized.

So I was between the proverbial rock and a hard place.

I spent some time looking through the code to see how I could 'fix' this problem. Then it occurred to me that perhaps it was not a problem with the code. Maybe, this could fixed with a different configuration. The OpenSessionInView filter just needs information about the Hibernate Session, SessionFactory, DataSource, etc. It does not need to know about the MessageBroker and the MessageBroker does not need the Hibernate information.

So instead of trying to fix the code, I looked at how to fix my configuration.

I looked at my application config, and removed all of the elements that were related to Blaze integration and created a separate configuration for the servlet to load. I now have the ContextLoaderListener initialize part of the application config and the servlet the other part.

Here is a portion of the web.xml file configuration:

    <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/main-application-config.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<filter>
<filter-name>hibernate-session</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernate-session</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>redpointnotes</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/blaze-config.xml</param-value>
</init-param>

<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>redpointnotes</servlet-name>
<url-pattern>/spring/*</url-pattern>
</servlet-mapping>




The blaze-config.xml file looks like the following:
  <bean id="mySpringManagedMessageBroker" 
class="org.springframework.flex.messaging.MessageBrokerFactoryBean" />

<bean id="flexMessageBroker" abstract="true">
<property name="messageBroker" ref="mySpringManagedMessageBroker" />
</bean>

<!-- bean id name is the name that will be exposed to Flex remoting as the destination name -->
<bean id="redpointNotesServiceDestination" parent="flexMessageBroker"
class="org.springframework.flex.messaging.remoting.FlexRemotingServiceExporter">
<property name="service" ref="notesService" />
</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

<!-- map request paths at /messagebroker to the BlazeDS MessageBroker -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/messagebroker/*=mySpringManagedMessageBroker
</value>
</property>
</bean>

<!-- dispatches requests mapped to a message broker -->
<bean class="org.springframework.flex.messaging.servlet.MessageBrokerHandlerAdapter" />




So by breaking up the configuration I was able to get around this 'chicken-n-egg' problem but I will still look to see if there is a more elegant way to handle this. If anyone has a better way of dealing with this, please post a comment so we can all benefit.

I hope this helps save someone some time.

Saturday, January 24, 2009

Flex Camp Chicago 2009

Hi Everyone

I just got back from Flex Camp Chicago 2009 and I would first like to thank everyone that worked so hard to make this event happen. It was a very informative camp with a wide range technical perspectives - both from presenters and the people in attendance.

There was so much covered during those two days, so instead of trying to summarize it all I am going to take certain presentations and topics and blog on those over the next few days.

The second day Keynote was from James Ward. He created his 'Top 10 things to like' and Flex. Here is a summary of that list. I am also including some audio clips of each of the 10 topics he discussed. Since some of what he did was actual code, it will be a little hard to follow just the audio but it is still worth the listen.:

#1 Threads with PixelBender
As you may know - the FlashPlayer is single threaded, at least as far as our applications are concerned, and this therefore makes long running calculations very difficult to do. With PixelBender, you have the ability to create very lightweight threads of execution. Now - there are plenty of reasons to not get too excited about this. First, it is not a robust threading model that those of us in the Java world have come to know and love ( or not love ). Second, you have to know how to program with the PixelBender language. What I did find interesting is the number of times threading the Flash Player came up during the conference. There definitely seems to be mounting pressure for a multithreaded environment capability.




#2 Text Layout Framework
This was very cool and I am really looking forward to these new features. You can find all of the details on the Adobe Labs site, but here is a quick summary.
* Flowing text around columns - including selection. True multi-column output is now going to be part of the framework.
* Text alpha for fading, etc. You can fade text in/out with a single font.
* Bidirectional text, vertical text in over 30 writing systems. Now that is 'International'




#3 Openness
Adobe continues to make more of its platform open which allows developers like us to really embrace and build upon the great work at Adobe. Currently AMF, Flex SDK, BlazeDS are all examples of their open source. Adobe recently announced the RTMP spec will be made open allowing for more choices in the streaming media. Last - but not least - check out bugs.adobe.com. If you are find a bug check to see if it is in the bug database, and if not submit. If you do find, please vote for it. This is our communication channel back to Adobe on what we feel is important to get fixed.




#4 Tour de Flex
This one is really awesome! You can take the Tour here but finish reading the top 10 list before you leave.
If you are fan of the component explorer - you are going to love Tour de Flex. It is similar to the component explorer except it is an AIR application, and it has much more content. You can see all of the components, interact with examples, see source code - just like with component explorer, but now the documentation is also integrated in. In addition to this, components offered by the community will also be available through Tour de Flex. There is also a great Eclipse plugin that integrates with Tour de Flex using another very cool new technology called Merapi ( thats my second topic for the blog post ).





#5 Portable RIAS
What does this mean? I can hear you ask. We are all now familiar with running Flex applications via a web browser - but have you ever thought about running a Flex application from a PDF document? For detailed information checkout James Ward's posting on Portable RIAs. James showed a demo of how he took the dashboard application, exported it to PDF and was able to actually run the application from the PDF document. The PDF was also able to update its data from the server. PDF Reader 9 and above comes with the flash player embedded which makes all of this possible.





#6 Flex 4 Gumbo
This is the new version of Flex and the Flex builder. There are a lot of exciting changes coming and the Gumbo SDK is available now from Adobe Labs. There are some great resource links on this page and I highly recommend you check them out.





#7 Rapid Application Development without the Rapids.
In this part of his presentation he talked about the new workflow and wizard capabilities to get applications up and running quickly. He was able to very quickly get a data driven, synchronized application up and running.




#8 AMF
James discussed and showed the Census demo which shows you how much better AMF is to all other protocols to communicate between Flex and the server. He made an interesting comments as part of an answer to a question. James commented that he would always use AMF over all other protocols unless there was a good reason not to. He also showed the group how easy it was to use RemoteObject with a remote server.




#9 RIA Best Practices.
James did not enumerate any particular best practices, but commented on the state of best practices and what RIA developers should consider. These practices are independent of the RIA technology and he is working with people from the Microsoft and JavaFX team. They have a site that you can reach here called RIA Practices.




#10 You
The developer community that maintains the interest level in the technology and all of the developers contributing to the open source movement.




Thanks so much for checking out my blog and come back often as I will be adding additional entries for other Flex Camp Chicago 2009 topics. My next blog entry will talk about Merapi which is a project to allow AIR and Java to communicate with one another. I see some really cool possibilities with this.