    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one or more
    003     * contributor license agreements.  See the NOTICE file distributed with
    004     * this work for additional information regarding copyright ownership.
    005     * The ASF licenses this file to You under the Apache License, Version 2.0
    006     * (the "License"); you may not use this file except in compliance with
    007     * the License.  You may obtain a copy of the License at
    008     *
    009     *     http://www.apache.org/licenses/LICENSE-2.0
    010     *
    011     * Unless required by applicable law or agreed to in writing, software
    012     * distributed under the License is distributed on an "AS IS" BASIS,
    013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014     * See the License for the specific language governing permissions and
    015     * limitations under the License.
    016     */
    017    package org.apache.commons.scxml.env.servlet;
    018
    019    import javax.servlet.ServletContext;
    020
    021    import org.apache.commons.scxml.PathResolver;
    022
    023    /**
    024     * A wrapper around ServletContext that implements PathResolver.
    025     *
    026     * @see org.apache.commons.scxml.PathResolver
    027     */
    028    public class ServletContextResolver implements PathResolver {
    029
    030        /** Cannot accept a null ServletContext, it will just throw
    031         *  NullPointerException down the road. */
    032        private static final String ERR_SERVLET_CTX_NULL =
    033            "ServletContextResolver cannot be instantiated with a null"
    034            + " ServletContext";
    035
    036        /** The SevletContext we will use to resolve paths. */
    037        private ServletContext ctx = null;
    038
    039        /**
    040         * Constructor.
    041         *
    042         * @param ctx The ServletContext instance for this web application.
    043         */
    044        public ServletContextResolver(final ServletContext ctx) {
    045            if (ctx == null) {
    046                throw new IllegalArgumentException(ERR_SERVLET_CTX_NULL);
    047            }
    048            this.ctx = ctx;
    049        }
    050
    051        /**
    052         * Delegates to the underlying ServletContext's getRealPath(String).
    053         *
    054         * @param ctxPath context sensitive path, can be a relative URL
    055         * @return resolved path (an absolute URL) or <code>null</code>
    056         * @see org.apache.commons.scxml.PathResolver#resolvePath(java.lang.String)
    057         */
    058        public String resolvePath(final String ctxPath) {
    059            return ctx.getRealPath(ctxPath);
    060        }
    061
    062        /**
    063         * Retrieve the PathResolver rooted at the given path.
    064         *
    065         * @param ctxPath context sensitive path, can be a relative URL
    066         * @return returns a new resolver rooted at ctxPath
    067         * @see org.apache.commons.scxml.PathResolver#getResolver(java.lang.String)
    068         */
    069        public PathResolver getResolver(final String ctxPath) {
    070            return this;
    071        }
    072
    073    }
    074