Jxta初始化过程(二)

这篇帖子重点讲NetPeerGroup的创建过程。其中包含两个问题:
  1. 缓存管理(Cm)是在哪里初始化的
  2. WorldPeerGroup是用哪个类初始化的
接上文,先看下面的代码
java 代码
 
  1. // create, and Start the default jxta NetPeerGroup  
  2. NetPeerGroupFactory factory = new NetPeerGroupFactory(config.getPlatformConfig(), instanceHome);  
  3. netPeerGroup = factory.getInterface();  
使用工厂模式实现的。在 NetPeerGroupFactory的构造函数中,首先创建了WorldPeerGroup(同样使用了工厂模式),随后使用创建了 WorldPeerGroup的子对等组NetPeerGroup
java 代码
  1. /** 
  2.  *  Constructs a Net Peer Group and the World Peer Group using the 
  3.  *  configuration specified by the provided ConfigParams and using the 
  4.  *  specified storeHome location for persistence. The resulting Net Peer  
  5.  *  Group instance will use the default ID, Name and Description. 
  6.  * 
  7.  *  @param config The configuration to use for the newly created World Peer 
  8.  *  Group and Net Peer Groups. 
  9.  *  @param storeHome The optional location that the World Peer Group, the 
  10.  *  Net Peer Group and its' services should use for storing persistent and 
  11.  *  transient information. May be {@code null} if the World Peer Group is  
  12.  *  not provided a persistent store (though this not currently supported). 
  13.  *  @throws PeerGroupException Thrown for problems constructing the Net Peer 
  14.  *  Group. 
  15.  **/  
  16. public NetPeerGroupFactory( ConfigParams config, URI storeHome ) throws PeerGroupException {  
  17.     WorldPeerGroupFactory world = new WorldPeerGroupFactory( config, storeHome );  
  18.     PeerGroup worldGroup = world.getInterface();  
  19.       
  20.     NetGroupTunables tunables = new NetGroupTunables( ResourceBundle.getBundle("net.jxta.impl.config"), new NetGroupTunables() );  
  21.       
  22.     try {  
  23.         net = newNetPeerGroup( worldGroup, tunables.id, tunables.name, tunables.desc, null );  
  24.     } finally {  
  25.         worldGroup.unref();  
  26.     }  
  27. }  
在WorldPeerGroupFactory的构造函数中,调用了getDefaultWorldPeerGroupClass方法通过配置文件net.jxta.impl.config.properties来获得WorldPeerGroup的类名称,并调用newWorldPeerGroup实例化该类。从配置文件 net.jxta.impl.config.properties中,我们看到用于实例化WorldPeerGroup的类名为net.jxta.impl.peergroup.Platform,这样回答了开篇的第二个问题。
java 代码
  1. /** 
  2.  *  Constructs the World Peer Group using the specified configuration and  
  3.  *  using the specified storeHome location for persistence. 
  4.  * 
  5.  *  @param config The configuration to use for the World Peer Group. 
  6.  *  @param storeHome The optional location that the World Peer Group and its' 
  7.  *  services should use for storing persistent and transient information. 
  8.  *  May be null if the World Peer Group is not provided a 
  9.  *  persistent store (though this not currently supported). 
  10.  *  @throws PeerGroupException Thrown for problems constructing the World 
  11.  *  Peer Group. 
  12.  */  
  13. public WorldPeerGroupFactory( ConfigParams config, URI storeHome ) throws PeerGroupException {  
  14.       
  15.     world = newWorldPeerGroup( getDefaultWorldPeerGroupClass(), config, storeHome );  
  16. }  
第一个问题,缓存管理初始化的具体地点位于Platform的直接父类StdPeerGroup的initFirst中,该方法在 newWorldPeerGroup方法中被调用(该调用关系稍微复杂且有些微妙)。
java 代码
 
  1. // initialize cm before starting services. Do not refer to assignedID, as it could be  
  2. // null, in which case the group ID has been generated automatically by super.initFirst()  
  3. try {  
  4.     cm = new Cm(getHomeThreadGroup(), getStoreHome(), getPeerGroupID().getUniqueValue().toString(), Cm.DEFAULT_GC_MAX_INTERVAL, false);  
  5. catch (Exception e) {  
  6.     if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {  
  7.         LOG.log(Level.SEVERE, "Error during creation of local store", e);  
  8.     }  
  9.     throw new PeerGroupException("Error during creation of local store", e);  
  10. }  
通过Cm的构造函数,可以看到如果硬盘上已存在缓存的实例化文件,那么会直接从硬盘上读取,否则重新创建。这也是为什么如果我们只调用DiscoveryService.flushAdvertisement方法(该方法仅作用于内存结构,不改变硬盘上的持久化文件)将某个adv删除后,而当程序重新启动后,该adv依然起作用的原因了(除非将.cache目录全部删除)。
java 代码
 
  1. rootDir = new File(ROOTDIRBASE, areaName);  
  2. rootDir = new File(rootDir.getAbsolutePath());  
  3. if (!rootDir.exists()) {  
  4.     // We need to create the directory  
  5.     if (!rootDir.mkdirs()) {  
  6.         throw new RuntimeException("Cm cannot create directory " + rootDir);  
  7.     }  
  8. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值