01: // Copyright 2006 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.internal.services;
16:
17: import static org.apache.tapestry.ioc.internal.util.Defense.notNull;
18:
19: import java.net.URL;
20:
21: import org.apache.tapestry.ioc.Resource;
22: import org.apache.tapestry.ioc.internal.util.AbstractResource;
23: import org.apache.tapestry.services.Context;
24:
25: /**
26: * A resource stored with in the web application context.
27: */
28: public class ContextResource extends AbstractResource {
29: private static final int PRIME = 37;
30:
31: private Context _context;
32:
33: public ContextResource(Context context, String path) {
34: super (path);
35:
36: notNull(context, "context");
37:
38: _context = context;
39: }
40:
41: @Override
42: public String toString() {
43: return String.format("context:%s", getPath());
44: }
45:
46: @Override
47: protected Resource newResource(String path) {
48: return new ContextResource(_context, path);
49: }
50:
51: public URL toURL() {
52: // This is so easy to screw up; ClassLoader.getResource() doesn't want a leading slash,
53: // and HttpServletContext.getResource() does. This is what I mean when I say that
54: // a framework is an accumulation of the combined experience of many users and developers.
55:
56: return _context.getResource("/" + getPath());
57: }
58:
59: @Override
60: public int hashCode() {
61: return PRIME * _context.hashCode() + getPath().hashCode();
62: }
63:
64: @Override
65: public boolean equals(Object obj) {
66: if (this == obj)
67: return true;
68: if (obj == null)
69: return false;
70: if (getClass() != obj.getClass())
71: return false;
72:
73: final ContextResource other = (ContextResource) obj;
74:
75: return _context == other._context
76: && getPath().equals(other.getPath());
77: }
78:
79: }
|