1 /* 2 * $Id: MethodBindingValidator.java,v 1.5 2007/04/27 22:00:03 ofung Exp $ 3 */ 4 5 /* 6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 7 * 8 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. 9 * 10 * The contents of this file are subject to the terms of either the GNU 11 * General Public License Version 2 only ("GPL") or the Common Development 12 * and Distribution License("CDDL") (collectively, the "License"). You 13 * may not use this file except in compliance with the License. You can obtain 14 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html 15 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific 16 * language governing permissions and limitations under the License. 17 * 18 * When distributing the software, include this License Header Notice in each 19 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. 20 * Sun designates this particular file as subject to the "Classpath" exception 21 * as provided by Sun in the GPL Version 2 section of the License file that 22 * accompanied this code. If applicable, add the following below the License 23 * Header, with the fields enclosed by brackets [] replaced by your own 24 * identifying information: "Portions Copyrighted [year] 25 * [name of copyright owner]" 26 * 27 * Contributor(s): 28 * 29 * If you wish your version of this file to be governed by only the CDDL or 30 * only the GPL Version 2, indicate your decision by adding "[Contributor] 31 * elects to include this software in this distribution under the [CDDL or GPL 32 * Version 2] license." If you don't indicate a single choice of license, a 33 * recipient has the option to distribute your version of this file under 34 * either the CDDL, the GPL Version 2 or to extend the choice of license to 35 * its licensees as provided above. However, if you add GPL Version 2 code 36 * and therefore, elected the GPL Version 2 license, then the option applies 37 * only if the new code is made subject to such option by the copyright 38 * holder. 39 */ 40 41 package javax.faces.component; 42 43 import javax.faces.validator.Validator; 44 import javax.faces.validator.ValidatorException; 45 46 import javax.faces.el.MethodBinding; 47 import javax.faces.el.EvaluationException; 48 import javax.faces.context.FacesContext; 49 50 /** 51 * <p><strong>MethodBindingValidator</strong> is an {@link 52 * ValidatorListener} that wraps a {@link MethodBinding}. When it 53 * receives a {@link ActionEvent}, it executes a method on an 54 * object identified by the {@link MethodBinding}.</p> 55 */ 56 57 class MethodBindingValidator extends MethodBindingAdapterBase implements Validator, StateHolder { 58 59 60 // ------------------------------------------------------ Instance Variables 61 62 private MethodBinding methodBinding = null; 63 64 public MethodBindingValidator() {} 65 66 67 /** 68 * <p>Construct a {@link Validator} that contains a {@link 69 * MethodBinding}.</p> 70 */ 71 public MethodBindingValidator(MethodBinding methodBinding) { 72 73 super(); 74 this.methodBinding = methodBinding; 75 76 } 77 78 public MethodBinding getWrapped() { 79 return methodBinding; 80 } 81 82 83 // ------------------------------------------------------- Validator 84 85 public void validate(FacesContext context, 86 UIComponent component, 87 Object value) throws ValidatorException { 88 if (null == context || null == component) { 89 throw new NullPointerException(); 90 } 91 try { 92 methodBinding.invoke(context, new Object[] {context, component, 93 value}); 94 } 95 catch (EvaluationException ee) { 96 Throwable cause = this.getExpectedCause(ValidatorException.class, 97 ee); 98 if (cause instanceof ValidatorException) { 99 throw ((ValidatorException) cause); 100 } 101 if (cause instanceof RuntimeException) { 102 throw ((RuntimeException) cause); 103 } 104 throw new IllegalStateException(ee); 105 } 106 107 108 } 109 110 111 112 // 113 // Methods from StateHolder 114 // 115 116 117 118 public Object saveState(FacesContext context) { 119 if (context == null) { 120 throw new NullPointerException(); 121 } 122 Object result = null; 123 if (!tranzient) { 124 if (methodBinding instanceof StateHolder) { 125 Object [] stateStruct = new Object[2]; 126 127 // save the actual state of our wrapped methodBinding 128 stateStruct[0] = ((StateHolder)methodBinding).saveState(context); 129 // save the class name of the methodBinding impl 130 stateStruct[1] = methodBinding.getClass().getName(); 131 132 result = stateStruct; 133 } 134 else { 135 result = methodBinding; 136 } 137 } 138 139 return result; 140 } 141 142 public void restoreState(FacesContext context, Object state) { 143 if (context == null) { 144 throw new NullPointerException(); 145 } 146 // if we have state 147 if (null == state) { 148 return; 149 } 150 151 if (!(state instanceof MethodBinding)) { 152 Object [] stateStruct = (Object []) state; 153 Object savedState = stateStruct[0]; 154 String className = stateStruct[1].toString(); 155 MethodBinding result = null; 156 157 Class toRestoreClass; 158 if (null != className) { 159 try { 160 toRestoreClass = loadClass(className, this); 161 } 162 catch (ClassNotFoundException e) { 163 throw new IllegalStateException(e); 164 } 165 166 if (null != toRestoreClass) { 167 try { 168 result = 169 (MethodBinding) toRestoreClass.newInstance(); 170 } 171 catch (InstantiationException e) { 172 throw new IllegalStateException(e); 173 } 174 catch (IllegalAccessException a) { 175 throw new IllegalStateException(a); 176 } 177 } 178 179 if (null != result && null != savedState) { 180 // don't need to check transient, since that was 181 // done on the saving side. 182 ((StateHolder)result).restoreState(context, savedState); 183 } 184 methodBinding = result; 185 } 186 } 187 else { 188 methodBinding = (MethodBinding) state; 189 } 190 } 191 192 private boolean tranzient = false; 193 194 public boolean isTransient() { 195 return tranzient; 196 } 197 198 public void setTransient(boolean newTransientValue) { 199 tranzient = newTransientValue; 200 } 201 202 // 203 // Helper methods for StateHolder 204 // 205 206 private static Class loadClass(String name, 207 Object fallbackClass) throws ClassNotFoundException { 208 ClassLoader loader = 209 Thread.currentThread().getContextClassLoader(); 210 if (loader == null) { 211 loader = fallbackClass.getClass().getClassLoader(); 212 } 213 return Class.forName(name, false, loader); 214 } 215 }