Merge pull request #44 from spekr/master

Close streams in finally clause
This commit is contained in:
Matthias Kurz 2018-07-17 12:15:48 +02:00 committed by GitHub
commit 7553eba073
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 89 additions and 52 deletions

View File

@ -1,18 +1,17 @@
package com.redfin.sitemapgenerator; package com.redfin.sitemapgenerator;
import org.xml.sax.SAXException;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
import org.xml.sax.SAXException;
abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGenerator<U,THIS>> { abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGenerator<U,THIS>> {
/** 50000 URLs per sitemap maximum */ /** 50000 URLs per sitemap maximum */
public static final int MAX_URLS_PER_SITEMAP = 50000; public static final int MAX_URLS_PER_SITEMAP = 50000;
@ -69,7 +68,11 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
if (!allowMultipleSitemaps) throw new RuntimeException("More than " + maxUrls + " urls, but allowMultipleSitemaps is false. Enable allowMultipleSitemaps to split the sitemap into multiple files with a sitemap index."); if (!allowMultipleSitemaps) throw new RuntimeException("More than " + maxUrls + " urls, but allowMultipleSitemaps is false. Enable allowMultipleSitemaps to split the sitemap into multiple files with a sitemap index.");
if (baseDir != null) { if (baseDir != null) {
if (mapCount == 0) mapCount++; if (mapCount == 0) mapCount++;
writeSiteMap(); try {
writeSiteMap();
} catch(IOException ex) {
throw new RuntimeException("Closing of stream failed.", ex);
}
mapCount++; mapCount++;
urls.clear(); urls.clear();
} }
@ -105,9 +108,8 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
* or write out one sitemap immediately. * or write out one sitemap immediately.
* @param urls the URLs to add to this sitemap * @param urls the URLs to add to this sitemap
* @return this * @return this
* @throws MalformedURLException
*/ */
public THIS addUrls(String... urls) throws MalformedURLException { public THIS addUrls(String... urls) {
for (String url : urls) addUrl(url); for (String url : urls) addUrl(url);
return getThis(); return getThis();
} }
@ -117,16 +119,15 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
* or else write out one sitemap immediately. * or else write out one sitemap immediately.
* @param url the URL to add to this sitemap * @param url the URL to add to this sitemap
* @return this * @return this
* @throws MalformedURLException
*/ */
public THIS addUrl(String url) throws MalformedURLException { public THIS addUrl(String url) {
U sitemapUrl; U sitemapUrl;
try { try {
sitemapUrl = renderer.getUrlClass().getConstructor(String.class).newInstance(url); sitemapUrl = renderer.getUrlClass().getConstructor(String.class).newInstance(url);
return addUrl(sitemapUrl);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
return addUrl(sitemapUrl);
} }
/** Add multiple URLs of the appropriate type to this sitemap, one at a time. /** Add multiple URLs of the appropriate type to this sitemap, one at a time.
@ -150,10 +151,10 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
U sitemapUrl; U sitemapUrl;
try { try {
sitemapUrl = renderer.getUrlClass().getConstructor(URL.class).newInstance(url); sitemapUrl = renderer.getUrlClass().getConstructor(URL.class).newInstance(url);
return addUrl(sitemapUrl);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
return addUrl(sitemapUrl);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -168,7 +169,11 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
public List<File> write() { public List<File> write() {
if (finished) throw new RuntimeException("Sitemap already printed; you must create a new generator to make more sitemaps"); if (finished) throw new RuntimeException("Sitemap already printed; you must create a new generator to make more sitemaps");
if (!allowEmptySitemap && urls.isEmpty() && mapCount == 0) throw new RuntimeException("No URLs added, sitemap would be empty; you must add some URLs with addUrls"); if (!allowEmptySitemap && urls.isEmpty() && mapCount == 0) throw new RuntimeException("No URLs added, sitemap would be empty; you must add some URLs with addUrls");
writeSiteMap(); try {
writeSiteMap();
} catch (IOException ex) {
throw new RuntimeException("Closing of streams has failed at some point.", ex);
}
finished = true; finished = true;
return outFiles; return outFiles;
} }
@ -231,7 +236,7 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
return outFile; return outFile;
} }
private void writeSiteMap() { private void writeSiteMap() throws IOException {
if (baseDir == null) { if (baseDir == null) {
throw new NullPointerException("To write to files, baseDir must not be null"); throw new NullPointerException("To write to files, baseDir must not be null");
} }
@ -244,8 +249,9 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
} }
File outFile = new File(baseDir, fileNamePrefix+fileNameSuffix); File outFile = new File(baseDir, fileNamePrefix+fileNameSuffix);
outFiles.add(outFile); outFiles.add(outFile);
OutputStreamWriter out = null;
try { try {
OutputStreamWriter out;
if (gzip) { if (gzip) {
FileOutputStream fileStream = new FileOutputStream(outFile); FileOutputStream fileStream = new FileOutputStream(outFile);
GZIPOutputStream gzipStream = new GZIPOutputStream(fileStream); GZIPOutputStream gzipStream = new GZIPOutputStream(fileStream);
@ -253,13 +259,19 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
} else { } else {
out = new OutputStreamWriter(new FileOutputStream(outFile), Charset.forName("UTF-8").newEncoder()); out = new OutputStreamWriter(new FileOutputStream(outFile), Charset.forName("UTF-8").newEncoder());
} }
writeSiteMap(out); writeSiteMap(out);
out.flush();
if (autoValidate) SitemapValidator.validateWebSitemap(outFile); if (autoValidate) SitemapValidator.validateWebSitemap(outFile);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("Problem writing sitemap file " + outFile, e); throw new RuntimeException("Problem writing sitemap file " + outFile, e);
} catch (SAXException e) { } catch (SAXException e) {
throw new RuntimeException("Sitemap file failed to validate (bug?)", e); throw new RuntimeException("Sitemap file failed to validate (bug?)", e);
} finally {
if(out != null) {
out.close();
}
} }
} }
@ -267,7 +279,6 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
writeSiteMapAsString(sb, urls); writeSiteMapAsString(sb, urls);
out.write(sb.toString()); out.write(sb.toString());
out.close();
} }
} }

View File

@ -1,5 +1,7 @@
package com.redfin.sitemapgenerator; package com.redfin.sitemapgenerator;
import org.xml.sax.SAXException;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
@ -9,8 +11,6 @@ import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import org.xml.sax.SAXException;
/** /**
* Builds a sitemap index, which points only to other sitemaps. * Builds a sitemap index, which points only to other sitemaps.
* @author Dan Fabulich * @author Dan Fabulich
@ -223,15 +223,27 @@ public class SitemapIndexGenerator {
public void write() { public void write() {
if (!allowEmptyIndex && urls.isEmpty()) throw new RuntimeException("No URLs added, sitemap index would be empty; you must add some URLs with addUrls"); if (!allowEmptyIndex && urls.isEmpty()) throw new RuntimeException("No URLs added, sitemap index would be empty; you must add some URLs with addUrls");
try { try {
// TODO gzip? is that legal for a sitemap index? FileWriter out = null;
FileWriter out = new FileWriter(outFile); try {
writeSiteMap(out); // TODO gzip? is that legal for a sitemap index?
if (autoValidate) SitemapValidator.validateSitemapIndex(outFile); out = new FileWriter(outFile);
} catch (IOException e) { writeSiteMap(out);
throw new RuntimeException("Problem writing sitemap index file " + outFile, e); out.flush();
} catch (SAXException e) {
throw new RuntimeException("Problem validating sitemap index file (bug?)", e); if (autoValidate) SitemapValidator.validateSitemapIndex(outFile);
} catch (IOException e) {
throw new RuntimeException("Problem writing sitemap index file " + outFile, e);
} catch (SAXException e) {
throw new RuntimeException("Problem validating sitemap index file (bug?)", e);
} finally {
if(out != null) {
out.close();
}
}
} catch (IOException ex) {
throw new RuntimeException("Closing of stream has failed.", ex);
} }
} }
private void writeSiteMap(OutputStreamWriter out) throws IOException { private void writeSiteMap(OutputStreamWriter out) throws IOException {
@ -254,7 +266,6 @@ public class SitemapIndexGenerator {
out.write(" </sitemap>\n"); out.write(" </sitemap>\n");
} }
out.write("</sitemapindex>"); out.write("</sitemapindex>");
out.close();
} }
} }

View File

@ -1,9 +1,7 @@
package com.redfin.sitemapgenerator; package com.redfin.sitemapgenerator;
import java.io.File; import org.xml.sax.InputSource;
import java.io.FileReader; import org.xml.sax.SAXException;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.XMLConstants; import javax.xml.XMLConstants;
import javax.xml.transform.sax.SAXSource; import javax.xml.transform.sax.SAXSource;
@ -11,9 +9,10 @@ import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema; import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory; import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator; import javax.xml.validation.Validator;
import java.io.File;
import org.xml.sax.InputSource; import java.io.FileReader;
import org.xml.sax.SAXException; import java.io.IOException;
import java.io.InputStream;
/** Validates sitemaps and sitemap indexes /** Validates sitemaps and sitemap indexes
* *
@ -39,23 +38,30 @@ public class SitemapValidator {
private synchronized static void lazyLoad() { private synchronized static void lazyLoad() {
if (sitemapSchema != null) return; if (sitemapSchema != null) return;
SchemaFactory factory = SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try { try {
InputStream stream = SitemapValidator.class.getResourceAsStream("sitemap.xsd"); sitemapSchema = lazyLoad(factory, "sitemap.xsd");
if (stream == null) throw new RuntimeException("BUG Couldn't load sitemap.xsd"); sitemapIndexSchema = lazyLoad(factory, "siteindex.xsd");
StreamSource source = new StreamSource(stream);
sitemapSchema = factory.newSchema(source);
stream.close();
stream = SitemapValidator.class.getResourceAsStream("siteindex.xsd");
if (stream == null) throw new RuntimeException("BUG Couldn't load siteindex.xsd");
source = new StreamSource(stream);
sitemapIndexSchema = factory.newSchema(source);
stream.close();
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("BUG", e); throw new RuntimeException("BUG", e);
} }
} }
private synchronized static Schema lazyLoad(SchemaFactory factory, String resource) throws IOException, SAXException {
InputStream stream = null;
try {
stream = SitemapValidator.class.getResourceAsStream(resource);
if (stream == null) throw new RuntimeException("BUG Couldn't load " + resource);
StreamSource source = new StreamSource(stream);
return factory.newSchema(source);
} finally {
if(stream != null) {
stream.close();
}
}
}
/** Validates an ordinary web sitemap file (NOT a Google-specific sitemap) */ /** Validates an ordinary web sitemap file (NOT a Google-specific sitemap) */
public static void validateWebSitemap(File sitemap) throws SAXException { public static void validateWebSitemap(File sitemap) throws SAXException {
@ -70,15 +76,24 @@ public class SitemapValidator {
} }
private static void validateXml(File sitemap, Schema schema) throws SAXException { private static void validateXml(File sitemap, Schema schema) throws SAXException {
Validator validator = schema.newValidator();
try { try {
FileReader reader = new FileReader(sitemap); Validator validator = schema.newValidator();
SAXSource source = new SAXSource(new InputSource(reader)); FileReader reader = null;
validator.validate(source); try {
reader.close(); reader = new FileReader(sitemap);
} catch (IOException e) { SAXSource source = new SAXSource(new InputSource(reader));
throw new RuntimeException(e); validator.validate(source);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if(reader != null) {
reader.close();
}
}
} catch (IOException ex) {
throw new RuntimeException("Unable to close stream.", ex);
} }
} }
} }