KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > faces > UIComponentFactory


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

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 /**
16  * Jun 2, 2004
17  * @author: Tuan Nguyen
18  * @email: tuan08@users.sourceforge.net
19  * @version: $ID$
20  **/

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 JavaDoc createUIComponent(String JavaDoc className) throws Exception JavaDoc {
36     ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader() ;
37     Class JavaDoc clazz = cl.loadClass(className) ;
38     return createUIComponent(clazz) ;
39   }
40   
41   final public Object JavaDoc createUIComponent(Class JavaDoc clazz) throws Exception JavaDoc {
42     PortalContainer manager = PortalContainer.getInstance() ;
43     return createUIComponent(clazz, manager) ;
44   }
45   
46   private Object JavaDoc createUIComponent(Class JavaDoc clazz, PortalContainer manager) throws Exception JavaDoc {
47     Constructor constructor = findBiggestConstructor(clazz) ;
48     Class JavaDoc[] parameters = constructor.getParameterTypes() ;
49     Object JavaDoc[] args = new Object JavaDoc[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 JavaDoc("Cannot find the service " + parameters[i].getName()) ;
59         }
60       }
61     }
62     Object JavaDoc result = constructor.newInstance(args) ;
63     return result ;
64   }
65   
66   private Constructor findBiggestConstructor(Class JavaDoc 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 JavaDoc cl) throws Exception JavaDoc {
80     String JavaDoc 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