KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > portletcontainer > impl > portletAPIImp > PortletContextImpl


1 /**
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5 package org.exoplatform.services.portletcontainer.impl.portletAPIImp;
6
7 import java.io.InputStream JavaDoc;
8 import java.net.MalformedURLException JavaDoc;
9 import java.util.Enumeration JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
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 JavaDoc;
20 import javax.servlet.ServletContext JavaDoc;
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 /**
37  * Created by The eXo Platform SARL
38  * Author : Tuan Nguyen
39  * tuan08@users.sourceforge.net
40  * Date: Jul 27, 2003
41  * Time: 2:13:09 AM
42  */

43 public class PortletContextImpl implements PortletContext, ExoPortletContext {
44   private ServletContext JavaDoc servletContext_;
45   private Log log;
46
47   public PortletContextImpl(ServletContext JavaDoc scontext) {
48     this.log = LogUtil.getLog("org.exoplatform.services.portletcontainer");
49     servletContext_ = scontext;
50   }
51
52   public String JavaDoc getServerInfo() {
53     return servletContext_.getServerInfo();
54   }
55
56   public PortletRequestDispatcher getRequestDispatcher(String JavaDoc 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 JavaDoc 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 JavaDoc name) {
72     RequestDispatcher rD = null;
73     try {
74       rD = servletContext_.getNamedDispatcher(name);
75       if (rD == null)
76         return null;
77     } catch (IllegalArgumentException JavaDoc 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 JavaDoc getResourceAsStream(String JavaDoc path) {
85     return servletContext_.getResourceAsStream(path);
86   }
87
88   public int getMajorVersion() {
89     return 1;//servletContext_.getMajorVersion() ;
90
}
91
92   public int getMinorVersion() {
93     return 0;//servletContext_.getMinorVersion() ;
94
}
95
96   public String JavaDoc getMimeType(String JavaDoc file) {
97     return servletContext_.getMimeType(file);
98   }
99
100   public String JavaDoc getRealPath(String JavaDoc path) {
101     return servletContext_.getRealPath(path);
102   }
103
104   public java.util.Set JavaDoc getResourcePaths(String JavaDoc path) {
105     return servletContext_.getResourcePaths(path);
106   }
107
108   public java.net.URL JavaDoc getResource(String JavaDoc path) throws MalformedURLException JavaDoc {
109     if (!path.startsWith("/"))
110       throw new MalformedURLException JavaDoc("path must start with /");
111     return servletContext_.getResource(path);
112   }
113
114   public java.lang.Object JavaDoc getAttribute(String JavaDoc name) {
115     if (name == null) {
116       throw new IllegalArgumentException JavaDoc("The attribute name cannot be null");
117     }
118     return servletContext_.getAttribute(name);
119   }
120
121   public void removeAttribute(java.lang.String JavaDoc name) {
122     if (name == null) {
123       throw new IllegalArgumentException JavaDoc("The attribute name cannot be null");
124     }
125     servletContext_.removeAttribute(name);
126   }
127
128   public void setAttribute(String JavaDoc name, Object JavaDoc value) {
129     if (name == null) {
130       throw new IllegalArgumentException JavaDoc("The attribute name cannot be null");
131     }
132     //when the value is null, should have the same effect as removeAttribute (Spec)
133
if (value == null) {
134       servletContext_.removeAttribute(name);
135     } else {
136       servletContext_.setAttribute(name, value);
137     }
138   }
139
140   public Enumeration JavaDoc getAttributeNames() {
141     return servletContext_.getAttributeNames();
142   }
143
144   public String JavaDoc getInitParameter(String JavaDoc name) {
145     if (name == null)
146       throw new IllegalArgumentException JavaDoc("argument must not be null");
147     return servletContext_.getInitParameter(name);
148   }
149
150   public Enumeration JavaDoc getInitParameterNames() {
151     return servletContext_.getInitParameterNames();
152   }
153
154   public String JavaDoc getPortletContextName() {
155     return servletContext_.getServletContextName();
156   }
157
158   public ServletContext JavaDoc getWrappedServletContext() {
159     return servletContext_;
160   }
161
162   public void send(String JavaDoc 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 JavaDoc portlets = portletApp.getPortlet();
173       for (Iterator JavaDoc iterator = portlets.iterator(); iterator.hasNext();) {
174         Portlet portlet = (Portlet) iterator.next();
175         String JavaDoc portletName = portlet.getPortletName();
176         if (portletName.equals(portletToAccessName)) {
177           List JavaDoc messageListeners = portlet.getMessageListener();
178           for (Iterator JavaDoc 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 JavaDoc listenerName = messageListenerData.getListenerName();
182             String JavaDoc listenerClass = messageListenerData.getListenerClass();
183             String JavaDoc key = servletContext_.getServletContextName() + Constants.MESSAGE_LISTENER_ENCODER + listenerName;
184             MessageListener listener = (MessageListener) manager.getComponentInstance(key);
185             if (listener == null) {
186               ClassLoader JavaDoc 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 JavaDoc 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 JavaDoc msg) {
209     servletContext_.log(msg);
210   }
211
212   public void log(java.lang.String JavaDoc message, java.lang.Throwable JavaDoc throwable) {
213     servletContext_.log(message, throwable);
214   }
215 }
216
Popular Tags