01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.php
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package edu.iu.uis.eden.engine.node.var.schemes;
19:
20: import java.io.IOException;
21: import java.io.InputStream;
22:
23: import org.apache.log4j.Logger;
24:
25: import edu.iu.uis.eden.engine.RouteContext;
26: import edu.iu.uis.eden.engine.node.PropertiesUtil;
27: import edu.iu.uis.eden.engine.node.var.Property;
28: import edu.iu.uis.eden.engine.node.var.PropertyScheme;
29:
30: /**
31: * A property scheme that loads resources from the class loader.
32: *
33: * @author Aaron Hamid (arh14 at cornell dot edu)
34: */
35: public class ResourceScheme implements PropertyScheme {
36: private static final Logger LOG = Logger
37: .getLogger(ResourceScheme.class);
38:
39: public String getName() {
40: return "resource";
41: }
42:
43: public String getShortName() {
44: return "res";
45: }
46:
47: public Object load(Property property, RouteContext context) {
48: String resource;
49: boolean relative = false;
50: // if (property.locator.startsWith("/")) {
51: resource = property.locator;
52: // } else {
53: // relative = true;
54: // String prefix;
55: // /* if a resource prefix is set, use it */
56: // if (state.getResourcePrefix() != null) {
57: // prefix = state.getResourcePrefix();
58: // } else {
59: // /* otherwise use the location of the Script class */
60: // prefix = Script.class.getPackage().getName().replace('.', '/');
61: // }
62: // if (!prefix.endsWith("/")) {
63: // prefix += "/";
64: // }
65: // resource = prefix + property.locator;
66: // }
67: String resStr = property.locator
68: + (relative ? "(" + resource + ")" : "");
69: LOG.info("Reading resource " + resStr + "...");
70:
71: InputStream is = getClass().getResourceAsStream(resource);
72: if (is == null) {
73: throw new RuntimeException("Resource not found: " + resStr);
74: }
75: try {
76: return PropertiesUtil.readResource(is);
77: } catch (IOException ioe) {
78: throw new RuntimeException("Error loading resource: "
79: + resStr, ioe);
80: }
81: }
82:
83: public String toString() {
84: return "[ResourceScheme]";
85: }
86: }
|