注:本文實現的思路不是原創,思路原文來自:
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;
- }
- //should have call to return connection to the pool
- 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="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
大功告成,接下來在程序中使用:
- //....
- HashMap contextMap = new HashMap();
//....
HashMap contextMap = new HashMap();
- //...
- contextMap.put(MyJdbcDriver.DATASOURCE_KEY_CONNECTION, conn); //conn爲系統中獲得的數據庫連接
- task.setAppContext( contextMap ); //IRunAndRenderTask
- .
//...
contextMap.put(MyJdbcDriver.DATASOURCE_KEY_CONNECTION, conn); //conn爲系統中獲得的數據庫連接
task.setAppContext( contextMap ); //IRunAndRenderTask
//...
這樣,無論在設計時指定了什麼數據源,都會在運行時通過已有的conn連接數據庫了。
由於是這兩天才接觸的BIRT,文章有不足之處還請多多指正,謝謝。