001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.el.lang;
019:
020: import java.io.Externalizable;
021: import java.io.IOException;
022: import java.io.ObjectInput;
023: import java.io.ObjectOutput;
024: import java.lang.reflect.Method;
025: import java.util.HashMap;
026: import java.util.Map;
027:
028: import javax.el.FunctionMapper;
029:
030: import org.apache.el.util.ReflectionUtil;
031:
032: /**
033: * @author Jacob Hookom [jacob@hookom.net]
034: * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: markt $
035: */
036: public class FunctionMapperImpl extends FunctionMapper implements
037: Externalizable {
038:
039: private static final long serialVersionUID = 1L;
040:
041: protected Map functions = null;
042:
043: /*
044: * (non-Javadoc)
045: *
046: * @see javax.el.FunctionMapper#resolveFunction(java.lang.String,
047: * java.lang.String)
048: */
049: public Method resolveFunction(String prefix, String localName) {
050: if (this .functions != null) {
051: Function f = (Function) this .functions.get(prefix + ":"
052: + localName);
053: return f.getMethod();
054: }
055: return null;
056: }
057:
058: public void addFunction(String prefix, String localName, Method m) {
059: if (this .functions == null) {
060: this .functions = new HashMap();
061: }
062: Function f = new Function(prefix, localName, m);
063: synchronized (this ) {
064: this .functions.put(prefix + ":" + localName, f);
065: }
066: }
067:
068: /*
069: * (non-Javadoc)
070: *
071: * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput)
072: */
073: public void writeExternal(ObjectOutput out) throws IOException {
074: out.writeObject(this .functions);
075: }
076:
077: /*
078: * (non-Javadoc)
079: *
080: * @see java.io.Externalizable#readExternal(java.io.ObjectInput)
081: */
082: public void readExternal(ObjectInput in) throws IOException,
083: ClassNotFoundException {
084: this .functions = (Map) in.readObject();
085: }
086:
087: public static class Function implements Externalizable {
088:
089: protected transient Method m;
090: protected String owner;
091: protected String name;
092: protected String[] types;
093: protected String prefix;
094: protected String localName;
095:
096: /**
097: *
098: */
099: public Function(String prefix, String localName, Method m) {
100: if (localName == null) {
101: throw new NullPointerException(
102: "LocalName cannot be null");
103: }
104: if (m == null) {
105: throw new NullPointerException("Method cannot be null");
106: }
107: this .prefix = prefix;
108: this .localName = localName;
109: this .m = m;
110: }
111:
112: public Function() {
113: // for serialization
114: }
115:
116: /*
117: * (non-Javadoc)
118: *
119: * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput)
120: */
121: public void writeExternal(ObjectOutput out) throws IOException {
122: out.writeUTF((this .prefix != null) ? this .prefix : "");
123: out.writeUTF(this .localName);
124: out.writeUTF(this .m.getDeclaringClass().getName());
125: out.writeUTF(this .m.getName());
126: out.writeObject(ReflectionUtil.toTypeNameArray(this .m
127: .getParameterTypes()));
128: }
129:
130: /*
131: * (non-Javadoc)
132: *
133: * @see java.io.Externalizable#readExternal(java.io.ObjectInput)
134: */
135: public void readExternal(ObjectInput in) throws IOException,
136: ClassNotFoundException {
137:
138: this .prefix = in.readUTF();
139: if ("".equals(this .prefix))
140: this .prefix = null;
141: this .localName = in.readUTF();
142: this .owner = in.readUTF();
143: this .name = in.readUTF();
144: this .types = (String[]) in.readObject();
145: }
146:
147: public Method getMethod() {
148: if (this .m == null) {
149: try {
150: Class t = Class.forName(this .owner);
151: Class[] p = ReflectionUtil.toTypeArray(this .types);
152: this .m = t.getMethod(this .name, p);
153: } catch (Exception e) {
154: e.printStackTrace();
155: }
156: }
157: return this .m;
158: }
159:
160: public boolean matches(String prefix, String localName) {
161: if (this .prefix != null) {
162: if (prefix == null)
163: return false;
164: if (!this .prefix.equals(prefix))
165: return false;
166: }
167: return this .localName.equals(localName);
168: }
169:
170: /* (non-Javadoc)
171: * @see java.lang.Object#equals(java.lang.Object)
172: */
173: public boolean equals(Object obj) {
174: if (obj instanceof Function) {
175: return this .hashCode() == obj.hashCode();
176: }
177: return false;
178: }
179:
180: /* (non-Javadoc)
181: * @see java.lang.Object#hashCode()
182: */
183: public int hashCode() {
184: return (this.prefix + this.localName).hashCode();
185: }
186: }
187:
188: }
|