KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > pluto > factory > LocalPortletInvokerImpl


1 /*
2  * Copyright 2004,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.pluto.factory;
17
18 import java.io.IOException JavaDoc;
19
20 import javax.portlet.ActionRequest;
21 import javax.portlet.ActionResponse;
22 import javax.portlet.Portlet;
23 import javax.portlet.PortletConfig;
24 import javax.portlet.PortletContext;
25 import javax.portlet.PortletException;
26 import javax.portlet.PortletRequest;
27 import javax.portlet.RenderRequest;
28 import javax.portlet.RenderResponse;
29 import javax.servlet.ServletConfig JavaDoc;
30
31 import org.apache.avalon.framework.activity.Initializable;
32 import org.apache.avalon.framework.container.ContainerUtil;
33 import org.apache.avalon.framework.context.Context;
34 import org.apache.avalon.framework.context.ContextException;
35 import org.apache.avalon.framework.context.Contextualizable;
36 import org.apache.avalon.framework.logger.AbstractLogEnabled;
37 import org.apache.avalon.framework.service.ServiceException;
38 import org.apache.avalon.framework.service.ServiceManager;
39 import org.apache.avalon.framework.service.Serviceable;
40 import org.apache.cocoon.util.ClassUtils;
41 import org.apache.pluto.factory.PortletObjectAccess;
42 import org.apache.pluto.invoker.PortletInvoker;
43 import org.apache.pluto.om.portlet.PortletDefinition;
44
45 /**
46  * This is an invoker for a "local" portlet, which is a portlet running inside Cocoon.
47  *
48  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
49  *
50  * @version CVS $Id: LocalPortletInvokerImpl.java 123407 2004-12-27 13:51:59Z cziegeler $
51  */

52 public class LocalPortletInvokerImpl
53 extends AbstractLogEnabled
54 implements PortletInvoker, Contextualizable, Serviceable, Initializable {
55     
56     /** servlet configuration */
57     protected final ServletConfig JavaDoc servletConfig;
58
59     /** The portlet definition */
60     protected final PortletDefinition portletDefinition;
61
62     /** The portlet */
63     protected Portlet portlet;
64     
65     /** The avalon context */
66     protected Context context;
67     
68     /** The service manager */
69     protected ServiceManager manager;
70     
71     /* (non-Javadoc)
72      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
73      */

74     public void contextualize(Context context) throws ContextException {
75         this.context = context;
76     }
77     
78     /* (non-Javadoc)
79      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
80      */

81     public void service(ServiceManager manager) throws ServiceException {
82         this.manager = manager;
83     }
84
85     /* (non-Javadoc)
86      * @see org.apache.avalon.framework.activity.Initializable#initialize()
87      */

88     public void initialize() throws Exception JavaDoc {
89         if (this.portlet != null) {
90             try {
91                 ContainerUtil.enableLogging(this.portlet, this.getLogger());
92                 ContainerUtil.contextualize(this.portlet, this.context);
93                 ContainerUtil.service(this.portlet, this.manager);
94                 ContainerUtil.initialize(this.portlet);
95             } catch (Exception JavaDoc ignore) {
96                 // we ignore the exception here and throw later on a portlet exception
97
this.getLogger().warn("Unable to initialize local portlet invoker.", ignore);
98                 this.portlet = null;
99             }
100         }
101     }
102
103     /**
104      * Constructor
105      */

106     public LocalPortletInvokerImpl(PortletDefinition portletDefinition,
107                                    ServletConfig JavaDoc servletConfig) {
108         this.portletDefinition = portletDefinition;
109         this.servletConfig = servletConfig;
110         
111         try {
112             final String JavaDoc clazzName = portletDefinition.getClassName();
113             this.portlet = (Portlet)ClassUtils.newInstance(clazzName);
114         } catch (Exception JavaDoc ignore) {
115             // we ignore the exception here and throw later on a portlet exception
116
this.getLogger().warn("Unable to initialize local portlet invoker.", ignore);
117         }
118     }
119
120     /* (non-Javadoc)
121      * @see org.apache.pluto.invoker.PortletInvoker#action(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
122      */

123     public void action(ActionRequest request,
124                        ActionResponse response)
125     throws PortletException, IOException JavaDoc {
126         if ( this.portlet == null ) {
127             throw new PortletException("Unable to instantiate portlet from class " + this.portletDefinition.getClassName());
128         }
129         this.portlet.processAction(request, response);
130     }
131
132     /* (non-Javadoc)
133      * @see org.apache.pluto.invoker.PortletInvoker#render(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
134      */

135     public void render(RenderRequest request, RenderResponse response)
136     throws PortletException, IOException JavaDoc {
137         if ( this.portlet == null ) {
138             throw new PortletException("Unable to instantiate portlet from class " + this.portletDefinition.getClassName());
139         }
140         try {
141             request.setAttribute(org.apache.pluto.Constants.METHOD_ID,
142                 org.apache.pluto.Constants.METHOD_RENDER);
143             request.setAttribute(org.apache.pluto.Constants.PORTLET_REQUEST, request);
144             request.setAttribute(org.apache.pluto.Constants.PORTLET_RESPONSE,
145                 response);
146             this.portlet.render(request, response);
147         } finally {
148             request.removeAttribute(org.apache.pluto.Constants.METHOD_ID);
149             request.removeAttribute(org.apache.pluto.Constants.PORTLET_REQUEST);
150             request.removeAttribute(org.apache.pluto.Constants.PORTLET_RESPONSE);
151         }
152
153     }
154
155     /* (non-Javadoc)
156      * @see org.apache.pluto.invoker.PortletInvoker#load(javax.portlet.PortletRequest, javax.portlet.RenderResponse)
157      */

158     public void load(PortletRequest request, RenderResponse response)
159     throws PortletException {
160         if ( this.portlet == null ) {
161             throw new PortletException("Unable to instantiate portlet from class " + this.portletDefinition.getClassName());
162         }
163         PortletContext portletContext;
164         PortletConfig portletConfig;
165         portletContext = PortletObjectAccess.getPortletContext(this.servletConfig.getServletContext(),
166                 portletDefinition.getPortletApplicationDefinition());
167         portletConfig = PortletObjectAccess.getPortletConfig(this.servletConfig,
168                 portletContext,
169                 portletDefinition);
170         this.portlet.init(portletConfig);
171     }
172
173     /**
174      * Destroy the associated portlet
175      */

176     public void destroy() {
177         if (this.portlet != null ) {
178             this.portlet.destroy();
179             this.portlet = null;
180         }
181     }
182 }
183
Popular Tags