001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.shiro.web.servlet;
020    
021    import javax.servlet.ServletContext;
022    
023    /**
024     * Base implementation for any components that need to access the web application's {@link ServletContext ServletContext}.
025     *
026     * @since 0.2
027     */
028    public class ServletContextSupport {
029    
030        //TODO - complete JavaDoc
031        private ServletContext servletContext = null;
032    
033        public ServletContext getServletContext() {
034            return servletContext;
035        }
036    
037        public void setServletContext(ServletContext servletContext) {
038            this.servletContext = servletContext;
039        }
040    
041        @SuppressWarnings({"UnusedDeclaration"})
042        protected String getContextInitParam(String paramName) {
043            return getServletContext().getInitParameter(paramName);
044        }
045    
046        private ServletContext getRequiredServletContext() {
047            ServletContext servletContext = getServletContext();
048            if (servletContext == null) {
049                String msg = "ServletContext property must be set via the setServletContext method.";
050                throw new IllegalStateException(msg);
051            }
052            return servletContext;
053        }
054    
055        @SuppressWarnings({"UnusedDeclaration"})
056        protected void setContextAttribute(String key, Object value) {
057            if (value == null) {
058                removeContextAttribute(key);
059            } else {
060                getRequiredServletContext().setAttribute(key, value);
061            }
062        }
063    
064        @SuppressWarnings({"UnusedDeclaration"})
065        protected Object getContextAttribute(String key) {
066            return getRequiredServletContext().getAttribute(key);
067        }
068    
069        protected void removeContextAttribute(String key) {
070            getRequiredServletContext().removeAttribute(key);
071        }
072    
073        /**
074         * It is highly recommended not to override this method directly, and instead override the
075         * {@link #toStringBuilder() toStringBuilder()} method, a better-performing alternative.
076         *
077         * @return the String representation of this instance.
078         */
079        @Override
080        public String toString() {
081            return toStringBuilder().toString();
082        }
083    
084        /**
085         * Same concept as {@link #toString() toString()}, but returns a {@link StringBuilder} instance instead.
086         *
087         * @return a StringBuilder instance to use for appending String data that will eventually be returned from a
088         *         {@code toString()} invocation.
089         */
090        protected StringBuilder toStringBuilder() {
091            return new StringBuilder(super.toString());
092        }
093    }