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 */ 019package org.apache.shiro.web.servlet; 020 021import 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 */ 028public class ServletContextSupport { 029 030 private ServletContext servletContext; 031 032 public ServletContext getServletContext() { 033 return servletContext; 034 } 035 036 public void setServletContext(ServletContext servletContext) { 037 this.servletContext = servletContext; 038 } 039 040 @SuppressWarnings({"UnusedDeclaration"}) 041 protected String getContextInitParam(String paramName) { 042 return getServletContext().getInitParameter(paramName); 043 } 044 045 private ServletContext getRequiredServletContext() { 046 ServletContext servletContext = getServletContext(); 047 if (servletContext == null) { 048 String msg = "ServletContext property must be set via the setServletContext method."; 049 throw new IllegalStateException(msg); 050 } 051 return servletContext; 052 } 053 054 @SuppressWarnings({"UnusedDeclaration"}) 055 protected void setContextAttribute(String key, Object value) { 056 if (value == null) { 057 removeContextAttribute(key); 058 } else { 059 getRequiredServletContext().setAttribute(key, value); 060 } 061 } 062 063 @SuppressWarnings({"UnusedDeclaration"}) 064 protected Object getContextAttribute(String key) { 065 return getRequiredServletContext().getAttribute(key); 066 } 067 068 protected void removeContextAttribute(String key) { 069 getRequiredServletContext().removeAttribute(key); 070 } 071 072 /** 073 * It is highly recommended not to override this method directly, and instead override the 074 * {@link #toStringBuilder() toStringBuilder()} method, a better-performing alternative. 075 * 076 * @return the String representation of this instance. 077 */ 078 @Override 079 public String toString() { 080 return toStringBuilder().toString(); 081 } 082 083 /** 084 * Same concept as {@link #toString() toString()}, but returns a {@link StringBuilder} instance instead. 085 * 086 * @return a StringBuilder instance to use for appending String data that will eventually be returned from a 087 * {@code toString()} invocation. 088 */ 089 protected StringBuilder toStringBuilder() { 090 return new StringBuilder(super.toString()); 091 } 092}