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.container.state.impl;
018:
019: import java.util.Collections;
020: import java.util.Enumeration;
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: import javax.portlet.PortletMode;
025: import javax.portlet.WindowState;
026: import javax.servlet.http.HttpServletRequest;
027:
028: import org.apache.jetspeed.PortalContext;
029: import org.apache.jetspeed.container.state.FailedToCreateNavStateException;
030: import org.apache.jetspeed.container.state.NavigationalState;
031: import org.apache.jetspeed.container.state.NavigationalStateComponent;
032: import org.apache.jetspeed.container.url.PortalURL;
033: import org.apache.jetspeed.util.ArgUtil;
034: import org.springframework.beans.BeansException;
035: import org.springframework.beans.factory.BeanFactory;
036: import org.springframework.beans.factory.BeanFactoryAware;
037:
038: /**
039: * JetspeedNavigationalStateComponent
040: *
041: * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
042: * @version $Id: JetspeedNavigationalStateComponent.java 517124 2007-03-12 08:10:25Z ate $
043: */
044: public class JetspeedNavigationalStateComponent implements
045: NavigationalStateComponent, BeanFactoryAware {
046: private final String navBeanName;
047: private final String urlBeanName;
048: private final String desktopUrlBeanName;
049:
050: // maps containing allowed PortletMode and WindowState objects on their lowercase name
051: // ensuring only allowed, and always the same objects are returned and allowing comparision by value
052: private final Map supportedPortletModes = Collections
053: .synchronizedMap(new HashMap());
054: private final Map supportedWindowStates = Collections
055: .synchronizedMap(new HashMap());
056:
057: private BeanFactory beanFactory;
058:
059: /**
060: * @param navBeanName
061: * name of the bean implementing Navigational State instances
062: * @param urlBeanName
063: * name of the bean implementing Portal URL instances
064: * @param navCodecBeanName
065: * name of the bean implementing Navigational State Codec instance
066: * @throws ClassNotFoundException
067: * if <code>navClassName</code> or <code>urlClassName</code>
068: * do not exist.
069: */
070: public JetspeedNavigationalStateComponent(String navBeanName,
071: String urlBeanName, PortalContext portalContext,
072: String desktopUrlBeanName) throws ClassNotFoundException {
073: ArgUtil.assertNotNull(String.class, navBeanName, this );
074: ArgUtil.assertNotNull(String.class, urlBeanName, this );
075: this .navBeanName = navBeanName;
076: this .urlBeanName = urlBeanName;
077: this .desktopUrlBeanName = desktopUrlBeanName;
078:
079: Enumeration portletModesEnum = portalContext
080: .getSupportedPortletModes();
081: PortletMode portletMode;
082: while (portletModesEnum.hasMoreElements()) {
083: portletMode = (PortletMode) portletModesEnum.nextElement();
084: supportedPortletModes.put(portletMode.toString(),
085: portletMode);
086: }
087: Enumeration windowStatesEnum = portalContext
088: .getSupportedWindowStates();
089: WindowState windowState;
090: while (windowStatesEnum.hasMoreElements()) {
091: windowState = (WindowState) windowStatesEnum.nextElement();
092: supportedWindowStates.put(windowState.toString(),
093: windowState);
094: }
095: }
096:
097: /**
098: *
099: * <p>
100: * create
101: * </p>
102: *
103: * @see org.apache.jetspeed.container.state.NavigationalStateComponent#create(org.apache.jetspeed.request.RequestContext)
104: * @return @throws
105: * FailedToCreateNavStateException if the nav state could not be
106: * created. Under normal circumstances, this should not happen.
107: */
108: public NavigationalState create()
109: throws FailedToCreateNavStateException {
110: try {
111: return (NavigationalState) beanFactory.getBean(navBeanName,
112: NavigationalState.class);
113: } catch (BeansException e) {
114: throw new FailedToCreateNavStateException(
115: "Spring failed to create the NavigationalState bean.",
116: e);
117: }
118: }
119:
120: /**
121: *
122: * <p>
123: * createURL
124: * </p>
125: *
126: * @see org.apache.jetspeed.container.state.NavigationalStateComponent#createURL(org.apache.jetspeed.request.RequestContext)
127: * @param context
128: * @return
129: */
130: public PortalURL createURL(HttpServletRequest request,
131: String characterEncoding) {
132: PortalURL url = (PortalURL) beanFactory.getBean(urlBeanName,
133: PortalURL.class);
134: url.setRequest(request);
135: url.setCharacterEncoding(characterEncoding);
136: return url;
137: }
138:
139: public WindowState lookupWindowState(String name) {
140: return (WindowState) supportedWindowStates.get(name
141: .toLowerCase());
142: }
143:
144: public PortletMode lookupPortletMode(String name) {
145: return (PortletMode) supportedPortletModes.get(name
146: .toLowerCase());
147: }
148:
149: public void setBeanFactory(BeanFactory beanFactory)
150: throws BeansException {
151: this .beanFactory = beanFactory;
152: }
153:
154: public PortalURL createDesktopURL(HttpServletRequest request,
155: String characterEncoding) {
156: PortalURL url = (PortalURL) beanFactory.getBean(
157: desktopUrlBeanName, PortalURL.class);
158: url.setRequest(request);
159: url.setCharacterEncoding(characterEncoding);
160: return url;
161: }
162: }
|