NIFI-259: Adjusting validation logic for Connect String to catch errors at startup.

This commit is contained in:
Aldrin Piri 2016-01-29 08:43:32 -06:00
parent 6902812678
commit f47168213b
1 changed files with 16 additions and 5 deletions

View File

@ -50,6 +50,7 @@ import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZKUtil; import org.apache.zookeeper.ZKUtil;
import org.apache.zookeeper.ZooDefs.Ids; import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.client.ConnectStringParser;
import org.apache.zookeeper.data.ACL; import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat; import org.apache.zookeeper.data.Stat;
@ -62,7 +63,18 @@ public class ZooKeeperStateProvider extends AbstractStateProvider {
.name("Connect String") .name("Connect String")
.description("The ZooKeeper Connect String to use. This is a comma-separated list of hostname/IP and port tuples, such as \"host1:2181,host2:2181,127.0.0.1:2181\". If a port is not " + .description("The ZooKeeper Connect String to use. This is a comma-separated list of hostname/IP and port tuples, such as \"host1:2181,host2:2181,127.0.0.1:2181\". If a port is not " +
"specified it defaults to the ZooKeeper client port default of 2181") "specified it defaults to the ZooKeeper client port default of 2181")
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR) .addValidator(new Validator() {
@Override
public ValidationResult validate(String subject, String input, ValidationContext context) {
final String connectionString = context.getProperty(CONNECTION_STRING).getValue();
try {
new ConnectStringParser(connectionString);
} catch (Exception e) {
return new ValidationResult.Builder().subject(subject).input(input).explanation("Invalid Connect String: " + connectionString).valid(false).build();
}
return new ValidationResult.Builder().subject(subject).input(input).explanation("Valid Connect String").valid(true).build();
}
})
.required(false) .required(false)
.build(); .build();
static final PropertyDescriptor SESSION_TIMEOUT = new PropertyDescriptor.Builder() static final PropertyDescriptor SESSION_TIMEOUT = new PropertyDescriptor.Builder()
@ -148,7 +160,6 @@ public class ZooKeeperStateProvider extends AbstractStateProvider {
.build()); .build());
} }
} }
return validationFailures; return validationFailures;
} }
@ -251,7 +262,7 @@ public class ZooKeeperStateProvider extends AbstractStateProvider {
private byte[] serialize(final Map<String, String> stateValues) throws IOException { private byte[] serialize(final Map<String, String> stateValues) throws IOException {
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final DataOutputStream dos = new DataOutputStream(baos)) { final DataOutputStream dos = new DataOutputStream(baos)) {
dos.writeByte(ENCODING_VERSION); dos.writeByte(ENCODING_VERSION);
dos.writeInt(stateValues.size()); dos.writeInt(stateValues.size());
for (final Map.Entry<String, String> entry : stateValues.entrySet()) { for (final Map.Entry<String, String> entry : stateValues.entrySet()) {
@ -273,7 +284,7 @@ public class ZooKeeperStateProvider extends AbstractStateProvider {
private StateMap deserialize(final byte[] data, final int recordVersion, final String componentId) throws IOException { private StateMap deserialize(final byte[] data, final int recordVersion, final String componentId) throws IOException {
try (final ByteArrayInputStream bais = new ByteArrayInputStream(data); try (final ByteArrayInputStream bais = new ByteArrayInputStream(data);
final DataInputStream dis = new DataInputStream(bais)) { final DataInputStream dis = new DataInputStream(bais)) {
final byte encodingVersion = dis.readByte(); final byte encodingVersion = dis.readByte();
if (encodingVersion > ENCODING_VERSION) { if (encodingVersion > ENCODING_VERSION) {
@ -422,6 +433,6 @@ public class ZooKeeperStateProvider extends AbstractStateProvider {
@Override @Override
public void clear(final String componentId) throws IOException { public void clear(final String componentId) throws IOException {
verifyEnabled(); verifyEnabled();
setState(Collections.<String, String> emptyMap(), componentId); setState(Collections.<String, String>emptyMap(), componentId);
} }
} }