3838import java .io .InputStream ;
3939import java .io .ObjectInputStream ;
4040import java .net .URI ;
41+ import java .net .URL ;
42+ import java .net .URLConnection ;
4143import java .util .ArrayList ;
4244import java .util .Collections ;
4345import 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