Skip to content

Commit

Permalink
HTTP files reuse the same temporary file for downloading (#149).
Browse files Browse the repository at this point in the history
  • Loading branch information
jshiell committed Apr 10, 2015
1 parent 7cc78e7 commit 5499d7e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 54 deletions.
39 changes: 20 additions & 19 deletions CheckStyle-IDEA.ipr
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,26 @@
<entry key="check-test-classes" value="false" />
<entry key="location-0" value="LOCAL_FILE:$PRJ_DIR$/test-configs/amigo-cs5.0.xml:A2" />
<entry key="location-1" value="LOCAL_FILE:$PRJ_DIR$/test-configs/amigo-cs5.0.xml:Amigo" />
<entry key="location-10" value="CLASSPATH:/sun_checks.xml:The default CheckStyle rules" />
<entry key="location-11" value="LOCAL_FILE:$PRJ_DIR$/test-configs/yazino-checkstyle-rules.xml:Yazino" />
<entry key="location-12" value="LOCAL_FILE:$PRJ_DIR$/test-configs/beyondtrack_checks.xml:BT" />
<entry key="location-13" value="LOCAL_FILE:$PRJ_DIR$/test-configs/import-control.xml:ImportControl" />
<entry key="location-14" value="LOCAL_FILE:$PRJ_DIR$/test-configs/foundation_checkstyle.xml:Issue 119" />
<entry key="location-15" value="LOCAL_FILE:$PRJ_DIR$/test-configs/issue-130.xml:Issue 130" />
<entry key="location-16" value="LOCAL_FILE:$PRJ_DIR$/test-configs/issue-23.checkstyle:Issue 23" />
<entry key="location-2" value="LOCAL_FILE:$PRJ_DIR$/test-configs/issue-53.xml:Issue 53" />
<entry key="location-3" value="LOCAL_FILE:$PRJ_DIR$/test-configs/beyondtrack_checks.xml:Issue 86" />
<entry key="location-4" value="LOCAL_FILE:$PRJ_DIR$/test-configs/issue-96.xml:Issue 96" />
<entry key="location-5" value="LOCAL_FILE:$PRJ_DIR$/test-configs/issue-98.xml:Issue 98" />
<entry key="location-6" value="LOCAL_FILE:$PRJ_DIR$/test-configs/testy.jar!/yazino-checkstyle-rules.xml:JAR test" />
<entry key="location-7" value="LOCAL_FILE:$PRJ_DIR$/test-configs/man-checkstyle.xml:Man" />
<entry key="location-8" value="LOCAL_FILE:$PRJ_DIR$/test-configs/man-checkstyle.xml:Man2" />
<entry key="location-9" value="LOCAL_FILE:$PRJ_DIR$/test-configs/sonar.xml:Sonar" />
<entry key="property-11.checkstyle.suppressions.file" value="$PROJECT_DIR$/test-configs/yazino-checkstyle-suppressions.xml" />
<entry key="property-14.checkstyle.suppressions.file" value="checkstyleSuppressions.xml" />
<entry key="property-5.samedir" value="" />
<entry key="property-6.checkstyle.suppressions.file" value="yazino-checkstyle-suppressions.xml" />
<entry key="location-10" value="LOCAL_FILE:$PRJ_DIR$/test-configs/issue-98.xml:Issue 98" />
<entry key="location-11" value="LOCAL_FILE:$PRJ_DIR$/test-configs/testy.jar!/yazino-checkstyle-rules.xml:JAR test" />
<entry key="location-12" value="LOCAL_FILE:$PRJ_DIR$/test-configs/man-checkstyle.xml:Man" />
<entry key="location-13" value="LOCAL_FILE:$PRJ_DIR$/test-configs/man-checkstyle.xml:Man2" />
<entry key="location-14" value="LOCAL_FILE:$PRJ_DIR$/test-configs/sonar.xml:Sonar" />
<entry key="location-15" value="CLASSPATH:/sun_checks.xml:The default CheckStyle rules" />
<entry key="location-16" value="LOCAL_FILE:$PRJ_DIR$/test-configs/yazino-checkstyle-rules.xml:Yazino" />
<entry key="location-17" value="HTTP_URL:https://simia-infinitus.com/amigo-cs5.0.xml:Amigo HTTP" />
<entry key="location-2" value="LOCAL_FILE:$PRJ_DIR$/test-configs/beyondtrack_checks.xml:BT" />
<entry key="location-3" value="LOCAL_FILE:$PRJ_DIR$/test-configs/import-control.xml:ImportControl" />
<entry key="location-4" value="LOCAL_FILE:$PRJ_DIR$/test-configs/foundation_checkstyle.xml:Issue 119" />
<entry key="location-5" value="LOCAL_FILE:$PRJ_DIR$/test-configs/issue-130.xml:Issue 130" />
<entry key="location-6" value="LOCAL_FILE:$PRJ_DIR$/test-configs/issue-23.checkstyle:Issue 23" />
<entry key="location-7" value="LOCAL_FILE:$PRJ_DIR$/test-configs/issue-53.xml:Issue 53" />
<entry key="location-8" value="LOCAL_FILE:$PRJ_DIR$/test-configs/beyondtrack_checks.xml:Issue 86" />
<entry key="location-9" value="LOCAL_FILE:$PRJ_DIR$/test-configs/issue-96.xml:Issue 96" />
<entry key="property-10.samedir" value="" />
<entry key="property-11.checkstyle.suppressions.file" value="yazino-checkstyle-suppressions.xml" />
<entry key="property-16.checkstyle.suppressions.file" value="$PROJECT_DIR$/test-configs/yazino-checkstyle-suppressions.xml" />
<entry key="property-4.checkstyle.suppressions.file" value="checkstyleSuppressions.xml" />
<entry key="suppress-errors" value="false" />
<entry key="thirdparty-classpath" value="$PROJECT_DIR$/../Third Party/CreateYourOwnCheckStyleCheck/BlundellCheckstyle/target/blundell-checkstyle-checks-1.0.jar" />
</map>
Expand Down
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ LICENCE file.
Version History
---------------

4.14.1 - Fixed: HTTP rules files should result in fewer temporary files (#149).
4.14.1 - Fixed: findFile now uses ReadAction (#140).
4.14.0 - New: Moved to CheckStyle 6.5, which requires Java 7 or above.
4.14.0 - New: Moved to Java 8. Please make sure IDEA is running on JDK 8. OS X users must use the 14.1 build with the bundled JDK.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class HTTPURLConfigurationLocation extends ConfigurationLocation {

private static final Log LOG = LogFactory.getLog(HTTPURLConfigurationLocation.class);

private File tempFile;

/**
* Create a new URL configuration.
*/
Expand All @@ -29,53 +31,59 @@ public class HTTPURLConfigurationLocation extends ConfigurationLocation {

@NotNull
protected InputStream resolveFile() throws IOException {
Reader reader = null;
Writer writer = null;
try {
final URL url = new URL(getLocation());
final URLConnection urlConnection = url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(false);
urlConnection.setAllowUserInteraction(false);

if (url.getUserInfo() != null) {
final String basicAuth = "Basic " + DatatypeConverter.printBase64Binary(url.getUserInfo().getBytes());
urlConnection.setRequestProperty("Authorization", basicAuth);
}
return new FileInputStream(writeFileTo(connectionTo(getLocation()), temporaryFile()));

} catch (IOException e) {
LOG.error("Couldn't read URL: " + getLocation(), e);
throw e;
}
}

@NotNull
private URLConnection connectionTo(final String location) throws IOException {
final URL url = new URL(location);
final URLConnection urlConnection = url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(false);
urlConnection.setAllowUserInteraction(false);

addBasicAuth(url, urlConnection);

final File tempFile = File.createTempFile("checkStyle", ".xml");
tempFile.deleteOnExit();
writer = new BufferedWriter(new FileWriter(tempFile));
return urlConnection;
}

urlConnection.connect();
reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
private void addBasicAuth(final URL url, final URLConnection urlConnection) {
if (url.getUserInfo() != null) {
urlConnection.setRequestProperty("Authorization",
"Basic " + DatatypeConverter.printBase64Binary(url.getUserInfo().getBytes()));
}
}

private File writeFileTo(final URLConnection urlConnection, final File destinationFile) throws IOException {
urlConnection.connect();

try (Writer writer = new BufferedWriter(new FileWriter(destinationFile));
Reader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()))) {
int readChar;
while ((readChar = reader.read()) != -1) {
writer.write(readChar);
}

writer.flush();
return new FileInputStream(tempFile);

} catch (IOException e) {
LOG.error("Couldn't read URL: " + getLocation(), e);
throw e;
return destinationFile;
}
}

} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// ignore
}
}
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
// ignore
}
@NotNull
private File temporaryFile() throws IOException {
synchronized (this) {
if (tempFile == null) {
tempFile = File.createTempFile("checkStyle", ".xml");
tempFile.deleteOnExit();
}
return tempFile;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<change-notes>
<![CDATA[
<ul>
<li>4.14.1: Fixed: HTTP rules files should result in fewer temporary files (#149).</li>
<li>4.14.1: Fixed: findFile now uses ReadAction (#140).</li>
<li>4.14.0: New: Moved to CheckStyle 6.5, which requires Java 7 or above.</li>
<li>4.14.0: New: Moved to Java 8. Please make sure IDEA is running on JDK 8. OS X users must use the 14.1 build with the bundled JDK.</li>
Expand Down

0 comments on commit 5499d7e

Please sign in to comment.