IBM FileNet Content Java API 簡介

2008 年 6 月 24 日

 

原文地址: http://www.ibm.com/developerworks/cn/data/library/techarticles/dm-0806wangys/

本文介紹 IBM FileNet P8 4.0 Platform 提供的 Content Java API。首先對 FileNet P8 Content Engine 和 API 進行概要介紹, 並說明了一些基本概念,隨後詳細介紹了 FileNet Content Engine提供的基於 EJB 和 Web Service 的應用程序開發接口。藉助本文,讀者可以輕鬆的掌握 P8 Content Engine API 的使用方法,並通過運行和閱讀本文提供的例子來了解各個 API 的功能和使用方法。

FileNet Content Engine 概述

IBM FileNet Content Engine 是 IBM FileNet P8 平臺上負責內容管理的核心解決方案。企業數據可以分爲三類:

 

  • 結構化數據(Structured Data)- 被數據庫應用程序嚴格管理的數據。例如關係數據庫中的數據或 ERP 系統中的數據。
  • 半結構化數據(Semi-structured Data)- 被數據庫應用程序進行鬆散管理的數據 . 例如 Email, Messaging。
  • 非結構化數據(Unstructured Data)- 不被任何應用程序或數據服務器管理和控制的數據 . 例如員工的家庭住址 , 傳真文件 , Microsoft word 文檔等。

 

FileNet Content Engine 對這三類數據進行統一集中式管理,從而提供企業內容管理(ECM – Enterprise Content Management)解決方案。具體功能包括:對物理數據建立邏輯索引;對內容的搜索和存取;版本管理以及不同層次的安全性管理等。同時,它的文檔管理功能與 FileNet Process Manager 相結合 , 還可以實現企業工作流和業務過程的自動化實現和管理 .

基本概念

這裏介紹一些 FileNet Content Engine 中的基本概念,將有助於讀者閱讀和理解下面的內容。

FileNet P8 Domain

FileNet P8 Domain 是一個邏輯上的集合,它包含一系列物理資源(比如 Object Store 等),還包含 Content Engine 服務器,客戶端需要通過這些服務器提供的服務來訪問同一個 Domain 中的物理資源。同時它還關聯到一個或多個 security realm,來完成對用戶的身份認證和權限控制。FileNet 中的任一資源,必須屬於並只屬於一個 Domain。

Object Store

一個 Object Store 是一個獨立的、存在於 Domain 的對象。它提供了對資源的訪問和存取,這裏的資源包括 documents,folders, custom objects, class descriptions, property descriptions, security policies 等等,以及有關這些資源的元數據。Object Store 代表了這些資源在 server 上的存儲位置。藉助 Object Store 對象提供的方法,開發者可以對 Content Engine Server 中的資源進行操作。

Document

Document 對象表示存在於 Content Engine Object Store 中的一個唯一版本的文檔。Document 可以被理解成以下兩種形式:

  • 用戶創建的文檔:例如 Microsoft Word 文檔、圖片、HTML 文件等等。
  • 包含用戶文檔信息的對象:在 Content Engine 中用這個對象來唯一表示一個特定版本的用戶文檔。

一個 document 可以有零或多個內容元素。如果一個 document 包含內容,它的內容可以存儲於 Content Engine 服務器(稱爲 content transfer element),也可以存貯於外部(稱爲 content reference element)。

 




回頁首


FileNet Content Engine Java APIs 概述

如何在自己的 JAVA 應用程序中訪問 FileNet 內容引擎並對其中的內容進行增刪改查的操作?FileNet 提供了基於 JAVA 的一整套應用程序接口 (API),藉助這些 API,開發者可以完成對 Content Engine Server 的所有操作。這些操作根據應用場景的不同可以分爲三類:

  • Runtime Application: 這類應用程序在運行時與 Content Engine Server 中的 Object Store 和 Documents 進行互操作。
  • Administration Application: 主要負責完成對 server 的配置。
  • Metadata Authoring Application: 對原數據進行操作,例如定義 Class, 創建 properties 等。

本文中的例子主要針對 Runtime Application 類的 API,也是最常用的這一類接口進行介紹。

FileNet Content Engine 支持兩種通訊協議,EJB 和 Content Engine Web Service (CEWS)。下圖說明了這些協議在 Content Engine 中的轉換和交互,讀者可以對 FileNet Content Engine 對外提供的接口及內部的實現機制有一個架構上的理解。


圖 1:Content Engine 應用程序接口架構
Content Engine 應用程序接口架構

開發者可以選擇任意一種通訊協議與 FileNet Content Engine server 建立連接。接口開發包中的 Connection 對象代表客戶端與 FileNet P8 Domain 之間的一個邏輯連接,連接中包含的 URI(Universal Resource Identifier)表明了這個連接的目標服務器地址、端口號以及所採用的通訊協議(EJB 或 CEWS)。

EJB

Content Engine 通過 J2EE 應用服務器中的 EJB 容器對外暴露其功能。Content Engine Java API 通過 EJB 協議與 server 之間通過傳遞串行化的 Java 對象發送請求並接收響應。基於 EJB 協議的 Java API 是目前可用的最快的交互方式。

EJB 監聽器提供了本地(Local)和遠程(Remote)接口。對於那些與 Content Engine EAR 包綁定在一起 J2EE 組件(位於一個 EAR 包裏),可以直接使用本地接口(local interface)。其他的遠程調用則可以通過遠程接口(remote interface)來實現。Content Engine API 利用 EJB 藉口,通過 J2EE 服務器提供的安全性機制對用戶透明的實現了自動安全性身份認證和事務管理。

CEWS

Content Engine Web Service(CEWS)基於 SOAP 協議與 Content Engine 服務器進行交互。CEWS 通過 HTTP 協議將 XML 格式的請求發送到 Content Engine 服務器,並接收 XML 格式的響應。

Web Service 監聽器由一個 Servlet 實現,在 J2EE 應用服務器的 web container 中運行。除了不能完成對事務的管理,CEWS 與 EJB 提供了完全相同的功能。Web Service Listener 接收到來自客戶端的請求後,將這個 request 轉發到 EJB 層,並通過 EJB 與 Content Engine server 進行互操作。

 




回頁首


基於 EJB 的 Java API 示例

這一節我們用幾個例子來說明如何使用 FileNet Java EJB API 對 CE 進行添加 document,讀取 document 內容,修改屬性以及查詢 document 等操作。示例中 FileNet CE 和示例程序均安裝在 WebSphere 應用服務器上,且這些 WAS Server 必須共用同一個 LDAP 作爲其用戶註冊 (user registry)。


圖 2:示例工程拓撲
示例工程拓撲

使用 Content Engine Java API 在運行時需要一些 JAR 包 . 這些 jar 包可以在 FileNet 的安裝目錄下找到。定製程序需要將這些 jar 文件包含在其 classpath 中。

表 1. 使用 EJB 傳輸協議的 Content Engine Java API 客戶端所需的 jar 包


清單 1.

                
<security-role>
  <role-name>CEUser</role-name>
</security-role>



圖 3:爲角色分配用戶或組
爲角色分配用戶或組

清單 2. 與 FileNet 建立連接

清單 3.向 Content Engine 添加文檔並修改其屬性

//create new document
Document doc = Factory.Document.createInstance(this.objectStore, "document");
//assemble name
String documentTitle = "dWSample";
//set properties
doc.getProperties().putValue("documenttitle", documentTitle);
// get input Stream from the instantiated object of the document you will save.
ByteArrayInputStream input = new ByteArrayInputStream(myDoc.getContent());
ContentTransfer ct = Factory.ContentTransfer.createInstance(this.objectStore);
ct.setCaptureSource(input);
// Set the document MIME type
ct.set_ContentType("application/pdf");
ct.set_RetrievalName(documentTitle);
ContentElementList cel = Factory.ContentElement.createList();
cel.add(ct);
doc.set_ContentElements(cel);
doc.checkin(AutoClassify.DO_NOT_AUTO_CLASSIFY, CheckinType.MAJOR_VERSION);
doc.save(RefreshMode.REFRESH);
...

從以上示例中可以看出 Document 是一個物理文件的集合(ContentElementList),我們可以爲一個 Document 對象添加多個文件。

清單 4.獲取文件內容
/*retrieve document content by documentId. */
Document doc = Factory.Document.fetchInstance(this.objectStore, new Id(documentId), null);
ContentElementList cel = doc.get_ContentElements();
ContentTransfer ct = (ContentTransfer) cel.get(0);
int contentSize = ct.get_ContentSize().intValue();
InputStream content = ct.accessContentStream();
byte[] output = new byte[contentSize];
content.read(output);



清單 5.查詢
                
String sqlText ="SELECT * FROM [BatchReport] WHERE ([ReportType] = 'xxx' 
AND [ReportName] = 'xx') ORDER BY [DateCreated]";

//preform query
SearchSQL sql = new SearchSQL(sqlText);
SearchScope search = new SearchScope(this.objectStore);
IndependentObjectSet ios = search.fetchObjects(sql, null, null, null);
if(ios.isEmpty()){
   logger.log(Level.FINE,"no reports found");
}
Iterator docIt = ios.iterator();
...

 

SQL 語句的構造可以藉助 FileNet Content Engine Enterprise Manager 中的 Content Engine Query Builder 提供的功能。利用 Query Builder 的 Simple View 下提供的圖形界面進行 SQL 語句構造,再轉到 SQL View 下進行修改和調整,如去掉不需要查詢的屬性。


圖 4:使用 Query Builder 的 Simple View
使用 Query Builder 的 Simple View

圖 5:使用 Query Builder 的 SQL View
使用 Query Builder 的 SQL View




回頁首


基於 Web Service 的 Java API 示例

使用 FileNet Java EJB API 或者 FileNet Java Web Service API 均可以在應用服務器環境中開發基於 web 或 EJB 的客戶應用程序。但如果客戶應用程序爲獨立的 Java 程序,因爲缺少了應用服務器的支持,只能使用 FileNet Java Web Service API 進行開發。下面是一個使用 FileNet CEWS API 進行文檔創建、上傳、和保存的例子。例子中使用 IBM Rational® Application Developer V6.0 生成的 web client stub。在運行時需要將 WebShpere 應用服務器運行時環境加入該項目。


清單 6. 使用 CEWS API 創建並保存文件

                
String strRetrievalName = "createDocumentContent";
// We first need to create a Create verb, populate it
CheckinAction verbCheckin = new CheckinAction();
CreateAction createVerb = new CreateAction();
createVerb.setAutoUniqueContainmentName(new Boolean(true));
createVerb.setClassId(docClass);

ChangeRequestType objChange = new ChangeRequestType();
objChange.setAction(new ActionType[2]);
objChange.setAction(0,(ActionType)createVerb);
objChange.setAction(1,(ActionType)verbCheckin);
……
// Build a list of properties to set in the new document, 
//also set properties and content data into stub. 
ModifiablePropertyType[] objInputProps = new ModifiablePropertyType[lngPropCount];
SingletonString objString = new SingletonString();
……
// Send off the request
ChangeResponseType[] objResponseArray = null;
ExecuteChangesRequest objRequest = new ExecuteChangesRequest();
objRequest.setRefresh(new Boolean(true));
objRequest.setChangeRequest(new ChangeRequestType[1]);
objRequest.setChangeRequest(0,objChange);
try
{
	if(fnStub instanceof FNCEWS35SoapBindingStub){
		FNCEWS35SoapBindingStub fnSoapStub = (FNCEWS35SoapBindingStub)fnStub;
		fnSoapStub.setUsername(userAccount);
		fnSoapStub.setPassword(userPassword);
		objResponseArray = fnSoapStub.executeChanges(objRequest);
	}else{
		throw new Error("Unable to locate a correct binding.");
	}
}catch(Exception ex){……}
return true;

 

由於 WebSphere 客戶端不支持 MIME 和 DIME 附件,所以上傳的文件必須使用 InlineContent 類型 . 下面是一個用 CEWS 構造出的 SOAP 報文的實例。


清單 7. 與 FileNet 建立連接

                
<soapenv:Header>
  <Security xmlns="http://schemas.xmlsoap.org/ws/2002/12/secext">
    <hd:UsernameToken xmlns:hd="http://schemas.xmlsoap.org/ws/2002/12/secext">
      <hd:Username>jim</hd:Username> 
      <hd:Password>pwd4jim</hd:Password> 
    </hd:UsernameToken>
  </Security>
……
</soapenv:Header>

<soapenv:Body>
  <p596:ExecuteChangesRequest refresh="1" 
  xmlns:p596="http://www.filenet.com/ns/fnce/2005/02/ws/schema">
    <p596:ChangeRequest id="1">
      <p596:TargetSpecification classId="ObjectStore" objectId="CEStore" /> 
      <p596:Action xsi:type="p596:CreateAction" classId="Document" 
        autoUniqueContainmentName="1" 
        xmlns:p596="http://www.filenet.com/ns/fnce/2005/02/ws/schema" /> 
      <p596:Action xsi:type="p596:CheckinAction" 
        xmlns:p596="http://www.filenet.com/ns/fnce/2005/02/ws/schema" /> 
<p596:ActionProperties xmlns:p596="http://www.filenet.com/ns/fnce/2005/02/ws/schema">
        <p596:Property xsi:type="p596:SingletonString" propertyId="DocumentTitle">
          <p596:Value>dWSample</p596:Value> 
        </p596:Property>
        <p596:Property xsi:type="p596:ListOfObject" propertyId="ContentElements">
          <p596:Value classId="ContentTransfer" dependentAction="Insert">
            <p596:Property xsi:type="p596:SingletonString" propertyId="ContentType">
                 <p596:Value>text/plain</p596:Value> 
            </p596:Property>
            <p596:Property xsi:type="p596:SingletonString" propertyId="RetrievalName">
                 <p596:Value>createDocumentContent</p596:Value> 
            </p596:Property>
            <p596:Property xsi:type="p596:ContentData" propertyId="Content">
                 <p596:Value xsi:type="p596:InlineContent">
                <p596:Binary>DQpUaGlzIHByb2dyYW0gaXMgZ</p596:Binary> 
                 </p596:Value>
            </p596:Property>
          </p596:Value>
        </p596:Property>
      </p596:ActionProperties>
            ……
    </p596:ChangeRequest>
  </p596:ExecuteChangesRequest>
</soapenv:Body>





回頁首


結束語

本文介紹了 IBM FileNet Content Engine 的一些基本概念和架構,重點說明了基於 EJB 和 CEWS 應用程序開發接口的用法。文中提到的全部源代碼都可在文末的下載專區中獲得。希望本文對你的開發工作有所幫助。

 




回頁首


參考資源


作者簡介

 

王亦帥,具有8年Java開發經驗。2006年加入IBM CDL部門,參與設計和開發J2EE應用程序。作者曾參與《數字圖書館》一書的寫作並於2003年出版。

 

 

程煒,主要從事構建於J2EE平臺的行業解決方案的開發。

//construct connection uri
String uriStr = new String("iiop://<host>:<port>");
 
//get connection
Connection conn = Factory.Connection.getConnection(uriStr);
 
//get Domain object;
Domain domain = Factory.Domain.fetchInstance(conn,"domainName", null);
 
//get ObjectStore object
ObjectStore os = Factory.ObjectStore.fetchInstance(domain, "objectStoreName", null);
 
            

 

FileNet CE API 提供了一個 Connection 對象,所有的 API 調用都通過這個對象進行傳遞,同時它還以對用戶透明的方式處理調用時的身份認證。<host> 爲 FileNet CE 所在的 WAS server 機器名或 IP 地址,<port> 通常爲 WebSphere application server 的 bootstrap 端口。在獲得 Connection 對象之後可以根據名字進而獲得 Domain 對象和 ObjectStore 對象,ObjectStore 對象是其他文檔操作的參數。在獲得 Connection 對象後並不意味着與 FileNet CE 的連接已經成功,只有在成功獲得 Domain 對象之後才能確定。

Jace.jar FileNet Content Engine Java API 核心 JAR 包
log4j-1.2.13.jar The LOG4J framework for Content Engine Java API logging.

 

示例程序是一個 web 應用,在該應用中需要首先定義一個角色用以訪問 FileNet CE,程序部署後需要爲該角色分配對應的用戶或組。這樣該應用程序將使用應用服務器提供的安全機制訪問 CE 服務器進行身份認證和操作鑑權。分配的用戶或組必須存在於將要訪問的 FileNet P8 Domain 的 Security Realm 中,且具有相應的操作權限。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章