1 5 package org.exoplatform.faces; 6 7 import java.util.* ; 8 import java.lang.reflect.* ; 9 import javax.faces.component.UIComponent ; 10 import javax.faces.context.FacesContext ; 11 import org.exoplatform.container.PortalContainer; 12 import org.exoplatform.container.SessionContainer; 13 import org.exoplatform.faces.context.*; 14 import org.exoplatform.faces.core.component.UIExoComponent; 15 21 public class UIComponentFactory { 22 private ResourceBundle res_ ; 23 24 public UIComponentFactory(ResourceBundle res) { 25 res_ = res ; 26 } 27 28 public UIComponentFactory() { 29 FacesContext context = FacesContext.getCurrentInstance() ; 30 PortletExternalContext econtext = 31 (PortletExternalContext) context.getExternalContext() ; 32 res_ = econtext.getApplicationResourceBundle() ; 33 } 34 35 final public Object createUIComponent(String className) throws Exception { 36 ClassLoader cl = Thread.currentThread().getContextClassLoader() ; 37 Class clazz = cl.loadClass(className) ; 38 return createUIComponent(clazz) ; 39 } 40 41 final public Object createUIComponent(Class clazz) throws Exception { 42 PortalContainer manager = PortalContainer.getInstance() ; 43 return createUIComponent(clazz, manager) ; 44 } 45 46 private Object createUIComponent(Class clazz, PortalContainer manager) throws Exception { 47 Constructor constructor = findBiggestConstructor(clazz) ; 48 Class [] parameters = constructor.getParameterTypes() ; 49 Object [] args = new Object [parameters.length] ; 50 for (int i = 0; i < args.length; i++) { 51 if(UIComponent.class.isAssignableFrom(parameters[i])) { 52 args[i] = createUIComponent(parameters[i], manager) ; 53 } else if(ResourceBundle.class.isAssignableFrom(parameters[i])) { 54 args[i] = res_ ; 55 } else { 56 args[i] = manager.getComponentInstanceOfType(parameters[i]) ; 57 if (args[i] == null) { 58 throw new Exception ("Cannot find the service " + parameters[i].getName()) ; 59 } 60 } 61 } 62 Object result = constructor.newInstance(args) ; 63 return result ; 64 } 65 66 private Constructor findBiggestConstructor(Class clazz) { 67 Constructor bc = null ; 68 Constructor[] constructors = clazz.getConstructors() ; 69 for(int i = 0 ; i < constructors.length; i++) { 70 if(bc == null) { 71 bc = constructors[i] ; 72 } else if(bc.getParameterTypes().length < constructors[i].getParameterTypes().length) { 73 bc = constructors[i] ; 74 } 75 } 76 return bc ; 77 } 78 79 static public UIExoComponent createComponent(Class cl) throws Exception { 80 String className = cl.getName() ; 81 int idx = className.lastIndexOf('.') ; 82 className = className.substring(idx + 1, className.length()) ; 83 SessionContainer scontainer = SessionContainer.getInstance() ; 84 UIExoComponent uiComponent = (UIExoComponent)scontainer.createComponent(cl) ; 85 uiComponent.setId(className) ; 86 return uiComponent ; 87 } 88 } 89 | Popular Tags |