Bump to 2.12-SNAPSHOT and use of java.nio.Path
Change-Id: I72fb8c5cfc2f37096f0b2892b11d0baa832b559e
diff --git a/src/main/java/com/googlesource/gerrit/plugins/scripting/scala/ScalaPluginProvider.java b/src/main/java/com/googlesource/gerrit/plugins/scripting/scala/ScalaPluginProvider.java
index 9eeffb5..eb4fa13 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/scripting/scala/ScalaPluginProvider.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/scripting/scala/ScalaPluginProvider.java
@@ -24,7 +24,7 @@
import org.eclipse.jgit.internal.storage.file.FileSnapshot;
-import java.io.File;
+import java.nio.file.Path;
/**
* Scala scripting plugins.
@@ -62,7 +62,7 @@
}
@Override
- public ServerPlugin get(File srcFile,
+ public ServerPlugin get(Path srcFile,
FileSnapshot snapshot, PluginDescription description)
throws InvalidPluginException {
ScalaPluginScriptEngine scriptEngine = scriptEngineProvider.get();
@@ -73,13 +73,13 @@
}
@Override
- public boolean handles(File srcFile) {
- return srcFile.getName().toLowerCase().endsWith(SCALA_EXTENSION);
+ public boolean handles(Path srcFile) {
+ return srcFile.toString().toLowerCase().endsWith(SCALA_EXTENSION);
}
@Override
- public String getPluginName(File srcFile) {
- String srcFileName = srcFile.getName();
+ public String getPluginName(Path srcFile) {
+ String srcFileName = srcFile.getFileName().toString();
int endPos = srcFileName.lastIndexOf('-');
if (endPos == -1) {
endPos = srcFileName.lastIndexOf('.');
diff --git a/src/main/java/com/googlesource/gerrit/plugins/scripting/scala/ScalaPluginScanner.java b/src/main/java/com/googlesource/gerrit/plugins/scripting/scala/ScalaPluginScanner.java
index 485a87c..e3135dd 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/scripting/scala/ScalaPluginScanner.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/scripting/scala/ScalaPluginScanner.java
@@ -21,16 +21,16 @@
import com.googlesource.gerrit.plugins.web.WebPluginScanner;
-import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.file.Path;
import java.util.Enumeration;
import java.util.Set;
public class ScalaPluginScanner extends AbstractPreloadedPluginScanner {
private final WebPluginScanner webScanner;
- public ScalaPluginScanner(String pluginName, File srcFile,
+ public ScalaPluginScanner(String pluginName, Path srcFile,
ScalaPluginScriptEngine scriptEngine) throws InvalidPluginException {
super(pluginName, getPluginVersion(srcFile), loadScriptClasses(srcFile,
scriptEngine), Plugin.ApiType.PLUGIN);
@@ -38,8 +38,8 @@
this.webScanner = new WebPluginScanner(srcFile);
}
- private static String getPluginVersion(File srcFile) {
- String srcFileName = srcFile.getName();
+ private static String getPluginVersion(Path srcFile) {
+ String srcFileName = srcFile.getFileName().toString();
int startPos = srcFileName.lastIndexOf('-');
if (startPos == -1) {
return "0";
@@ -48,7 +48,7 @@
return srcFileName.substring(startPos + 1, endPos);
}
- private static Set<Class<?>> loadScriptClasses(File srcFile,
+ private static Set<Class<?>> loadScriptClasses(Path srcFile,
ScalaPluginScriptEngine scriptEngine) throws InvalidPluginException {
try {
return scriptEngine.eval(srcFile);
@@ -58,14 +58,17 @@
}
}
- public Optional<PluginEntry> getEntry(String resourcePath) {
+ @Override
+ public Optional<PluginEntry> getEntry(String resourcePath) throws IOException {
return webScanner.getEntry(resourcePath);
}
+ @Override
public InputStream getInputStream(PluginEntry entry) throws IOException {
return webScanner.getInputStream(entry);
}
+ @Override
public Enumeration<PluginEntry> entries() {
return webScanner.entries();
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/scripting/scala/ScalaPluginScriptEngine.java b/src/main/java/com/googlesource/gerrit/plugins/scripting/scala/ScalaPluginScriptEngine.java
index 91b8eb1..ccabb82 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/scripting/scala/ScalaPluginScriptEngine.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/scripting/scala/ScalaPluginScriptEngine.java
@@ -16,6 +16,7 @@
import static scala.collection.JavaConversions.asScalaBuffer;
import com.google.common.base.Function;
+import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.inject.Inject;
@@ -36,10 +37,8 @@
import scala.tools.nsc.Global;
import scala.tools.nsc.Global.Run;
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileReader;
import java.io.IOException;
+import java.nio.charset.Charset;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
@@ -158,11 +157,11 @@
this.reporter = reporter;
}
- public Set<Class<?>> eval(File scalaFile) throws IOException,
+ public Set<Class<?>> eval(Path scalaFile) throws IOException,
ClassNotFoundException {
- if (scalaFile.isFile()) {
+ if (Files.isRegularFile(scalaFile)) {
return evalFiles(Arrays.asList(scalaFile));
- } else if (scalaFile.isDirectory()) {
+ } else if (Files.isDirectory(scalaFile)) {
return evalDirectory(scalaFile);
} else {
throw new IOException("File " + scalaFile
@@ -170,20 +169,19 @@
}
}
- private Set<Class<?>> evalDirectory(File scalaFile) throws IOException,
+ private Set<Class<?>> evalDirectory(Path scalaFile) throws IOException,
ClassNotFoundException {
- final List<File> scalaFiles = Lists.newArrayList();
+ final List<Path> scalaFiles = Lists.newArrayList();
- Files.walkFileTree(scalaFile.toPath(),
+ Files.walkFileTree(scalaFile,
EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs)
throws IOException {
- File file = path.toFile();
- String fileName = file.getName();
- if (file.isFile() && fileName.endsWith(ScalaPluginProvider.SCALA_EXTENSION)) {
- scalaFiles.add(file);
+ String fileName = path.getFileName().toString();
+ if (Files.isRegularFile(path) && fileName.endsWith(ScalaPluginProvider.SCALA_EXTENSION)) {
+ scalaFiles.add(path);
}
return FileVisitResult.CONTINUE;
}
@@ -191,15 +189,15 @@
return evalFiles(scalaFiles);
}
- private Set<Class<?>> evalFiles(List<File> scalaFiles) throws IOException,
+ private Set<Class<?>> evalFiles(List<Path> scalaFiles) throws IOException,
ClassNotFoundException {
Set<Class<?>> classes = Sets.newHashSet();
- List<SourceFile> scalaSourceFiles = Lists.transform(scalaFiles, new Function<File,SourceFile>() {
+ List<SourceFile> scalaSourceFiles = Lists.transform(scalaFiles, new Function<Path,SourceFile>() {
@Override
- public SourceFile apply(File scalaFile) {
+ public SourceFile apply(Path scalaFile) {
try {
- return new BatchSourceFile(scalaFile.getName(), readScalaFile(scalaFile));
+ return new BatchSourceFile(scalaFile.toString(), readScalaFile(scalaFile));
} catch (IOException e) {
throw new IllegalArgumentException("Cannot load scala file " + scalaFile, e);
}
@@ -227,20 +225,11 @@
return classes;
}
- private Seq<Object> readScalaFile(File scalaFile) throws IOException {
- BufferedReader reader = new BufferedReader(new FileReader(scalaFile));
- StringBuilder scalaCode = new StringBuilder();
- try {
- String line;
- while (null != (line = reader.readLine())) {
- scalaCode.append(line);
- scalaCode.append("\n");
- }
- } finally {
- reader.close();
- }
+ private Seq<Object> readScalaFile(Path scalaFile) throws IOException {
+ List<String> scalaFileLines = Files.readAllLines(scalaFile, Charset.forName("UTF-8"));
+ String scalaCode = Joiner.on('\n').join(scalaFileLines);
- List<Object> chars = new ArrayList<Object>();
+ List<Object> chars = new ArrayList<>();
for (char c : scalaCode.toString().toCharArray()) {
chars.add(Char.unbox(c));
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/web/LookAheadFileInputStream.java b/src/main/java/com/googlesource/gerrit/plugins/web/LookAheadFileInputStream.java
index e8c858f..d1a672e 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/web/LookAheadFileInputStream.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/web/LookAheadFileInputStream.java
@@ -14,9 +14,9 @@
package com.googlesource.gerrit.plugins.web;
import java.io.BufferedInputStream;
-import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.Arrays;
class LookAheadFileInputStream extends BufferedInputStream {
@@ -29,14 +29,14 @@
private final String fileExtension;
private final String fileName;
- private final File currentDir;
+ private final Path currentDir;
- public LookAheadFileInputStream(File inputFile) throws IOException {
- super(new FileInputStream(inputFile), BUFFER_SIZE);
+ public LookAheadFileInputStream(Path inputFile) throws IOException {
+ super(Files.newInputStream(inputFile), BUFFER_SIZE);
- fileName = inputFile.getName();
+ fileName = inputFile.getFileName().toString();
fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1);
- currentDir = inputFile.getParentFile();
+ currentDir = inputFile.getParent();
}
@Override
@@ -45,6 +45,7 @@
+ " buffer=\'" + new String(buf, pos, count - pos) + "'";
}
+ @Override
public synchronized int read() throws IOException {
lastChar = super.read();
if (isNewLine()) {
@@ -99,7 +100,7 @@
return fileName;
}
- public File getCurrentDir() {
+ public Path getCurrentDir() {
return currentDir;
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/web/SSIPageInputStream.java b/src/main/java/com/googlesource/gerrit/plugins/web/SSIPageInputStream.java
index a112dd3..965b97a 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/web/SSIPageInputStream.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/web/SSIPageInputStream.java
@@ -13,9 +13,10 @@
// limitations under the License.
package com.googlesource.gerrit.plugins.web;
-import java.io.File;
import java.io.FilterInputStream;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.Stack;
public class SSIPageInputStream extends FilterInputStream {
@@ -25,11 +26,11 @@
private LookAheadFileInputStream currentIs;
private Stack<LookAheadFileInputStream> fileInputStreamStack;
- private final File basePath;
+ private final Path basePath;
- public SSIPageInputStream(File basePath, String filePath)
+ public SSIPageInputStream(Path basePath, String filePath)
throws IOException {
- super(new LookAheadFileInputStream(new File(basePath, filePath)));
+ super(new LookAheadFileInputStream(basePath.resolve(filePath)));
this.basePath = basePath;
this.fileInputStreamStack = new Stack<>();
@@ -60,8 +61,8 @@
private void push(String includeFileName) throws IOException {
fileInputStreamStack.push(currentIs);
- File inputFile = getFile(includeFileName);
- if (!inputFile.exists()) {
+ Path inputFile = getFile(includeFileName);
+ if (!Files.exists(inputFile)) {
throw new IOException("Cannot find file '" + includeFileName
+ "' included in " + currentIs.getFileName() + ":"
+ currentIs.getLineNr());
@@ -70,11 +71,11 @@
in = currentIs;
}
- private File getFile(String includeFileName) {
+ private Path getFile(String includeFileName) {
if (includeFileName.startsWith("/")) {
- return new File(basePath, includeFileName);
+ return basePath.resolve(includeFileName);
} else {
- return new File(currentIs.getCurrentDir(), includeFileName);
+ return currentIs.getCurrentDir().resolve(includeFileName);
}
}
@@ -110,10 +111,12 @@
}
}
+ @Override
public int read(byte b[]) throws IOException {
return read(b, 0, b.length);
}
+ @Override
public int read(byte b[], int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
diff --git a/src/main/java/com/googlesource/gerrit/plugins/web/WebPluginScanner.java b/src/main/java/com/googlesource/gerrit/plugins/web/WebPluginScanner.java
index fc73408..f2af09b 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/web/WebPluginScanner.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/web/WebPluginScanner.java
@@ -20,8 +20,6 @@
import com.google.gerrit.server.plugins.PluginEntry;
import com.google.inject.Inject;
-import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
@@ -39,10 +37,10 @@
import java.util.jar.Manifest;
public class WebPluginScanner implements PluginContentScanner {
- private final File staticResourcesPath;
+ private final Path staticResourcesPath;
@Inject
- public WebPluginScanner(File rootDir) {
+ public WebPluginScanner(Path rootDir) {
this.staticResourcesPath = rootDir;
}
@@ -59,32 +57,33 @@
}
@Override
- public Optional<PluginEntry> getEntry(String resourcePath) {
- File resourceFile = getResourceFile(resourcePath);
- if (resourceFile.exists() && resourceFile.length() > 0) {
+ public Optional<PluginEntry> getEntry(String resourcePath) throws IOException {
+ Path resourceFile = getResourceFile(resourcePath);
+ if (Files.exists(resourceFile) && Files.size(resourceFile) > 0) {
return resourceOf(resourcePath);
} else {
return Optional.absent();
}
}
- private Optional<PluginEntry> resourceOf(String resourcePath) {
- File file = getResourceFile(resourcePath);
- if (file.exists() && file.length() > 0) {
+ private Optional<PluginEntry> resourceOf(String resourcePath) throws IOException {
+ Path file = getResourceFile(resourcePath);
+ long fileSize = Files.size(file);
+ if (Files.exists(file) && fileSize > 0) {
+ long fileLastModifiedTimeMillis = Files.getLastModifiedTime(file).toMillis();
if (resourcePath.endsWith("html")) {
- return Optional.of(new PluginEntry(resourcePath, file.lastModified()));
+ return Optional.of(new PluginEntry(resourcePath, fileLastModifiedTimeMillis));
} else {
- return Optional.of(new PluginEntry(resourcePath, file.lastModified(),
- Optional.of(file.length())));
+ return Optional.of(new PluginEntry(resourcePath, fileLastModifiedTimeMillis,
+ Optional.of(fileSize)));
}
} else {
return Optional.absent();
}
}
- private File getResourceFile(String resourcePath) {
- File resourceFile = new File(staticResourcesPath, resourcePath);
- return resourceFile;
+ private Path getResourceFile(String resourcePath) {
+ return staticResourcesPath.resolve(resourcePath);
}
@Override
@@ -94,7 +93,7 @@
if(name.endsWith("html")) {
return new SSIPageInputStream(staticResourcesPath, name);
} else {
- return new FileInputStream(getResourceFile(name));
+ return Files.newInputStream(getResourceFile(name));
}
}
@@ -102,10 +101,10 @@
public Enumeration<PluginEntry> entries() {
final List<PluginEntry> resourcesList = Lists.newArrayList();
try {
- Files.walkFileTree(staticResourcesPath.toPath(),
+ Files.walkFileTree(staticResourcesPath,
EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
new SimpleFileVisitor<Path>() {
- private int basicPathLength = staticResourcesPath.getAbsolutePath()
+ private int basicPathLength = staticResourcesPath.toAbsolutePath().toString()
.length();
@Override