001: /*
002: * Copyright 2002-2007 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: * http://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:
017: package org.springframework.web.portlet.context;
018:
019: import javax.portlet.PortletConfig;
020: import javax.portlet.PortletContext;
021: import javax.servlet.ServletContext;
022:
023: import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
024: import org.springframework.context.ApplicationContext;
025: import org.springframework.context.support.StaticApplicationContext;
026: import org.springframework.core.io.Resource;
027: import org.springframework.core.io.support.ResourcePatternResolver;
028: import org.springframework.web.context.WebApplicationContext;
029: import org.springframework.web.context.request.RequestScope;
030: import org.springframework.web.context.request.SessionScope;
031:
032: /**
033: * Static Portlet-based ApplicationContext implementation for testing.
034: * Not intended for use in production applications.
035: *
036: * <p>Implements the
037: * {@link org.springframework.web.portlet.context.ConfigurablePortletApplicationContext}
038: * interface to allow for direct replacement of an {@link XmlPortletApplicationContext},
039: * despite not actually supporting external configuration files.
040: *
041: * <p>Interprets resource paths as portlet context resources, that is, as paths
042: * beneath the portlet application root. Absolute paths, for example for files
043: * outside the portlet app root, can be accessed via "file:" URLs, as implemented
044: * by {@link org.springframework.core.io.DefaultResourceLoader}.
045: *
046: * @author Rod Johnson
047: * @author Juergen Hoeller
048: * @author Mark Fisher
049: * @since 2.0
050: */
051: public class StaticPortletApplicationContext extends
052: StaticApplicationContext implements
053: ConfigurablePortletApplicationContext {
054:
055: private ServletContext servletContext;
056:
057: private PortletContext portletContext;
058:
059: private PortletConfig portletConfig;
060:
061: private String namespace;
062:
063: public StaticPortletApplicationContext() {
064: setDisplayName("Root Portlet ApplicationContext");
065: }
066:
067: public void setParent(ApplicationContext parent) {
068: super .setParent(parent);
069: if (parent instanceof WebApplicationContext) {
070: this .servletContext = ((WebApplicationContext) parent)
071: .getServletContext();
072: }
073: }
074:
075: public ServletContext getServletContext() {
076: return this .servletContext;
077: }
078:
079: public void setPortletContext(PortletContext portletContext) {
080: this .portletContext = portletContext;
081: }
082:
083: public PortletContext getPortletContext() {
084: return this .portletContext;
085: }
086:
087: public void setPortletConfig(PortletConfig portletConfig) {
088: this .portletConfig = portletConfig;
089: if (portletConfig != null && this .portletContext == null) {
090: this .portletContext = portletConfig.getPortletContext();
091: }
092: }
093:
094: public PortletConfig getPortletConfig() {
095: return this .portletConfig;
096: }
097:
098: public void setNamespace(String namespace) {
099: this .namespace = namespace;
100: if (namespace != null) {
101: setDisplayName("Portlet ApplicationContext for namespace '"
102: + namespace + "'");
103: }
104: }
105:
106: public String getNamespace() {
107: return this .namespace;
108: }
109:
110: public void setConfigLocations(String[] configLocations) {
111: throw new UnsupportedOperationException(
112: "StaticPortletApplicationContext does not support config locations");
113: }
114:
115: public String[] getConfigLocations() {
116: return null;
117: }
118:
119: /**
120: * Register request/session scopes, a {@link PortletContextAwareProcessor}, etc.
121: */
122: protected void postProcessBeanFactory(
123: ConfigurableListableBeanFactory beanFactory) {
124: beanFactory.registerScope(SCOPE_REQUEST, new RequestScope());
125: beanFactory.registerScope(SCOPE_SESSION,
126: new SessionScope(false));
127: beanFactory.registerScope(SCOPE_GLOBAL_SESSION,
128: new SessionScope(true));
129:
130: beanFactory
131: .addBeanPostProcessor(new PortletContextAwareProcessor(
132: this .portletContext, this .portletConfig));
133: beanFactory
134: .ignoreDependencyInterface(PortletContextAware.class);
135: beanFactory.ignoreDependencyInterface(PortletConfigAware.class);
136: }
137:
138: /**
139: * This implementation supports file paths beneath the root of the PortletContext.
140: * @see PortletContextResource
141: */
142: protected Resource getResourceByPath(String path) {
143: return new PortletContextResource(this .portletContext, path);
144: }
145:
146: /**
147: * This implementation supports pattern matching in unexpanded WARs too.
148: * @see PortletContextResourcePatternResolver
149: */
150: protected ResourcePatternResolver getResourcePatternResolver() {
151: return new PortletContextResourcePatternResolver(this);
152: }
153:
154: }
|