01: /*
02: * Copyright 2002-2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.web.portlet.context;
18:
19: import java.io.File;
20:
21: import javax.portlet.PortletContext;
22:
23: import org.springframework.context.support.ApplicationObjectSupport;
24: import org.springframework.web.portlet.util.PortletUtils;
25:
26: /**
27: * Convenient superclass for application objects running in a Portlet ApplicationContext.
28: * Provides getApplicationContext, getServletContext, and getTempDir methods.
29: *
30: * @author Juergen Hoeller
31: * @since 2.0
32: */
33: public abstract class PortletApplicationObjectSupport extends
34: ApplicationObjectSupport implements PortletContextAware {
35:
36: private PortletContext portletContext;
37:
38: public void setPortletContext(PortletContext portletContext) {
39: this .portletContext = portletContext;
40: }
41:
42: /**
43: * Overrides the base class behavior to enforce running in an ApplicationContext.
44: * All accessors will throw IllegalStateException if not running in a context.
45: * @see #getApplicationContext()
46: * @see #getMessageSourceAccessor()
47: * @see #getPortletContext()
48: * @see #getTempDir()
49: */
50: protected boolean isContextRequired() {
51: return true;
52: }
53:
54: /**
55: * Return the current PortletContext.
56: * @throws IllegalStateException if not running within a PortletContext
57: */
58: protected final PortletContext getPortletContext()
59: throws IllegalStateException {
60: if (this .portletContext == null) {
61: throw new IllegalStateException(
62: "PortletApplicationObjectSupport instance [" + this
63: + "] does not run within a PortletContext");
64: }
65: return this .portletContext;
66: }
67:
68: /**
69: * Return the temporary directory for the current web application,
70: * as provided by the servlet container.
71: * @return the File representing the temporary directory
72: * @throws IllegalStateException if not running within a PortletContext
73: * @see org.springframework.web.portlet.util.PortletUtils#getTempDir(javax.portlet.PortletContext)
74: */
75: protected final File getTempDir() throws IllegalStateException {
76: return PortletUtils.getTempDir(getPortletContext());
77: }
78:
79: }
|