HfClient的创建步骤有三个:
- HFClient.createNewInstance()
- HFClient.setCryptoSuite()
- HFClient.setUserContext()
1. HFClient.createNewInstance():主要是初始化了一个核心线程为0,最大线程为Max_Integer的线程池
这个方法调用了一下HFClient的私有构造方法
/**
* createNewInstance create a new instance of the HFClient
*
* @return client
*/
public static HFClient createNewInstance() {
return new HFClient();
}
私有构造方法中是一个线程池的初始化
private HFClient() {
executorService = new ThreadPoolExecutor(CLIENT_THREAD_EXECUTOR_COREPOOLSIZE, CLIENT_THREAD_EXECUTOR_MAXIMUMPOOLSIZE,
CLIENT_THREAD_EXECUTOR_KEEPALIVETIME, CLIENT_THREAD_EXECUTOR_KEEPALIVETIMEUNIT,
new SynchronousQueue<Runnable>(),
r -> {
Thread t = threadFactory.newThread(r);
t.setDaemon(true);
return t;
});
}
线程池的配置信息在Config类里边,配置信息初始化在Config的私有构造方法中。SDK中的配置的逻辑是先从一个文件中进行读取配置,读取失败就加载默认配置。内个文件的路径是org.hyperledger.fabric.sdk.configuration.config.properties,但是找了半天也没找到内个配置文件。这个很让人头疼,我估计SDK就只是为了加载默认配置,而实际运行调用SDK的时候也正是跑的默认配置信息。主要代码如下
private Config() {
File loadFile;
FileInputStream configProps;
try {
从文件中加载默认配置
} catch (IOException e) {
打印错误日志
} finally {
如果文件加载配置出错,加载默认配置
}
}
2.HFClient.setCryptoSuite() 这个方法中只设置了一下cryptoSuite
public void setCryptoSuite(CryptoSuite cryptoSuite) throws CryptoException, InvalidArgumentException {
if (null == cryptoSuite) {
throw new InvalidArgumentException("CryptoSuite paramter is null.");
}
if (this.cryptoSuite != null && cryptoSuite != this.cryptoSuite) {
throw new InvalidArgumentException("CryptoSuite may only be set once.");
}
// if (cryptoSuiteFactory == null) {
// cryptoSuiteFactory = cryptoSuite.getCryptoSuiteFactory();
// } else {
// if (cryptoSuiteFactory != cryptoSuite.getCryptoSuiteFactory()) {
// throw new InvalidArgumentException("CryptoSuite is not derivied from cryptosuite factory");
// }
// }
this.cryptoSuite = cryptoSuite;
}
3. HFClient.setUserContext() 这个方法首先对cryptoSuite 进行了非空校验,之后对用户证书进行校验
static void userContextCheck(User userContext) throws InvalidArgumentException {
if (userContext == null) {
throw new InvalidArgumentException("UserContext is null");
}
final String userName = userContext.getName();
if (Utils.isNullOrEmpty(userName)) {
throw new InvalidArgumentException("UserContext user's name missing.");
}
Enrollment enrollment = userContext.getEnrollment();
if (enrollment == null) {
throw new InvalidArgumentException(format("UserContext for user %s has no enrollment set.", userName));
}
if (enrollment instanceof X509Enrollment) {
if (Utils.isNullOrEmpty(enrollment.getCert())) {
throw new InvalidArgumentException(format("UserContext for user %s enrollment missing user certificate.", userName));
}
if (null == enrollment.getKey()) {
throw new InvalidArgumentException(format("UserContext for user %s has Enrollment missing signing key", userName));
}
}
if (Utils.isNullOrEmpty(userContext.getMspId())) {
throw new InvalidArgumentException(format("UserContext for user %s has user's MSPID missing.", userName));
}
}