Skip to content

Commit a0cd84f

Browse files
committed
Removed possible ArrayIndexOutOfBoundsException.
Fixed code formatting and comments.
1 parent 0173383 commit a0cd84f

File tree

4 files changed

+44
-48
lines changed

4 files changed

+44
-48
lines changed

gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/DnsExample.java

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@
3535
import java.util.Map;
3636
import java.util.concurrent.TimeUnit;
3737

38-
/**
38+
/*
3939
* An example of using Google Cloud DNS.
4040
*
41-
* <p>This example creates, deletes, gets, and lists zones, and creates and deletes DNS records of
42-
* type A.
41+
* <p>This example creates, deletes, gets, and lists zones. It also creates and deletes DNS records
42+
* of type A, and lists DNS records.
4343
*
4444
* <p>Steps needed for running the example:
4545
* <ol>
@@ -57,10 +57,10 @@
5757
* quota}</li>
5858
* </ol>
5959
*
60-
* <p>The first parameter is an optional {@code project_id} (logged-in project will be used if not
61-
* supplied). The second parameter is a DNS operation (list, delete, create,...). The remaining
62-
* arguments are specific to the operation. See each action's run method for the specific
63-
* interaction.
60+
* <p>The first parameter is an optional {@code project_id} (project specified in the Google Cloud
61+
* SDK configuration (see {@code gcloud config list}) will be used if not supplied). The second
62+
* parameter is a DNS operation (list, delete, create, ...). The remaining arguments are specific to
63+
* the operation. See each action's {@code run} method for the specific arguments.
6464
*/
6565
public class DnsExample {
6666

@@ -78,7 +78,7 @@ private interface DnsAction {
7878
private static class CreateZoneAction implements DnsAction {
7979

8080
/**
81-
* Creates a zone with the provided name, dns name and description (in this order).
81+
* Creates a zone with the provided name, DNS name and description (in this order).
8282
*/
8383
@Override
8484
public void run(Dns dns, String... args) {
@@ -113,8 +113,7 @@ public void run(Dns dns, String... args) {
113113
if (zoneIterator.hasNext()) {
114114
System.out.println("The project contains the following zones:");
115115
while (zoneIterator.hasNext()) {
116-
Zone zone = zoneIterator.next();
117-
printZone(zone);
116+
printZone(zoneIterator.next());
118117
}
119118
} else {
120119
System.out.println("Project contains no zones.");
@@ -211,18 +210,10 @@ public void run(Dns dns, String... args) {
211210
.delete(record)
212211
.build();
213212
changeRequest = dns.applyChangeRequest(zoneName, changeRequest);
214-
System.out.printf("The request for deleting A record %s for zone %s was successfully " +
215-
"submitted and assigned ID %s.%n", recordName, zoneName, changeRequest.id());
213+
System.out.printf("The request for deleting A record %s for zone %s was successfully "
214+
+ "submitted and assigned ID %s.%n", recordName, zoneName, changeRequest.id());
216215
System.out.print("Waiting for deletion to happen...");
217-
while (changeRequest.status().equals(ChangeRequest.Status.PENDING)) {
218-
System.out.print(".");
219-
try {
220-
Thread.sleep(500);
221-
} catch (InterruptedException e) {
222-
System.err.println("Thread was interrupted while waiting.");
223-
}
224-
changeRequest = dns.getChangeRequest(zoneName, changeRequest.id());
225-
}
216+
waitForChangeToFinish(dns, zoneName, changeRequest);
226217
System.out.printf("%nThe deletion has been completed.%n");
227218
}
228219

@@ -262,22 +253,12 @@ public void run(Dns dns, String... args) {
262253
.records(ImmutableList.of(ip))
263254
.ttl(ttl, TimeUnit.SECONDS)
264255
.build();
265-
ChangeRequest changeRequest = ChangeRequest.builder()
266-
.add(record)
267-
.build();
256+
ChangeRequest changeRequest = ChangeRequest.builder().add(record).build();
268257
changeRequest = dns.applyChangeRequest(zoneName, changeRequest);
269-
System.out.printf("The request for adding A record %s for zone %s was successfully " +
270-
"submitted and assigned ID %s.%n", recordName, zoneName, changeRequest.id());
258+
System.out.printf("The request for adding A record %s for zone %s was successfully "
259+
+ "submitted and assigned ID %s.%n", recordName, zoneName, changeRequest.id());
271260
System.out.print("Waiting for addition to happen...");
272-
while (changeRequest.status().equals(ChangeRequest.Status.PENDING)) {
273-
System.out.print(".");
274-
try {
275-
Thread.sleep(500);
276-
} catch (InterruptedException e) {
277-
System.err.println("Thread was interrupted while waiting.");
278-
}
279-
changeRequest = dns.getChangeRequest(zoneName, changeRequest.id());
280-
}
261+
waitForChangeToFinish(dns, zoneName, changeRequest);
281262
System.out.printf("The addition has been completed.%n");
282263
}
283264

@@ -333,8 +314,8 @@ public boolean check(String... args) {
333314
private static class ListChangesAction implements DnsAction {
334315

335316
/**
336-
* Lists all the changes for a given zone. Optionally, an order, "descending" or "ascending" can
337-
* be specified using the last parameter.
317+
* Lists all the changes for a given zone. Optionally, an order ("descending" or "ascending")
318+
* can be specified using the last parameter.
338319
*/
339320
@Override
340321
public void run(Dns dns, String... args) {
@@ -398,7 +379,7 @@ public void run(Dns dns, String... args) {
398379

399380
@Override
400381
public boolean check(String... args) {
401-
if (args.length == 0) {
382+
if (args.length == 0 || args.length == 1) {
402383
return true;
403384
}
404385
if ("records".equals(args[1])) {
@@ -462,6 +443,21 @@ private static void printZone(Zone zone) {
462443
System.out.printf("Name servers: %s%n", Joiner.on(", ").join(zone.nameServers()));
463444
}
464445

446+
private static ChangeRequest waitForChangeToFinish(Dns dns, String zoneName,
447+
ChangeRequest request) {
448+
ChangeRequest current = request;
449+
while (current.status().equals(ChangeRequest.Status.PENDING)) {
450+
System.out.print(".");
451+
try {
452+
Thread.sleep(500);
453+
} catch (InterruptedException e) {
454+
System.err.println("Thread was interrupted while waiting.");
455+
}
456+
current = dns.getChangeRequest(zoneName, current.id());
457+
}
458+
return current;
459+
}
460+
465461
private static void printUsage() {
466462
StringBuilder actionAndParams = new StringBuilder();
467463
for (Map.Entry<String, DnsAction> entry : ACTIONS.entrySet()) {
@@ -508,12 +504,13 @@ public static void main(String... args) throws Exception {
508504
return;
509505
} catch (Exception ex) {
510506
System.out.println("Failed to parse request.");
507+
System.out.println("Expected: " + action.params());
511508
ex.printStackTrace();
512509
return;
513510
}
514511
if (valid) {
515512
DnsOptions.Builder optionsBuilder = DnsOptions.builder();
516-
if(projectId != null) {
513+
if (projectId != null) {
517514
optionsBuilder.projectId(projectId);
518515
}
519516
Dns dns = optionsBuilder.build().service();

gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateAndListDnsRecords.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
public class CreateAndListDnsRecords {
3838

3939
public static void main(String... args) {
40-
4140
// Create a service object.
4241
// The project ID and credentials will be inferred from the environment.
4342
Dns dns = DnsOptions.defaultInstance().service();
@@ -63,7 +62,7 @@ public static void main(String... args) {
6362
Iterator<DnsRecord> recordIterator = zone.listDnsRecords().iterateAll();
6463
while (recordIterator.hasNext()) {
6564
DnsRecord current = recordIterator.next();
66-
if(toCreate.name().equals(current.name()) && toCreate.type().equals(current.type())) {
65+
if (toCreate.name().equals(current.name()) && toCreate.type().equals(current.type())) {
6766
changeBuilder.delete(current);
6867
}
6968
}

gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/CreateAndListZones.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ public static void main(String... args) {
5151
Zone createdZone = dns.create(zoneInfo);
5252
System.out.printf("Zone was created and assigned ID %s.%n", createdZone.id());
5353

54-
// Now list all the zones within this project and print them using the default toString
54+
// Now list all the zones within this project
5555
Iterator<Zone> zoneIterator = dns.listZones().iterateAll();
5656
int counter = 1;
5757
while (zoneIterator.hasNext()) {
58-
System.out.printf("#%d.: %s", counter, zoneIterator.next().toString());
58+
System.out.printf("#%d.: %s%n%n", counter, zoneIterator.next().toString());
5959
counter++;
6060
}
6161
}

gcloud-java-examples/src/main/java/com/google/gcloud/examples/dns/snippets/DeleteZone.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@
3636
public class DeleteZone {
3737

3838
public static void main(String... args) {
39-
4039
// Create a service object.
4140
// The project ID and credentials will be inferred from the environment.
42-
Dns dns = DnsOptions.builder().build().service();
41+
Dns dns = DnsOptions.defaultInstance().service();
4342

4443
// Change this to a zone name that exists within your project and that you want to delete.
4544
String zoneName = "some-sample-zone";
@@ -63,14 +62,15 @@ public static void main(String... args) {
6362
changeRequest = dns.applyChangeRequest(zoneName, changeRequest);
6463

6564
// Wait for change to finish, but save data traffic by transferring only ID and status
66-
Dns.ChangeRequestOption option = Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS);
65+
Dns.ChangeRequestOption option =
66+
Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS);
6767
while (ChangeRequest.Status.PENDING.equals(changeRequest.status())) {
6868
System.out.println("Waiting for change to complete. Going to sleep for 500ms...");
6969
try {
7070
Thread.sleep(500);
7171
} catch (InterruptedException e) {
72-
System.err.println("The thread was interrupted while waiting for change request to be " +
73-
"processed.");
72+
System.err.println("The thread was interrupted while waiting for change request to be "
73+
+ "processed.");
7474
}
7575
// Update the change, but fetch only change ID and status
7676
changeRequest = dns.getChangeRequest(zoneName, changeRequest.id(), option);

0 commit comments

Comments
 (0)