View Javadoc

    1   /*
    2    *
    3    *   Copyright 2005-2006 The Apache Software Foundation.
    4    *
    5    *  Licensed under the Apache License, Version 2.0 (the "License");
    6    *  you may not use this file except in compliance with the License.
    7    *  You may obtain a copy of the License at
    8    *
    9    *      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   */
    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