From: dajevtic <nu...@jb...> - 2005-05-17 13:02:42
|
Ok, will do. For now I am posting the two portlets I wrote yesterday, in order to check TreeCache functionality for inter portlet communication. It is quick and dirty, so please ignore the fact that this is not production ready code. Maybe it will give some insight to others, who are thinking about implementing interportlet communication. Any constructive criticism is more than welcome. I want to use a Wrapper class so that the portlets themselves do not have to access (and configure connection) to the Cache itself. DoCachePortlet Source code: import java.io.IOException; import java.io.PrintWriter; import java.security.Principal; import java.util.Map; import javax.management.MBeanServer; import javax.management.MalformedObjectNameException; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.GenericPortlet; import javax.portlet.PortletConfig; import javax.portlet.PortletException; import javax.portlet.PortletSecurityException; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import org.jboss.cache.CacheException; import org.jboss.cache.TreeCacheMBean; import org.jboss.mx.util.MBeanProxyExt; import org.jboss.mx.util.MBeanServerLocator; /** * XDoclet generated portlet description * * @portlet.portlet * description="This portlet displays a form with 3 text fields an a submit button." * display-name="DoCachePortlet" * expiration-cache="0" * name="DoCachePortlet" * * @portlet.portlet-init-param * name="CacheService" * value="jboss.cache:service=TreeCache" * * @portlet.portlet-info * title="CacheInputForm" * * @portlet.supports * mime-type="text/html" * modes="VIEW" * * @author Danijel Jevtic <danijel_point_jevtic_at_livemediagroup_dot_de> * @version 1.0 */ public class DoCachePortlet extends GenericPortlet { private String cacheService; private MBeanServer server; private TreeCacheMBean cache; private static final String CONTENT_TYPE = "text/html"; public void init(PortletConfig config) throws PortletException { super.init(config); // cacheService e.g. 'jboss.cache:service=TreeCache' cacheService = getInitParameter("CacheService"); if (cacheService == null) { throw new PortletException("No cache defined"); } // locate MBeanServer server = MBeanServerLocator.locate(); try { // get the tree cache cache=(TreeCacheMBean)MBeanProxyExt.create(TreeCacheMBean.class, cacheService, server); } catch (MalformedObjectNameException mone) { throw new PortletException(mone); } } public void doView(RenderRequest request, RenderResponse response) throws PortletException, PortletSecurityException, IOException { response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); out.print("<form name=\"somename\" method=\"post\" action=\"" + response.createActionURL() + "\">"); out.print("<input type=\"text\" name=\"input1\"/>"); out.print("<input type=\"text\" name=\"input2\"/>"); out.print("<input type=\"text\" name=\"input3\"/>"); out.print("<input type=\"submit\"/>"); out.print(""); } public void processAction(ActionRequest request, ActionResponse response) throws PortletException, PortletSecurityException, IOException { String tree; Principal user = request.getUserPrincipal(); if (user == null) { tree = request.getPortletSession().getId() + "/" + getPortletName(); } else { tree = user.getName() + "/" + getPortletName(); } try { writeToCache(tree, request.getParameterMap()); } catch (CacheException ce) { throw new PortletException(ce); } } private void clearCache(String id) throws CacheException { cache.remove("/" + id); } private void writeToCache(String id, Map parameterMap) throws CacheException { clearCache(id); cache.put("/" + id, parameterMap); } } DoReceivePortlet source code: import java.io.IOException; import java.io.PrintWriter; import java.security.Principal; import java.util.Enumeration; import java.util.Hashtable; import java.util.Iterator; import java.util.Set; import javax.management.MBeanServer; import javax.management.MalformedObjectNameException; import javax.portlet.GenericPortlet; import javax.portlet.PortletConfig; import javax.portlet.PortletException; import javax.portlet.PortletSecurityException; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import org.jboss.cache.CacheException; import org.jboss.cache.TreeCacheMBean; import org.jboss.mx.util.MBeanProxyExt; import org.jboss.mx.util.MBeanServerLocator; /** * XDoclet generated portlet description * * @portlet.portlet * description="This portlet displays the form previous form posts." * display-name="DoReceivePortlet" * expiration-cache="0" * name="DoReceivePortlet" * * @portlet.portlet-init-param * name="CacheService" * value="jboss.cache:service=TreeCache" * * @portlet.portlet-info * title="CacheDisplay" * * @portlet.supports * mime-type="text/html" * modes="VIEW" * * @author Danijel Jevtic <danijel_point_jevtic_at_livemediagroup_dot_de> * @version 1.0 */ public class DoReceivePortlet extends GenericPortlet { private static final String CONTENT_TYPE = "text/html"; // the name of the portlet which posted the form values private static final String TRUSTED_PORTLET_NAME = "DoCachePortlet"; private String cacheService; private MBeanServer server; private TreeCacheMBean cache; public void init(PortletConfig config) throws PortletException { super.init(config); // cacheService e.g. 'jboss.cache:service=TreeCache' cacheService = getInitParameter("CacheService"); if (cacheService == null) { throw new PortletException("No cache defined"); } // locate MBeanServer server = MBeanServerLocator.locate(); try { // get the tree cache cache=(TreeCacheMBean)MBeanProxyExt.create(TreeCacheMBean.class, cacheService, server); } catch (MalformedObjectNameException mone) { throw new PortletException(mone); } } public void doView(RenderRequest request, RenderResponse response) throws PortletException, PortletSecurityException, IOException { String sessionId = request.getPortletSession().getId(); response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); Hashtable parameters; String tree; Principal user = request.getUserPrincipal(); if (user == null) { out.print("Using your session ID for parameter storage"); tree = request.getPortletSession().getId() + "/" + TRUSTED_PORTLET_NAME; } else { out.print("Using your user name for parameter storage"); tree = user.getName() + "/" + TRUSTED_PORTLET_NAME; } try { parameters = getParameters(tree); } catch (CacheException ce) { throw new PortletException(ce); } if (!parameters.isEmpty()) { out.print("You entered the following form parameters:"); Enumeration keys = parameters.keys(); while (keys.hasMoreElements()) { Object key = keys.nextElement(); String[] value = (String[])parameters.get(key); out.print(key + " = " + value[0] + ""); } } else { out.print("The cache was empty. You probably haven't filled it yet!"); } } private Hashtable getParameters(String id) throws CacheException { Hashtable parameters = new Hashtable(); Set set = cache.getKeys("/" + id); if (set != null) { Iterator setIterator = set.iterator(); while (setIterator.hasNext()) { String key = (String)setIterator.next(); parameters.put(key, cache.get("/" + id, key)); } } clearCache(id); return parameters; } private void clearCache(String id) throws CacheException { cache.remove("/" + id); } } View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=3877994#3877994 Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=3877994 |