注:本文实现的思路不是原创,思路原文来自:
http://birtworld.blogspot.com/2005/12/using-supplied-connection-with-birt.html
本文将介绍BIRT2.2 详细的扩展与配置:
在ECLIPSE中新建一个工程 new-> Bussiness Intelligence Report Tools -> Web Project 名字:MyBirt
并设置好服务器信息(本文选用Tomcat5.5、JDK1.5)。
建好工程以后,需要添家JDBC驱动程序到报表引擎中,本例连接ORACLE,因此将ORACLE的JDBC驱动包复制到 工程站点下的 /WEB-INF/platform/plugins/org.eclipse.birt.report.data.oda.jdbc_xxxxxxx/drivers目录下。
接下来就是继承OdaJdbcDriver类了,再此之前需要在ClassPath中Add oda-jdbc.jar 和 org.eclipse.datatools.connectivity.oda_xxxxxxxxxxxx.jar这两个包,前者位于上边提到的drivers路径的上一级,后着则在plugins下。 新建一个Class:MyJdbcDriver,指定到某个包路径下,代码如下:
- package com.wenmin.birt.data.oda.jdbc;
-
- import org.eclipse.birt.report.data.oda.jdbc.OdaJdbcDriver;
- import java.sql.Connection;
- import java.util.HashMap;
- import java.util.Properties;
-
- import org.eclipse.birt.report.data.oda.jdbc.*;
- import org.eclipse.datatools.connectivity.oda.IConnection;
- import org.eclipse.datatools.connectivity.oda.OdaException;
-
- public class MyJdbcDriver extends OdaJdbcDriver
- {
-
- private Connection passedInConnection;
- public static final String DATASOURCE_KEY_CONNECTION = "com.wenmin.birt.data.odb.jdbc.MyJdbcDriver";
-
- public void setAppContext( Object context ) throws OdaException
- {
- HashMap ctx = (HashMap)context;
- passedInConnection = (java.sql.Connection)ctx.get(DATASOURCE_KEY_CONNECTION);
-
- }
-
- public IConnection getConnection(String connectionClassName) throws OdaException
- {
- if( passedInConnection != null){
- return new appContextDBConnection();
- }else{
- return new org.eclipse.birt.report.data.oda.jdbc.Connection();
- }
- }
-
-
-
- private class appContextDBConnection extends org.eclipse.birt.report.data.oda.jdbc.Connection
- {
-
-
- public void open(Properties connProperties) throws OdaException
- {
- super.jdbcConn = passedInConnection;
-
- }
-
- public void close( ) throws OdaException
- {
- if ( jdbcConn == null )
- {
- return;
- }
-
- jdbcConn = null;
- }
-
-
-
- }
- }
package com.wenmin.birt.data.oda.jdbc;
import org.eclipse.birt.report.data.oda.jdbc.OdaJdbcDriver;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Properties;
import org.eclipse.birt.report.data.oda.jdbc.*;
import org.eclipse.datatools.connectivity.oda.IConnection;
import org.eclipse.datatools.connectivity.oda.OdaException;
public class MyJdbcDriver extends OdaJdbcDriver
{
private Connection passedInConnection;
public static final String DATASOURCE_KEY_CONNECTION = "com.wenmin.birt.data.odb.jdbc.MyJdbcDriver";
public void setAppContext( Object context ) throws OdaException
{
HashMap ctx = (HashMap)context;
passedInConnection = (java.sql.Connection)ctx.get(DATASOURCE_KEY_CONNECTION);
}
public IConnection getConnection(String connectionClassName) throws OdaException
{
if( passedInConnection != null){
return new appContextDBConnection();
}else{
return new org.eclipse.birt.report.data.oda.jdbc.Connection();
}
}
private class appContextDBConnection extends org.eclipse.birt.report.data.oda.jdbc.Connection
{
public void open(Properties connProperties) throws OdaException
{
super.jdbcConn = passedInConnection;
}
public void close( ) throws OdaException
{
if ( jdbcConn == null )
{
return;
}
//should have call to return connection to the pool
jdbcConn = null;
}
}
}
OK,接下来我们来打个包,利用ECLIPSE EXPORT一个 jar包,(“怎么做?”,“。。。拖下去,暴头!”)。
本例导出包名字为:cst_driver.jar,拷贝到/WEB-INF/platform/plugins/org.eclipse.birt.report.data.oda.jdbc_xxxxxxx/目录下。
最后就是修改配置文件,第一个需要改的文件plugin.xml位于 /WEB-INF/platform/plugins/org.eclipse.birt.report.data.oda.jdbc_xxxxxxx/目录下
找到如下节点:
- ...
- <extension
- point="org.eclipse.datatools.connectivity.oda.dataSource">
- <dataSource
- odaVersion="3.1"
- driverClass="org.eclipse.birt.report.data.oda.jdbc.OdaJdbcDriver"
- defaultDisplayName="%datasource.name"
- setThreadContextClassLoader="false"
- id="%oda.data.source.id">
- ....
...
<extension
point="org.eclipse.datatools.connectivity.oda.dataSource">
<dataSource
odaVersion="3.1"
driverClass="org.eclipse.birt.report.data.oda.jdbc.OdaJdbcDriver"
defaultDisplayName="%datasource.name"
setThreadContextClassLoader="false"
id="%oda.data.source.id">
....
修改为:
- ...
- <extension
- point="org.eclipse.datatools.connectivity.oda.dataSource">
- <dataSource
- odaVersion="3.1"
- driverClass="com.wenmin.birt.data.oda.jdbc.MyJdbcDriver"
- defaultDisplayName="%datasource.name"
- setThreadContextClassLoader="false"
- id="%oda.data.source.id">
- ...
...
<extension
point="org.eclipse.datatools.connectivity.oda.dataSource">
<dataSource
odaVersion="3.1"
driverClass="com.wenmin.birt.data.oda.jdbc.MyJdbcDriver"
defaultDisplayName="%datasource.name"
setThreadContextClassLoader="false"
id="%oda.data.source.id">
...
其他内容不需要改变。
然后在META-INF下的MANIFEST.MF中找到如下位置,添加刚才导出的包cst_driver.jar
- Bundle-ClassPath: oda-jdbc.jar,derby.jar,cst_driver.jar
Bundle-ClassPath: oda-jdbc.jar,derby.jar,cst_driver.jar
大功告成,接下来在程序中使用:
-
- HashMap contextMap = new HashMap();
//....
HashMap contextMap = new HashMap();
-
- contextMap.put(MyJdbcDriver.DATASOURCE_KEY_CONNECTION, conn);
- task.setAppContext( contextMap );
- .
//...
contextMap.put(MyJdbcDriver.DATASOURCE_KEY_CONNECTION, conn); //conn为系统中获得的数据库连接
task.setAppContext( contextMap ); //IRunAndRenderTask
//...
这样,无论在设计时指定了什么数据源,都会在运行时通过已有的conn连接数据库了。
由于是这两天才接触的BIRT,文章有不足之处还请多多指正,谢谢。