Skip to content

Commit 426efef

Browse files
committed
npe fix if uri-based repository search fails
see MenoData/Time4J#464
1 parent fc11aa9 commit 426efef

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>net.time4j</groupId>
66
<artifactId>time4j-tzdata</artifactId>
7-
<version>1.7-2016a</version>
7+
<version>1.8-2016a</version>
88
<packaging>jar</packaging>
99
<name>Time4J-TZDATA</name>
1010

@@ -208,7 +208,7 @@
208208
<dependency>
209209
<groupId>net.time4j</groupId>
210210
<artifactId>time4j-i18n</artifactId>
211-
<version>3.5</version>
211+
<version>[3.5,]</version>
212212
<scope>test</scope>
213213
</dependency>
214214
<dependency>

src/main/java/net/time4j/tz/spi/TimezoneRepositoryProviderSPI.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import java.io.InputStream;
3939
import java.io.ObjectInputStream;
4040
import java.net.URI;
41+
import java.net.URL;
42+
import java.net.URLConnection;
4143
import java.util.ArrayList;
4244
import java.util.Collections;
4345
import java.util.HashMap;
@@ -79,6 +81,7 @@ public TimezoneRepositoryProviderSPI() {
7981
URI uri = null;
8082
InputStream is = null;
8183
DataInputStream dis = null;
84+
IllegalStateException ise = null;
8285

8386
String tmpVersion = "";
8487
String tmpLocation = "";
@@ -108,7 +111,7 @@ public TimezoneRepositoryProviderSPI() {
108111
}
109112

110113
try {
111-
String path = null;
114+
String path = "tzrepo/" + file;
112115

113116
if (repositoryPath != null) {
114117
File f = new File(repositoryPath, file);
@@ -117,20 +120,31 @@ public TimezoneRepositoryProviderSPI() {
117120
if (f.exists()) {
118121
uri = f.toURI();
119122
} else {
120-
throw new FileNotFoundException(
121-
"Path to tz-repository not found: " + f);
123+
throw new FileNotFoundException("Path to tz-repository not found: " + f);
122124
}
123125
} else {
124-
path = f.toString();
125-
uri = ResourceLoader.getInstance().locate("tzdata", getReference(), path);
126+
uri = ResourceLoader.getInstance().locate("tzdata", getReference(), f.toString());
126127
}
127128
} else {
128-
path = "tzrepo/" + file;
129129
uri = ResourceLoader.getInstance().locate("tzdata", getReference(), path);
130130
}
131131

132132
if (uri != null) {
133133
is = ResourceLoader.getInstance().load(uri, true);
134+
135+
if (is == null) {
136+
// fallback if something has gone wrong (maybe invalid uri from protection domain etc.)
137+
URL url = getReference().getClassLoader().getResource(path);
138+
if (url == null) {
139+
throw new FileNotFoundException("Classloader cannot access tz-repository: " + path);
140+
} else {
141+
URLConnection conn = url.openConnection();
142+
conn.setUseCaches(false);
143+
conn.connect(); // explicit for clarity
144+
is = conn.getInputStream();
145+
}
146+
}
147+
134148
dis = new DataInputStream(is);
135149
tmpLocation = uri.toString();
136150
checkMagicLabel(dis, tmpLocation);
@@ -188,8 +202,7 @@ public TimezoneRepositoryProviderSPI() {
188202
}
189203

190204
} catch (IOException ioe) {
191-
System.out.println("Warning: TZ-repository not available. => " + ioe.getMessage());
192-
ioe.printStackTrace(System.err);
205+
ise = new IllegalStateException("[ERROR] TZ-repository not available. => " + ioe.getMessage(), ioe);
193206
} finally {
194207
if (is != null) {
195208
try {
@@ -200,6 +213,10 @@ public TimezoneRepositoryProviderSPI() {
200213
}
201214
}
202215

216+
if (ise != null) {
217+
throw ise;
218+
}
219+
203220
this.version = tmpVersion;
204221
this.location = tmpLocation;
205222
this.data = Collections.unmodifiableMap(tmpData);

0 commit comments

Comments
 (0)