1 16 package org.directwebremoting.faces; 17 18 import javax.faces.application.Application; 19 import javax.faces.context.FacesContext; 20 import javax.faces.el.ValueBinding; 21 import javax.faces.el.VariableResolver; 22 23 import org.directwebremoting.create.AbstractCreator; 24 import org.directwebremoting.extend.Creator; 25 import org.directwebremoting.util.LocalUtil; 26 import org.directwebremoting.util.Logger; 27 28 35 public class JsfCreator extends AbstractCreator implements Creator 36 { 37 40 public Class getType() 41 { 42 if (instanceType == null) 43 { 44 try 45 { 46 instanceType = getInstance().getClass(); 47 } 48 catch (InstantiationException ex) 49 { 50 log.error("Failed to instansiate object to detect type.", ex); 51 return Object .class; 52 } 53 } 54 55 return instanceType; 56 } 57 58 61 public Object getInstance() throws InstantiationException 62 { 63 FacesContext facesContext = FacesContext.getCurrentInstance(); 64 if (facesContext == null) 65 { 66 log.error("Object " + getManagedBeanName() + " cannot be created since the faces context is null"); 67 return null; 68 } 69 70 Application application = facesContext.getApplication(); 71 Object resolvedObject = null; 72 73 if (isVBExpression(getManagedBeanName())) 74 { 75 ValueBinding vb = application.createValueBinding(getManagedBeanName()); 76 if (vb != null) 77 { 78 resolvedObject = vb.getValue(facesContext); 79 } 80 } 81 else 82 { 83 VariableResolver resolver = application.getVariableResolver(); 84 resolvedObject = resolver.resolveVariable(facesContext, getManagedBeanName()); 85 } 86 87 return resolvedObject; 88 } 89 90 95 public static boolean isVBExpression(String expression) 96 { 97 if (expression == null) 98 { 99 return false; 100 } 101 102 int start = expression.indexOf("#{"); 103 int end = expression.indexOf('}'); 104 105 return start != -1 && start < end; 106 } 107 108 111 public String getManagedBeanName() 112 { 113 return managedBeanName; 114 } 115 116 119 public void setManagedBeanName(String managedBeanName) 120 { 121 this.managedBeanName = managedBeanName; 122 } 123 124 128 public void setClass(String classname) 129 { 130 try 131 { 132 this.instanceType = LocalUtil.classForName(classname); 133 } 134 catch (ClassNotFoundException ex) 135 { 136 throw new IllegalArgumentException ("Creator.ClassNotFound"); 137 } 138 } 139 140 143 private String managedBeanName; 144 145 148 private Class instanceType; 149 150 153 private static final Logger log = Logger.getLogger(JsfCreator.class); 154 } 155 | Popular Tags |