From portal-commits at lists.jboss.org Mon Mar 17 12:50:00 2008 Content-Type: multipart/mixed; boundary="===============8044356877177806824==" MIME-Version: 1.0 From: portal-commits at lists.jboss.org To: portal-commits at lists.jboss.org Subject: [portal-commits] JBoss Portal SVN: r10296 - modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/samples/basic. Date: Mon, 17 Mar 2008 12:50:00 -0400 Message-ID: --===============8044356877177806824== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Author: julien(a)jboss.com Date: 2008-03-17 12:50:00 -0400 (Mon, 17 Mar 2008) New Revision: 10296 Added: modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/sam= ples/basic/RandomEventPortlet.java Log: added missing random portlet Added: modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet= /samples/basic/RandomEventPortlet.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/sa= mples/basic/RandomEventPortlet.java (rev 0) +++ modules/portlet/trunk/samples/src/main/java/org/jboss/portal/portlet/sa= mples/basic/RandomEventPortlet.java 2008-03-17 16:50:00 UTC (rev 10296) @@ -0,0 +1,125 @@ +/*************************************************************************= ***** + * JBoss, a division of Red Hat = * + * Copyright 2008, Red Hat Middleware, LLC, and individual = * + * contributors as indicated by the @authors tag. See the = * + * copyright.txt in the distribution for a full listing of = * + * individual contributors. = * + * = * + * This is free software; you can redistribute it and/or modify it = * + * under the terms of the GNU Lesser General Public License as = * + * published by the Free Software Foundation; either version 2.1 of = * + * the License, or (at your option) any later version. = * + * = * + * This software is distributed in the hope that it will be useful, = * + * but WITHOUT ANY WARRANTY; without even the implied warranty of = * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU = * + * Lesser General Public License for more details. = * + * = * + * You should have received a copy of the GNU Lesser General Public = * + * License along with this software; if not, write to the Free = * + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA = * + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. = * + *************************************************************************= *****/ +package org.jboss.portal.portlet.samples.basic; + +import javax.portlet.GenericPortlet; +import javax.portlet.ActionRequest; +import javax.portlet.ActionResponse; +import javax.portlet.PortletException; +import javax.portlet.EventRequest; +import javax.portlet.EventResponse; +import javax.portlet.RenderRequest; +import javax.portlet.RenderResponse; +import javax.portlet.PortletURL; +import javax.portlet.Event; +import javax.portlet.StateAwareResponse; +import javax.xml.namespace.QName; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Random; + +/** + * @author Julien Viet + * @version $Revision: 630 $ + */ +public class RandomEventPortlet extends GenericPortlet +{ + + /** . */ + private static final QName[] QNAMES =3D { + new QName("urn:jboss:portal:samples:basic", "Event1"), + new QName("urn:jboss:portal:samples:basic", "Event2"), + new QName("urn:jboss:portal:samples:basic", "Event3") + }; + + /** . */ + private static final Random random =3D new Random(); + + private static QName pickQName() + { + synchronized (random) + { + return QNAMES[random.nextInt(QNAMES.length)]; + } + } + + private static boolean shouldFail() + { + synchronized (random) + { + return random.nextInt(3) =3D=3D 0; + } + } + + public void processAction(ActionRequest req, ActionResponse resp) throw= s PortletException, IOException + { + int repeat =3D Integer.parseInt(req.getParameter("repeat")); + + // + publishEvent(resp, repeat); + } + + public void processEvent(EventRequest req, EventResponse resp) throws P= ortletException, IOException + { + Event event =3D req.getEvent(); + + // + int repeat =3D ((Integer)event.getValue()); + String name =3D event.getName(); + System.out.println("Portlet " + getPortletConfig().getPortletName() = + " received the event (" + name + "," + repeat + ")"); + + // + if (shouldFail()) + { + System.out.println("Portlet " + getPortletConfig().getPortletName= () + " decided to fail"); + + // + throw new PortletException("Don't be scarred, this is expected to= happen"); + } + + // + if (repeat > 0) + { + publishEvent(resp, repeat - 1); + } + } + + private void publishEvent(StateAwareResponse resp, int repeat) + { + QName name =3D pickQName(); + resp.setEvent(name, repeat); + System.out.println("Portlet " + getPortletConfig().getPortletName() = + " generated the event (" + name + "," + repeat + ")"); + } + + public void render(RenderRequest req, RenderResponse resp) throws Portl= etException, IOException + { + PortletURL actionURL =3D resp.createActionURL(); + + // + PrintWriter writer =3D resp.getWriter(); + writer.println("
"); + writer.println(""); + writer.println(""); + writer.println("
"); + } +} --===============8044356877177806824==--