什么是JAVA内容仓库(Java Content Repository)(3)

开发我们的例子程序
jackrabbit已经配置好了,现在让我们来创建我们的示例程序。这个例子程序将调用JCR-170 API。很显然,我们需要做两件事情:一个是作为后台的对数据进行增删改查(持久层),另一个是开发相对应的UI界面(WEB 层)。首先,让我们定义一个DAO接口。这个接口BlogEntryDAO.java 如下:
public   interface  BlogEntryDAO {
    
public   void  insertBlogEntry(BlogEntryDTO blogEntryDTO)
        
throws  BlogApplicationException;
    
public   void  updateBlogEntry(BlogEntryDTO blogEntryDTO)
        
throws  BlogApplicationException;
    
public  ArrayList getBlogList()
        
throws  BlogApplicationException;
    
public  BlogEntryDTO getBlogEntry(String blogTitle)
        
throws  BlogApplicationException;
    
public   void  removeBlogEntry(String blogTitle)
        
throws  BlogApplicationException;
    
public  ArrayList searchBlogList(String userName)
        
throws  BlogApplicationException;
    
public   void  attachFileToBlogEntry(String blogTitle, InputStream uploadInputStream)
        
throws  BlogApplicationException;
    
public  InputStream getAttachedFile(String blogTitle)
        
throws  BlogApplicationException;
}

正如你看到的,这个接口提供了增删改查的方法,同时还提供了两个方法来处理附件。接下来,我们需要一个DTO对象用来在两个层之间传递数据。
public   class  BlogEntryDTO {

    
private  String userName;
    
private  String title;
    
private  String blogContent;
    
private  Calendar creationTime;

    
// Getter and setter methods for each of these properties        
}

这里我们将仅仅讨论持久层。

连接jackrabbit
现在,第一件事情是开发一个组件,获得一个到jackrabbit内容仓库的连接。为了简单,我们将在程序启动的时候获得这个连接,然后在程序停止的时候释放这个连接。这里我们使用了Struts ,所以我们需要开发一个PlugIn 类。如下:
public   class  JackrabbitPlugin  implements  PlugIn{
    
public   static  Session session;
    
public   void  destroy() {
        session.logout();
    }
    
public   void  init(ActionServlet actionServlet, ModuleConfig moduleConfig) 
    
throws  ServletException {
        
try  {
            System.setProperty(
" org.apache.jackrabbit.repository.home " ,
                
" c:/temp/Blogging " );
            Repository repository 
=   new  TransientRepository();
            session 
=  repository.login( new  SimpleCredentials( " username " ,
                    
" password " .toCharArray()));
        } 
catch  (LoginException e) {
            
throw   new  ServletException(e);
        } 
catch  (IOException e) {
            
throw   new  ServletException(e);
        } 
catch  (RepositoryException e) {
            
throw   new  ServletException(e);            
        }
    }
    
public   static  Session getSession() {
        
return  session;
    }
}

init()方法将会在程序启动的时候调用,destroy()将会在程序停止的时候调用。我们在init()方法里获得了到jackrabbit内容仓库的连接。看看代码,我们做的第一件事是设定了org.apache.jackrabbit.repository.home这个系统属性,在上篇文章里提到,这个属性是用来指向我们的内容仓库主目录。这里我们设定它为c:/temp/blogging。接下来,我们创建了TransientRepository的一个实例。这是jackrabbit提供的类,它提供了一个到内容仓库的代理。它在第一个session 打开的时候自动启动内容仓库,在最后一个session 关闭的时候自动关闭内容仓库。
一旦我们获得了一个内容仓库对象,我们就可以调用它的login() 方法来打开一个连接。login() 方法需要一个Credential 对象作为参数。如果Credential 对象是NULL,jackrabbit会认为其他的机制做了这个验证(比如JAAS)。login() 方法还可以传入一个workspace名字作为参数,如果不传入这个参数,jackrabbit会返回一个session对象指向默认的workspace。注意workspace和session是一对一的,即一个session仅对应一个workspace。(注:如果不传入Credential对象,返回的session对workspace是只读的)

增加内容
连接已经建立起来了,下面让我们实现BlogEntryDAO这个接口。第一个我们想实现的方法是插入数据 insertBlogEntry()
public   void  insertBlogEntry(BlogEntryDTO blogEntryDTO)
            
throws  BlogApplicationException {
        Session session 
=  JackrabbitPlugin.getSession();
        Node rootNode 
=  session.getRootNode();
        Node blogEntry 
=  rootNode.addNode( " blogEntry " );
        blogEntry.setProperty(
" title " , blogEntryDTO.getTitle());
        blogEntry.setProperty(
" blogContent " , blogEntryDTO.getBlogContent());
        blogEntry.setProperty(
" creationTime " , blogEntryDTO.getCreationTime());
        blogEntry.setProperty(
" userName " , blogEntryDTO.getUserName());            
        session.save();
}


首先获得session 对象,即到内容仓库特定workspace的连接。然后,我们在这个session 对象上调用getRootNode() 方法,获得这个workspace的根节点,这个根节点的路径是("/").一旦我们获得这个根节点,我们就可以通过addNode()方法在这个根节点下增加新的子节点。新节点的名字是blogEntry. 通过setProperty() 方法我们把数据存储到节点的property里。正如我们先前说明的,真实的数据是存储在property元素里,property元素是叶子。
注意session.save() 这行代码。这个方法是必须调用的,这个方法调用之前,任何 Node,Property的改变都被保存在这个session的一个临时区域里,其他的和该session连接到相同workspace的session都看不到这些改变。当这个方法被调用并被成功执行后,这些Node,Property的改变才会被持久化到这个session关联的workspace里,同时所有与这个workspace关联的session才可见这些变化。相对应的,Session.refresh(false)将会丢弃所有这些改变。item.save()和Item.refresh(false)作用相似,只是影响范围限定在单个Item上(注意,包括它的子节点)
 

From: http://www.blogjava.net/RongHao/archive/2007/01/25/96065.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Day Management AG, Switzerland 11-May-2005 Release note Product: Specification for Content Repository for Java(tm) Technology API 1.0 Version: 1.0 Type: Full Release ====================================================================== 0. Index ---------------------------------------------------------------------- 1. Introduction 2. Release Contents 3. Copyright Notice 1. Introduction ---------------------------------------------------------------------- This Package contains the Specification for the Content Repository for Java(tm) Technology API. For questions or comments feel free to use jsr-170-comments@jcp.org Further information about JSR 170 can be found at http://www.jcp.org/en/jsr/detail?id=170 The Reference Implementation (RI) and the Technology Compatibility Kit (TCK) and the Specification Changelog may be downloaded from http://www.day.com/content/en/product/jsr_170.html 2. Release Contents ---------------------------------------------------------------------- All paths listed are relative to the installation directory of the ZIP file. This package consists of the following components: /docs generated Javadoc /lib pre-built API .jar-file /license Applicable License /spec Specification documents /src API Source code 3. Copyright notice ---------------------------------------------------------------------- Copyright ? 1993-2005 Day Management AG, Switzerland. All rights reserved. DAY, the DAY logo, Communiqu? ContentBus and CRX Content Repository Extreme are registered trademarks and service marks, or are trademarks and service marks, of Day Management AG, in various countries around the world. All other product names and company logos mentioned in the information, documents or other items provided or available herein may be the trademarks of their respective owners.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值