001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed;
018:
019: import java.util.Collection;
020: import java.util.Enumeration;
021: import java.util.HashMap;
022:
023: import javax.portlet.PortletMode;
024: import javax.portlet.WindowState;
025:
026: import org.apache.jetspeed.administration.PortalConfiguration;
027: import org.apache.jetspeed.container.PortletRequestContext;
028: import org.apache.jetspeed.engine.Engine;
029: import org.apache.jetspeed.om.common.portlet.PortletApplication;
030: import org.apache.pluto.util.Enumerator;
031:
032: /**
033: * Implementation of Portal Context associated with running thread of the engine
034: *
035: * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
036: * @version $Id: JetspeedPortalContext.java 553375 2007-07-05 05:37:00Z taylor $
037: */
038: public class JetspeedPortalContext implements PortalContext {
039: private static final String SUPPORTED_WINDOWSTATE_ATTR = "supported.windowstate";
040: private static final String SUPPORTED_PORTLETMODE_ATTR = "supported.portletmode";
041: private static final String PORTAL_VERSION_ATTR = "portal.version";
042: private static final String PORTAL_NAME_ATTR = "portal.name";
043:
044: /**
045: * The engine associated with this context.
046: */
047: private Engine engine = null;
048:
049: /**
050: * Runtime attributes.
051: */
052: private HashMap attributes = new HashMap();
053:
054: /**
055: * Configuration state
056: */
057: private PortalConfiguration configuration = null;
058:
059: /**
060: * The base from which the Jetspped application will operate.
061: */
062: private String applicationRoot;
063:
064: private final String portalInfo;
065:
066: public JetspeedPortalContext(Engine engine,
067: PortalConfiguration configuration, String applicationRoot) {
068: this .engine = engine;
069: this .configuration = configuration;
070: this .applicationRoot = applicationRoot;
071:
072: portalInfo = configuration.getString(PORTAL_NAME_ATTR) + "/"
073: + configuration.getString(PORTAL_VERSION_ATTR);
074:
075: // Inititalize supported portlet modes and window states
076: String[] supportedModes = configuration
077: .getStringArray(SUPPORTED_PORTLETMODE_ATTR);
078: String[] supportedStates = configuration
079: .getStringArray(SUPPORTED_WINDOWSTATE_ATTR);
080: new JetspeedActions(supportedModes, supportedStates);
081: }
082:
083: // ------------------------------------------------------------------------
084: // A C C E S S O R S
085: // ------------------------------------------------------------------------
086:
087: /**
088: * Returns the configuration properties for this Jetspeed engine context.
089: *
090: * @return a <code>Configuration</code> containing the configuration properties for this Jetspeed context.
091: */
092: public PortalConfiguration getConfiguration() {
093: return configuration;
094: }
095:
096: public String getConfigurationProperty(String key) {
097: return configuration.getString(key);
098: }
099:
100: public String getConfigurationProperty(String key,
101: String defaultValue) {
102: return configuration.getString(key, defaultValue);
103: }
104:
105: /**
106: * Set the configuration properties for this Jetspeed engine context.
107: *
108: * @param configuration - the configuration properties
109: */
110: public void setConfiguration(PortalConfiguration configuration) {
111: this .configuration = configuration;
112: }
113:
114: /**
115: * Returns the application root for this Jetspeed engine context.
116: *
117: * @return a <code>String</code> containing the application root path for this Jetspeed context.
118: */
119: public String getApplicationRoot() {
120: return applicationRoot;
121: }
122:
123: /**
124: * Sets the application root path for this Jetspeed engine context.
125: *
126: * @param applicationRoot - the applicationRoot path on the file system.
127: */
128: public void setApplicationRoot(String applicationRoot) {
129: this .applicationRoot = applicationRoot;
130: }
131:
132: /**
133: * Returns the engine associated with this context.
134: *
135: * @return an <code>Engine</code> associated with this context
136: */
137: public Engine getEngine() {
138: return this .engine;
139: }
140:
141: /**
142: * Returns the engine attribute with the given name, or null if there is no attribute by that name.
143: *
144: * @return an <code>Object</code> containing the value of the attribute, or null if no attribute exists matching the given name
145: */
146: public Object getAttribute(String name) {
147: return attributes.get(name);
148: }
149:
150: /**
151: * Binds an object to a given attribute name in this servlet context.
152: *
153: * @param name - a <code>String</code> specifying the name of the attribute
154: * @param value - an <code>Object</code> representing the attribute to be bound
155: */
156: public void setAttribute(String name, Object value) {
157: attributes.put(name, value);
158: }
159:
160: /* (non-Javadoc)
161: * @see javax.portlet.PortalContext#getProperty(java.lang.String)
162: */
163: public String getProperty(String name) {
164: if (name == null) {
165: throw new IllegalArgumentException("Property name == null");
166: }
167: return (String) configuration.getString(name);
168: }
169:
170: /* (non-Javadoc)
171: * @see javax.portlet.PortalContext#getPropertyNames()
172: */
173: public Enumeration getPropertyNames() {
174: return new Enumerator(configuration.getKeys());
175: }
176:
177: private Collection getSupportedModes() {
178: PortletRequestContext ctx = PortletRequestContext.getContext();
179: if (ctx != null) {
180: PortletApplication pa = ((PortletApplication) ctx
181: .getPortletDefinition()
182: .getPortletApplicationDefinition());
183: return pa.getSupportedPortletModes();
184: }
185: return JetspeedActions.getStandardPortletModes();
186: }
187:
188: /* (non-Javadoc)
189: * @see javax.portlet.PortalContext#getSupportedPortletModes()
190: */
191: public Enumeration getSupportedPortletModes() {
192: return new Enumerator(getSupportedModes());
193: }
194:
195: public boolean isPortletModeAllowed(PortletMode mode) {
196: return getSupportedModes().contains(mode);
197: }
198:
199: private Collection getSupportedStates() {
200: PortletRequestContext ctx = PortletRequestContext.getContext();
201: if (ctx != null) {
202: PortletApplication pa = ((PortletApplication) ctx
203: .getPortletDefinition()
204: .getPortletApplicationDefinition());
205: return pa.getSupportedWindowStates();
206: }
207: return JetspeedActions.getStandardWindowStates();
208: }
209:
210: /* (non-Javadoc)
211: * @see javax.portlet.PortalContext#getSupportedWindowStates()
212: */
213: public Enumeration getSupportedWindowStates() {
214: return new Enumerator(getSupportedStates());
215: }
216:
217: public boolean isWindowStateAllowed(WindowState state) {
218: return getSupportedStates().contains(state);
219: }
220:
221: /* (non-Javadoc)
222: * @see javax.portlet.PortalContext#getPortalInfo()
223: */
224: public String getPortalInfo() {
225: return portalInfo;
226: }
227: }
|