KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > portlet > context > StaticPortletApplicationContext


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 javax.portlet.PortletConfig;
20 import javax.portlet.PortletContext;
21 import javax.servlet.ServletContext JavaDoc;
22
23 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
24 import org.springframework.context.ApplicationContext;
25 import org.springframework.context.support.StaticApplicationContext;
26 import org.springframework.core.io.Resource;
27 import org.springframework.core.io.support.ResourcePatternResolver;
28 import org.springframework.web.context.WebApplicationContext;
29 import org.springframework.web.context.request.RequestScope;
30 import org.springframework.web.context.request.SessionScope;
31
32 /**
33  * Static Portlet-based ApplicationContext implementation for testing.
34  * Not intended for use in production applications.
35  *
36  * <p>Implements the
37  * {@link org.springframework.web.portlet.context.ConfigurablePortletApplicationContext}
38  * interface to allow for direct replacement of an {@link XmlPortletApplicationContext},
39  * despite not actually supporting external configuration files.
40  *
41  * <p>Interprets resource paths as portlet context resources, that is, as paths
42  * beneath the portlet application root. Absolute paths, for example for files
43  * outside the portlet app root, can be accessed via "file:" URLs, as implemented
44  * by {@link org.springframework.core.io.DefaultResourceLoader}.
45  *
46  * @author Rod Johnson
47  * @author Juergen Hoeller
48  * @author Mark Fisher
49  * @since 2.0
50  */

51 public class StaticPortletApplicationContext extends StaticApplicationContext
52         implements ConfigurablePortletApplicationContext {
53
54     private ServletContext JavaDoc servletContext;
55     
56     private PortletContext portletContext;
57
58     private PortletConfig portletConfig;
59
60     private String JavaDoc namespace;
61
62
63     public StaticPortletApplicationContext() {
64         setDisplayName("Root Portlet ApplicationContext");
65     }
66     
67     public void setParent(ApplicationContext parent) {
68         super.setParent(parent);
69         if (parent instanceof WebApplicationContext) {
70             this.servletContext = ((WebApplicationContext) parent).getServletContext();
71         }
72     }
73
74     public ServletContext JavaDoc getServletContext() {
75         return this.servletContext;
76     }
77
78     public void setPortletContext(PortletContext portletContext) {
79         this.portletContext = portletContext;
80     }
81
82     public PortletContext getPortletContext() {
83         return this.portletContext;
84     }
85
86     public void setPortletConfig(PortletConfig portletConfig) {
87         this.portletConfig = portletConfig;
88         if (portletConfig != null && this.portletContext == null) {
89             this.portletContext = portletConfig.getPortletContext();
90         }
91     }
92
93     public PortletConfig getPortletConfig() {
94         return this.portletConfig;
95     }
96
97     public void setNamespace(String JavaDoc namespace) {
98         this.namespace = namespace;
99         if (namespace != null) {
100             setDisplayName("Portlet ApplicationContext for namespace '" + namespace + "'");
101         }
102     }
103
104     public String JavaDoc getNamespace() {
105         return this.namespace;
106     }
107
108     public void setConfigLocations(String JavaDoc[] configLocations) {
109         throw new UnsupportedOperationException JavaDoc("StaticPortletApplicationContext does not support config locations");
110     }
111
112     public String JavaDoc[] getConfigLocations() {
113         return null;
114     }
115
116
117     /**
118      * Register request/session scopes, a {@link PortletContextAwareProcessor}, etc.
119      */

120     protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
121         beanFactory.registerScope(SCOPE_REQUEST, new RequestScope());
122         beanFactory.registerScope(SCOPE_SESSION, new SessionScope(false));
123         beanFactory.registerScope(SCOPE_GLOBAL_SESSION, new SessionScope(true));
124
125         beanFactory.addBeanPostProcessor(new PortletContextAwareProcessor(this.portletContext, this.portletConfig));
126         beanFactory.ignoreDependencyInterface(PortletContextAware.class);
127         beanFactory.ignoreDependencyInterface(PortletConfigAware.class);
128     }
129
130     /**
131      * This implementation supports file paths beneath the root of the PortletContext.
132      * @see PortletContextResource
133      */

134     protected Resource getResourceByPath(String JavaDoc path) {
135         return new PortletContextResource(this.portletContext, path);
136     }
137
138     /**
139      * This implementation supports pattern matching in unexpanded WARs too.
140      * @see PortletContextResourcePatternResolver
141      */

142     protected ResourcePatternResolver getResourcePatternResolver() {
143         return new PortletContextResourcePatternResolver(this);
144     }
145
146 }
147
Popular Tags