From b393f5b9fd16641b14383ea709c750be8e006061 Mon Sep 17 00:00:00 2001 From: Michael Simacek <msimacek@redhat.com> Date: Thu, 19 Mar 2015 17:13:05 +0100 Subject: [PATCH 2/4] Port to guava-18 --- .../main/java/org/sonar/core/review/ReviewDao.java | 4 +-- .../java/org/sonar/api/utils/HttpDownloader.java | 34 ++++++++++++++++++---- .../sonar/api/utils/command/CommandExecutor.java | 23 +++++++++++---- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/sonar-core/src/main/java/org/sonar/core/review/ReviewDao.java b/sonar-core/src/main/java/org/sonar/core/review/ReviewDao.java index ba5572f..7a7aad7 100644 --- a/sonar-core/src/main/java/org/sonar/core/review/ReviewDao.java +++ b/sonar-core/src/main/java/org/sonar/core/review/ReviewDao.java @@ -22,7 +22,7 @@ package org.sonar.core.review; import com.google.common.base.Preconditions; import com.google.common.base.Predicate; import com.google.common.base.Predicates; -import com.google.common.cache.Cache; +import com.google.common.cache.LoadingCache; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.collect.Collections2; @@ -37,7 +37,7 @@ import java.util.Collection; public class ReviewDao implements BatchComponent, ServerComponent { private final MyBatis mybatis; - private final Cache<Long, Collection<ReviewDto>> cacheByResource; + private final LoadingCache<Long, Collection<ReviewDto>> cacheByResource; public ReviewDao(MyBatis mybatis) { this.mybatis = mybatis; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java index 7b57626..8e4dae7 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java @@ -20,14 +20,13 @@ package org.sonar.api.utils; import com.google.common.annotations.VisibleForTesting; - import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import com.google.common.io.ByteStreams; import com.google.common.io.CharStreams; import com.google.common.io.Files; -import com.google.common.io.InputSupplier; + import org.apache.commons.io.FileUtils; import org.slf4j.LoggerFactory; import org.sonar.api.BatchComponent; @@ -36,8 +35,10 @@ import org.sonar.api.config.Settings; import org.sonar.api.platform.Server; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.net.Authenticator; import java.net.HttpURLConnection; import java.net.PasswordAuthentication; @@ -137,7 +138,12 @@ public class HttpDownloader extends UriReader.SchemeProcessor implements BatchCo @Override byte[] readBytes(URI uri) { try { - return ByteStreams.toByteArray(new HttpInputSupplier(uri)); + InputStream is = null; + try { + return ByteStreams.toByteArray(is = new HttpInputSupplier(uri).getInput()); + } finally { + if (is != null) is.close(); + } } catch (IOException e) { throw failToDownload(uri, e); } @@ -146,7 +152,14 @@ public class HttpDownloader extends UriReader.SchemeProcessor implements BatchCo @Override String readString(URI uri, Charset charset) { try { - return CharStreams.toString(CharStreams.newReaderSupplier(new HttpInputSupplier(uri), charset)); + InputStream is = null; + try { + is = new HttpInputSupplier(uri).getInput(); + InputStreamReader rdr = new InputStreamReader(is, charset); + return CharStreams.toString(rdr); + } finally { + if (is != null) is.close(); + } } catch (IOException e) { throw failToDownload(uri, e); } @@ -169,8 +182,17 @@ public class HttpDownloader extends UriReader.SchemeProcessor implements BatchCo } public void download(URI uri, File toFile) { + FileOutputStream fo = null; + InputStream is = null; try { - Files.copy(new HttpInputSupplier(uri), toFile); + try { + fo = new FileOutputStream(toFile); + is = new HttpInputSupplier(uri).getInput(); + ByteStreams.copy(is, fo); + } finally { + if (fo != null) fo.close(); + if (is != null) is.close(); + } } catch (IOException e) { FileUtils.deleteQuietly(toFile); throw failToDownload(uri, e); @@ -181,7 +203,7 @@ public class HttpDownloader extends UriReader.SchemeProcessor implements BatchCo return new SonarException(String.format("Fail to download the file: %s (%s)", uri, getProxySynthesis(uri)), e); } - class HttpInputSupplier implements InputSupplier<InputStream> { + class HttpInputSupplier { private final URI uri; HttpInputSupplier(URI uri) { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java index b5da739..bae7079 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java @@ -23,6 +23,7 @@ import com.google.common.io.Closeables; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.Closeable; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -129,12 +130,22 @@ public class CommandExecutor { return execute(command, new DefaultConsumer(), new DefaultConsumer(), timeoutMilliseconds); } + private static final Logger closeablesLogger = LoggerFactory.getLogger(Closeables.class.getName()); + + private static void closeQuietly(Closeable closeable) { + try { + Closeables.close(closeable, true); + } catch (IOException e) { + closeablesLogger.warn("IOException should not have been thrown.", e); + } + } + private void closeStreams(Process process) { if (process != null) { - Closeables.closeQuietly(process.getInputStream()); - Closeables.closeQuietly(process.getInputStream()); - Closeables.closeQuietly(process.getOutputStream()); - Closeables.closeQuietly(process.getErrorStream()); + closeQuietly(process.getInputStream()); + closeQuietly(process.getInputStream()); + closeQuietly(process.getOutputStream()); + closeQuietly(process.getErrorStream()); } } @@ -172,8 +183,8 @@ public class CommandExecutor { exception = ioe; } finally { - Closeables.closeQuietly(br); - Closeables.closeQuietly(isr); + closeQuietly(br); + closeQuietly(isr); } } -- 2.5.0