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.services.information;
18  
19  import java.util.Map;
20  
21  import javax.servlet.ServletConfig;
22  import javax.servlet.ServletRequest;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletRequestWrapper;
25  
26  import org.apache.pluto.factory.Factory;
27  import org.apache.pluto.services.information.DynamicInformationProvider;
28  import org.apache.pluto.services.information.InformationProviderService;
29  import org.apache.pluto.services.information.StaticInformationProvider;
30  
31  import org.apache.jetspeed.aggregator.CurrentWorkerContext;
32  
33  /***
34   * Factory class for getting Information Provider access
35   *
36   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
37   * @version $Id: InformationProviderServiceImpl.java 587813 2007-10-24 08:20:39Z woonsan $
38   */
39  public class InformationProviderServiceImpl implements Factory, InformationProviderService
40  {
41      private javax.servlet.ServletConfig servletConfig;
42      private final StaticInformationProvider staticInformationProvider;
43      
44      public InformationProviderServiceImpl(StaticInformationProvider staticInformationProvider, ServletConfig config)
45      {
46          this.staticInformationProvider = staticInformationProvider;
47          
48      }
49  
50      public void init(ServletConfig config, Map properties) throws Exception
51      {
52          // does nothing at all    
53      }
54  
55      public StaticInformationProvider getStaticProvider()
56      {        
57          return staticInformationProvider;
58      }
59  
60      public DynamicInformationProvider getDynamicProvider(HttpServletRequest request)
61      {
62          DynamicInformationProvider provider = null;
63          
64          boolean isParallel = CurrentWorkerContext.getParallelRenderingMode();
65          ServletRequest servletRequest = null;
66          
67          if (isParallel)
68          {
69              // request should be an instance of org.apache.jetspeed.engine.servlet.ServletRequestImpl
70              // unwrap the real request instance provided by the container to synchronize
71              servletRequest = ((HttpServletRequestWrapper) request).getRequest();
72  
73              // if it is not an instance of HttpServletRequestWrapper any more, then it is not AdjustedSRTServletRequest instance.
74              if (servletRequest instanceof HttpServletRequestWrapper)
75              {
76                  servletRequest = ((HttpServletRequestWrapper) servletRequest).getRequest();
77              }
78              
79              synchronized (servletRequest)
80              {
81                  provider = (DynamicInformationProvider) servletRequest.getAttribute("org.apache.jetspeed.engine.core.DynamicInformationProvider");
82              }
83          }
84          else
85          {
86              provider = (DynamicInformationProvider) request.getAttribute("org.apache.jetspeed.engine.core.DynamicInformationProvider");
87          }
88  
89          if (provider == null)
90          {
91              provider = new DynamicInformationProviderImpl(request, servletConfig);
92              
93              if (isParallel)
94              {
95                  synchronized (servletRequest)
96                  {
97                      servletRequest.setAttribute("org.apache.jetspeed.engine.core.DynamicInformationProvider", provider);
98                  }
99              }
100             else
101             {
102                 request.setAttribute("org.apache.jetspeed.engine.core.DynamicInformationProvider", provider);
103             }
104         }
105 
106         return provider;
107     }
108 
109     /***
110      * <p>
111      * destroy
112      * </p>
113      *
114      * @see org.apache.pluto.factory.Factory#destroy()
115      * @throws java.lang.Exception
116      */
117     public void destroy() throws Exception
118     {       
119        // also does nothing
120     }
121 }