1 package org.exoplatform.portlet.faces.lifecycle; 2 3 import java.util.*; 4 import javax.faces.FacesException; 5 import javax.faces.event.PhaseId; 6 import javax.faces.lifecycle.Lifecycle; 7 import javax.faces.lifecycle.LifecycleFactory; 8 import org.apache.commons.logging.Log; 9 import org.exoplatform.services.log.LogUtil; 10 11 import com.sun.faces.util.Util; 12 import com.sun.faces.lifecycle.* ; 13 14 public class ExoLifecycleFactory extends LifecycleFactory { 15 protected static Log log_ = LogUtil.getLog("org.exoplatform.portal.faces") ; 16 static final int FIRST_PHASE; 17 static final int LAST_PHASE; 18 protected HashMap lifecycleMap; 19 20 static 21 { 22 FIRST_PHASE = PhaseId.RESTORE_VIEW.getOrdinal(); 23 LAST_PHASE = PhaseId.RENDER_RESPONSE.getOrdinal(); 24 } 25 26 static class LifecycleWrapper { 27 Lifecycle instance; 28 boolean created; 29 30 LifecycleWrapper(Lifecycle newInstance, boolean newCreated) { 31 instance = null; 32 created = false; 33 instance = newInstance; 34 created = newCreated; 35 } 36 } 37 38 39 public ExoLifecycleFactory() { 40 lifecycleMap = null; 41 lifecycleMap = new HashMap(); 42 lifecycleMap.put("DEFAULT", new LifecycleWrapper(new ExoLifecycle(), false)); 43 } 44 45 boolean alreadyCreated(String lifecycleId) { 46 LifecycleWrapper wrapper = (LifecycleWrapper)lifecycleMap.get(lifecycleId); 47 return null != wrapper && wrapper.created; 48 } 49 50 Lifecycle verifyRegisterArgs(String lifecycleId, int phaseId, Phase phase) { 51 String message = null; 52 LifecycleWrapper wrapper = null; 53 Lifecycle result = null; 54 Object params[] = { lifecycleId }; 55 if(null == lifecycleId || null == phase) 56 throw new NullPointerException ("lifecycleId or phase is null"); 57 if(null == (wrapper = (LifecycleWrapper)lifecycleMap.get(lifecycleId))) { 58 message = "lifecycle id is not found" ; 59 throw new IllegalArgumentException (message); 60 } 61 result = wrapper.instance; 62 Util.doAssert(null != result); 63 if(alreadyCreated(lifecycleId)) { 64 message = "LIFECYCLE ID ALREADY ADDED" ; 65 throw new IllegalStateException (message); 66 } 67 68 if(FIRST_PHASE > phaseId || phaseId > LAST_PHASE) { 69 params = (new Object [] { Integer.toString(phaseId) }); 70 message = "PHASE ID OUT OF BOUNDS" ; 71 throw new IllegalArgumentException (message); 72 } else { 73 return result; 74 } 75 } 76 77 public void addLifecycle(String lifecycleId, Lifecycle lifecycle) { 78 if(lifecycleId == null || lifecycle == null) 79 throw new NullPointerException ("NULL PARAMETERS ERROR"); 80 if(null != lifecycleMap.get(lifecycleId)) { 81 Object params[] = { lifecycleId }; 82 String message = "LIFECYCLE ID ALREADY ADDED" ; 83 throw new IllegalArgumentException (message); 84 } 85 lifecycleMap.put(lifecycleId, new LifecycleWrapper(lifecycle, false)); 86 } 87 88 public Lifecycle getLifecycle(String lifecycleId) throws FacesException { 89 Lifecycle result = null; 90 LifecycleWrapper wrapper = null; 91 if(null == lifecycleId) 92 throw new NullPointerException ("com.sun.faces.NULL_PARAMETERS_ERROR") ; 93 try { 94 wrapper = (LifecycleWrapper)lifecycleMap.get(lifecycleId); 95 result = wrapper.instance; 96 wrapper.created = true; 97 } catch(Throwable e) { 98 Object params[] = { lifecycleId }; 99 String message = "CANT CREATE LIFECYCLE ERROR" ; 100 throw new FacesException(message); 101 } 102 return result; 103 } 104 105 public Iterator getLifecycleIds() { 106 return lifecycleMap.keySet().iterator(); 107 } 108 } 109 | Popular Tags |