1 5 package org.exoplatform.services.portletcontainer.impl.portletAPIImp; 6 7 import java.io.InputStream ; 8 import java.net.MalformedURLException ; 9 import java.util.Enumeration ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 13 import javax.portlet.ActionRequest; 14 import javax.portlet.PortletContext; 15 import javax.portlet.PortletException; 16 import javax.portlet.PortletRequest; 17 import javax.portlet.PortletRequestDispatcher; 18 import javax.portlet.RenderRequest; 19 import javax.servlet.RequestDispatcher ; 20 import javax.servlet.ServletContext ; 21 22 import org.apache.commons.logging.Log; 23 import org.exoplatform.Constants; 24 import org.exoplatform.container.PortalContainer; 25 import org.exoplatform.services.log.LogUtil; 26 import org.exoplatform.services.portletcontainer.ExoPortletContext; 27 import org.exoplatform.services.portletcontainer.event.MessageListener; 28 import org.exoplatform.services.portletcontainer.event.PortletMessage; 29 import org.exoplatform.services.portletcontainer.impl.PortletApplicationsHolder; 30 import org.exoplatform.services.portletcontainer.impl.PortletContainerConf; 31 import org.exoplatform.services.portletcontainer.impl.event.MessageEventImpl; 32 import org.exoplatform.services.portletcontainer.pci.model.Portlet; 33 import org.exoplatform.services.portletcontainer.pci.model.PortletApp; 34 35 36 43 public class PortletContextImpl implements PortletContext, ExoPortletContext { 44 private ServletContext servletContext_; 45 private Log log; 46 47 public PortletContextImpl(ServletContext scontext) { 48 this.log = LogUtil.getLog("org.exoplatform.services.portletcontainer"); 49 servletContext_ = scontext; 50 } 51 52 public String getServerInfo() { 53 return servletContext_.getServerInfo(); 54 } 55 56 public PortletRequestDispatcher getRequestDispatcher(String path) { 57 if (path == null || !path.startsWith("/")) 58 return null; 59 RequestDispatcher rD = null; 60 try { 61 rD = servletContext_.getRequestDispatcher(path); 62 if (rD == null) 63 return null; 64 } catch (IllegalArgumentException e) { 65 log.error("Can not lookup request dispatcher", e); 66 return null; 67 } 68 return new PortletRequestDispatcherImp(rD, path); 69 } 70 71 public PortletRequestDispatcher getNamedDispatcher(String name) { 72 RequestDispatcher rD = null; 73 try { 74 rD = servletContext_.getNamedDispatcher(name); 75 if (rD == null) 76 return null; 77 } catch (IllegalArgumentException e) { 78 log.error("Can not lookup request dispatcher", e); 79 return null; 80 } 81 return new PortletRequestDispatcherImp(rD, name); 82 } 83 84 public InputStream getResourceAsStream(String path) { 85 return servletContext_.getResourceAsStream(path); 86 } 87 88 public int getMajorVersion() { 89 return 1; } 91 92 public int getMinorVersion() { 93 return 0; } 95 96 public String getMimeType(String file) { 97 return servletContext_.getMimeType(file); 98 } 99 100 public String getRealPath(String path) { 101 return servletContext_.getRealPath(path); 102 } 103 104 public java.util.Set getResourcePaths(String path) { 105 return servletContext_.getResourcePaths(path); 106 } 107 108 public java.net.URL getResource(String path) throws MalformedURLException { 109 if (!path.startsWith("/")) 110 throw new MalformedURLException ("path must start with /"); 111 return servletContext_.getResource(path); 112 } 113 114 public java.lang.Object getAttribute(String name) { 115 if (name == null) { 116 throw new IllegalArgumentException ("The attribute name cannot be null"); 117 } 118 return servletContext_.getAttribute(name); 119 } 120 121 public void removeAttribute(java.lang.String name) { 122 if (name == null) { 123 throw new IllegalArgumentException ("The attribute name cannot be null"); 124 } 125 servletContext_.removeAttribute(name); 126 } 127 128 public void setAttribute(String name, Object value) { 129 if (name == null) { 130 throw new IllegalArgumentException ("The attribute name cannot be null"); 131 } 132 if (value == null) { 134 servletContext_.removeAttribute(name); 135 } else { 136 servletContext_.setAttribute(name, value); 137 } 138 } 139 140 public Enumeration getAttributeNames() { 141 return servletContext_.getAttributeNames(); 142 } 143 144 public String getInitParameter(String name) { 145 if (name == null) 146 throw new IllegalArgumentException ("argument must not be null"); 147 return servletContext_.getInitParameter(name); 148 } 149 150 public Enumeration getInitParameterNames() { 151 return servletContext_.getInitParameterNames(); 152 } 153 154 public String getPortletContextName() { 155 return servletContext_.getServletContextName(); 156 } 157 158 public ServletContext getWrappedServletContext() { 159 return servletContext_; 160 } 161 162 public void send(String portletToAccessName, PortletMessage portletMessage, 163 PortletRequest portletRequest) throws PortletException { 164 try { 165 if (portletRequest instanceof RenderRequest) { 166 throw new PortletException("can not send message in render method"); 167 } 168 PortalContainer manager = PortalContainer.getInstance(); 169 PortletApplicationsHolder service = 170 (PortletApplicationsHolder) manager.getComponentInstanceOfType(PortletApplicationsHolder.class); 171 PortletApp portletApp = service.getPortletApplication(servletContext_.getServletContextName()); 172 List portlets = portletApp.getPortlet(); 173 for (Iterator iterator = portlets.iterator(); iterator.hasNext();) { 174 Portlet portlet = (Portlet) iterator.next(); 175 String portletName = portlet.getPortletName(); 176 if (portletName.equals(portletToAccessName)) { 177 List messageListeners = portlet.getMessageListener(); 178 for (Iterator iterator1 = messageListeners.iterator(); iterator1.hasNext();) { 179 org.exoplatform.services.portletcontainer.pci.model.MessageListener messageListenerData = 180 (org.exoplatform.services.portletcontainer.pci.model.MessageListener) iterator1.next(); 181 String listenerName = messageListenerData.getListenerName(); 182 String listenerClass = messageListenerData.getListenerClass(); 183 String key = servletContext_.getServletContextName() + Constants.MESSAGE_LISTENER_ENCODER + listenerName; 184 MessageListener listener = (MessageListener) manager.getComponentInstance(key); 185 if (listener == null) { 186 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 187 manager.registerComponentImplementation(key, cl.loadClass(listenerClass)); 188 listener = (MessageListener) manager.getComponentInstance(key); 189 } 190 listener.messageReceived(new MessageEventImpl(portletMessage, 191 (ActionRequest) portletRequest)); 192 } 193 break; 194 } 195 } 196 } catch (Exception ex) { 197 log.error("Can not send message", ex); 198 throw new PortletException(ex); 199 } 200 } 201 202 public boolean isSessionShared() { 203 PortletContainerConf conf = (PortletContainerConf) PortalContainer.getInstance(). 204 getComponentInstanceOfType(PortletContainerConf.class); 205 return conf.isSharedSessionEnable(); 206 } 207 208 public void log(java.lang.String msg) { 209 servletContext_.log(msg); 210 } 211 212 public void log(java.lang.String message, java.lang.Throwable throwable) { 213 servletContext_.log(message, throwable); 214 } 215 } 216 | Popular Tags |