Skip to content

Commit

Permalink
nfs41: simplify try-catch-return logic
Browse files Browse the repository at this point in the history
move return statement into try-catch block for better readability

Acked-by: Lea Morschel
Acked-by: Paul Millar
Target: master
  • Loading branch information
kofemann committed Oct 7, 2020
1 parent cf8ea4a commit aeccd6f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 38 deletions.
32 changes: 13 additions & 19 deletions core/src/main/java/org/dcache/nfs/v4/FlexFileLayoutDriver.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016 - 2019 Deutsches Elektronen-Synchroton,
* Copyright (c) 2016 - 2020 Deutsches Elektronen-Synchroton,
* Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY
*
* This library is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -45,7 +45,6 @@
import org.dcache.nfs.v4.xdr.stateid4;
import org.dcache.nfs.v4.xdr.uint32_t;
import org.dcache.nfs.v4.xdr.utf8str_mixed;
import org.dcache.oncrpc4j.rpc.OncRpcException;
import org.dcache.oncrpc4j.xdr.Xdr;

import static com.google.common.base.Preconditions.checkArgument;
Expand Down Expand Up @@ -135,25 +134,21 @@ public device_addr4 getDeviceAddress(InetSocketAddress... deviceAddress) throws
flexfile_type.ffda_netaddrs.value[i] = new netaddr4(deviceAddress[i]);
}

byte[] retBytes;
try(Xdr xdr = new Xdr(128)) {
xdr.beginEncoding();
flexfile_type.xdrEncode(xdr);
xdr.endEncoding();
retBytes = xdr.getBytes();
} catch (OncRpcException e) {
/* forced by interface, should never happen. */
throw new RuntimeException("Unexpected OncRpcException:" + e.getMessage(), e);

device_addr4 addr = new device_addr4();
addr.da_layout_type = layouttype4.LAYOUT4_FLEX_FILES.getValue();
addr.da_addr_body = xdr.getBytes();

return addr;
} catch (IOException e) {
/* forced by interface, should never happen. */
throw new RuntimeException("Unexpected IOException:" + e.getMessage(), e);
}

device_addr4 addr = new device_addr4();
addr.da_layout_type = layouttype4.LAYOUT4_FLEX_FILES.getValue();
addr.da_addr_body = retBytes;

return addr;
}

@Override
Expand All @@ -168,20 +163,19 @@ public layout_content4 getLayoutContent(stateid4 stateid, int stripeSize, nfs_fh
layout.ffl_flags4 = layoutFlags;
layout.ffl_stats_collect_hint = new uint32_t(0);

byte[] body;
try (Xdr xdr = new Xdr(512)) {
xdr.beginEncoding();
layout.xdrEncode(xdr);
xdr.endEncoding();
body = xdr.getBytes();

layout_content4 content = new layout_content4();
content.loc_type = layouttype4.LAYOUT4_FLEX_FILES.getValue();
content.loc_body = xdr.getBytes();

return content;
} catch (IOException e) {
throw new ServerFaultException("failed to encode layout body", e);
}

layout_content4 content = new layout_content4();
content.loc_type = layouttype4.LAYOUT4_FLEX_FILES.getValue();
content.loc_body = body;
return content;
}

private ff_data_server4 createDataserver(deviceid4 deviceid,
Expand Down
33 changes: 14 additions & 19 deletions core/src/main/java/org/dcache/nfs/v4/NfsV41FileLayoutDriver.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 - 2019 Deutsches Elektronen-Synchroton,
* Copyright (c) 2009 - 2020 Deutsches Elektronen-Synchroton,
* Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY
*
* This library is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -37,7 +37,6 @@
import org.dcache.nfs.v4.xdr.offset4;
import org.dcache.nfs.v4.xdr.stateid4;
import org.dcache.nfs.v4.xdr.uint32_t;
import org.dcache.oncrpc4j.rpc.OncRpcException;
import org.dcache.oncrpc4j.xdr.Xdr;

import static com.google.common.base.Preconditions.checkArgument;
Expand Down Expand Up @@ -69,25 +68,21 @@ public device_addr4 getDeviceAddress(InetSocketAddress... deviceAddress) {
file_type.nflda_stripe_indices = new uint32_t[1];
file_type.nflda_stripe_indices[0] = new uint32_t(0);

byte[] retBytes;
try(Xdr xdr = new Xdr(128)){
xdr.beginEncoding();
file_type.xdrEncode(xdr);
xdr.endEncoding();
retBytes = xdr.getBytes();
} catch (OncRpcException e) {
/* forced by interface, should never happen. */
throw new RuntimeException("Unexpected OncRpcException:", e);

device_addr4 addr = new device_addr4();
addr.da_layout_type = layouttype4.LAYOUT4_NFSV4_1_FILES.getValue();
addr.da_addr_body = xdr.getBytes();

return addr;
} catch (IOException e) {
/* forced by interface, should never happen. */
throw new RuntimeException("Unexpected IOException:", e);
}

device_addr4 addr = new device_addr4();
addr.da_layout_type = layouttype4.LAYOUT4_NFSV4_1_FILES.getValue();
addr.da_addr_body = retBytes;

return addr;
}

@Override
Expand Down Expand Up @@ -123,21 +118,21 @@ public layout_content4 getLayoutContent(stateid4 stateid, int stripeSize, nfs_fh
//where the striping pattern starts
layout.nfl_pattern_offset = new offset4(0);

byte[] body;
try (Xdr xdr = new Xdr(512)) {
xdr.beginEncoding();
layout.xdrEncode(xdr);
xdr.endEncoding();
body = xdr.getBytes();

layout_content4 content = new layout_content4();
content.loc_type = layouttype4.LAYOUT4_NFSV4_1_FILES.getValue();
content.loc_body = xdr.getBytes();

return content;

} catch (IOException e) {
throw new ServerFaultException("failed to encode layout body");
}

layout_content4 content = new layout_content4();
content.loc_type = layouttype4.LAYOUT4_NFSV4_1_FILES.getValue();
content.loc_body = body;

return content;
}

/**
Expand Down

0 comments on commit aeccd6f

Please sign in to comment.