/* PHPRPCServer.java
*
* Author: Ma Bingyao <andot@ujn.edu.cn>
* Copyright: CoolCode.CN
* Version: 2.1
* LastModified: 2006-07-31
* This library is free. You can redistribute it and/or modify it.
* http://www.coolcode.cn/?p=203
*/
package PHPRPC;
import java.lang.*;
import java.lang.reflect.*;
import java.math.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
class RemoteFunction
{
public String name;
public Object obj;
public Class cls;
public Method[] functions;
public RemoteFunction(String name, Object obj, Class cls, Method[] functions)
{
this.name = name;
this.obj = obj;
this.cls = cls;
this.functions = functions;
}
}
class PHPRPCServer
{
private ServletRequest request;
private ServletResponse response;
private HttpSession session;
private ArrayList functions;
private RemoteFunction[] rfs;
private boolean debug;
private String charset;
private boolean encode;
private boolean byref;
private boolean encrypt;
private int encryptmode;
private byte[] key;
private String result;
private String arguments;
private String output;
private String callback;
private int errno;
private String errstr;
//private static Regex test = new Regex("[\0-\037\042\047\134]", RegexOptions.Compiled);
public PHPRPCServer(ServletRequest request, ServletResponse response, HttpSession session)
{
this.request = request;
this.response = response;
this.session = session;
this.functions = new ArrayList();
this.encode = true;
this.encrypt = false;
this.encryptmode = 0;
this.errno = 0;
this.errstr = "";
this.output = "";
this.callback = "";
this.byref = true;
this.key = null;
}
public void add(String function, Object obj)
{
add(new String[] { function }, obj, obj.getClass());
}
public void add(String[] functions, Object obj)
{
add(functions, obj, obj.getClass());
}
public void add(String function, Class cls)
{
add(new String[] { function }, null, cls);
}
public void add(String[] functions, Class cls)
{
add(functions, null, cls);
}
private void add(String[] functions, Object obj, Class cls)
{
Method[] ms = cls.getMethods();
for (int i = 0, n = functions.length; i < n; i++)
{
ArrayList fs = new ArrayList();
for (int j = 0, m = ms.length; j < m; j++)
{
if (Modifier.isPublic(ms[j].getModifiers()) && functions[i].toLowerCase() == ms[j].getName().toLowerCase())
{
fs.add(ms[j]);
}
}
this.functions.add(new RemoteFunction(functions[i], obj, cls, (Method[])fs.toArray(new Method[0])));
}
}
}