15.01.2015 Views

4th International Conference on Principles and Practices ... - MADOC

4th International Conference on Principles and Practices ... - MADOC

4th International Conference on Principles and Practices ... - MADOC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

2. FRAMEWORK<br />

The Framework for Unified Resource Management (Furm)<br />

represents dependencies am<strong>on</strong>g resources using trees [12]. In a<br />

resource tree, given a parent resource P <strong>and</strong> a child resource C, C<br />

is dependent <strong>on</strong> P. That is, <strong>on</strong>ce P is closed, C has no further use.<br />

By extensi<strong>on</strong>, given any two resources X <strong>and</strong> Y where X is an<br />

ancestor of Y, Y is dependent <strong>on</strong> X, either directly or indirectly.<br />

All resources are therefore dependent <strong>on</strong> the tree’s root resource.<br />

This resource tree abstracti<strong>on</strong> is primarily useful in two ways.<br />

First, the tree provides a unified way to manage resources. This<br />

fills the gap left by Java’s Closeable interface. The resource tree<br />

provides a c<strong>on</strong>sistent way to view resources, <strong>and</strong> makes it<br />

possible for all resources to be released in an identical manner.<br />

Sec<strong>on</strong>d, the resource tree materializes resource dependencies in<br />

an explicit fashi<strong>on</strong>. While an input stream might be associated<br />

with a socket, it may not be evident to any code using the stream<br />

that this is the case. However, if the socket <strong>and</strong> its streams are<br />

part of the same resource tree, the relati<strong>on</strong>ship becomes clear.<br />

The c<strong>on</strong>necti<strong>on</strong> in the tree ties a parent <strong>and</strong> child together<br />

logically <strong>and</strong> simply.<br />

Furm models two types of dependencies: substantive<br />

dependencies <strong>and</strong> logical dependencies. Figures 2-1 <strong>and</strong> 2-2<br />

illustrate substantive <strong>and</strong> logical dependencies, respectively.<br />

Substantive dependencies are those dependencies that might be<br />

described as c<strong>on</strong>crete, physical, or real dependencies. These are<br />

forced dependencies, or dependencies in which the child resource<br />

truly has no meaning in the absence of the parent resource. The<br />

resource dependency examples used so far have all been<br />

substantive dependencies.<br />

Logical dependencies are semantic dependencies, or groupings of<br />

resources. For example, the input <strong>and</strong> output streams used by a<br />

block of code may not have any true, enforced dependencies, but<br />

they may still be logically dependent, in that without both,<br />

Socket<br />

executi<strong>on</strong> cannot successfully proceed. It is therefore useful to<br />

represent them as siblings with a comm<strong>on</strong> parent. Neither the<br />

input stream nor the output stream has a substantive dependency<br />

<strong>on</strong> the other, but they are logically c<strong>on</strong>nected. Having a comm<strong>on</strong><br />

parent materializes this relati<strong>on</strong>ship. In logical dependencies, the<br />

parent will often be a pseudo-resource, i.e., the parent represents a<br />

group of resources, <strong>and</strong> does not itself represent a particular<br />

resource. It exists to support the semantic relati<strong>on</strong>ship of its<br />

children. A comm<strong>on</strong> use of logical dependencies is to group all<br />

the resources of a segment of a program. This might be a code<br />

block, a specific object, or a thread of executi<strong>on</strong>. Figure 2-3<br />

shows a resource group associated with a thread, which c<strong>on</strong>tains a<br />

database c<strong>on</strong>necti<strong>on</strong> <strong>and</strong> a socket, each with its own<br />

dependencies.<br />

It has been shown that the resource tree can represent<br />

dependencies am<strong>on</strong>g resources. In additi<strong>on</strong>, Furm propagates<br />

resource releases from ancestors to descendents. If a resource in<br />

the tree is released, then all of its children will be released, <strong>and</strong> in<br />

turn the children’s children, etc. Rather than releasing a number<br />

of individual resources, <strong>on</strong>ly the parent resource needs to be<br />

explicitly released. Entire subtrees of resources can be released<br />

with a single call. This has the immediate effect that cleanup<br />

code, with its large code overhead, is greatly reduced.<br />

2.1 Using Furm<br />

Here we present a simple method fetch(), with the job of<br />

c<strong>on</strong>necting to a server, sending a message, <strong>and</strong> returning the<br />

resp<strong>on</strong>se to the caller. (See Figure 2-4.) In the event of failure,<br />

the resp<strong>on</strong>se is null.<br />

This method is implemented correctly, with respect to resource<br />

management. The three resources used are a Socket, an<br />

InputStream, <strong>and</strong> an OutputStream. The ByteArrayOutputStream<br />

is not c<strong>on</strong>sidered a resource here, because it does not need to be<br />

closed.<br />

While the task of this code is simple, the decisi<strong>on</strong> to correctly<br />

close resources <strong>and</strong> h<strong>and</strong>le all checked excepti<strong>on</strong>s – as opposed to<br />

discarding them or allowing them to propagate to the caller – has<br />

caused massive code expansi<strong>on</strong>. If excepti<strong>on</strong>s could be safely<br />

ignored, this method could be reduced from 44 lines to <strong>on</strong>ly 18.<br />

Note especially how the nested try-finally blocks turn cleanup, a<br />

c<strong>on</strong>ceptually minor detail, into the most dominant detail.<br />

Socket Input Stream<br />

Socket Output Stream<br />

Figure 2-1. Substantive dependencies<br />

Resource Group<br />

Resource Group<br />

Database<br />

C<strong>on</strong>necti<strong>on</strong><br />

Socket<br />

File Input Stream<br />

File Output Stream<br />

Statement Socket Input Stream Socket Output Stream<br />

Figure 2-2. Logical dependencies<br />

Figure 2-3. An example resource tree<br />

116

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!