SPARK-2356: Guard against missing resources

When a resource is missing, Spark should log and return null, rather than throw an unchecked exception.
This commit is contained in:
Guus der Kinderen
2025-10-09 16:33:17 +02:00
parent 834a6fe8a7
commit 0734d54743
12 changed files with 52 additions and 28 deletions

View File

@ -15,6 +15,8 @@
*/ */
package org.jivesoftware.resource; package org.jivesoftware.resource;
import org.jivesoftware.spark.util.log.Log;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import java.io.IOException; import java.io.IOException;
@ -55,9 +57,14 @@ public class ConfigurationRes {
} }
public static ImageIcon getImageIcon(String imageName) { public static ImageIcon getImageIcon(String imageName) {
final String iconURI = getString(imageName); try {
final URL imageURL = cl.getResource(iconURI); final String iconURI = getString(imageName);
return new ImageIcon(imageURL); final URL imageURL = cl.getResource(iconURI);
return new ImageIcon(imageURL);
} catch (Throwable t) {
Log.warning(imageName + " not found.", t);
}
return null;
} }
public static URL getURL(String propertyName) { public static URL getURL(String propertyName) {

View File

@ -208,8 +208,8 @@ public class Default {
cache.put(imageName, icon); cache.put(imageName, icon);
return icon; return icon;
} }
catch (Exception ex) { catch (Throwable t) {
Log.debug(imageName + " not found."); Log.warning(imageName + " not found.", t);
} }
return null; return null;
} }

View File

@ -15,6 +15,8 @@
*/ */
package org.jivesoftware.resource; package org.jivesoftware.resource;
import org.jivesoftware.spark.util.log.Log;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import java.io.IOException; import java.io.IOException;
@ -52,9 +54,14 @@ public class SoundsRes {
} }
public static ImageIcon getImageIcon(String imageName) { public static ImageIcon getImageIcon(String imageName) {
final String iconURI = getString(imageName); try {
final URL imageURL = cl.getResource(iconURI); final String iconURI = getString(imageName);
return new ImageIcon(imageURL); final URL imageURL = cl.getResource(iconURI);
return new ImageIcon(imageURL);
} catch (Throwable t) {
Log.warning(imageName + " not found.", t);
}
return null;
} }
public static URL getURL(String propertyName) { public static URL getURL(String propertyName) {

View File

@ -355,8 +355,8 @@ public class SparkRes {
try { try {
final URL imageURL = getURL(imageName); final URL imageURL = getURL(imageName);
return new ImageIcon(imageURL); return new ImageIcon(imageURL);
} catch (Exception ex) { } catch (Throwable t) {
Log.error(imageName + " not found."); Log.warning(imageName + " not found.", t);
} }
return null; return null;
} }

View File

@ -16,6 +16,7 @@
package org.jivesoftware.fastpath.resources; package org.jivesoftware.fastpath.resources;
import org.jivesoftware.fastpath.FastpathPlugin; import org.jivesoftware.fastpath.FastpathPlugin;
import org.jivesoftware.spark.util.log.Log;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import javax.swing.JEditorPane; import javax.swing.JEditorPane;
@ -267,8 +268,8 @@ public class FastpathRes {
final URL imageURL = cl.getResource(iconURI); final URL imageURL = cl.getResource(iconURI);
return new ImageIcon(imageURL); return new ImageIcon(imageURL);
} }
catch (Exception ex) { catch (Throwable t) {
System.out.println(imageName + " not found."); Log.warning(imageName + " not found.", t);
} }
return null; return null;
} }

View File

@ -206,8 +206,9 @@ public class IncomingCallUI extends JPanel {
avatarLabel.validate(); avatarLabel.validate();
avatarLabel.repaint(); avatarLabel.repaint();
} }
catch (Exception e) { catch (Throwable t) {
// no issue // no issue
Log.warning("Unable to generate image from avatar", t);
} }
} }

View File

@ -20,6 +20,8 @@ import java.util.PropertyResourceBundle;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.net.URL; import java.net.URL;
import org.jivesoftware.spark.util.log.Log;
/** /**
* Use for Phone Res Internationalization. * Use for Phone Res Internationalization.
* *
@ -48,8 +50,8 @@ public class JinglePhoneRes {
final URL imageURL = JinglePhoneRes.cl.getResource(iconURI); final URL imageURL = JinglePhoneRes.cl.getResource(iconURI);
return new ImageIcon(imageURL); return new ImageIcon(imageURL);
} }
catch (Exception ex) { catch (Throwable t) {
System.out.println(imageName + " not found."); Log.warning(imageName + " not found.", t);
} }
return null; return null;
} }

View File

@ -45,9 +45,14 @@ public class OTRResources {
* @return * @return
*/ */
public static ImageIcon getIcon(String fileName) { public static ImageIcon getIcon(String fileName) {
final ClassLoader cl = OTRResources.class.getClassLoader(); try {
ImageIcon icon = new ImageIcon(cl.getResource(fileName)); final ClassLoader cl = OTRResources.class.getClassLoader();
return icon; ImageIcon icon = new ImageIcon(cl.getResource(fileName));
return icon;
} catch (Throwable t) {
Log.warning(fileName + " not found.", t);
}
return null;
} }
/** /**

View File

@ -1,6 +1,7 @@
package org.jivesoftware.game.reversi; package org.jivesoftware.game.reversi;
import org.jivesoftware.resource.UTF8Control; import org.jivesoftware.resource.UTF8Control;
import org.jivesoftware.spark.util.log.Log;
import java.net.URL; import java.net.URL;
import java.util.PropertyResourceBundle; import java.util.PropertyResourceBundle;
@ -42,9 +43,9 @@ public class ReversiRes {
final URL imageURL = ReversiRes.cl.getResource(iconURI); final URL imageURL = ReversiRes.cl.getResource(iconURI);
return new ImageIcon(imageURL); return new ImageIcon(imageURL);
} }
catch (Exception ex) { catch (Throwable t) {
System.out.println(imageName + " not found."); Log.warning(imageName + " not found.", t);
} }
return null; return null;
} }

View File

@ -58,8 +58,8 @@ public class PhoneRes {
final URL imageURL = cl.getResource(iconURI); final URL imageURL = cl.getResource(iconURI);
return new ImageIcon(imageURL); return new ImageIcon(imageURL);
} }
catch (Exception ex) { catch (Throwable t) {
System.out.println(imageName + " not found."); Log.warn(imageName + " not found.", t);
} }
return null; return null;
} }
@ -79,4 +79,4 @@ public class PhoneRes {
} }
return null; return null;
} }
} }

View File

@ -220,8 +220,8 @@ public class IncomingCallUI extends JPanel {
avatarLabel.validate(); avatarLabel.validate();
avatarLabel.repaint(); avatarLabel.repaint();
} }
catch (Exception e) { catch (Throwable t) {
// no issue Log.warning("Unable to generate image from avatar", t);
} }
} }

View File

@ -203,8 +203,8 @@ public class OutgoingCallUI extends JPanel {
avatarLabel.validate(); avatarLabel.validate();
avatarLabel.repaint(); avatarLabel.repaint();
} }
catch (Exception e) { catch (Throwable t) {
// no issue Log.warning("Unable to generate image from avatar", t);
} }
} }