View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.container.state.impl;
18  
19  import java.util.Collections;
20  import java.util.Enumeration;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import javax.portlet.PortletMode;
25  import javax.portlet.WindowState;
26  import javax.servlet.http.HttpServletRequest;
27  
28  import org.apache.jetspeed.PortalContext;
29  import org.apache.jetspeed.container.state.FailedToCreateNavStateException;
30  import org.apache.jetspeed.container.state.NavigationalState;
31  import org.apache.jetspeed.container.state.NavigationalStateComponent;
32  import org.apache.jetspeed.container.url.PortalURL;
33  import org.apache.jetspeed.util.ArgUtil;
34  import org.springframework.beans.BeansException;
35  import org.springframework.beans.factory.BeanFactory;
36  import org.springframework.beans.factory.BeanFactoryAware;
37  
38  /***
39   * JetspeedNavigationalStateComponent
40   *
41   * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
42   * @version $Id: JetspeedNavigationalStateComponent.java 517124 2007-03-12 08:10:25Z ate $
43   */
44  public class JetspeedNavigationalStateComponent implements NavigationalStateComponent, BeanFactoryAware
45  {
46      private final String navBeanName;
47      private final String urlBeanName;    
48      private final String desktopUrlBeanName;
49      
50      // maps containing allowed PortletMode and WindowState objects on their lowercase name
51      // ensuring only allowed, and always the same objects are returned and allowing comparision by value
52      private final Map supportedPortletModes = Collections.synchronizedMap(new HashMap());
53      private final Map supportedWindowStates = Collections.synchronizedMap(new HashMap());
54  
55      private BeanFactory beanFactory;
56      
57  
58      /***
59       * @param navBeanName
60       *            name of the bean implementing Navigational State instances
61       * @param urlBeanName
62       *            name of the bean implementing Portal URL instances
63       * @param navCodecBeanName
64       *            name of the bean implementing Navigational State Codec instance
65       * @throws ClassNotFoundException
66       *             if <code>navClassName</code> or <code>urlClassName</code>
67       *             do not exist.
68       */
69      public JetspeedNavigationalStateComponent(String  navBeanName, String urlBeanName, PortalContext portalContext, String desktopUrlBeanName)
70              throws ClassNotFoundException
71      {
72          ArgUtil.assertNotNull(String.class, navBeanName, this);
73          ArgUtil.assertNotNull(String.class, urlBeanName, this);        
74          this.navBeanName = navBeanName;
75          this.urlBeanName = urlBeanName;
76          this.desktopUrlBeanName = desktopUrlBeanName;
77  
78          Enumeration portletModesEnum = portalContext.getSupportedPortletModes();
79          PortletMode portletMode;
80          while ( portletModesEnum.hasMoreElements() )
81          {
82              portletMode = (PortletMode)portletModesEnum.nextElement();
83              supportedPortletModes.put(portletMode.toString(), portletMode);
84          }
85          Enumeration windowStatesEnum = portalContext.getSupportedWindowStates();
86          WindowState windowState;
87          while ( windowStatesEnum.hasMoreElements() )
88          {
89              windowState = (WindowState)windowStatesEnum.nextElement();
90              supportedWindowStates.put(windowState.toString(), windowState);
91          }
92      }
93  
94      /***
95       *
96       * <p>
97       * create
98       * </p>
99       *
100      * @see org.apache.jetspeed.container.state.NavigationalStateComponent#create(org.apache.jetspeed.request.RequestContext)
101      * @return @throws
102      *         FailedToCreateNavStateException if the nav state could not be
103      *         created. Under normal circumstances, this should not happen.
104      */
105     public NavigationalState create() throws FailedToCreateNavStateException
106     {
107         try
108         {
109             return (NavigationalState) beanFactory.getBean(navBeanName, NavigationalState.class);
110         }
111         catch (BeansException e)
112         {           
113             throw new FailedToCreateNavStateException("Spring failed to create the NavigationalState bean.", e);
114         }
115     }
116 
117    /***
118     *
119     * <p>
120     * createURL
121     * </p>
122     *
123     * @see org.apache.jetspeed.container.state.NavigationalStateComponent#createURL(org.apache.jetspeed.request.RequestContext)
124     * @param context
125     * @return
126     */
127     public PortalURL createURL( HttpServletRequest request, String characterEncoding )
128     {
129         PortalURL url = (PortalURL) beanFactory.getBean(urlBeanName, PortalURL.class);
130         url.setRequest(request);
131         url.setCharacterEncoding(characterEncoding);
132         return url;
133     }
134 
135     public WindowState lookupWindowState( String name )
136     {
137         return (WindowState)supportedWindowStates.get(name.toLowerCase());
138     }
139 
140     public PortletMode lookupPortletMode( String name )
141     {
142         return (PortletMode)supportedPortletModes.get(name.toLowerCase());
143     }
144 
145     public void setBeanFactory(BeanFactory beanFactory) throws BeansException
146     {
147         this.beanFactory = beanFactory;        
148     }
149     
150     public PortalURL createDesktopURL(HttpServletRequest request, String characterEncoding)
151     {
152         PortalURL url = (PortalURL) beanFactory.getBean(desktopUrlBeanName, PortalURL.class);
153         url.setRequest(request);
154         url.setCharacterEncoding(characterEncoding);
155         return url;        
156     }
157 }