For those interested in creating modular sql map applications (a single sqlmapclient with sqlmaps loaded dynamically from multiple jars) here's how I went about it:

I have a Spring/Ibatis application in which I wanted to load Spring Beans/Ibatis DAOs from jars present on the classpath. To accomplish this, I created a new Spring bean, extending the existing org.springframework.orm.ibatis.SqlMapClientFactoryBean.
This bean takes additional properties templateLocation and sqlmapLocation, where templateLocation is a single filename and sqlmapLocation is a path pattern, such as "/config/*sqlmap.xml".

In this bean, I searched the classpath for a template SQL-map-config.xml and for any *sqlmap.xml files.
I then wrote a new temporary sql-map-config.xml file and inserted <sqlMap url=""/> entries for all the sqlmap.xml files on the classpath.
I then build the sqlmap client using the new sql-map-config.xml file via the SqlMapBuilder and delete the temporary file.

In my initial tests, it's working fine - I have a core DAO jar file and an extension DAO jar file both running inside a web application, and Spring/Ibatis seem to be working happily.

It doesn't stand much chance of working without Spring however, as it is tightly coupled to the SqlMapClientFactoryBean and makes use of Spring utility classes to search the classpath.

It's also a bit ugly in places - writing a temporary xml file to build the sqlmapclient from smells bad to me, but I don't know of any other way to get an sqlmapclient...

Questions, ideas, comments?

Cheers,
Gareth Moorst.

----- Original Message ----
From: Gareth Moorst <[EMAIL PROTECTED]>
To: [email protected]
Sent: Wednesday, 30 August, 2006 4:45:00 PM
Subject: Re: Creating SQLMaps at runtime

Clinton,

The classpath scanning code is much simpler than I expected - in Spring, it just boils down to:

[classLoader].getResources("folder/filename.xml");

which returns an Enumeration of URLs.
The javadoc notes that there must be a folder for the method call to work reliably when searching inside jar files.

Allowing wildcards in the filename is a bit more complex and involves finding the classpath roots and iterating through the entries in classpath jars or folders to see if they match the pattern.

The code is all in org.springframework.core.io.support.PathMatchingResourcePatternResolver for the interested.

So scanning the classpath for named files is pretty easy to do... shall I expect it in the next release? ;)

What remains is for me to figure out a way to mung the retrieved sql map files into an sql map client.

Can anyone point me in the right direction?

Cheers,
Gareth.

----- Original Message ----
From: Gareth Moorst <[EMAIL PROTECTED]>
To: [email protected]
Sent: Wednesday, 30 August, 2006 3:16:46 PM
Subject: Fw: Creating SQLMaps at runtime



----- Forwarded Message ----
From: Gareth Moorst <[EMAIL PROTECTED]>
To: Clinton Begin <[EMAIL PROTECTED]>
Sent: Wednesday, 30 August, 2006 3:16:03 PM
Subject: Re: Creating SQLMaps at runtime

Cheers for the quick reply..

As for searching the classpath, I'm not sure yet.
I only found out earlier today that Spring allows you to do just that - you can create an ApplicationContext using a string like 'classpath*:context.xml' to find all the context.xml files on the classpath - that's how I was going to sort out the spring side of things.

I'll take a look at the spring source and see how they do it.

Cheers,
Gareth

----- Original Message ----
From: Clinton Begin <[EMAIL PROTECTED]>
To: [email protected]; Gareth Moorst <[EMAIL PROTECTED]>
Sent: Wednesday, 30 August, 2006 3:09:02 PM
Subject: Re: Creating SQLMaps at runtime


>> search the classpath

How are you going to do that?  If that were an easy thing to do reliably, we'd probably just do it for you by allowing:  <sqlMap resource="/*.xml" />

Any ideas?

Cheers,
Clinton

On 8/30/06, Gareth Moorst <[EMAIL PROTECTED]> wrote:
Hi,


What I'm trying to achieve is a way to have a set of 'core' sqlmaps, which are always loaded, and a further set of optional sqlmaps, which are only loaded if present.
In particular, I want to search the classpath for sqlmap files at startup and 'inject' them all into a running Spring configured sql map client.





Reply via email to