How to safely load resources in Cooee

Why you need to change how you load resources

Cooee supports different runtime environments - currently a standard servlet container and an OSGI environment. If you choose to follow a standard resource loading paradigm such as:

MyjavaClass.class.getResourceAsStream(...);

You run the distinct risk of your code not being portable between a standard environment and an OSGI environment. Cooee takes the difficulty out of manging the differences in these environments via its Resource.java class.

Implementation under a standard servlet container

Cooee will simply attempt to locate your Resource based on the fully qualified class path passed in

Implementation under OSGI

Cooee will scan each bundle currently visible to it for your resource. The resource must be visible to the Cooee bundle in order for Cooee to display the resource.

Resource Loading from Existing Cooee classes

All existing Cooee classes load their resources in an environment neutral manner via Resource.java. As such, all classes such as ResourceImageStream.java should be passed fully qualified paths for images.  Put simply, if you're not directly loading Streams into Cooee classes, then you don't need to worry about this issue.

How to safely load Cooee resources

Resource.java
public static String getResourceAsString(String resourceName) ;
public static byte getResourceAsByteArray(String resourceName);
public static InputStream getResourceAsStream(String resourceName);

Always use a method from Resource.java

So instead of:

MyJavaClass.class.getResourceAsStream("/org/karora/samplecode/CooeeRox.jpg");

Use:

Resource.getResourceAsStream("/org/karora/samplecode/CooeeRox.jpg");

Labels

 
(None)