Resource loading

A method loads a resource in a software application. The software application comprises an ordered search path identifying at least one of a plurality of resource containers. Each of the resource containers includes a timestamp and an index in the ordered search path. The method includes loading a resource from an originating resource container, the originating resource container having an index in the ordered search path, identifying a set of resource containers in the ordered search path, each of the set of resource containers having an index less than or equal to the index of the originating resource container, generating a cached resource as a copy of the loaded resource, the cached resource comprising the index of the originating resource container and a cached resource timestamp, the cached resource timestamp corresponding to a maximum timestamp of the resource containers in the set of resource containers, and marking the cached resource as invalid in response to a determination that a resource container in the set of resource containers has a timestamp later than the cached resource timestamp, and the identified resource container comprises the cached resource.

BACKGROUND OF THE INVENTION

The present invention relates to loading resources in software applications, and more particularly, to detecting stale cached resources.

Software applications are often arranged as a suite of separate software components or resources. A main component or runtime environment will import resources from resource containers in order to operate as a complete software application. For example, an application may locate and access runtime functions stored in dynamic link libraries, or classes stored in class libraries. In order to locate such resources, a search path is used to indicate the location(s) of one or more such resource containers. For example, in the Java™ runtime environment a CLASSPATH environment variable can be used to define where classes can be found (Java is a trademark of Sun Microsystems Corp. in the United States, other countries, or both). The CLASSPATH is a list of locations (such as directories in a file system or fully qualified class library locations) and is used at runtime when a new class is loaded. Such search paths are ordered such that the application searches for required resources at a first location, before proceeding to subsequent locations in a sequential manner.

FIG. 1is a block diagram of a resource processor108for loading resources from one or more resource containers100in the prior art. The resource processor108can be an application at runtime, or a runtime environment such as a Java virtual machine. The one or more resource containers100can be, for example, library files, class files, Java archive (jar) files or directories in a file system. Each of the resource containers100has a container identifier102such as the container name (e.g. a fully qualified jar file name or a fully qualified directory name). Within a resource container100can reside one or more resources104. The one or more resources104can be, for example, class files or runtime libraries. Each of the resources104has an identifier, such as a class file name.

The resource processor108includes a resource loader110such as a class loader. The resource loader110is able to locate a resource104in a resource container100and load it into a memory of the computer system for use by the resource processor108. The resource processor108further comprises a resource cache112, which is a reserved section of memory in a computer system for storing one or more resources116which have been loaded from resource containers100. The resource cache112can be a shared memory cache serving multiple resource loaders (not shown) or multiple resource processors (not shown). For example, the resource cache112can serve multiple Java class loader instances. The resource cache112can therefore exist outside the resource processor108, such as on a separate computer system communicatively connected to the resource processor108. The resources116stored in the resource cache112can be stored based on usage criteria, such as most frequently used resources. Each of the resources116in the resource cache112includes a resource identifier118. It will be appreciated that the resources116stored in the resource cache104substantially reflect the resources104stored in resource containers100when they are loaded into the resource cache112by the resource loader110. Thus, the resources116stored in the resource cache112are copies of the resources104stored in resource containers100.

When searching for a particular resource the resource loader110uses a resource search path114. The resource search path114includes an ordered container list120which is a list of resource containers. Each entry in the resource search path114includes an index122(the means by which the ordered container list120is ordered) and a container identifier124(corresponding to a container identifier102of one of the resource containers100).

At runtime, the resource processor108requests that the resource loader110loads a particular resource identified by a resource identifier106. The resource loader110first checks if the required resource is resident in the resource cache112. If so, the resource can be quickly loaded from the resource cache112. If not, the resource loader110locates the resource by searching sequentially through each of the resource containers in the ordered container list120of the resource search path114. When a resource container is located with the required resource identifier106the resource loader110loads the required resource and may further add the loaded resource to the resource cache112.

BRIEF SUMMARY OF THE INVENTION

According to a first aspect of the present invention, a method loads a resource in a software application. The software application comprises an ordered search path identifying at least one of a plurality of resource containers. Each of the resource containers having a timestamp and an index in the ordered search path. The method comprises loading a resource from an originating resource container, the originating resource container having an index in the ordered search path, identifying a set of resource containers in the ordered search path, each of the set of resource containers having an index less than or equal to the index of the originating resource container, generating a cached resource as a copy of the loaded resource, the cached resource comprising the index of the originating resource container and a cached resource timestamp, the cached resource timestamp corresponding to a maximum timestamp of the resource containers in the set of resource containers, and marking the cached resource as invalid in response to a determination that a resource container in the set of resource containers has a timestamp later than the cached resource timestamp, and the identified resource container comprises the cached resource.

According to another aspect of the present invention, an apparatus loads a resource in a software application. The software application comprises an ordered search path identifying at least one of a plurality of resource containers. Each of the resource containers includes a timestamp and an index in the ordered search path. The apparatus comprises a loading module loading a resource from an originating resource container, the originating resource container having an index in the ordered search path, an identification module identifying a set of resource containers in the ordered search path, each of the set of resource containers having an index less than or equal to the index of the originating resource container, a generation module generating a cached resource as a copy of the loaded resource, the cached resource comprising the index of the originating resource container and a cached resource timestamp, the cached resource timestamp corresponding to a maximum timestamp of the resource containers in the set of resource containers, and a marking module marking the cached resource as invalid in response to a determination that a resource container in the set of resource containers has a timestamp later than the cached resource timestamp and the identified resource container comprises the cached resource.

According to yet another aspect of the present invention, a computer program product loads a resource in a software application. The software application comprises an ordered search path identifying at least one of a plurality of resource containers. Each of the resource containers includes a timestamp and an index in the ordered search path. The computer program product comprises a computer usable medium having computer useable program code embodied therewith. The computer useable program code comprises computer usable program code configured to load a resource from an originating resource container, the originating resource container having an index in the ordered search path, computer usable program code configured to identify a set of resource containers in the ordered search path, each of the set of resource containers having an index less than or equal to the index of the originating resource container, computer usable program code configured to generate a cached resource as a copy of the loaded resource, the cached resource comprising the index of the originating resource container and a cached resource timestamp, the cached resource timestamp corresponding to a maximum timestamp of the resource containers in the set of resource containers, and computer usable program code configured to mark the cached resource as invalid in response to a determination that a resource container in the set of resource containers has a timestamp later than the cached resource timestamp and the identified resource container comprises the cached resource.

Other aspects and features of the present invention will become apparent to those ordinarily skilled in the art or science to which it pertains upon review of the following description in conjunction with the accompanying figures.

DETAILED DESCRIPTION OF THE INVENTION

FIG. 2is a block diagram of a computer system suitable for the operation of embodiments of the present invention. A central processor unit (CPU)202is communicatively connected to a storage204and an input/output (I/O) interface206via a data bus208. The storage204can be any read/write storage device such as a random access memory (RAM) or a non-volatile storage device. An example of a non-volatile storage device includes a disk or tape storage device. The I/O interface206is an interface to devices for the input or output of data, or for both input and output of data. Examples of I/O devices connectable to I/O interface206include a keyboard, a mouse, a display (such as a monitor) and a network connection.

FIG. 3is a block diagram of a resource processor308for loading resources from one or more resource containers300in accordance with an aspect of the present invention. Many of the elements ofFIG. 3are identical to those described above with respect toFIG. 1and these will not be repeated here. Each of the resource containers300ofFIG. 3further include a timestamp303which reflects the creation time of a resource container or the time of the most recent modification to the resource container. For example, the timestamp303can be a date and time attribute of a jar file. Alternatively, the timestamp303could be stored in an ordered container list320, which is described in detail below. Furthermore, the resources316stored in the resource cache312include, in addition to the resource identifier318, a container path index326and a maximum timestamp328. The container path index326is an index of a resource container in the ordered container list320which contains the resource. The maximum timestamp328is the latest (highest) timestamp of all of the resource containers in the ordered container list320up to and including the resource container with the container path index326. A method for calculating the maximum timestamp328is considered in detail below with respect toFIG. 4d. Additionally, the resource loader310includes a stale cache checker311which determines whether resource in the resource cache312is stale. A resource in the resource cache312is stale if a newer version of the resource would be loaded from one of the resource containers300in an equivalent system without a resource cache312. A method of the stale cache checker311for determining if a resource is stale is considered in detail below with respect toFIG. 4c.

FIG. 4ais a flowchart for a method of the resource loader308ofFIG. 3to load a resource with a required resource identifier in accordance with an aspect of the present invention. At step402the resource loader310determines if a resource with the required resource identifier is stored in the resource cache312. If the resource is not stored in the resource cache312the method proceeds to step406where the method loads a resource with the required resource identifier from the resource containers300using the resource search path314using the method ofFIG. 4bconsidered in detail below. If, at step402, a resource with the required resource identifier is stored in the resource cache312the method proceeds to step404. At step404the stale cache checker311determines if the resource with the required resource identifier in the resource cache312is stale. This is achieved using the method ofFIG. 4cconsidered in detail below. If step404determines that the resource is stale the method proceeds to step406. Alternatively, if the resource is not stale the method proceeds to step408where the resource loader310loads the resource with the required identifier directly from the resource cache312.

FIG. 4bis a flowchart for a method of the resource loader310ofFIG. 3to load a resource with a required resource identifier from a resource container in accordance with an aspect of the present invention. At step420a loop is initiated through all of the resource containers in the ordered container list320. The loop of step420is sequential starting at a first entry in the ordered container list320(for example, starting at a lowest index322). At step422, for a current resource container in the ordered container list320, the method determines if the current resource container contains a resource with the required resource identifier. If the current resource container does not contain a resource with the required resource identifier the method proceeds to step428where the method loops back to step420until the end of the ordered container list320is reached. Alternatively, if step422determines that the current resource container does contain a resource with the required resource identifier, the method proceeds to step424where the resource loader loads the resource with the required resource identifier from the current resource container. Subsequently, at step426, the resource loader creates a cached copy of the loaded resource. In an alternative embodiment the creation of the cached copy of the resource at step426can be dependent upon criteria such as the frequency of use of the resource. If, after looping through all resource containers in the ordered container list320a resource with the required resource identifier is not located, the method notes this as step430.

FIG. 4cis a flowchart for a method of the stale cache checker311ofFIG. 3to determine if a cached resource in the resource cache312ofFIG. 3is stale in accordance with an aspect of the present invention. At step440a loop is initiated through all of the resource containers in the ordered container list320. The loop of step440is sequential starting at a first entry in the ordered container list320(for example, starting at a lowest index322). At step442, for a current resource container in the ordered container list320, the method determines if a timestamp303of the current resource container is greater than the maximum timestamp328of the cached resource. If the timestamp303of the current resource container is not greater than the maximum timestamp328of the cached resource the method proceeds to step448. Alternatively, If the timestamp303of the current resource container is greater than the maximum timestamp328of the cached resource the method proceeds to step444. At step444the method determines if the current resource container contains a resource with the resource identifier318of the cached resource. If the current resource container contains a resource with the resource identifier318of the cached resource the method proceeds to step446, otherwise the method proceeds to step448. At step446the method determines that the cached resource is stale and the method terminates. At step448the method determines if the index of the current resource container in the ordered container list320is the same as the container path index326of the cached resource. If the index of the current resource container in the ordered container list320is not the same as the container path index326of the cached resource, the method loops back to step440for a next resource container in the ordered container list320. Otherwise the method proceeds to step450where the method determines that cached resource is not stale and the method terminates. In this way the method ofFIG. 4cis able to determine if a cached resource is stale by verifying that no resource container in the ordered container list320up to and including the resource container having the container path index326has a timestamp later than the maximum timestamp328and contains a resource with the resource identifier of the cached resource.

FIG. 4dis a flowchart for a method of the resource loader310ofFIG. 3to calculate the maximum timestamp328of a cached resource in accordance with an aspect of the present invention. At step460a loop is initiated through all of the resource containers in the ordered container list320. The loop of step460is sequential starting at a first entry in the ordered container list320(for example, starting at a lowest index322). At step462, for a current resource container in the ordered container list320, the method determines if the current resource container is the first resource container processed by the loop. If so, the method proceeds to step468where the maximum timestamp328of the cached resource is set to be the value of the timestamp of the current resource container. If step462determines that the current resource container is not the first resource container processed by the loop, the method proceeds to step464where the method determines if the timestamp of the current resource container is greater that the maximum timestamp328. If so, the method proceeds to step468. Alternatively, the method proceeds to step466where the method is looped until the current container has an index which is the same as the container path index326of the cached resource. In this way the method ofFIG. 4dassigns a value to the maximum timestamp328of a newly cached resource which is the latest (highest) timestamp of all of the resource containers in the ordered container list320up to and including the resource container with the container path index326.

An aspect of the present invention will now be considered in use by way of example only with reference toFIG. 5andFIGS. 6ato6e.FIG. 5is an illustration of an exemplary time line of changes to Java archive (jar) files and class loading operations in accordance with an aspect of the present invention.FIG. 6ais a block diagram of a Java virtual machine (JVM)608for loading classes from one or more Java archive (jar) files602,604and606in accordance with an aspect of the present invention. ConsideringFIG. 6afirst, the JVM608includes a class loader610comprising a stale cache checker611. The class loader610has generally the same function as the resource loader310ofFIG. 3. The JVM608further includes a class cache612(analogous to the resource cache312) and a classpath614(analogous to the resource search path314) having an ordered list of jar files620. The class cache612of the JVM608is initially empty. The classpath614includes entries for three jar files: “A.jar” with an index of “1”; “B.jar” with an index of “2”; and “C.jar” with an index of “3”. For each of these jar files a resource container is illustrated. Resource container602represents a jar file with a container identifier6022of “A.jar”. Resource container602further has a timestamp6024with a value of “7”. Additionally, resource container602includes two classes6026and6028, each having a class identifier60262and60282with values “L” and “M” respectively. Resource container604represents a jar file with a container identifier6042of “B.jar”. Resource container604further has a timestamp6044with a value of “4”. Additionally, resource container604includes two classes6046and6048, each having a class identifier60462and60482with values “Q” and “R” respectively. Resource container606represents a jar file with a container identifier6062of “C.jar”. Resource container606further has a timestamp6064with a value of “5”. Additionally, resource container605includes two classes6066and6068, each having a class identifier60662and60682with values “T” and “U” respectively.

Referring now toFIG. 5, a example timeline comprising eighteen points in time is illustrated with events for these points in time indicated by a bold arrow with associated description. The time line and events occurring at particular points in time will now be used to demonstrate an aspect of the present invention in use. At time “4” the resource container604with the identifier “B.jar” is created. Hence, resource container604has a timestamp6024of “4”. Similarly, at time “5” the resource container606with the identifier “C.jar” is created. Hence, resource container606has a timestamp6064of “5”. Also, at time “7” the resource container602with the identifier “A.jar” is created. Hence resource container602has a timestamp6024of “7”.

At time “10” the JVM608instructs the classloader610to load a class with the identifier “R”. Referring to the method ofFIG. 4ato load a resource with a required resource identifier, at step402the classloader610determines if a class with the required class identifier is stored in the class cache612. The class cache612is empty so the method proceeds to step406where the method loads a class with the required class identifier from the resource containers602,604and606using the method ofFIG. 4b. Turning, therefore, to the method ofFIG. 4bfor loading a class with the class identifier “R”, at step420a loop is initiated through all of the resource containers in the ordered list of jar files620. Starting at the first jar file with an index of “1” (“A.jar”), the method determines at step422if the corresponding resource container602with the identifier “A.jar” contains a class with the class identifier “R”. Resource container602does not contain a class with the class identifier “R” and so the method proceeds to step428where the method loops back to step420for the next jar file in the ordered list of jar files620. For the next jar file with an index of “2” (“B.jar”) the method determines at step422if the corresponding resource container604with the identifier “B.jar” contains a class with the class identifier “R”. Resource container604does contain the class6048with the identifier “R” and so the method proceeds to step424where the class loader610loads class6048from resource container604. Subsequently, at step426, the class loader610creates a cached copy6122of class6048in the class cache612.FIG. 6bis a block diagram of the JVM608ofFIG. 6awith the class6048“R” loaded and cached in the class cache612in accordance with an aspect of the present invention. The cached copy6122of the class6048has a class identifier61222with a value of “R”, and a classpath index61224with a value of “2”. The classpath index61224is the index of the resource container604in the ordered list of jar files620. Since resource container604has an identifier of “B.jar” and “B.jar” has an index of “2” in the ordered list of jar files620, the classpath index61224of the cached class6122has a value of “2”. The class6122in the class cache612also has a maximum timestamp61226with a value of “7”. This is determined using the method ofFIG. 4das being the latest (highest) timestamp of all of the resource containers in the ordered list of jar files620up to and including the jar file with an index of the classpath index61224. The jar file in the ordered list of jar files620with an index of the classpath index61224of “2” is “B.jar”. Thus, the maximum timestamp is the latest timestamp of the resource containers with identifiers “A.jar” and “B.jar”, these being the resource containers with an index in the ordered list of jar files620up to and including the value “2”. The latest timestamp of the resource containers with identifiers “A.jar” and “B.jar” is the timestamp of resource container602which is “7”. Hence, the maximum timestamp61226has the value “7”.

Referring again toFIG. 5, at time “12” a new class is added to resource container602(“A.jar”) with a class identifier of “R”.FIG. 6cis a block diagram of the JVM608ofFIG. 6bwith a new class6029“R” added to the “A.jar” Java archive (jar) file in accordance with an aspect of the present invention.FIG. 6cfurther includes a change to the value of the timestamp6024of resource container602to reflect the change to the resource container at time point “12”. Further inFIG. 5, at time “14” the JVM608instructs the classloader610to once again load a class with the identifier “R”. Referring to the method ofFIG. 4ato load a resource with a required resource identifier, at step402the classloader610determines if a class with the required class identifier “R” is stored in the class cache612. Class6122with the class identifier61222“R” is stored in the class cache612so the method proceeds to step404. At step404the stale cache checker611determines if class6122in the class cache612is stale using the method ofFIG. 4c.

Turning, therefore, to the method ofFIG. 4cto determine if the cached class6122is stale, at step440a loop is initiated through all of the jar files in the ordered list of jar files620, starting with “A.jar” corresponding to resource container602. At step442the method determines if the timestamp6024of resource container602is greater than the maximum timestamp61226of the cached class6122. The timestamp6024has a value of “12” (seeFIG. 6c) and the maximum timestamp61226has a value of “7”. Thus, the timestamp of resource container602is greater than the maximum timestamp61226of cached class6122and the method proceeds to step444. At step444the method determines if resource container602contains a class with a class identifier value of “R” (corresponding to the class identifier61222of the cached class6122). Resource container602does contains a class6029with the class identifier60292of “R” and so the method proceeds to step446. At step446the method ofFIG. 4cconcludes that the class6122is stale.

Thus, returning to the method ofFIG. 4a, step404determined that class6122is stale and the method proceeds to step406where the method loads a class with the required class identifier from the resource containers602,604and606using the method ofFIG. 4b. Turning, therefore, to the method ofFIG. 4bfor loading a class with the class identifier “R”, at step420a loop is initiated through all of the resource containers in the ordered list of jar files620. Starting at the first jar file with an index of “1” (“A.jar”), the method determines at step422if the corresponding resource container602with the identifier “A.jar” contains a class with the class identifier “R”. Resource container602does contain a class6029with the class identifier “R” and so the method proceeds to step424where the class loader610loads class6029from resource container602. Subsequently, at step426, the class loader610creates a cached copy6124of class6029in the class cache612.FIG. 6dis a block diagram of the JVM608ofFIG. 6cwith the class6029“R” loaded and cached in the class cache612in accordance with an aspect of the present invention. The cached copy6124of the class6029has a class identifier61242with a value of “R”, and a classpath index61244with a value of “1”. The classpath index61244is the index of the resource container602in the ordered list of jar files620. Since resource container602has an identifier of “A.jar” and “A.jar” has an index of “1” in the ordered list of jar files620, the classpath index61244of the cached class6124has a value of “1”. The class6124in the class cache612also has a maximum timestamp61246with a value of “12”. This is determined using the method ofFIG. 4das being the latest (highest) timestamp of all of the resource containers in the ordered list of jar files620up to and including the jar file with an index of the classpath index61244. The jar file in the ordered list of jar files620with an index of the classpath index61244of “1” is “A.jar”. Thus, the maximum timestamp is the timestamp of the resource containers with identifier “A.jar”, this being the resource container with an index in the ordered list of jar files620up to and including the value “1”. Hence, the maximum timestamp61246has the value “12”.

Referring again toFIG. 5, at time “16” a new class is added to resource container606(“C.jar”) with a class identifier of “R”.FIG. 6eis a block diagram of the JVM608ofFIG. 6bwith a new class6069“R” added to the “C.jar” Java archive (jar) file in accordance with an aspect of the present invention.FIG. 6efurther includes a change to the value of the timestamp6064of resource container606to reflect the change to the resource container at time point “16”. Further inFIG. 5, at time “18” the JVM608instructs the classloader610to once again load a class with the identifier “R”. Referring to the method ofFIG. 4ato load a resource with a required resource identifier, at step402the classloader610determines if a class with the required class identifier “R” is stored in the class cache612. Class6124with the class identifier61242“R” is stored in the class cache612so the method proceeds to step404. At step404the stale cache checker611determines if class6124in the class cache612is stale using the method ofFIG. 4c.

Turning, therefore, to the method ofFIG. 4cto determine if the cached class6124is stale, at step440a loop is initiated through all of the jar files in the ordered list of jar files620, starting with “A.jar” corresponding to resource container602. At step442the method determines if the timestamp6024of resource container602is not greater than the maximum timestamp61246of the cached class6122. The timestamp6024has a value of “12” (seeFIG. 6e) and the maximum timestamp61246also has a value of “12”. Thus, the timestamp of resource container602is not greater than the maximum timestamp61246of cached class6122and the method proceeds to step448. At step448the method determines if the index of the resource container602in the ordered list of jar files620is the same as the classpath index61244of the cached class6124. The resource container602has container identifier “A.jar” which has an index of “1” in the ordered list of jar files620. The classpath index61244of the cached class6124also has a value of “1”. The method therefore proceeds to step450where the method determines that cached class6124is not stale.

Thus, returning to the method ofFIG. 4a, step404determined that class6122is not stale and at step408the class loader610loads the class6124with the class identifier61242of “R” from the class cache612. Thus, the examples charted by the timeline ofFIG. 5illustrate how aspects of the present invention illustrated inFIG. 3andFIGS. 4ato4dprovide a mechanism for detecting stale resources in a resource cache without a need to refresh the resource cache and without a need to search through all resource containers in a resource search path to verify that the resource has not been updated. The inclusion of a container path index326provides an indicator of how much of the ordered container list320must be processed to determine if a cached resource316is stale. Further, the maximum timestamp328allows a determination that a cached resource is out of date.