You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Gordon Ross <G....@ccw.gov.uk> on 2007/03/08 14:54:34 UTC

Using IBATIS From within Tomcat (& Eclipse)

I'm new to iBatis, and want to use it from within a Tomcat servlet that
I'm developing under Eclipse.

To start with, I knocked up a simple command line app to make sure I
could to what I wanted with IBATIS, and that worked fine. So I
progressed to including IBATIS within my servlet.

My problem is when I try to initalise the sqlmap. I've got the
boiler-plate code of:

	String resource = "SqlMap.xml";
	Reader reader = Resources.getResourceAsReader(resource);
	sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);

However, I always get the java exception of:

java.io.IOException: Could not find resource SqlMap.xml

Now this wasn't totally surprised as I wasn't sure about the basic CWD
of a tomcat app. In this case (when running under Eclipse) it appears to
be my home directory. So I put the sqlmap.xml file there, and IBATIS
still complains that it can't find the file.

However, if I do a quick:

File file = new java.io.File("SqlMap.xml");
if (file.canRead())
    System.err.println("Can Read");
else
    System.err.println("Can't Read");

Right before the IBATIS initalisation code, I get a "Can Read" message
on the Tomcat console.

So what's going on here ? How can I point IBATIS to the XML config
files under Tomcat (& Eclipse)

I'm using IBATIS 2.3.0.677, with Tomcat 5.5.20 and Eclipse 3.2.2

Thanks,

GTG

Re: Using IBATIS From within Tomcat (& Eclipse)

Posted by Ted Schrader <te...@gmail.com>.
Hi Gordon,

My approach has been to include all the XML and my custom SQLConfig
class in a sqlmaps package within the Java source.  This doesn't
necessarily put all the XML on the classpath but it seems easier to
work with.  The file structure looks like this:

com.myapp.sqlmaps
    SqlConfig.java
    sql-map-config.xml
    MyQueries.xml

The SqlConfig.java file is based on the example in the Developer's
Guide, but here it is in the guise of a Struts PlugIn:

=================================================
package com.myapp.sqlmaps;

import java.io.Reader;

import javax.servlet.ServletException;

import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

/**
 * Connects iBATIS to Struts via the PlugIn mechanism.
 */
public class SqlConfig implements PlugIn {

	/** Location of the iBatis sqlMap config file.
         * This will be set to
         * "/WEB-INF/classes/com/myapp/sqlmaps/sql-map-config.xml"
         * by the Struts engine per it's config.
         */
	private String sqlMapConfigFile;
	
	/** iBatis sqlMapClient. */
	private static SqlMapClient sqlMap;

	/** Default constructor. */
	public SqlConfig() {}

	/** Bring the object into scope. */
	public void init(ActionServlet servlet, ModuleConfig config)
        throws javax.servlet.ServletException
    {
    	try {
			Reader reader = Resources.getResourceAsReader(this.getSqlMapConfigFile());
			sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
    	}
    	catch(Exception exception) {
    		throw new ServletException("Error initializing SqlConfig class.
Cause: " + exception.toString());
    	}
	}
	
	/** Take the object out of scope. */	
    public void destroy() {}

	/**
	 * Get the SqlMapClient.
	 * @return static instance of SqlMapClient
	 */
    public static SqlMapClient getSqlMapInstance() {
    	return sqlMap;
    }

    public String getSqlMapConfigFile() {
        return sqlMapConfigFile;
    }

    public void setSqlMapConfigFile(String string) {
        sqlMapConfigFile = string;
    }

}
=================================================

Inside the sql-map-config.xml file, I have:

<sqlMap resource="com/myapp/sqlmaps/MyQueries.xml"/>

Perhaps this will help.

Ted



On 08/03/07, Gordon Ross <G....@ccw.gov.uk> wrote:
> >>> On 08 March 2007 at 14:31, in message
> <Pi...@localhost.localdomain>, Graeme J
> Sweeney
> <ib...@gjsweeney.com> wrote:
> > On Thu, 8 Mar 2007, Gordon Ross wrote:
> >
> >> Now this wasn't totally surprised as I wasn't sure about the basic
> CWD
> >> of a tomcat app. In this case (when running under Eclipse) it
> appears to
> >> be my home directory. So I put the sqlmap.xml file there, and
> IBATIS
> >> still complains that it can't find the file.
> >
> > I can't speak for how Eclipse influences the set up but I would
> expect
> > your SqlMap.xml to reside in WEB-INF/classes in your document root.
> >
> > e.g. /home/user/public_html/WEB-INF/classes
>
> Oh, I know where they live. The problem is getting IBATIS to be able to
> read them.
>
> >> So what's going on here ? How can I point IBATIS to the XML config
> >> files under Tomcat (& Eclipse)
> >
> > You might want to try getResourceAsStream too.
>
> OK. I changed my "Reader reader = Resources..." line to be:
>
> Reader reader = new InputStreamReader(new
> FileInputStream("sqlMap.xml"));
>
> It can now read and open the sqlMap.xml file, but it can't open and
> read the files pointed to by the "<sqlMap resource="blah.xml" />
>
> I get (ultimately) the error "Could not find resource
> /home/gtg/Item.xml";
>
> So back at square one.
>
> Anyway, I think I've solved it. I found (via google) that
> getResourcesAsReader actually looks for files relative to the CLASSPATH.
> Once I amend my paths to this, it all appears to work (Well, IBATIS
> does, the rest of my code doesn't !)
>
> The problem appears to be the errors being returned by the
> getResourceAsReader method. The error is wrong. It says it was trying to
> open /home/gtg/SiteMap.xml (or whatever) but actually it was trying to
> open the file elsewhere.. *sigh*
>
> Thanks,
>
> GTG
>

Re: Using IBATIS From within Tomcat (& Eclipse)

Posted by Gordon Ross <G....@ccw.gov.uk>.
>>> On 08 March 2007 at 14:31, in message
<Pi...@localhost.localdomain>, Graeme J
Sweeney
<ib...@gjsweeney.com> wrote:
> On Thu, 8 Mar 2007, Gordon Ross wrote:
> 
>> Now this wasn't totally surprised as I wasn't sure about the basic
CWD
>> of a tomcat app. In this case (when running under Eclipse) it
appears to
>> be my home directory. So I put the sqlmap.xml file there, and
IBATIS
>> still complains that it can't find the file.
> 
> I can't speak for how Eclipse influences the set up but I would
expect
> your SqlMap.xml to reside in WEB-INF/classes in your document root.
> 
> e.g. /home/user/public_html/WEB-INF/classes

Oh, I know where they live. The problem is getting IBATIS to be able to
read them.

>> So what's going on here ? How can I point IBATIS to the XML config
>> files under Tomcat (& Eclipse)
> 
> You might want to try getResourceAsStream too.

OK. I changed my "Reader reader = Resources..." line to be:

Reader reader = new InputStreamReader(new
FileInputStream("sqlMap.xml"));

It can now read and open the sqlMap.xml file, but it can't open and
read the files pointed to by the "<sqlMap resource="blah.xml" />

I get (ultimately) the error "Could not find resource
/home/gtg/Item.xml";

So back at square one.

Anyway, I think I've solved it. I found (via google) that
getResourcesAsReader actually looks for files relative to the CLASSPATH.
Once I amend my paths to this, it all appears to work (Well, IBATIS
does, the rest of my code doesn't !)

The problem appears to be the errors being returned by the
getResourceAsReader method. The error is wrong. It says it was trying to
open /home/gtg/SiteMap.xml (or whatever) but actually it was trying to
open the file elsewhere.. *sigh*

Thanks,

GTG

Re: Using IBATIS From within Tomcat (& Eclipse)

Posted by Graeme J Sweeney <ib...@gjsweeney.com>.
On Thu, 8 Mar 2007, Gordon Ross wrote:

> Now this wasn't totally surprised as I wasn't sure about the basic CWD
> of a tomcat app. In this case (when running under Eclipse) it appears to
> be my home directory. So I put the sqlmap.xml file there, and IBATIS
> still complains that it can't find the file.

I can't speak for how Eclipse influences the set up but I would expect
your SqlMap.xml to reside in WEB-INF/classes in your document root.

e.g. /home/user/public_html/WEB-INF/classes

> So what's going on here ? How can I point IBATIS to the XML config
> files under Tomcat (& Eclipse)

You might want to try getResourceAsStream too.

-- 
Graeme -