Skip to content

Commit

Permalink
npe fix if uri-based repository search fails
Browse files Browse the repository at this point in the history
  • Loading branch information
MenoData committed Mar 3, 2016
1 parent fc11aa9 commit 426efef
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.time4j</groupId>
<artifactId>time4j-tzdata</artifactId>
<version>1.7-2016a</version>
<version>1.8-2016a</version>
<packaging>jar</packaging>
<name>Time4J-TZDATA</name>

Expand Down Expand Up @@ -208,7 +208,7 @@
<dependency>
<groupId>net.time4j</groupId>
<artifactId>time4j-i18n</artifactId>
<version>3.5</version>
<version>[3.5,]</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
33 changes: 25 additions & 8 deletions src/main/java/net/time4j/tz/spi/TimezoneRepositoryProviderSPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -79,6 +81,7 @@ public TimezoneRepositoryProviderSPI() {
URI uri = null;
InputStream is = null;
DataInputStream dis = null;
IllegalStateException ise = null;

String tmpVersion = "";
String tmpLocation = "";
Expand Down Expand Up @@ -108,7 +111,7 @@ public TimezoneRepositoryProviderSPI() {
}

try {
String path = null;
String path = "tzrepo/" + file;

if (repositoryPath != null) {
File f = new File(repositoryPath, file);
Expand All @@ -117,20 +120,31 @@ public TimezoneRepositoryProviderSPI() {
if (f.exists()) {
uri = f.toURI();
} else {
throw new FileNotFoundException(
"Path to tz-repository not found: " + f);
throw new FileNotFoundException("Path to tz-repository not found: " + f);
}
} else {
path = f.toString();
uri = ResourceLoader.getInstance().locate("tzdata", getReference(), path);
uri = ResourceLoader.getInstance().locate("tzdata", getReference(), f.toString());
}
} else {
path = "tzrepo/" + file;
uri = ResourceLoader.getInstance().locate("tzdata", getReference(), path);
}

if (uri != null) {
is = ResourceLoader.getInstance().load(uri, true);

if (is == null) {
// fallback if something has gone wrong (maybe invalid uri from protection domain etc.)
URL url = getReference().getClassLoader().getResource(path);
if (url == null) {
throw new FileNotFoundException("Classloader cannot access tz-repository: " + path);
} else {
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
conn.connect(); // explicit for clarity
is = conn.getInputStream();
}
}

dis = new DataInputStream(is);
tmpLocation = uri.toString();
checkMagicLabel(dis, tmpLocation);
Expand Down Expand Up @@ -188,8 +202,7 @@ public TimezoneRepositoryProviderSPI() {
}

} catch (IOException ioe) {
System.out.println("Warning: TZ-repository not available. => " + ioe.getMessage());
ioe.printStackTrace(System.err);
ise = new IllegalStateException("[ERROR] TZ-repository not available. => " + ioe.getMessage(), ioe);
} finally {
if (is != null) {
try {
Expand All @@ -200,6 +213,10 @@ public TimezoneRepositoryProviderSPI() {
}
}

if (ise != null) {
throw ise;
}

this.version = tmpVersion;
this.location = tmpLocation;
this.data = Collections.unmodifiableMap(tmpData);
Expand Down

0 comments on commit 426efef

Please sign in to comment.