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 Maarten Meijer <mj...@xs4all.nl> on 2008/02/14 16:55:39 UTC

Using Ibatis as core for an Eclipse SQL plugin

Hi,

I'm working on Eclipse Mylyn bug 184532: [connector] Generic SQL  
connector
https://bugs.eclipse.org/bugs/show_bug.cgi?id=184532

As I have some experience with Ibatis as database connection for a  
servlet I try to use it here again.
Benefits of ibatis for this bug:
The XML configurable SQL result sets makes it possible to map from  
almost any accessible database to the Mylyn task model.

I've succeeded in opening the SqlMapConfix.xml with parameters in a  
properties file using the following code:

public class TasksSqlMapConfig {
	private static SqlMapClient sqlMap = null;

	public static SqlMapClient getSqlMapInstance() throws CoreException {
		if (null == sqlMap) {
			try {
				InputStream resource = null;
				Path mapPath = new Path("maps/SqlMapConfig.xml");
				resource =  
FileLocator.openStream(IbatisCorePlugin.getDefault().getBundle(),  
mapPath, false);
				// replace with read from pref later
				Path propPath = new Path("maps/db.properties");
				InputStream propStream =  
FileLocator.openStream(IbatisCorePlugin.getDefault().getBundle(),  
propPath,
						false);
				Properties properties = new Properties();
				properties.load(propStream);

				sqlMap = SqlMapClientBuilder.buildSqlMapClient(resource,  
properties);
			} catch (Exception e) {
				Status init = new Status(IStatus.ERROR,  
IbatisCorePlugin.PLUGIN_ID, "Could not initialize SqlMaps", e);
				StatusHandler.log(init);
				throw new CoreException(init);
			}
		}
		return sqlMap;
	}

	private TasksSqlMapConfig() {
		super();	// Singleton
	}
}

This will start to parse the XML alright and use the properties as  
replacement. However when it is time to locate the Sqlmap.xml files  
referenced in the SqlMapConfig.xml an error occurs. Eclipse bundles  
all have their own class path so maybe ibatis has problems locating  
the resource.

Is it possible to insert a replacement or additional class loader?
I've tries using Resources.setDefaultClassLoader but this isn't used  
anywhere,  
likecom 
.ibatis.common.resources.Resources.getResourceAsStream(ClassLoader,  
String)

Is this option fully implemented? Is there a tutorial available, I  
have "Ibaqtis in Action" but haven't found anything.


Re: Using Ibatis as core for an Eclipse SQL plugin

Posted by Ma...@prolifics.de.
We are developing an Eclipse Rich Client project using iBatis and faced 
the same problem.
Our solution is to use the Eclipse Buddy mechanism (see 
http://www.eclipsezone.com/articles/eclipse-vms/ and 
http://help.eclipse.org/help31/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/misc/bundle_manifest.html 
for details).

We explicitly register our plugins that contain the sqlmaps.
That means in the manifest of the iBatis plugin the buddy entry is 
Eclipse-BuddyPolicy: registered
and in the plugins containing the sqlmaps we have Eclipse-RegisterBuddy: 
com.ibatis

I recon that for your purpose the "dependent" or "global" setting might be 
helpful

HTH
Marc Heimann
Software Engineer

Prolifics Deutschland GmbH
Notkestr. 3, D-22607 Hamburg
phone +49 (0)40 890 667-70
fax    +49 (0)40 890 667-99
marc.heimann@prolifics.de
2007 IBM Award Winner for Overall Technical Excellence
SOA... Building Future Business Solutions Today
Handelsregister: Hamburg, HRB 89903
Geschäftsführer: Ulrich Frotscher


Maarten Meijer <mj...@xs4all.nl> wrote on 14.02.2008 16:55:39:

> Hi,
> 
> I'm working on Eclipse Mylyn bug 184532: [connector] Generic SQL 
> connector
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=184532
> 
> As I have some experience with Ibatis as database connection for a 
> servlet I try to use it here again.
> Benefits of ibatis for this bug:
> The XML configurable SQL result sets makes it possible to map from 
> almost any accessible database to the Mylyn task model.
> 
> I've succeeded in opening the SqlMapConfix.xml with parameters in a 
> properties file using the following code:
> 
> public class TasksSqlMapConfig {
>    private static SqlMapClient sqlMap = null;
> 
>    public static SqlMapClient getSqlMapInstance() throws CoreException {
>       if (null == sqlMap) {
>          try {
>             InputStream resource = null;
>             Path mapPath = new Path("maps/SqlMapConfig.xml");
>             resource = 
> FileLocator.openStream(IbatisCorePlugin.getDefault().getBundle(), 
> mapPath, false);
>             // replace with read from pref later
>             Path propPath = new Path("maps/db.properties");
>             InputStream propStream = 
> FileLocator.openStream(IbatisCorePlugin.getDefault().getBundle(), 
> propPath,
>                   false);
>             Properties properties = new Properties();
>             properties.load(propStream);
> 
>             sqlMap = SqlMapClientBuilder.buildSqlMapClient(resource, 
> properties);
>          } catch (Exception e) {
>             Status init = new Status(IStatus.ERROR, 
> IbatisCorePlugin.PLUGIN_ID, "Could not initialize SqlMaps", e);
>             StatusHandler.log(init);
>             throw new CoreException(init);
>          }
>       }
>       return sqlMap;
>    }
> 
>    private TasksSqlMapConfig() {
>       super();   // Singleton
>    }
> }
> 
> This will start to parse the XML alright and use the properties as 
> replacement. However when it is time to locate the Sqlmap.xml files 
> referenced in the SqlMapConfig.xml an error occurs. Eclipse bundles 
> all have their own class path so maybe ibatis has problems locating 
> the resource.
> 
> Is it possible to insert a replacement or additional class loader?
> I've tries using Resources.setDefaultClassLoader but this isn't used 
> anywhere, 
> likecom 
> .ibatis.common.resources.Resources.getResourceAsStream(ClassLoader, 
> String)
> 
> Is this option fully implemented? Is there a tutorial available, I 
> have "Ibaqtis in Action" but haven't found anything.
> 

Re: Using Ibatis as core for an Eclipse SQL plugin

Posted by Jeff Butler <je...@gmail.com>.
Calling setDefaultClassLoader should work.  It's not used on the method you
mention because you pass in a specific class loader.  So, call
setDefaultClassLoader before you do anything, then call the method overloads
that do not take a classloader as a parm - getResourceAsStream(String).

If no specific classloader is passed in, then iBATIS will use the default
class loader if one exists, or the thread context classloader.  This method
is the method used to load sqlMap files listed in the sqlMapConfig (unless
you list them by URL).

Confusing enough?

Jeff Butler

On Thu, Feb 14, 2008 at 9:55 AM, Maarten Meijer <mj...@xs4all.nl> wrote:

> Hi,
>
> I'm working on Eclipse Mylyn bug 184532: [connector] Generic SQL
> connector
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=184532
>
> As I have some experience with Ibatis as database connection for a
> servlet I try to use it here again.
> Benefits of ibatis for this bug:
> The XML configurable SQL result sets makes it possible to map from
> almost any accessible database to the Mylyn task model.
>
> I've succeeded in opening the SqlMapConfix.xml with parameters in a
> properties file using the following code:
>
> public class TasksSqlMapConfig {
>        private static SqlMapClient sqlMap = null;
>
>        public static SqlMapClient getSqlMapInstance() throws CoreException
> {
>                if (null == sqlMap) {
>                        try {
>                                InputStream resource = null;
>                                Path mapPath = new
> Path("maps/SqlMapConfig.xml");
>                                resource =
> FileLocator.openStream(IbatisCorePlugin.getDefault().getBundle(),
> mapPath, false);
>                                // replace with read from pref later
>                                Path propPath = new
> Path("maps/db.properties");
>                                InputStream propStream =
> FileLocator.openStream(IbatisCorePlugin.getDefault().getBundle(),
> propPath,
>                                                false);
>                                Properties properties = new Properties();
>                                properties.load(propStream);
>
>                                sqlMap =
> SqlMapClientBuilder.buildSqlMapClient(resource,
> properties);
>                        } catch (Exception e) {
>                                Status init = new Status(IStatus.ERROR,
> IbatisCorePlugin.PLUGIN_ID, "Could not initialize SqlMaps", e);
>                                StatusHandler.log(init);
>                                throw new CoreException(init);
>                        }
>                }
>                return sqlMap;
>        }
>
>        private TasksSqlMapConfig() {
>                super();        // Singleton
>        }
> }
>
> This will start to parse the XML alright and use the properties as
> replacement. However when it is time to locate the Sqlmap.xml files
> referenced in the SqlMapConfig.xml an error occurs. Eclipse bundles
> all have their own class path so maybe ibatis has problems locating
> the resource.
>
> Is it possible to insert a replacement or additional class loader?
> I've tries using Resources.setDefaultClassLoader but this isn't used
> anywhere,
> likecom
> .ibatis.common.resources.Resources.getResourceAsStream(ClassLoader,
> String)
>
> Is this option fully implemented? Is there a tutorial available, I
> have "Ibaqtis in Action" but haven't found anything.
>
>