01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
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: package org.apache.commons.scxml.env.servlet;
18:
19: import javax.servlet.ServletContext;
20:
21: import org.apache.commons.scxml.PathResolver;
22:
23: /**
24: * A wrapper around ServletContext that implements PathResolver.
25: *
26: * @see org.apache.commons.scxml.PathResolver
27: */
28: public class ServletContextResolver implements PathResolver {
29:
30: /** Cannot accept a null ServletContext, it will just throw
31: * NullPointerException down the road. */
32: private static final String ERR_SERVLET_CTX_NULL = "ServletContextResolver cannot be instantiated with a null"
33: + " ServletContext";
34:
35: /** The SevletContext we will use to resolve paths. */
36: private ServletContext ctx = null;
37:
38: /**
39: * Constructor.
40: *
41: * @param ctx The ServletContext instance for this web application.
42: */
43: public ServletContextResolver(final ServletContext ctx) {
44: if (ctx == null) {
45: throw new IllegalArgumentException(ERR_SERVLET_CTX_NULL);
46: }
47: this .ctx = ctx;
48: }
49:
50: /**
51: * Delegates to the underlying ServletContext's getRealPath(String).
52: *
53: * @param ctxPath context sensitive path, can be a relative URL
54: * @return resolved path (an absolute URL) or <code>null</code>
55: * @see org.apache.commons.scxml.PathResolver#resolvePath(java.lang.String)
56: */
57: public String resolvePath(final String ctxPath) {
58: return ctx.getRealPath(ctxPath);
59: }
60:
61: /**
62: * Retrieve the PathResolver rooted at the given path.
63: *
64: * @param ctxPath context sensitive path, can be a relative URL
65: * @return returns a new resolver rooted at ctxPath
66: * @see org.apache.commons.scxml.PathResolver#getResolver(java.lang.String)
67: */
68: public PathResolver getResolver(final String ctxPath) {
69: return this;
70: }
71:
72: }
|