1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 =
33 "ServletContextResolver cannot be instantiated with a null"
34 + " ServletContext";
35
36 /*** The SevletContext we will use to resolve paths. */
37 private ServletContext ctx = null;
38
39 /***
40 * Constructor.
41 *
42 * @param ctx The ServletContext instance for this web application.
43 */
44 public ServletContextResolver(final ServletContext ctx) {
45 if (ctx == null) {
46 throw new IllegalArgumentException(ERR_SERVLET_CTX_NULL);
47 }
48 this.ctx = ctx;
49 }
50
51 /***
52 * Delegates to the underlying ServletContext's getRealPath(String).
53 *
54 * @param ctxPath context sensitive path, can be a relative URL
55 * @return resolved path (an absolute URL) or <code>null</code>
56 * @see org.apache.commons.scxml.PathResolver#resolvePath(java.lang.String)
57 */
58 public String resolvePath(final String ctxPath) {
59 return ctx.getRealPath(ctxPath);
60 }
61
62 /***
63 * Retrieve the PathResolver rooted at the given path.
64 *
65 * @param ctxPath context sensitive path, can be a relative URL
66 * @return returns a new resolver rooted at ctxPath
67 * @see org.apache.commons.scxml.PathResolver#getResolver(java.lang.String)
68 */
69 public PathResolver getResolver(final String ctxPath) {
70 return this;
71 }
72
73 }
74