Model with the Eclipse Modeling Framework(EMF)

Just what is EMF anyway?

 

The Eclipse Modeling Framework (EMF) is an open source framework targeting Model-Driven Architecture (MDA) development. For the few of us lucky enough to get a UML model, it can help us turn the documentation into code. For the rest of us, it is another tool to convince your boss that spending the time to model your solution can really pay off. In addition to generating Java code with all sorts of bells and whistles, EMF can also generate Eclipse plug-ins and graphical, customizable editors. When you change your model (it happens, really), the EMF can keep the code synchronized to your model at the click of a button.

The EMF-generated code is no throw-away solution either. It supports the standard create, retrieve, update, and delete operations, and it also supports cardinality constraints, complex relationships and inheritance structures, containment definitions, and a suite of attribute descriptions. The generated code provides notification, referential integrity, and customizable persistence to XMI. All you have to do is create an object model, which you probably wanted to do anyway.

EMF is relatively new, but it shows promise and there are good signs for continued support. It is an implementation of a public standard, the Object Management Group’s Meta-Object Facility (MOF), and it now supports an enhancement of version 2. Further, EMF is the basis of the Eclipse projects EMF:XSD and Hyades, and is used by most of the IBM® WebSphere® Studio products. Version 2 development has already begun, and development builds should be coming out soon. The plans include better XML Schema support, more flexible code generation, and mapping between models.


 

Posted in Eclipse Plugin/RCP | 1 Comment

BMP(Bean Managed Persistency)调用过程

BMP是EJB学习中比较吃力的一环,主要难点在与EntityBean中的callback()方法并不是由client直接调用的,所以要掌握BMP必须要理解容器的底层调用过程。
 
今天抽空把整个过程整理了一下,应该比较详细了。
 
 
BMP 服务器调用过程及生命周期
 
EJB服务器: Weblogic 8.x
pooling设置: max 3, init 1
cach设置: max 2
 
 
客户端main()方法调用过程
public static void main(String[] args) throws RemoteException,
      FinderException, CreateException {

    AccountTestClient1 client = new AccountTestClient1();
    AccountRemoteHome home = client.accountRemoteHome;
   
//Step.1
    AccountRemote acc1 = home.create("acc1","QiuTian",3000);

 

//Step.2
    AccountRemote acc2 = home.create("acc2","QiuTian",4000);

 

//Step.3
    AccountRemote acc3 = home.create("acc3","QiuTian",4000);

 

//Step.4
    AccountRemote acc4 = home.create("acc4","QiuTian",4000);

 

//Step.5

    acc1.getAccountno();

 

//Step.6
    acc2.getAccountno();

 

//Step.7
    AccountRemote acc5 = home.findByPrimaryKey(new AccountPK("acc4"));

 

//Step.8
    acc5.deposit(300);

 

//Step.9
    acc3.withdraw(300);

   
  }
}
 
 
 
 
 
 
服务器端控制台输出及注释

 

//instance(1)实例化
accountBeans.Account_6870og_Impl@1b31c23 null newInstance() invoked
accountBeans.Account_6870og_Impl@1b31c23 null setEntityContext() invo

//此时pool中存在实例 (1)
 
 
Step.1
//create方法被调用,创建新的acc1
//(1)被调入cache,同acc1绑定,成为准备状态
//此时pool中存在实例 none
//此时cache中存在实例(1)acc1

accountBeans.Account_6870og_Impl@1b31c23 acc1 ejbCreate() invoked
accountBeans.Account_6870og_Impl@1b31c23 acc1 setAccountno() invoked
accountBeans.Account_6870og_Impl@1b31c23 acc1 ejbPostCreate() invoked
accountBeans.Account_6870og_Impl@1b31c23 acc1 ejbStore() invoked
accountBeans.Account_6870og_Impl@1b31c23 acc1 getAccountno() invoked
End Step.1
//instance(2)实例化
//此时pool中存在实例 (2)
//此时cache中存在实例 (1)

accountBeans.Account_6870og_Impl@1b81d4f null newInstance() invoked
accountBeans.Account_6870og_Impl@1b81d4f null setEntityContext() invo
 
Step.2
//create方法被调用,创建新的acc2
//(2)被调入cache,同acc2绑定,成为准备状态

//此时pool中存在实例 none
//此时cache中存在实例(1)acc1(2)acc2

accountBeans.Account_6870og_Impl@1b81d4f acc2 ejbCreate() invoked
accountBeans.Account_6870og_Impl@1b81d4f acc2 setAccountno() invoked
accountBeans.Account_6870og_Impl@1b81d4f acc2 ejbPostCreate() invoked
accountBeans.Account_6870og_Impl@1b81d4f acc2 ejbStore() invoked
accountBeans.Account_6870og_Impl@1b81d4f acc2 getAccountno() invoked
End Step.2
 
 
//instance(3)实例化

//此时pool中存在实例 (3)
//此时cache中存在实例 (1)acc1(2)acc2

accountBeans.Account_6870og_Impl@fa385 null newInstance() invoked
accountBeans.Account_6870og_Impl@fa385 null setEntityContext() invoke
 
Step.3
//create方法被调用,创建新的acc3
//(3)被调入cache,同acc3绑定,发现cache已满,容器根据LRU规则将(1)passivate到pool中

//此时pool中存在实例 (1)acc1
//此时cache中存在实例(2)acc2(3)acc3

accountBeans.Account_6870og_Impl@fa385 acc3 ejbCreate() invoked
accountBeans.Account_6870og_Impl@fa385 acc3 setAccountno() invoked
accountBeans.Account_6870og_Impl@1b31c23 acc1 ejbPassivate() invoked
accountBeans.Account_6870og_Impl@fa385 acc3 ejbPostCreate() invoked
accountBeans.Account_6870og_Impl@fa385 acc3 ejbStore() invoked
accountBeans.Account_6870og_Impl@fa385 acc3 getAccountno() invoked
End Step.3
 
Step.4
//create方法被调用,创建新的acc4
//(1)被调入cache,同acc4绑定,发现cache已满,容器根据LRU规则将(2)passivate到pool中

//此时pool中存在实例 (2)acc2
//此时cache中存在实例(1)acc4(3)acc3

accountBeans.Account_6870og_Impl@1b31c23 acc4 ejbCreate() invoked
accountBeans.Account_6870og_Impl@1b31c23 acc4 setAccountno() invoked
accountBeans.Account_6870og_Impl@1b81d4f acc2 ejbPassivate() invoked
accountBeans.Account_6870og_Impl@1b31c23 acc4 ejbPostCreate() invoked
accountBeans.Account_6870og_Impl@1b31c23 acc4 ejbStore() invoked
accountBeans.Account_6870og_Impl@1b31c23 acc4 getAccountno() invoked
End Step.4
 
Step.5
//调用acc1.getAccountno()
//发现acc1已经不在cache中,(2)被Activate,将(2)同acc1绑定并调用load方法
//(2)被调入cache,同acc1绑定,发现cache已满,容器根据LRU规则将(3)passivate到pool中
//business method被调用
//store被调用
 
//此时pool中存在实例 (3)acc3
//此时cache中存在实例(2)acc1(1)acc4

accountBeans.Account_6870og_Impl@1b81d4f acc2 ejbActivate() invoked
accountBeans.Account_6870og_Impl@1b81d4f acc1 setAccountno() invoked
accountBeans.Account_6870og_Impl@1b81d4f acc1 ejbLoade() invoked
accountBeans.Account_6870og_Impl@fa385 acc3 ejbPassivate() invoked
accountBeans.Account_6870og_Impl@1b81d4f acc1 getAccountno() invoked
accountBeans.Account_6870og_Impl@1b81d4f acc1 ejbStore() invoked
accountBeans.Account_6870og_Impl@1b81d4f acc1 getAccountno() invoked
End Step.5
 
Step.6 
//调用acc2.getAccountno()
//发现acc2已经不在cache中,(3)被Activate,将(3)同acc2绑定并调用load方法
//(3)acc2被调入cache,发现cache已满,容器根据LRU规则将(1)passivate到pool中
//business method被调用
//store被调用
 
//此时pool中存在实例 (1)acc4
//此时cache中存在实例(3)acc2(2)acc1

accountBeans.Account_6870og_Impl@fa385 acc3 ejbActivate() invoked
accountBeans.Account_6870og_Impl@fa385 acc2 setAccountno() invoked
accountBeans.Account_6870og_Impl@fa385 acc2 ejbLoade() invoked
accountBeans.Account_6870og_Impl@1b31c23 acc4 ejbPassivate() invoked
accountBeans.Account_6870og_Impl@fa385 acc2 getAccountno() invoked
accountBeans.Account_6870og_Impl@fa385 acc2 ejbStore() invoked
accountBeans.Account_6870og_Impl@fa385 acc2 getAccountno() invoked
End Step.6
 
Step.7
//findByPrimaryKey("acc4")方法被调用
//在数据库中查找acc4,找到后
//(1)被Activate,将(1)同acc4绑定
//调用load方法重新构造acc4实例
//(1)acc4被调入cache,发现cache已满,容器根据LRU规则将(2)passivate到pool中
//此时pool中存在实例 (2)acc1
//此时cache中存在实例(1)acc4(3)acc2

accountBeans.Account_6870og_Impl@1b31c23 acc4 findByPrimaryKey() invo
accountBeans.Account_6870og_Impl@1b31c23 acc4 ejbActivate() invoked
accountBeans.Account_6870og_Impl@1b31c23 acc4 setAccountno() invoked
accountBeans.Account_6870og_Impl@1b31c23 acc4 ejbLoade() invoked
accountBeans.Account_6870og_Impl@1b81d4f acc1 ejbPassivate() invoked
End Step.7
 
Step.8
//调用acc5.deposit()
//store()被调用

accountBeans.Account_6870og_Impl@1b31c23 acc4 deposit() invoked
accountBeans.Account_6870og_Impl@1b31c23 acc4 ejbStore() invoked
accountBeans.Account_6870og_Impl@1b31c23 acc4 getAccountno() invoked
End Step.8
 
 
Step.9
//调用acc3.withdraw()
//发现acc3已不在cache中
//(2)被Activate,将(2)同acc3绑定并调用load方法
//(2)acc3被调入cache,发现cache已满,容器根据LRU规则将(3)passivate到pool中
//business method withdraw()被调用
//store被调用
 
//此时pool中存在实例 (3)acc2
//此时cache中存在实例(2)acc3(1)acc4

accountBeans.Account_6870og_Impl@1b81d4f acc1 ejbActivate() invoked
accountBeans.Account_6870og_Impl@1b81d4f acc3 setAccountno() invoked
accountBeans.Account_6870og_Impl@1b81d4f acc3 ejbLoade() invoked
accountBeans.Account_6870og_Impl@fa385 acc2 ejbPassivate() invoked
accountBeans.Account_6870og_Impl@1b81d4f acc3 withdraw() invoked
accountBeans.Account_6870og_Impl@1b81d4f acc3 ejbStore() invoked
accountBeans.Account_6870og_Impl@1b81d4f acc3 getAccountno() invoked
End Step.9
 

Posted in J2EE | 3 Comments

jsp乱码问题解答

通常来说,当请求递交到服务器端–>服务器端由servlet获得param–>执行相应的process(包括对数据库的操作)—>返回response显示给用户过程中 如果字符集不同会导致乱码问题。

当然,编码冲突主要因iso8859等西欧字符集和GBK或big5以及其他一些亚洲字符编码规则不同导致。但对于应用开发人员来说, 死抠编码解码规则没有太大意义,相比较而言,熟悉掌握整个http request/response过程中经过哪些字符/字节转换才会对乱码问题有更进一步的理解。以下是我结合实际问题并查阅相关文档后所总结的一些经验:

  1. 客户端通过表单提交的请求(参数)。这里的参数编码规则是由定义在html或jsp页面头部directives决定的,如果没有特别指定则默认为iso-8859-1服务器端接受请求,在某servlet中(jsp归结到底也是servlet)获得请求参数并进行相应逻辑操作。最常用的是request.getParameter(); 方法。 如果不加任何指定,这一方法其实完成了如下的几个工作 a.从请求报文中拿到参数的byte[] b.按照默认编码规则解码(iso-8859-1)得到一个String字符串
  2. 可能进行对数据库的操作,(增删改查),这里要注意同数据库的字符集统一,各数据库察看字符集的方法不同,一般都是通过查看数据字典获得
  3. 返回response给用户, 客户浏览器根据response的contentType属性制定的charset来显示response

那么为何有时候会出现乱码问题呢?

e.g: 从一个指定为gb2312编码的页面提交到服务器端,而服务器段直接调用request.getParameter()方法拿到字符串s,那么这个字符串直接返回到response一定会显示为乱码,因为原来的byte[]是gb2312的编码,用iso8859-1解码是一定会出乱码的。把字符串存入数据库和从数据库中取出字符串概念也是一样,如果数据库字符集是ascii那么rs.getString()拿到的字符串也是按ascii解码的,直接返回到response为gb2312的页面也会显示乱码.

解决方法通常有以下几种:

  1. 统一所有的字符集编码,推荐(utf-8),可创建全局过滤器
    在过滤器中过滤所有的request
    request.setCharacterEncoding("UTF-8")(需要与客户端浏览器的字符集编码统一)
    chain.doFilter(request,response);
    这里可以解决服务器段获取参数时编码冲突的问题,过滤后再调用request.getParameter()时在当前环境下就不会产生编码冲突
  2. 可修改相应服务器配置目录下的server.xml,以Tomcat为例: URIENcoding 设成UTF-8,原理同1
  3. 结合使用new String(byte[] byte, String charset) 和byte[] String.getBytes(String charset)方法。但要注意前者是解码(从字节流得到字符串)后者是编码(将字符串重新编码为字节流)
    举例来说,如果所拿到的字符串s是按iso-8859-1解码的,而想存入字符集为gb2312的数据库那么需要做如下转换String newS = new String(s.getBytes(‘ISO-8859-1"),"gb2312")以保证拿到gb2312码的字符串,这样就不会出现由于调用数据库操作而导致的字符集问题。

    Posted in J2EE | 3 Comments

    哦哟哟

    突然发现msn space还有点技术含量
     
    有很多想法,等有空来实现看看
     
    Posted in Uncategorized | Leave a comment