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