01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: ResourceFinderGroup.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.resources;
09:
10: import com.uwyn.rife.resources.ResourceFinder;
11: import com.uwyn.rife.resources.exceptions.CantOpenResourceStreamException;
12: import com.uwyn.rife.resources.exceptions.ResourceFinderErrorException;
13: import com.uwyn.rife.tools.InputStreamUser;
14: import java.net.URL;
15: import java.util.ArrayList;
16:
17: /**
18: * Allows a group of resource finders to acts as if they are one single
19: * resource finders. They will be consecutively used in their order of addition
20: * to the group.
21: *
22: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
23: * @version $Revision: 3634 $
24: * @see com.uwyn.rife.resources.ResourceFinder
25: * @since 1.0
26: */
27: public class ResourceFinderGroup extends AbstractResourceFinder {
28: private ArrayList<ResourceFinder> mResourceFinders = new ArrayList<ResourceFinder>();
29:
30: public ResourceFinderGroup add(ResourceFinder resourceFinder) {
31: mResourceFinders.add(resourceFinder);
32:
33: return this ;
34: }
35:
36: public URL getResource(String name) {
37: URL result = null;
38: for (ResourceFinder resource_finder : mResourceFinders) {
39: result = resource_finder.getResource(name);
40: if (result != null) {
41: return result;
42: }
43: }
44:
45: return null;
46: }
47:
48: public <ResultType> ResultType useStream(URL resource,
49: InputStreamUser user) throws ResourceFinderErrorException {
50: ResultType result = null;
51: for (ResourceFinder resource_finder : mResourceFinders) {
52: try {
53: result = (ResultType) resource_finder.useStream(
54: resource, user);
55: } catch (CantOpenResourceStreamException e) {
56: continue;
57: }
58:
59: return result;
60: }
61:
62: throw new CantOpenResourceStreamException(resource, null);
63: }
64:
65: public String getContent(URL resource, String encoding)
66: throws ResourceFinderErrorException {
67: String result = null;
68: for (ResourceFinder resource_finder : mResourceFinders) {
69: result = resource_finder.getContent(resource, encoding);
70: if (result != null) {
71: return result;
72: }
73: }
74:
75: return null;
76: }
77:
78: public long getModificationTime(URL resource)
79: throws ResourceFinderErrorException {
80: long result = -1;
81: for (ResourceFinder resource_finder : mResourceFinders) {
82: result = resource_finder.getModificationTime(resource);
83: if (result != -1) {
84: return result;
85: }
86: }
87:
88: return -1;
89: }
90: }
|