01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.sail.helpers;
07:
08: import org.openrdf.sail.Sail;
09: import org.openrdf.sail.StackableSail;
10:
11: /**
12: * Defines utility methods for working with Sails.
13: */
14: public class SailUtil {
15:
16: /**
17: * Searches a stack of Sails from top to bottom for a Sail that is
18: * an instance of the suppied class or interface. The first Sail that
19: * matches (i.e. the one closest to the top) is returned.
20: *
21: * @param topSail The top of the Sail stack.
22: * @param sailClass A class or interface.
23: * @return A Sail that is an instance of sailClass, or null if no such
24: * Sail was found.
25: */
26: public static Sail findSailInStack(Sail topSail,
27: Class<? extends Sail> sailClass) {
28: if (sailClass == null) {
29: return null;
30: }
31:
32: // Check Sails on the stack one by one, starting with the top-most.
33: Sail currentSail = topSail;
34:
35: while (currentSail != null) {
36: // Check current Sail
37: if (sailClass.isInstance(currentSail)) {
38: break;
39: }
40:
41: // Current Sail is not an instance of sailClass, check the
42: // rest of the stack
43: if (currentSail instanceof StackableSail) {
44: currentSail = ((StackableSail) currentSail)
45: .getBaseSail();
46: } else {
47: // We've reached the bottom of the stack
48: currentSail = null;
49: }
50: }
51:
52: return currentSail;
53: }
54:
55: }
|