001: /*
002: * (C) Copyright 2005 Nabh Information Systems, Inc.
003: *
004: * All copyright notices regarding Nabh's products MUST remain
005: * intact in the scripts and in the outputted HTML.
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021: package com.nabhinc.portlet.mvcportlet.actionprocessor;
022:
023: import java.io.IOException;
024:
025: import javax.portlet.ActionRequest;
026: import javax.portlet.ActionResponse;
027: import javax.portlet.PortletException;
028:
029: import org.w3c.dom.Element;
030:
031: import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
032: import com.nabhinc.portlet.mvcportlet.core.ActionProcessor;
033: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
034: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
035: import com.nabhinc.condition.Condition;
036: import com.nabhinc.condition.ConditionFactory;
037: import com.nabhinc.util.XMLUtil;
038:
039: /**
040: * To be used as a switch.
041: * Each branch in the switch is specified via a pre-condition and a label.
042: *
043: * @author Padmanabh Dabke
044: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
045: * @since MVCPortlet 0.8
046: */
047: public class Switch extends BaseRequestProcessor implements
048: ActionProcessor {
049:
050: private String[] sResults = null;
051: private Condition[] sConditions = null;
052:
053: /* (non-Javadoc)
054: * @see com.nabhinc.portlet.mvcportlet.core.ActionProcessor#process(javax.portlet.ActionRequest, javax.portlet.ActionResponse, com.nabhinc.portlet.mvcportlet.core.ActionConfig)
055: */
056: public String process(ActionRequest request,
057: ActionResponse response, ActionConfig actionConfig)
058: throws PortletException, IOException {
059: try {
060: for (int i = 0; i < sConditions.length; i++) {
061: if (sConditions[i].isSatisfied(request, actionConfig
062: .getPrecondition())) {
063: return sResults[i];
064: }
065: }
066: } catch (PortletException ex) {
067: throw ex;
068: //} catch (IOException ex) {
069: // throw ex;
070: } catch (Exception ex) {
071: throw new PortletException(
072: "Failed to evaluate a condition.", ex);
073: }
074: return "failure";
075: }
076:
077: /* (non-Javadoc)
078: * @see com.nabhinc.portlet.mvcportlet.core.Initable#init(org.w3c.dom.Element, com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig)
079: */
080: public void init(Element xmlConfig, ControllerPortletConfig config)
081: throws PortletException {
082: super .init(xmlConfig, config);
083: Element[] condElems = XMLUtil.getSubElements(xmlConfig,
084: "condition");
085: if (condElems == null || condElems.length == 0) {
086: throw new PortletException(
087: "At least one case must be defined in a switch.");
088: }
089:
090: sResults = new String[condElems.length];
091: sConditions = new Condition[condElems.length];
092: for (int i = 0; i < condElems.length; i++) {
093: sResults[i] = condElems[i].getAttribute("label");
094: if (sResults[i] == null || sResults[i].equals("")) {
095: throw new PortletException(
096: "Missing required attribute in a case: label.");
097: }
098: // String condClass = XMLUtil.getSubElementText(condElems[i], "class");
099: // if (condClass == null) throw new PortletException("Missing condition class element.");
100: try {
101: // Class cl = Class.forName(condClass);
102: // sConditions[i] = (Condition) cl.newInstance();
103: sConditions[i] = ConditionFactory.create(XMLUtil
104: .getFirstSubElement(condElems[i]));
105: /*
106: } catch (ClassNotFoundException ex) {
107: throw new PortletException("Failed to load condition class: " + condClass);
108: } catch (ClassCastException ex) {
109: throw new PortletException("Condition class does not implement Condition interface: " + condClass);
110: } catch (IllegalAccessException ex) {
111: throw new PortletException("Condition class constructor not accessible.");
112: } catch (InstantiationException ex) {
113: throw new PortletException("Exception instantiating condition class");
114: */
115: } catch (Exception ex) {
116: throw new PortletException(
117: "Exception initializing condition class.", ex);
118: }
119: }
120:
121: }
122:
123: }
|