3232import org .hyperledger .fabric .sdk .exception .InvalidArgumentException ;
3333import org .hyperledger .fabric .sdk .exception .ProposalException ;
3434import org .hyperledger .fabric .sdk .exception .TransactionException ;
35- import org .hyperledger .fabric .sdk .helper .Utils ;
3635import org .hyperledger .fabric .sdk .security .CryptoSuite ;
3736
3837import static java .lang .String .format ;
38+ import static org .hyperledger .fabric .sdk .User .userContextCheck ;
3939
4040public class HFClient {
4141
@@ -84,8 +84,10 @@ public void setCryptoSuite(CryptoSuite cryptoSuite) throws CryptoException, Inva
8484 throw new InvalidArgumentException ("CryptoSuite may only be set once." );
8585
8686 }
87+
88+ cryptoSuite .init ();
8789 this .cryptoSuite = cryptoSuite ;
88- this . cryptoSuite . init ();
90+
8991 }
9092
9193 /**
@@ -106,6 +108,7 @@ public static HFClient createNewInstance() {
106108 */
107109
108110 public Channel newChannel (String name ) throws InvalidArgumentException {
111+ clientCheck ();
109112 logger .trace ("Creating channel :" + name );
110113 Channel newChannel = Channel .createNewInstance (name , this );
111114 channels .put (name , newChannel );
@@ -127,6 +130,7 @@ public Channel newChannel(String name) throws InvalidArgumentException {
127130
128131 public Channel newChannel (String name , Orderer orderer , ChannelConfiguration channelConfiguration , byte []... channelConfigurationSignatures ) throws TransactionException , InvalidArgumentException {
129132
133+ clientCheck ();
130134 logger .trace ("Creating channel :" + name );
131135 Channel newChannel = Channel .createNewInstance (name , this , orderer , channelConfiguration , channelConfigurationSignatures );
132136 channels .put (name , newChannel );
@@ -166,6 +170,7 @@ public Channel newChannel(String name, Orderer orderer, ChannelConfiguration cha
166170 */
167171
168172 public Peer newPeer (String name , String grpcURL , Properties properties ) throws InvalidArgumentException {
173+ clientCheck ();
169174 return Peer .createNewInstance (name , grpcURL , properties );
170175 }
171176
@@ -179,6 +184,7 @@ public Peer newPeer(String name, String grpcURL, Properties properties) throws I
179184 */
180185
181186 public Peer newPeer (String name , String grpcURL ) throws InvalidArgumentException {
187+ clientCheck ();
182188 return Peer .createNewInstance (name , grpcURL , null );
183189 }
184190
@@ -245,37 +251,14 @@ public QueryByChaincodeRequest newQueryProposalRequest() {
245251
246252 public void setUserContext (User userContext ) throws InvalidArgumentException {
247253
248- if (userContext == null ) {
249- throw new InvalidArgumentException ("setUserContext is null" );
250- }
251- final String userName = userContext .getName ();
252- if (Utils .isNullOrEmpty (userName )) {
253- throw new InvalidArgumentException ("setUserContext user's name is missing" );
254- }
255-
256- Enrollment enrollment = userContext .getEnrollment ();
257- if (enrollment == null ) {
258- throw new InvalidArgumentException (format ("setUserContext for user %s has no Enrollment set" , userName ));
259- }
260-
261- if (Utils .isNullOrEmpty (userContext .getMspId ())) {
262- throw new InvalidArgumentException (format ("setUserContext for user %s has user's MSPID is missing" , userName ));
263- }
264-
265- if (Utils .isNullOrEmpty (userContext .getName ())) {
266- throw new InvalidArgumentException ("setUserContext user's name is missing" );
267- }
268-
269- if (Utils .isNullOrEmpty (enrollment .getCert ())) {
270- throw new InvalidArgumentException (format ("setUserContext for user %s Enrollment missing user certificate." , userName ));
271- }
272- if (null == enrollment .getKey ()) {
273- throw new InvalidArgumentException (format ("setUserContext for user %s has Enrollment missing signing key" , userName ));
254+ if (null == cryptoSuite ) {
255+ throw new InvalidArgumentException ("No cryptoSuite has been set." );
274256 }
257+ userContextCheck (userContext );
258+ this .userContext = userContext ;
275259
276260 logger .debug (format ("Setting user context to MSPID: %s user: %s" , userContext .getMspId (), userContext .getName ()));
277261
278- this .userContext = userContext ;
279262 }
280263
281264 /**
@@ -311,6 +294,7 @@ public void setUserContext(User userContext) throws InvalidArgumentException {
311294 */
312295
313296 public EventHub newEventHub (String name , String grpcURL , Properties properties ) throws InvalidArgumentException {
297+ clientCheck ();
314298 return EventHub .createNewInstance (name , grpcURL , executorService , properties );
315299 }
316300
@@ -324,6 +308,7 @@ public EventHub newEventHub(String name, String grpcURL, Properties properties)
324308 */
325309
326310 public EventHub newEventHub (String name , String grpcURL ) throws InvalidArgumentException {
311+ clientCheck ();
327312 return newEventHub (name , grpcURL , null );
328313 }
329314
@@ -337,6 +322,7 @@ public EventHub newEventHub(String name, String grpcURL) throws InvalidArgumentE
337322 */
338323
339324 public Orderer newOrderer (String name , String grpcURL ) throws InvalidArgumentException {
325+ clientCheck ();
340326 return newOrderer (name , grpcURL , null );
341327 }
342328
@@ -373,6 +359,7 @@ public Orderer newOrderer(String name, String grpcURL) throws InvalidArgumentExc
373359 */
374360
375361 public Orderer newOrderer (String name , String grpcURL , Properties properties ) throws InvalidArgumentException {
362+ clientCheck ();
376363 return Orderer .createNewInstance (name , grpcURL , properties );
377364 }
378365
@@ -386,9 +373,8 @@ public Orderer newOrderer(String name, String grpcURL, Properties properties) th
386373 */
387374 public Set <String > queryChannels (Peer peer ) throws InvalidArgumentException , ProposalException {
388375
389- if (userContext == null ) {
390- throw new InvalidArgumentException ("UserContext has not been set." );
391- }
376+ clientCheck ();
377+
392378 if (null == peer ) {
393379
394380 throw new InvalidArgumentException ("peer set to null" );
@@ -421,9 +407,8 @@ public Set<String> queryChannels(Peer peer) throws InvalidArgumentException, Pro
421407
422408 public List <ChaincodeInfo > queryInstalledChaincodes (Peer peer ) throws InvalidArgumentException , ProposalException {
423409
424- if (userContext == null ) {
425- throw new InvalidArgumentException ("UserContext has not been set." );
426- }
410+ clientCheck ();
411+
427412 if (null == peer ) {
428413
429414 throw new InvalidArgumentException ("peer set to null" );
@@ -454,6 +439,8 @@ public List<ChaincodeInfo> queryInstalledChaincodes(Peer peer) throws InvalidArg
454439
455440 public byte [] getChannelConfigurationSignature (ChannelConfiguration channelConfiguration , User signer ) throws InvalidArgumentException {
456441
442+ clientCheck ();
443+
457444 Channel systemChannel = Channel .newSystemChannel (this );
458445 return systemChannel .getChannelConfigurationSignature (channelConfiguration , signer );
459446
@@ -472,11 +459,23 @@ public byte[] getChannelConfigurationSignature(ChannelConfiguration channelConfi
472459 public Collection <ProposalResponse > sendInstallProposal (InstallProposalRequest installProposalRequest , Collection <Peer > peers )
473460 throws ProposalException , InvalidArgumentException {
474461
462+ clientCheck ();
463+
475464 installProposalRequest .setSubmitted ();
476465 Channel systemChannel = Channel .newSystemChannel (this );
477466
478467 return systemChannel .sendInstallProposal (installProposalRequest , peers );
479468
480469 }
481470
471+ private void clientCheck () throws InvalidArgumentException {
472+
473+ if (null == cryptoSuite ) {
474+ throw new InvalidArgumentException ("No cryptoSuite has been set." );
475+ }
476+
477+ userContextCheck (userContext );
478+
479+ }
480+
482481}
0 commit comments