001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.portlet.context;
018
019import java.io.File;
020import javax.portlet.PortletContext;
021
022import org.springframework.context.support.ApplicationObjectSupport;
023import org.springframework.web.portlet.util.PortletUtils;
024
025/**
026 * Convenient superclass for application objects running in a Portlet ApplicationContext.
027 * Provides getApplicationContext, getServletContext, and getTempDir methods.
028 *
029 * @author Juergen Hoeller
030 * @since 2.0
031 */
032public abstract class PortletApplicationObjectSupport extends ApplicationObjectSupport
033                implements PortletContextAware {
034
035        private PortletContext portletContext;
036
037
038        @Override
039        public void setPortletContext(PortletContext portletContext) {
040                this.portletContext = portletContext;
041        }
042
043
044        /**
045         * Overrides the base class behavior to enforce running in an ApplicationContext.
046         * All accessors will throw IllegalStateException if not running in a context.
047         * @see #getApplicationContext()
048         * @see #getMessageSourceAccessor()
049         * @see #getPortletContext()
050         * @see #getTempDir()
051         */
052        @Override
053        protected boolean isContextRequired() {
054                return true;
055        }
056
057        /**
058         * Return the current PortletContext.
059         * @throws IllegalStateException if not running within a PortletContext
060         */
061        protected final PortletContext getPortletContext() throws IllegalStateException {
062                if (this.portletContext == null) {
063                        throw new IllegalStateException(
064                                        "PortletApplicationObjectSupport instance [" + this + "] does not run within a PortletContext");
065                }
066                return this.portletContext;
067        }
068
069        /**
070         * Return the temporary directory for the current web application,
071         * as provided by the servlet container.
072         * @return the File representing the temporary directory
073         * @throws IllegalStateException if not running within a PortletContext
074         * @see org.springframework.web.portlet.util.PortletUtils#getTempDir(javax.portlet.PortletContext)
075         */
076        protected final File getTempDir() throws IllegalStateException {
077                return PortletUtils.getTempDir(getPortletContext());
078        }
079
080}