1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.scxml.env.servlet;
19
20 import javax.servlet.ServletContext;
21
22 import org.apache.commons.scxml.PathResolver;
23
24 /***
25 * A wrapper around ServletContext that implements PathResolver.
26 *
27 * @see org.apache.commons.scxml.PathResolver
28 */
29 public class ServletContextResolver implements PathResolver {
30
31 /*** Cannot accept a null ServletContext, it will just throw
32 * NullPointerException down the road. */
33 private static final String ERR_SERVLET_CTX_NULL =
34 "ServletContextResolver cannot be instantiated with a null"
35 + " ServletContext";
36
37 /*** The SevletContext we will use to resolve paths. */
38 private ServletContext ctx = null;
39
40 /***
41 * Constructor.
42 *
43 * @param ctx The ServletContext instance for this web application.
44 */
45 public ServletContextResolver(final ServletContext ctx) {
46 if (ctx == null) {
47 throw new IllegalArgumentException(ERR_SERVLET_CTX_NULL);
48 }
49 this.ctx = ctx;
50 }
51
52 /***
53 * Delegates to the underlying ServletContext's getRealPath(String).
54 *
55 * @param ctxPath context sensitive path, can be a relative URL
56 * @return resolved path (an absolute URL) or <code>null</code>
57 * @see org.apache.commons.scxml.PathResolver#resolvePath(java.lang.String)
58 */
59 public String resolvePath(final String ctxPath) {
60 return ctx.getRealPath(ctxPath);
61 }
62
63 /***
64 * Retrieve the PathResolver rooted at the given path.
65 *
66 * @param ctxPath context sensitive path, can be a relative URL
67 * @return returns a new resolver rooted at ctxPath
68 * @see org.apache.commons.scxml.PathResolver#getResolver(java.lang.String)
69 */
70 public PathResolver getResolver(final String ctxPath) {
71 return this;
72 }
73
74 }
75