001: /* Initiators.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Wed August 14 14:27:47 2007, Created by Dennis.Chen
010: }}IS_NOTE
011:
012: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.jsf.zul.impl;
020:
021: import java.util.Iterator;
022: import java.util.LinkedList;
023: import java.util.List;
024:
025: import org.zkoss.util.logging.Log;
026: import org.zkoss.zk.ui.Page;
027: import org.zkoss.zk.ui.UiException;
028: import org.zkoss.zk.ui.util.Initiator;
029:
030: /**
031: * A helper class used to handle {@link Initiator} in ZULJSF Component.
032: *
033: * @author Dennis.Chen
034: *
035: */
036: public class Initiators {
037: static final Log log = Log.lookup(Initiators.class);
038: private final List _inits = new LinkedList();
039:
040: /**
041: * Add new initiator and it's args into this handler.
042: * @param init the initiator
043: * @param args the args
044: */
045: public void addInitiator(Initiator init, List args) {
046: _inits.add(new Object[] { init, args });
047: }
048:
049: /** Invokes {@link Initiator#doInit}, if any, and returns
050: * an instance of{@link Initiators}.
051: */
052: public void doInit(Page page) {
053: Object[] objArr;
054: Initiator initiator;
055: List args;
056: for (Iterator it = _inits.iterator(); it.hasNext();) {
057: objArr = (Object[]) it.next();
058: initiator = (Initiator) objArr[0];
059: args = (List) objArr[1];
060: try {
061: initiator.doInit(page, args.toArray());
062: } catch (Throwable ex) {
063: throw UiException.Aide.wrap(ex);
064: }
065: }
066: }
067:
068: /**
069: * Handle all doAfterCompose of Initiator.
070: * @param page
071: * @throws Exception
072: */
073: public void doAfterCompose(Page page) throws Exception {
074: for (Iterator it = _inits.iterator(); it.hasNext();) {
075: ((Initiator) ((Object[]) it.next())[0])
076: .doAfterCompose(page);
077: }
078: }
079:
080: /** Invokes {@link Initiator#doCatch}.
081: * It eats all exception without throwing one (but logging).
082: * Caller has to re-throw the exception.
083: */
084: public void doCatch(Throwable t) {
085: for (Iterator it = _inits.iterator(); it.hasNext();) {
086: final Initiator init = ((Initiator) ((Object[]) it.next())[0]);
087: try {
088: init.doCatch(t);
089: } catch (Throwable ex) {
090: log.error(ex);
091: }
092: }
093: }
094:
095: /** Invokes {@link Initiator#doFinally}.
096: */
097: public void doFinally() {
098: Throwable t = null;
099: for (Iterator it = _inits.iterator(); it.hasNext();) {
100: final Initiator init = ((Initiator) ((Object[]) it.next())[0]);
101: try {
102: init.doFinally();
103: } catch (Throwable ex) {
104: log.error(ex);
105: if (t == null)
106: t = ex;
107: }
108: }
109: if (t != null)
110: throw UiException.Aide.wrap(t);
111: }
112:
113: }
|