1 14 15 package com.sun.facelets.el; 16 17 import java.io.Externalizable ; 18 import java.io.IOException ; 19 import java.io.ObjectInput ; 20 import java.io.ObjectOutput ; 21 import java.lang.reflect.Method ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 26 import javax.el.FunctionMapper; 27 28 import com.sun.el.util.ReflectionUtil; 29 30 39 public final class DefaultFunctionMapper extends FunctionMapper implements 40 Externalizable { 41 42 private static final long serialVersionUID = 1L; 43 44 private Map functions = null; 45 46 52 public Method resolveFunction(String prefix, String localName) { 53 if (this.functions != null) { 54 Function f = (Function) this.functions.get(prefix + ":" + localName); 55 return f.getMethod(); 56 } 57 return null; 58 } 59 60 public void addFunction(String prefix, String localName, Method m) { 61 if (this.functions == null) { 62 this.functions = new HashMap (); 63 } 64 Function f = new Function(prefix, localName, m); 65 synchronized (this) { 66 this.functions.put(prefix+":"+localName, f); 67 } 68 } 69 70 75 public void writeExternal(ObjectOutput out) throws IOException { 76 out.writeObject(this.functions); 77 } 78 79 84 public void readExternal(ObjectInput in) throws IOException , 85 ClassNotFoundException { 86 this.functions = (Map ) in.readObject(); 87 } 88 89 public String toString() { 90 StringBuffer sb = new StringBuffer (128); 91 sb.append("FunctionMapper[\n"); 92 for (Iterator itr = this.functions.values().iterator(); itr.hasNext(); ) { 93 sb.append(itr.next()).append('\n'); 94 } 95 sb.append(']'); 96 return sb.toString(); 97 } 98 99 private static class Function implements Externalizable { 100 101 private static final long serialVersionUID = 1L; 102 103 protected transient Method m; 104 protected String owner; 105 protected String name; 106 protected String [] types; 107 protected String prefix; 108 protected String localName; 109 110 113 public Function(String prefix, String localName, Method m) { 114 if (localName == null) { 115 throw new NullPointerException ("LocalName cannot be null"); 116 } 117 if (m == null) { 118 throw new NullPointerException ("Method cannot be null"); 119 } 120 this.prefix = prefix; 121 this.localName = localName; 122 this.m = m; 123 } 124 125 public Function() { 126 } 128 129 134 public void writeExternal(ObjectOutput out) throws IOException { 135 out.writeUTF((this.prefix != null) ? this.prefix : ""); 136 out.writeUTF(this.localName); 137 out.writeUTF(this.m.getDeclaringClass().getName()); 138 out.writeUTF(this.m.getName()); 139 out.writeObject(ReflectionUtil.toTypeNameArray(this.m.getParameterTypes())); 140 } 141 142 147 public void readExternal(ObjectInput in) throws IOException , 148 ClassNotFoundException { 149 150 this.prefix = in.readUTF(); 151 if ("".equals(this.prefix)) this.prefix = null; 152 this.localName = in.readUTF(); 153 this.owner = in.readUTF(); 154 this.name = in.readUTF(); 155 this.types = (String []) in.readObject(); 156 } 157 158 public Method getMethod() { 159 if (this.m == null) { 160 try { 161 Class t = Class.forName(this.owner); 162 Class [] p = ReflectionUtil.toTypeArray(this.types); 163 this.m = t.getMethod(this.name, p); 164 } catch (Exception e) { 165 e.printStackTrace(); 166 } 167 } 168 return this.m; 169 } 170 171 public boolean matches(String prefix, String localName) { 172 if (this.prefix != null) { 173 if (prefix == null) return false; 174 if (!this.prefix.equals(prefix)) return false; 175 } 176 return this.localName.equals(localName); 177 } 178 179 182 public boolean equals(Object obj) { 183 if (obj instanceof Function) { 184 return this.hashCode() == obj.hashCode(); 185 } 186 return false; 187 } 188 189 192 public int hashCode() { 193 return (this.prefix + this.localName).hashCode(); 194 } 195 196 public String toString() { 197 StringBuffer sb = new StringBuffer (32); 198 sb.append("Function["); 199 if (this.prefix != null) { 200 sb.append(this.prefix).append(':'); 201 } 202 sb.append(this.name).append("] "); 203 sb.append(this.m); 204 return sb.toString(); 205 } 206 } 207 } 208 | Popular Tags |