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.invoker;
18  
19  import java.util.Map;
20  
21  import javax.servlet.ServletConfig;
22  
23  import org.apache.jetspeed.PortalContext;
24  import org.apache.jetspeed.factory.PortletFactory;
25  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
26  import org.apache.pluto.factory.PortletInvokerFactory;
27  import org.apache.pluto.invoker.PortletInvoker;
28  import org.apache.pluto.om.portlet.PortletDefinition;
29  
30  /***
31   * <p>
32   * Portlet Invoker Factory creates portlet invokers based on the servlet context.
33   * This class is part of the contract between Pluto and the Jetspeed Portal as defined
34   * in the interfaces under <code>org.apache.pluto.factory</code>
35   * The Pluto container uses portlet invokers to abstract access to portlets.
36   * An invoker interfaces defines which actions are performed between the portal and container,
37   * namely action, render and optionally load. Portlet invoker factories are implemented by
38   * the portal implementation. The Pluto container uses pluggable portlet invoker factories
39   * in order to get portlet invokers, and then invoke methods on portlets (render, action, load). 
40   * </p>
41   * <p>
42   * The Portlet Invoker Factory is a Pluto factory. Pluto defines a basic lifecycle for Pluto
43   * factory services in the <code>org.apach.pluto.factory.Factory</code> interface with
44   * standard <code>init</code> and <code>destroy</code> methods.
45   * </p>
46   * <p>
47   * The Jetspeed portlet invoker factory supports two kinds of invokers: local and servlet.
48   * Local portlet invokers call portlets located in the same web applications.
49   * With local invokers, a simple java method invocation is called on the portlet.
50   * Servlet portlet invokers call portlets located in another web application.
51   * With servlet invokers, the servlet request dispatcher is used to call methods on the portlet. 
52   * </p>
53   * 
54   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
55   * @version $Id: PortletInvokerFactoryImpl.java 517124 2007-03-12 08:10:25Z ate $
56   */
57  public class PortletInvokerFactoryImpl
58      implements PortletInvokerFactory
59  {
60      
61      public final static String INVOKER_SERVLET_MAPPING_NAME = "factory.invoker.servlet.mapping.name";
62      public final static String DEFAULT_MAPPING_NAME = "/container";
63      
64      /*** The servlet configuration for the Jetspeed portal */
65      private final ServletConfig servletConfig;
66  
67      private final PortalContext portalContext;
68      
69      private final PortletFactory portletFactory;
70      
71      private final ServletPortletInvokerFactory servletPortletInvokerFactory;
72      
73      private final LocalPortletInvokerFactory localPortletInvokerFactory;
74              
75      public PortletInvokerFactoryImpl(ServletConfig servletConfig, PortalContext portalContext, 
76              PortletFactory portletFactory, ServletPortletInvokerFactory servletPortletInvokerFactory, LocalPortletInvokerFactory localPortletInvokerFactory)
77      {
78          this.servletConfig = servletConfig;        
79          this.portalContext = portalContext;        
80          this.portletFactory = portletFactory;
81          this.servletPortletInvokerFactory = servletPortletInvokerFactory;
82          this.localPortletInvokerFactory = localPortletInvokerFactory;                
83      }
84                 
85      /* (non-Javadoc)
86       * @see org.apache.pluto.factory.Factory#init(javax.servlet.ServletConfig, java.util.Map)
87       */
88      public void init(ServletConfig config, Map properties)
89      throws Exception
90      {
91          // does absolutely nothing
92      }
93  
94      /* (non-Javadoc)
95       * @see org.apache.pluto.factory.Factory#destroy()
96       */
97      public void destroy()
98      throws Exception
99      {
100     }
101 
102     /* (non-Javadoc)
103      * @see org.apache.pluto.factory.PortletInvokerFactory#getPortletInvoker(org.apache.pluto.om.portlet.PortletDefinition)
104      */
105     public PortletInvoker getPortletInvoker(PortletDefinition portletDefinition)
106     {
107         MutablePortletApplication app = (MutablePortletApplication)portletDefinition.getPortletApplicationDefinition();
108         if(app == null)
109         {
110         	throw new IllegalStateException("Portlet definition \""+portletDefinition.getName()+"\" is not assigned to a portlet application.");
111         }
112         
113         if (app.getApplicationType() == MutablePortletApplication.LOCAL)
114         {
115             LocalPortletInvoker localPortletInvoker = localPortletInvokerFactory.createInstance();
116             localPortletInvoker.activate(portletFactory, portletDefinition, servletConfig);
117             return localPortletInvoker;           
118         }
119         else
120         {             
121             ServletPortletInvoker servletPortletInvoker = servletPortletInvokerFactory.createInstance();
122             String servletMappingName = portalContext.getConfigurationProperty(INVOKER_SERVLET_MAPPING_NAME, DEFAULT_MAPPING_NAME);            
123             servletPortletInvoker.activate(portletFactory, portletDefinition, servletConfig, servletMappingName);            
124             return servletPortletInvoker;
125         }
126 
127     }
128     
129     /* (non-Javadoc)
130      * @see org.apache.pluto.factory.PortletInvokerFactory#releasePortletInvoker(org.apache.pluto.invoker.PortletInvoker)
131      */
132     public void releasePortletInvoker(PortletInvoker invoker)
133     {
134         // this is now taken care off by Spring's CommonsPoolingTargetSource
135 //        try
136 //        {
137 //            if (invoker instanceof ServletPortletInvoker)
138 //            {
139 //                servletInvokerFactory.releaseObject(invoker);                
140 //            }
141 //            else
142 //            {
143 //                localInvokerFactory.releaseObject(invoker);                            
144 //            }
145 //        }
146 //        catch (Exception e)
147 //        {
148 //            log.error(e);
149 //        }
150     }
151     
152 }