1 package org.jmage.resource;
2
3 import org.jmage.ApplicationContext;
4 import org.jmage.ImageRequest;
5 import org.jmage.JmageException;
6
7 import javax.media.jai.PlanarImage;
8 import java.net.URI;
9 import java.util.Iterator;
10 import java.util.List;
11
12 /***
13 * ResourceManager is a facade for all ResourceFactories.
14 */
15 public class DefaultResourceManager implements ResourceManager {
16 protected List resourceFactories;
17 private static final String RESOURCE_ERROR = "need to specify a resource, cannot be null";
18 private static final String RESOURCEFACTORY_ERROR = "unable to retrieve resource, all resource factories failed to handle.";
19
20 protected DefaultResourceManager() {
21
22 }
23
24 /***
25 * Creates a ResourceManager for an array of given ResourceFactories
26 *
27 * @param resourceFactories
28 */
29 public DefaultResourceManager(List resourceFactories) {
30 this.resourceFactories = resourceFactories;
31 }
32
33
34 public void configure(ApplicationContext context) {
35
36 }
37
38 /***
39 * Creates the PlanarImage from a URI resource and sets it on the ImageRequest
40 *
41 * @param request the image request
42 * @throws ResourceException
43 */
44 public void handle(ImageRequest request) throws JmageException {
45 PlanarImage image = (PlanarImage) this.createFrom(request);
46 request.setImage(image);
47 }
48
49 /***
50 * Creates a resource object to use from a given URI.
51 *
52 * @param request
53 * @return the local resource object.
54 * @throws org.jmage.resource.ResourceException
55 * if the resource could not be found, or not be handled by any of
56 * the configured ResourceFactory objects.
57 */
58 public Object createFrom(ImageRequest request) throws JmageException {
59 Object resourceObj = null;
60 if (request.getImageURI() == null) {
61 throw new ResourceException(RESOURCE_ERROR);
62 }
63
64 Iterator it = resourceFactories.iterator();
65 while (it.hasNext()) {
66 ResourceFactory factory = (ResourceFactory) it.next();
67 if (factory.canHandle(request.getImageURI())) {
68
69 factory.configureRequestProperties(request.getFilterChainProperties());
70
71 resourceObj = factory.createFrom(request.getImageURI());
72
73
74 factory.removeRequestProperties(request.getFilterChainProperties());
75 break;
76 }
77 }
78 if (resourceObj == null) {
79 throw new ResourceException(RESOURCEFACTORY_ERROR);
80 }
81
82 return resourceObj;
83 }
84
85 /***
86 * Creates a resource object to use from a given URI.
87 *
88 * @param resourceURI
89 * @return the local resource object.
90 * @throws org.jmage.resource.ResourceException
91 * if the resource could not be found, or not be handled by any of
92 * the configured ResourceFactory objects.
93 */
94 public Object createFrom(URI resourceURI) throws JmageException {
95 Object resourceObj = null;
96 if (resourceURI == null) {
97 throw new ResourceException(RESOURCE_ERROR);
98 }
99
100 Iterator it = resourceFactories.iterator();
101 while (it.hasNext()) {
102 ResourceFactory factory = (ResourceFactory) it.next();
103 if (factory.canHandle(resourceURI)) {
104
105 resourceObj = factory.createFrom(resourceURI);
106
107 break;
108 }
109 }
110 if (resourceObj == null) {
111 throw new ResourceException(RESOURCEFACTORY_ERROR);
112 }
113
114 return resourceObj;
115 }
116
117 public String toString() {
118 return "[" + this.getClass().getName() + "#" + this.hashCode() + "]";
119 }
120 }