1 17 18 package org.apache.el.lang; 19 20 import java.io.Externalizable ; 21 import java.io.IOException ; 22 import java.io.ObjectInput ; 23 import java.io.ObjectOutput ; 24 import java.lang.reflect.Method ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 28 import javax.el.FunctionMapper; 29 30 import org.apache.el.util.ReflectionUtil; 31 32 33 37 public class FunctionMapperImpl extends FunctionMapper implements 38 Externalizable { 39 40 private static final long serialVersionUID = 1L; 41 42 protected Map functions = null; 43 44 50 public Method resolveFunction(String prefix, String localName) { 51 if (this.functions != null) { 52 Function f = (Function) this.functions.get(prefix + ":" + localName); 53 return f.getMethod(); 54 } 55 return null; 56 } 57 58 public void addFunction(String prefix, String localName, Method m) { 59 if (this.functions == null) { 60 this.functions = new HashMap (); 61 } 62 Function f = new Function(prefix, localName, m); 63 synchronized (this) { 64 this.functions.put(prefix+":"+localName, f); 65 } 66 } 67 68 73 public void writeExternal(ObjectOutput out) throws IOException { 74 out.writeObject(this.functions); 75 } 76 77 82 public void readExternal(ObjectInput in) throws IOException , 83 ClassNotFoundException { 84 this.functions = (Map ) in.readObject(); 85 } 86 87 public static class Function implements Externalizable { 88 89 protected transient Method m; 90 protected String owner; 91 protected String name; 92 protected String [] types; 93 protected String prefix; 94 protected String localName; 95 96 99 public Function(String prefix, String localName, Method m) { 100 if (localName == null) { 101 throw new NullPointerException ("LocalName cannot be null"); 102 } 103 if (m == null) { 104 throw new NullPointerException ("Method cannot be null"); 105 } 106 this.prefix = prefix; 107 this.localName = localName; 108 this.m = m; 109 } 110 111 public Function() { 112 } 114 115 120 public void writeExternal(ObjectOutput out) throws IOException { 121 out.writeUTF((this.prefix != null) ? this.prefix : ""); 122 out.writeUTF(this.localName); 123 out.writeUTF(this.m.getDeclaringClass().getName()); 124 out.writeUTF(this.m.getName()); 125 out.writeObject(ReflectionUtil.toTypeNameArray(this.m.getParameterTypes())); 126 } 127 128 133 public void readExternal(ObjectInput in) throws IOException , 134 ClassNotFoundException { 135 136 this.prefix = in.readUTF(); 137 if ("".equals(this.prefix)) this.prefix = null; 138 this.localName = in.readUTF(); 139 this.owner = in.readUTF(); 140 this.name = in.readUTF(); 141 this.types = (String []) in.readObject(); 142 } 143 144 public Method getMethod() { 145 if (this.m == null) { 146 try { 147 Class t = Class.forName(this.owner); 148 Class [] p = ReflectionUtil.toTypeArray(this.types); 149 this.m = t.getMethod(this.name, p); 150 } catch (Exception e) { 151 e.printStackTrace(); 152 } 153 } 154 return this.m; 155 } 156 157 public boolean matches(String prefix, String localName) { 158 if (this.prefix != null) { 159 if (prefix == null) return false; 160 if (!this.prefix.equals(prefix)) return false; 161 } 162 return this.localName.equals(localName); 163 } 164 165 168 public boolean equals(Object obj) { 169 if (obj instanceof Function) { 170 return this.hashCode() == obj.hashCode(); 171 } 172 return false; 173 } 174 175 178 public int hashCode() { 179 return (this.prefix + this.localName).hashCode(); 180 } 181 } 182 183 } 184 | Popular Tags |