Skip to content

Commit 2089991

Browse files
authored
Merge pull request #50 from softlayer/java8
Bump support up to requiring Java 8
2 parents bdf37d0 + 4bc4844 commit 2089991

34 files changed

+482
-2391
lines changed

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
language: java
22
jdk:
33
- oraclejdk8
4-
- oraclejdk7
5-
- openjdk7
6-
- openjdk6
4+
- openjdk8
75
install: mvn install -DskipTests=true -Dmaven.javadoc.skip=true -Dgpg.skip=true

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
## Introduction
88

99
This library provides a JVM client for the [SoftLayer API](http://sldn.softlayer.com/article/SoftLayer-API-Overview). It
10-
has code generated and compiled via Maven. The client can work with any Java 6+ runtime. It uses the code generation
10+
has code generated and compiled via Maven. The client can work with any Java 8+ runtime. It uses the code generation
1111
project in `gen/` to generate the service and type related code. Although likely to work in resource-constrained
1212
environments (i.e. Android, J2ME, etc), using this is not recommended; Use the
1313
[REST](http://sldn.softlayer.com/article/REST) API instead.

examples/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</licenses>
1818
<properties>
1919
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20-
<java.version>1.6</java.version>
20+
<java.version>1.8</java.version>
2121
</properties>
2222
<dependencies>
2323
<dependency>
@@ -54,7 +54,7 @@
5454
<plugin>
5555
<groupId>org.apache.maven.plugins</groupId>
5656
<artifactId>maven-compiler-plugin</artifactId>
57-
<version>3.0</version>
57+
<version>3.7.0</version>
5858
<configuration>
5959
<source>${java.version}</source>
6060
<target>${java.version}</target>

examples/src/main/java/com/softlayer/api/example/AddSecurityGroupRule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void run(ApiClient client) throws Exception {
3131
rule.setDirection("ingress");
3232
rule.setProtocol("udp");
3333

34-
List<Rule> newRules = new ArrayList<Rule>();
34+
List<Rule> newRules = new ArrayList<>();
3535
newRules.add(rule);
3636

3737
// Now add the rule(s) to the security group

examples/src/main/java/com/softlayer/api/example/PaginationAsyncCallback.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.softlayer.api.example;
22

3-
import java.util.Comparator;
43
import java.util.List;
54
import java.util.NavigableSet;
65
import java.util.Set;
@@ -28,12 +27,9 @@ public void run(ApiClient client) throws Exception {
2827
((ThreadPooledHttpClientFactory) HttpClientFactory.getDefault()).setThreadPool(threadPool);
2928

3029
// A thread-safe set is needed to hold the resulting packages ordered by name
31-
final NavigableSet<Package> packages = new ConcurrentSkipListSet<Package>(new Comparator<Package>() {
32-
@Override
33-
public int compare(Package pkg1, Package pkg2) {
34-
return pkg1.getName().compareToIgnoreCase(pkg2.getName());
35-
}
36-
});
30+
final NavigableSet<Package> packages = new ConcurrentSkipListSet<>(
31+
(pkg1, pkg2) -> pkg1.getName().compareToIgnoreCase(pkg2.getName())
32+
);
3733

3834
// To know how many calls have to be made to get all items, an initial call is required to get the
3935
// first set of data AND the total count

examples/src/main/java/com/softlayer/api/example/PaginationAsyncPolling.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.softlayer.api.example;
22

33
import java.util.ArrayList;
4-
import java.util.Comparator;
54
import java.util.List;
65
import java.util.NavigableSet;
76
import java.util.TreeSet;
@@ -27,7 +26,7 @@ public void run(ApiClient client) throws Exception {
2726
((ThreadPooledHttpClientFactory) HttpClientFactory.getDefault()).setThreadPool(threadPool);
2827

2928
// Asynchronous responses are held so they can be waited on once all are submitted
30-
List<PackageResponseWrapper> responses = new ArrayList<PackageResponseWrapper>();
29+
List<PackageResponseWrapper> responses = new ArrayList<>();
3130

3231
// To know how many calls have to be made to get all items, an initial call is required to get the
3332
// first set of data AND the total count
@@ -53,12 +52,9 @@ public void run(ApiClient client) throws Exception {
5352
threadPool.shutdown();
5453

5554
// A set is needed to hold the resulting packages ordered by name
56-
final NavigableSet<Package> packages = new TreeSet<Package>(new Comparator<Package>() {
57-
@Override
58-
public int compare(Package pkg1, Package pkg2) {
59-
return pkg1.getName().compareToIgnoreCase(pkg2.getName());
60-
}
61-
});
55+
final NavigableSet<Package> packages = new TreeSet<>(
56+
(pkg1, pkg2) -> pkg1.getName().compareToIgnoreCase(pkg2.getName())
57+
);
6258

6359
// Unlike the callback approach, this approach guarantees they come in the order requested since a blocking
6460
// call to get() is in the request order

gen/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</licenses>
1818
<properties>
1919
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20-
<java.version>1.6</java.version>
20+
<java.version>1.8</java.version>
2121
<exec.mainClass>com.softlayer.api.gen.Main</exec.mainClass>
2222
</properties>
2323
<dependencies>
@@ -58,12 +58,12 @@
5858
<plugin>
5959
<groupId>org.apache.maven.plugins</groupId>
6060
<artifactId>maven-javadoc-plugin</artifactId>
61-
<version>2.9.1</version>
61+
<version>3.0.0-M1</version>
6262
</plugin>
6363
<plugin>
6464
<groupId>org.apache.maven.plugins</groupId>
6565
<artifactId>maven-compiler-plugin</artifactId>
66-
<version>3.0</version>
66+
<version>3.7.0</version>
6767
<configuration>
6868
<source>${java.version}</source>
6969
<target>${java.version}</target>

gen/src/main/java/com/softlayer/api/gen/ClassWriter.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.io.IOException;
88
import java.io.OutputStreamWriter;
99
import java.io.Writer;
10+
import java.nio.charset.StandardCharsets;
1011
import java.util.Arrays;
1112
import java.util.EnumSet;
1213
import java.util.HashMap;
@@ -21,7 +22,7 @@
2122
public class ClassWriter extends JavaWriter {
2223

2324
public static final String SLDN_URL_BASE_PATH = "http://sldn.softlayer.com/reference/";
24-
25+
2526
public static final String TYPE_API_CLIENT = "com.softlayer.api.ApiClient";
2627
public static final String TYPE_API_METHOD = "com.softlayer.api.annotation.ApiMethod";
2728
public static final String TYPE_API_PROPERTY = "com.softlayer.api.annotation.ApiProperty";
@@ -63,7 +64,7 @@ public static void emitType(File baseDir, TypeClass type, Meta meta) throws IOEx
6364
File fileDir = new File(baseDir, type.packageName.replace('.', '/'));
6465
fileDir.mkdirs();
6566
Writer writer = new BufferedWriter(new OutputStreamWriter(
66-
new FileOutputStream(new File(fileDir, type.className + ".java")), "UTF-8"));
67+
new FileOutputStream(new File(fileDir, type.className + ".java")), StandardCharsets.UTF_8));
6768
try {
6869
new ClassWriter(writer, type, meta).emitType();
6970
} finally {
@@ -97,7 +98,7 @@ public ClassWriter(Writer out, TypeClass type, Meta meta) {
9798

9899
public ClassWriter emitAnnotationWithAttrs(String annotationType, Object... attributes) throws IOException {
99100
int i = 0;
100-
Map<String, Object> attrMap = new HashMap<String, Object>(attributes.length / 2 + 1);
101+
Map<String, Object> attrMap = new HashMap<>(attributes.length / 2 + 1);
101102
while (i < attributes.length) {
102103
String key = attributes[i++].toString();
103104
attrMap.put(key, attributes[i++]);
@@ -134,7 +135,7 @@ public ClassWriter emitProperty(TypeClass.Property property) throws IOException
134135
emitJavadoc(property.meta.doc.replace("\n", "<br />\n"));
135136
}
136137

137-
Map<String, Object> params = new HashMap<String, Object>(2);
138+
Map<String, Object> params = new HashMap<>(2);
138139
if (!property.name.equals(property.meta.name)) {
139140
params.put("value", stringLiteral(property.meta.name));
140141
}
@@ -267,7 +268,7 @@ public ClassWriter emitServiceMethod(TypeClass.Method method, boolean async) thr
267268
"/" + method.meta.name + "\">" + type.meta.name + "::" + method.meta.name + "</a>";
268269
emitJavadoc(javadoc);
269270

270-
Map<String, Object> params = new HashMap<String, Object>(2);
271+
Map<String, Object> params = new HashMap<>(2);
271272
if (!method.name.equals(method.meta.name)) {
272273
params.put("value", stringLiteral(method.meta.name));
273274
}
@@ -433,8 +434,8 @@ public ClassWriter emitType() throws IOException {
433434
}
434435

435436
public ClassWriter emitTypeImports() throws IOException {
436-
Map<String, String> imports = new HashMap<String, String>(type.imports);
437-
437+
Map<String, String> imports = new HashMap<>(type.imports);
438+
438439
imports.remove("Mask");
439440
imports.remove(type.className);
440441
imports.put("ApiType", TYPE_API_TYPE);

gen/src/main/java/com/softlayer/api/gen/Generator.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.net.URL;
6+
import java.nio.file.FileVisitResult;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.nio.file.SimpleFileVisitor;
10+
import java.nio.file.attribute.BasicFileAttributes;
611
import java.util.ArrayList;
712
import java.util.List;
813
import java.util.Set;
@@ -15,7 +20,13 @@ public class Generator {
1520
private final URL metadataUrl;
1621
private final Restriction whitelist;
1722
private final Restriction blacklist;
18-
23+
24+
/**
25+
* @param dir The directory to generate classes into.
26+
* @param metadataUrl The metadata to generate from.
27+
* @param whitelist
28+
* @param blacklist
29+
*/
1930
public Generator(File dir, URL metadataUrl, Restriction whitelist, Restriction blacklist) {
2031
this.dir = dir;
2132
this.metadataUrl = metadataUrl;
@@ -32,7 +43,7 @@ public void buildClient() throws IOException {
3243
applyRestrictions(meta);
3344

3445
log("Generating source code");
35-
List<TypeClass> classes = new ArrayList<TypeClass>(meta.types.size());
46+
List<TypeClass> classes = new ArrayList<>(meta.types.size());
3647
for (Meta.Type type : meta.types.values()) {
3748
TypeClass typeClass = new MetaConverter(BASE_PKG, meta, type).buildTypeClass();
3849
ClassWriter.emitType(dir, typeClass, meta);
@@ -77,16 +88,22 @@ protected void log(String contents) {
7788
System.out.println(contents);
7889
}
7990

80-
public void recursivelyDelete(File file) {
81-
if (file.isDirectory()) {
82-
for (File child : file.listFiles()) {
83-
recursivelyDelete(child);
91+
public void recursivelyDelete(File file) throws IOException {
92+
if (!file.exists()) {
93+
return;
94+
}
95+
Files.walkFileTree(file.toPath(), new SimpleFileVisitor<Path>() {
96+
@Override
97+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
98+
Files.delete(file);
99+
return FileVisitResult.CONTINUE;
84100
}
85-
if (!file.delete()) {
86-
throw new RuntimeException("Unable to delete: " + file);
101+
102+
@Override
103+
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
104+
Files.delete(dir);
105+
return FileVisitResult.CONTINUE;
87106
}
88-
} else if (file.exists() && !file.delete()) {
89-
throw new RuntimeException("Unable to delete: " + file);
90-
}
107+
});
91108
}
92109
}

gen/src/main/java/com/softlayer/api/gen/Main.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
/** Entry point for the code generator */
1515
public class Main {
16+
17+
protected static final String METADATA_URL = "https://api.softlayer.com/metadata/v3.1";
18+
protected static final String DEFAULT_SOURCE_PATH = "../src/main/java";
1619

1720
public static final String USAGE =
1821
"Arguments:\n\n" +
@@ -35,15 +38,15 @@ public static void main(String[] args) throws Exception {
3538
Restriction whitelist;
3639
Restriction blacklist;
3740
try {
38-
List<String> argList = new ArrayList<String>(Arrays.asList(args));
41+
List<String> argList = Arrays.asList(args);
3942
if (argList.contains("--help")) {
4043
System.out.println(USAGE);
4144
return;
4245
}
4346
String dirString = getArg("--src", argList);
44-
dir = new File(dirString != null ? dirString : "../src/main/java");
47+
dir = new File(dirString != null ? dirString : DEFAULT_SOURCE_PATH);
4548
String urlString = getArg("--url", argList);
46-
url = new URL(urlString != null ? urlString : "https://api.softlayer.com/metadata/v3.1");
49+
url = new URL(urlString != null ? urlString : METADATA_URL);
4750
whitelist = getRestriction(getArg("--whitelist", argList));
4851
blacklist = getRestriction(getArg("--blacklist", argList));
4952
if (whitelist != null && blacklist != null) {
@@ -83,17 +86,13 @@ private static Restriction getRestriction(String filename) throws IOException {
8386
line = line.trim();
8487
if (line.contains("::")) {
8588
String pieces[] = line.split("::", 2);
86-
Set<String> methods = restriction.methods.get(pieces[0]);
87-
if (methods == null) {
88-
methods = new HashSet<String>();
89-
restriction.methods.put(pieces[0], methods);
90-
}
89+
Set<String> methods = restriction.methods.computeIfAbsent(pieces[0], key -> new HashSet<>());
9190
methods.add(pieces[1]);
9291
} else if (line.contains(".")) {
9392
String pieces[] = line.split(".", 2);
9493
Set<String> properties = restriction.properties.get(pieces[0]);
9594
if (properties == null) {
96-
properties = new HashSet<String>();
95+
properties = new HashSet<>();
9796
restriction.methods.put(pieces[0], properties);
9897
}
9998
properties.add(pieces[1]);

0 commit comments

Comments
 (0)