01: /*
02: * Copyright 2005-2007 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: // Created on May 8, 2006
18: package edu.iu.uis.eden.test.web.framework.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.test.web.framework.Property;
26: import edu.iu.uis.eden.test.web.framework.PropertyScheme;
27: import edu.iu.uis.eden.test.web.framework.Script;
28: import edu.iu.uis.eden.test.web.framework.ScriptState;
29: import edu.iu.uis.eden.test.web.framework.Util;
30:
31: /**
32: * A property scheme that loads resources from the class loader
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, ScriptState state) {
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: }
63: if (!prefix.endsWith("/")) {
64: prefix += "/";
65: }
66: resource = prefix + property.locator;
67: }
68: String resStr = property.locator
69: + (relative ? "(" + resource + ")" : "");
70: LOG.info("Reading resource " + resStr + "...");
71:
72: InputStream is = getClass().getResourceAsStream(resource);
73: if (is == null) {
74: throw new RuntimeException("Resource not found: " + resStr);
75: }
76: try {
77: return Util.readResource(is);
78: } catch (IOException ioe) {
79: throw new RuntimeException("Error loading resource: "
80: + resStr, ioe);
81: }
82: }
83:
84: public String toString() {
85: return "[ResourceScheme]";
86: }
87: }
|