001: /*
002: * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * - Redistribution in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
025: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
026: * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
027: * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
028: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
029: * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
030: * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
031: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
032: *
033: * You acknowledge that Software is not designed, licensed or intended
034: * for use in the design, construction, operation or maintenance of
035: * any nuclear facility.
036: */
037:
038: package com.sun.portal.portlet.ipc2;
039:
040: import com.sun.portal.portletappengine.ipc.EventNotRegisteredException;
041: import com.sun.portal.portletappengine.ipc.EventRequest;
042: import com.sun.portal.portletappengine.ipc.EventResponse;
043: import com.sun.portal.portletappengine.ipc.PortletEvent;
044: import com.sun.portal.portletappengine.ipc.PortletEventBroker;
045: import com.sun.portal.portletappengine.ipc.PortletEventListener;
046:
047: import javax.portlet.GenericPortlet;
048: import javax.portlet.PortletConfig;
049: import javax.portlet.PortletContext;
050: import javax.portlet.PortletException;
051: import javax.portlet.PortletRequestDispatcher;
052: import javax.portlet.RenderRequest;
053: import javax.portlet.RenderResponse;
054: import java.io.IOException;
055:
056: /**
057: * The DecisionPortlet participates in the InterPortletCommunication.
058: * This consumes the event generated by the PricePortlet which is in the
059: * same web application.
060: * This also generates the event that is consumed by the PricePortlet.
061: */
062: public class DecisionPortlet extends GenericPortlet implements
063: PortletEventListener {
064: private PortletContext pContext;
065:
066: public void init(PortletConfig config) throws PortletException {
067: super .init(config);
068: pContext = config.getPortletContext();
069: }
070:
071: public void handleEvent(EventRequest ereq, EventResponse eres) {
072: String eventName = ereq.getEventName();
073: if (eventName.equalsIgnoreCase("priceChanged")) {
074: String data = (String) ereq.getEventData();
075: int price_data = -1;
076: try {
077: price_data = Integer.parseInt(data);
078: } catch (NumberFormatException nfe) {
079: //do nothing.
080: }
081: PortletEventBroker peb = new PortletEventBroker(ereq);
082: if (price_data > 500) {
083: try {
084: PortletEvent pe = peb.createEvent("highPriceEvent");
085: pe.setEventData(data);
086: pe.fire();
087: } catch (EventNotRegisteredException e) {
088: e.printStackTrace();
089: }
090: } else {
091: try {
092: PortletEvent pe = peb.createEvent("lowPriceEvent");
093: pe.setEventData(data);
094: pe.fire();
095: } catch (EventNotRegisteredException e) {
096: e.printStackTrace();
097: }
098: }
099: eres.setRenderParameter("price", data);
100: } else if (eventName.equalsIgnoreCase("priceChanged2")) {
101: eres.setRenderParameter("price2", eventName);
102: } else if (eventName.equalsIgnoreCase("priceChanged3")) {
103: eres.setRenderParameter("price3", eventName);
104: } else if (eventName
105: .equalsIgnoreCase(PortletEvent.EVENT_HANDLING_FAILED)) {
106: String data = "eventHandlingFailed";
107: eres.setRenderParameter("data", data);
108: }
109:
110: }
111:
112: public void doView(RenderRequest request, RenderResponse response)
113: throws PortletException, IOException {
114: String contentPage = null;
115: String price = (String) request.getParameter("price");
116: int iPrice = -1;
117: if (price != null) {
118: try {
119: try {
120: iPrice = Integer.parseInt(price);
121: } catch (NumberFormatException nfe) {
122: //do nothing.
123: }
124: if (iPrice < 0) {
125: contentPage = "/decisionportlet/content.jsp";
126: } else if (iPrice > 500) {
127: contentPage = "/decisionportlet/content1.jsp";
128: } else {
129: contentPage = "/decisionportlet/content2.jsp";
130: }
131: response.setContentType(request
132: .getResponseContentType());
133: if (contentPage != null && contentPage.length() != 0) {
134: PortletRequestDispatcher dispatcher = pContext
135: .getRequestDispatcher(contentPage);
136: dispatcher.include(request, response);
137: }
138: } catch (IOException e) {
139: throw new PortletException(
140: "JSPPortlet.doView exception", e);
141: }
142: } else {
143: response.setContentType(request.getResponseContentType());
144: contentPage = "/decisionportlet/content.jsp";
145: if (contentPage != null && contentPage.length() != 0) {
146: try {
147: PortletRequestDispatcher dispatcher = pContext
148: .getRequestDispatcher(contentPage);
149: dispatcher.include(request, response);
150: } catch (IOException e) {
151: throw new PortletException(
152: "JSPPortlet.doView exception", e);
153: }
154: }
155: }
156: }
157: }
|