1 16 package org.apache.cocoon.portal.pluto.factory; 17 18 import java.io.IOException ; 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 ; 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 52 public class LocalPortletInvokerImpl 53 extends AbstractLogEnabled 54 implements PortletInvoker, Contextualizable, Serviceable, Initializable { 55 56 57 protected final ServletConfig servletConfig; 58 59 60 protected final PortletDefinition portletDefinition; 61 62 63 protected Portlet portlet; 64 65 66 protected Context context; 67 68 69 protected ServiceManager manager; 70 71 74 public void contextualize(Context context) throws ContextException { 75 this.context = context; 76 } 77 78 81 public void service(ServiceManager manager) throws ServiceException { 82 this.manager = manager; 83 } 84 85 88 public void initialize() throws Exception { 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 ignore) { 96 this.getLogger().warn("Unable to initialize local portlet invoker.", ignore); 98 this.portlet = null; 99 } 100 } 101 } 102 103 106 public LocalPortletInvokerImpl(PortletDefinition portletDefinition, 107 ServletConfig servletConfig) { 108 this.portletDefinition = portletDefinition; 109 this.servletConfig = servletConfig; 110 111 try { 112 final String clazzName = portletDefinition.getClassName(); 113 this.portlet = (Portlet)ClassUtils.newInstance(clazzName); 114 } catch (Exception ignore) { 115 this.getLogger().warn("Unable to initialize local portlet invoker.", ignore); 117 } 118 } 119 120 123 public void action(ActionRequest request, 124 ActionResponse response) 125 throws PortletException, IOException { 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 135 public void render(RenderRequest request, RenderResponse response) 136 throws PortletException, IOException { 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 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 176 public void destroy() { 177 if (this.portlet != null ) { 178 this.portlet.destroy(); 179 this.portlet = null; 180 } 181 } 182 } 183 | Popular Tags |