Skip to content

Commit b306d7b

Browse files
author
Luis Sanchez
committed
[FAB-3221] Java cc getFunctionAndParameters()
ChaincodeStub: - add getFunction() - add getParameters() - rename getArgsAsStrings() to getStringArgs() Change-Id: Iea537353f3c40b205b8f5fb0213a85733b5dbdab Signed-off-by: Luis Sanchez <[email protected]>
1 parent 908af1a commit b306d7b

File tree

9 files changed

+69
-38
lines changed

9 files changed

+69
-38
lines changed

core/chaincode/shim/java/src/main/java/org/hyperledger/fabric/shim/ChaincodeStub.java

+26-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
package org.hyperledger.fabric.shim;
1717

1818
import static java.nio.charset.StandardCharsets.UTF_8;
19+
import static java.util.stream.Collectors.toList;
1920

20-
import java.nio.charset.StandardCharsets;
2121
import java.util.Arrays;
2222
import java.util.List;
23-
import java.util.stream.Collectors;
2423

2524
import org.hyperledger.fabric.protos.peer.ChaincodeEventPackage.ChaincodeEvent;
2625
import org.hyperledger.fabric.protos.peer.ProposalResponsePackage.Response;
@@ -41,9 +40,31 @@ public interface ChaincodeStub {
4140
* {@link Chaincode#init(ChaincodeStub)} or
4241
* {@link Chaincode#invoke(ChaincodeStub)}.
4342
*
44-
* @return a list of arguments cast to a UTF-8 string
43+
* @return a list of arguments cast to UTF-8 strings
4544
*/
46-
List<String> getArgsAsStrings();
45+
List<String> getStringArgs();
46+
47+
/**
48+
* A convenience method that returns the first argument of the chaincode
49+
* invocation for use as a function name.
50+
*
51+
* The bytes of the first argument are decoded as a UTF-8 string.
52+
*
53+
* @return the function name
54+
*/
55+
String getFunction();
56+
57+
/**
58+
* A convenience method that returns all except the first argument of the
59+
* chaincode invocation for use as the parameters to the function returned
60+
* by #{@link ChaincodeStub#getFunction()}.
61+
*
62+
* The bytes of the arguments are decoded as a UTF-8 strings and returned as
63+
* a list of string parameters..
64+
*
65+
* @return a list of parameters
66+
*/
67+
List<String> getParameters();
4768

4869
/**
4970
* Returns the transaction id
@@ -134,7 +155,7 @@ default Response invokeChaincode(String chaincodeName, List<byte[]> args) {
134155
* @return
135156
*/
136157
default Response invokeChaincodeWithStringArgs(String chaincodeName, List<String> args, String channel) {
137-
return invokeChaincode(chaincodeName, args.stream().map(x->x.getBytes(StandardCharsets.UTF_8)).collect(Collectors.toList()), channel);
158+
return invokeChaincode(chaincodeName, args.stream().map(x->x.getBytes(UTF_8)).collect(toList()), channel);
138159
}
139160

140161
/**

core/chaincode/shim/java/src/main/java/org/hyperledger/fabric/shim/ChaincodeStubImpl.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.hyperledger.fabric.shim;
1818

19+
import static java.util.stream.Collectors.toList;
20+
1921
import java.util.Collections;
2022
import java.util.List;
2123
import java.util.stream.Collectors;
@@ -54,10 +56,26 @@ public List<byte[]> getArgs() {
5456
* @see org.hyperledger.fabric.shim.ChaincodeStub#getArgsAsStrings()
5557
*/
5658
@Override
57-
public List<String> getArgsAsStrings() {
59+
public List<String> getStringArgs() {
5860
return args.stream().map(x -> x.toStringUtf8()).collect(Collectors.toList());
5961
}
6062

63+
/* (non-Javadoc)
64+
* @see org.hyperledger.fabric.shim.ChaincodeStub#getFunction()
65+
*/
66+
@Override
67+
public String getFunction() {
68+
return getStringArgs().size() > 0 ? getStringArgs().get(0) : null;
69+
}
70+
71+
/* (non-Javadoc)
72+
* @see org.hyperledger.fabric.shim.ChaincodeStub#getParameters()
73+
*/
74+
@Override
75+
public List<String> getParameters() {
76+
return getStringArgs().stream().skip(1).collect(toList());
77+
}
78+
6179
/* (non-Javadoc)
6280
* @see org.hyperledger.fabric.shim.ChaincodeStub#setEvent(java.lang.String, byte[])
6381
*/

examples/chaincode/java/LinkExample/src/main/java/example/LinkExample.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ public Response init(ChaincodeStub stub) {
4343
@Override
4444
public Response invoke(ChaincodeStub stub) {
4545
try {
46-
final List<String> argList = stub.getArgsAsStrings();
47-
final String function = argList.get(0);
48-
final List<String> args = argList.subList(0, argList.size());
46+
final String function = stub.getFunction();
47+
final List<String> args = stub.getParameters();
4948

5049
switch (function) {
5150
case "init":

examples/chaincode/java/SimpleSample/src/main/java/example/SimpleSample.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import static org.hyperledger.fabric.shim.ChaincodeHelper.newInternalServerErrorResponse;
2323
import static org.hyperledger.fabric.shim.ChaincodeHelper.newSuccessResponse;
2424

25-
import java.util.List;
26-
2725
import javax.json.Json;
2826

2927
import org.apache.commons.logging.Log;
@@ -44,19 +42,18 @@ public class SimpleSample extends ChaincodeBase {
4442

4543
@Override
4644
public Response init(ChaincodeStub stub) {
47-
final List<String> args = stub.getArgsAsStrings();
48-
if(!args.get(0).equals("init")) {
49-
return newBadRequestResponse(format("Unknown function: %s", args.get(0)));
45+
final String function = stub.getFunction();
46+
if(!function.equals("init")) {
47+
return newBadRequestResponse(format("Unknown function: %s", function));
5048
}
51-
return init(stub, args.stream().skip(1).toArray(String[]::new));
49+
return init(stub, stub.getParameters().stream().toArray(String[]::new));
5250
}
5351

5452
@Override
5553
public Response invoke(ChaincodeStub stub) {
5654
try {
57-
final List<String> argList = stub.getArgsAsStrings();
58-
final String function = argList.get(0);
59-
final String[] args = argList.stream().skip(1).toArray(String[]::new);
55+
final String function = stub.getFunction();
56+
final String[] args = stub.getParameters().stream().toArray(String[]::new);
6057

6158
switch (function) {
6259
case "transfer":

examples/chaincode/java/chaincode_example02/src/main/java/example/Example02.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ public class Example02 extends ChaincodeBase {
3939
@Override
4040
public Response init(ChaincodeStub stub) {
4141
try {
42-
final List<String> args = stub.getArgsAsStrings();
43-
switch (args.get(0)) {
42+
final String function = stub.getFunction();
43+
switch (function) {
4444
case "init":
45-
return init(stub, args.stream().skip(1).toArray(String[]::new));
45+
return init(stub, stub.getParameters().stream().toArray(String[]::new));
4646
default:
47-
return newBadRequestResponse(format("Unknown function: %s", args.get(0)));
47+
return newBadRequestResponse(format("Unknown function: %s", function));
4848
}
4949
} catch (NumberFormatException e) {
5050
return newBadRequestResponse(e.toString());
@@ -59,9 +59,8 @@ public Response init(ChaincodeStub stub) {
5959
public Response invoke(ChaincodeStub stub) {
6060

6161
try {
62-
final List<String> argList = stub.getArgsAsStrings();
63-
final String function = argList.get(0);
64-
final String[] args = argList.stream().skip(1).toArray(String[]::new);
62+
final String function = stub.getFunction();
63+
final String[] args = stub.getParameters().stream().toArray(String[]::new);
6564

6665
switch (function) {
6766
case "invoke":

examples/chaincode/java/chaincode_example04/src/main/java/example/Example04.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public Response init(ChaincodeStub stub) {
3737
// expects to be called with: { "init", key, value }
3838
try {
3939

40-
final List<String> args = stub.getArgsAsStrings();
40+
final List<String> args = stub.getStringArgs();
4141
if(args.size() != 3) {
4242
return newBadRequestResponse("Incorrect number of arguments. Expecting \"init\" plus 2 more.");
4343
}
@@ -59,9 +59,8 @@ public Response init(ChaincodeStub stub) {
5959
public Response invoke(ChaincodeStub stub) {
6060
// expects to be called with: { "invoke"|"query", chaincodeName, key }
6161
try {
62-
final List<String> argList = stub.getArgsAsStrings();
63-
final String function = argList.get(0);
64-
final String[] args = argList.stream().skip(1).toArray(String[]::new);
62+
final String function = stub.getFunction();
63+
final String[] args = stub.getParameters().stream().toArray(String[]::new);
6564

6665
switch (function) {
6766
case "invoke":

examples/chaincode/java/chaincode_example05/src/main/java/example/Example05.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public Response init(ChaincodeStub stub) {
3535
// expects to be called with: { "init", key, value }
3636
try {
3737

38-
final List<String> args = stub.getArgsAsStrings();
38+
final List<String> args = stub.getStringArgs();
3939
if(args.size() != 3) {
4040
return newBadRequestResponse("Incorrect number of arguments. Expecting \"init\" plus 2 more.");
4141
}
@@ -57,9 +57,8 @@ public Response init(ChaincodeStub stub) {
5757
public Response invoke(ChaincodeStub stub) {
5858
// expects to be called with: { "invoke"|"query", chaincodeName, key }
5959
try {
60-
final List<String> argList = stub.getArgsAsStrings();
61-
final String function = argList.get(0);
62-
final String[] args = argList.stream().skip(1).toArray(String[]::new);
60+
final String function = stub.getFunction();
61+
final String[] args = stub.getParameters().stream().toArray(String[]::new);
6362

6463
switch (function) {
6564
case "invoke":

examples/chaincode/java/chaincode_example06/src/main/java/example/Example06.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ public Response init(ChaincodeStub stub) {
3232

3333
@Override
3434
public Response invoke(ChaincodeStub stub) {
35-
if(stub.getArgsAsStrings().isEmpty())
35+
if(stub.getArgs().isEmpty())
3636
return newBadRequestResponse(format("No arguments specified."));
3737

38-
switch (stub.getArgsAsStrings().get(0)) {
38+
switch (stub.getStringArgs().get(0)) {
3939
case "runtimeException":
4040
throw new RuntimeException("Exception thrown as requested.");
4141
default:

examples/chaincode/java/eventsender/src/main/java/example/EventSender.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,11 @@ public Response init(ChaincodeStub stub) {
4444
public Response invoke(ChaincodeStub stub) {
4545

4646
try {
47-
final List<String> argList = stub.getArgsAsStrings();
48-
final String function = argList.get(0);
47+
final String function = stub.getFunction();
4948

5049
switch (function) {
5150
case "invoke":
52-
return doInvoke(stub, argList.stream().skip(1).collect(toList()));
51+
return doInvoke(stub, stub.getParameters());
5352
case "query":
5453
return doQuery(stub);
5554
default:

0 commit comments

Comments
 (0)