net.hasor.rsf.hprose.server.HproseService Maven / Gradle / Ivy
The newest version!
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* HproseService.java *
* *
* hprose service class for Java. *
* *
* LastModified: Oct 16, 2016 *
* Author: Ma Bingyao *
* *
\**********************************************************/
package hprose.server;
import hprose.common.HandlerManager;
import hprose.common.HproseContext;
import hprose.common.HproseException;
import hprose.common.HproseFilter;
import hprose.common.HproseMethod;
import hprose.common.HproseMethods;
import hprose.common.HproseResultMode;
import hprose.io.ByteBufferStream;
import hprose.io.HproseMode;
import static hprose.io.HproseTags.TagArgument;
import static hprose.io.HproseTags.TagCall;
import static hprose.io.HproseTags.TagEnd;
import static hprose.io.HproseTags.TagError;
import static hprose.io.HproseTags.TagFunctions;
import static hprose.io.HproseTags.TagList;
import static hprose.io.HproseTags.TagOpenbrace;
import static hprose.io.HproseTags.TagResult;
import static hprose.io.HproseTags.TagTrue;
import hprose.io.serialize.Writer;
import hprose.io.unserialize.Reader;
import hprose.util.StrUtil;
import hprose.util.concurrent.Action;
import hprose.util.concurrent.AsyncFunc;
import hprose.util.concurrent.Call;
import hprose.util.concurrent.Func;
import hprose.util.concurrent.Promise;
import hprose.util.concurrent.Reducer;
import hprose.util.concurrent.Threads;
import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
public abstract class HproseService extends HandlerManager implements HproseClients {
private static volatile ScheduledExecutorService timerService = Executors.newSingleThreadScheduledExecutor();
static {
Threads.registerShutdownHandler(new Runnable() {
public void run() {
ScheduledExecutorService timer = timerService;
timerService = Executors.newSingleThreadScheduledExecutor();
timer.shutdownNow();
}
});
}
public HproseService() {
add("call", new Callable() {
public String call() throws Exception {
return UUID.randomUUID().toString();
}
}, "#", true);
}
private final ArrayList filters = new ArrayList();
private HproseMode mode = HproseMode.MemberMode;
private boolean debugEnabled = false;
private int errorDelay = 10000;
protected HproseServiceEvent event = null;
protected HproseMethods globalMethods = null;
private final static ThreadLocal currentContext = new ThreadLocal();
public static ServiceContext getCurrentContext() {
return currentContext.get();
}
public HproseMethods getGlobalMethods() {
if (globalMethods == null) {
globalMethods = new HproseMethods();
}
return globalMethods;
}
public void setGlobalMethods(HproseMethods methods) {
this.globalMethods = methods;
}
public final HproseMode getMode() {
return mode;
}
public final void setMode(HproseMode mode) {
this.mode = mode;
}
public final boolean isDebugEnabled() {
return debugEnabled;
}
public final void setDebugEnabled(boolean enabled) {
debugEnabled = enabled;
}
public int getErrorDelay() {
return errorDelay;
}
public void setErrorDelay(int errorDelay) {
this.errorDelay = errorDelay;
}
public final HproseServiceEvent getEvent() {
return this.event;
}
public final void setEvent(HproseServiceEvent event) {
this.event = event;
}
public final HproseFilter getFilter() {
if (filters.isEmpty()) {
return null;
}
return filters.get(0);
}
public final void setFilter(HproseFilter filter) {
if (!filters.isEmpty()) {
filters.clear();
}
if (filter != null) {
filters.add(filter);
}
}
public final HproseService addFilter(HproseFilter filter) {
if (filter != null) {
filters.add(filter);
}
return this;
}
public final boolean removeFilter(HproseFilter filter) {
return filters.remove(filter);
}
public final HproseService add(Method method, Object obj, String aliasName) {
getGlobalMethods().addMethod(method, obj, aliasName);
return this;
}
public final HproseService add(Method method, Object obj, String aliasName, HproseResultMode mode) {
getGlobalMethods().addMethod(method, obj, aliasName, mode);
return this;
}
public final HproseService add(Method method, Object obj, String aliasName, boolean simple) {
getGlobalMethods().addMethod(method, obj, aliasName, simple);
return this;
}
public final HproseService add(Method method, Object obj, String aliasName, HproseResultMode mode, boolean simple) {
getGlobalMethods().addMethod(method, obj, aliasName, mode, simple);
return this;
}
public final HproseService add(Method method, Object obj, String aliasName, HproseResultMode mode, boolean simple, boolean oneway) {
getGlobalMethods().addMethod(method, obj, aliasName, mode, simple, oneway);
return this;
}
public final HproseService add(Method method, Object obj) {
getGlobalMethods().addMethod(method, obj);
return this;
}
public final HproseService add(Method method, Object obj, HproseResultMode mode) {
getGlobalMethods().addMethod(method, obj, mode);
return this;
}
public final HproseService add(Method method, Object obj, boolean simple) {
getGlobalMethods().addMethod(method, obj, simple);
return this;
}
public final HproseService add(Method method, Object obj, HproseResultMode mode, boolean simple) {
getGlobalMethods().addMethod(method, obj, mode, simple);
return this;
}
public final HproseService add(Method method, Object obj, HproseResultMode mode, boolean simple, boolean oneway) {
getGlobalMethods().addMethod(method, obj, mode, simple, oneway);
return this;
}
public final HproseService add(String methodName, Object obj, Class>[] paramTypes, String aliasName) throws NoSuchMethodException {
getGlobalMethods().addMethod(methodName, obj, paramTypes, aliasName);
return this;
}
public final HproseService add(String methodName, Object obj, Class>[] paramTypes, String aliasName, HproseResultMode mode) throws NoSuchMethodException {
getGlobalMethods().addMethod(methodName, obj, paramTypes, aliasName, mode);
return this;
}
public final HproseService add(String methodName, Object obj, Class>[] paramTypes, String aliasName, boolean simple) throws NoSuchMethodException {
getGlobalMethods().addMethod(methodName, obj, paramTypes, aliasName, simple);
return this;
}
public final HproseService add(String methodName, Object obj, Class>[] paramTypes, String aliasName, HproseResultMode mode, boolean simple) throws NoSuchMethodException {
getGlobalMethods().addMethod(methodName, obj, paramTypes, aliasName, mode, simple);
return this;
}
public final HproseService add(String methodName, Object obj, Class>[] paramTypes, String aliasName, HproseResultMode mode, boolean simple, boolean oneway) throws NoSuchMethodException {
getGlobalMethods().addMethod(methodName, obj, paramTypes, aliasName, mode, simple, oneway);
return this;
}
public final HproseService add(String methodName, Class> type, Class>[] paramTypes, String aliasName) throws NoSuchMethodException {
getGlobalMethods().addMethod(methodName, type, paramTypes, aliasName);
return this;
}
public final HproseService add(String methodName, Class> type, Class>[] paramTypes, String aliasName, HproseResultMode mode) throws NoSuchMethodException {
getGlobalMethods().addMethod(methodName, type, paramTypes, aliasName, mode);
return this;
}
public final HproseService add(String methodName, Class> type, Class>[] paramTypes, String aliasName, boolean simple) throws NoSuchMethodException {
getGlobalMethods().addMethod(methodName, type, paramTypes, aliasName, simple);
return this;
}
public final HproseService add(String methodName, Class> type, Class>[] paramTypes, String aliasName, HproseResultMode mode, boolean simple) throws NoSuchMethodException {
getGlobalMethods().addMethod(methodName, type, paramTypes, aliasName, mode, simple);
return this;
}
public final HproseService add(String methodName, Class> type, Class>[] paramTypes, String aliasName, HproseResultMode mode, boolean simple, boolean oneway) throws NoSuchMethodException {
getGlobalMethods().addMethod(methodName, type, paramTypes, aliasName, mode, simple, oneway);
return this;
}
public final HproseService add(String methodName, Object obj, Class>[] paramTypes) throws NoSuchMethodException {
getGlobalMethods().addMethod(methodName, obj, paramTypes);
return this;
}
public final HproseService add(String methodName, Object obj, Class>[] paramTypes, HproseResultMode mode) throws NoSuchMethodException {
getGlobalMethods().addMethod(methodName, obj, paramTypes, mode);
return this;
}
public final HproseService add(String methodName, Object obj, Class>[] paramTypes, boolean simple) throws NoSuchMethodException {
getGlobalMethods().addMethod(methodName, obj, paramTypes, simple);
return this;
}
public final HproseService add(String methodName, Object obj, Class>[] paramTypes, HproseResultMode mode, boolean simple) throws NoSuchMethodException {
getGlobalMethods().addMethod(methodName, obj, paramTypes, mode, simple);
return this;
}
public final HproseService add(String methodName, Object obj, Class>[] paramTypes, HproseResultMode mode, boolean simple, boolean oneway) throws NoSuchMethodException {
getGlobalMethods().addMethod(methodName, obj, paramTypes, mode, simple, oneway);
return this;
}
public final HproseService add(String methodName, Class> type, Class>[] paramTypes) throws NoSuchMethodException {
getGlobalMethods().addMethod(methodName, type, paramTypes);
return this;
}
public final HproseService add(String methodName, Class> type, Class>[] paramTypes, HproseResultMode mode) throws NoSuchMethodException {
getGlobalMethods().addMethod(methodName, type, paramTypes, mode);
return this;
}
public final HproseService add(String methodName, Class> type, Class>[] paramTypes, boolean simple) throws NoSuchMethodException {
getGlobalMethods().addMethod(methodName, type, paramTypes, simple);
return this;
}
public final HproseService add(String methodName, Class> type, Class>[] paramTypes, HproseResultMode mode, boolean simple) throws NoSuchMethodException {
getGlobalMethods().addMethod(methodName, type, paramTypes, mode, simple);
return this;
}
public final HproseService add(String methodName, Class> type, Class>[] paramTypes, HproseResultMode mode, boolean simple, boolean oneway) throws NoSuchMethodException {
getGlobalMethods().addMethod(methodName, type, paramTypes, mode, simple, oneway);
return this;
}
public final HproseService add(String methodName, Object obj, String aliasName) {
getGlobalMethods().addMethod(methodName, obj, aliasName);
return this;
}
public final HproseService add(String methodName, Object obj, String aliasName, HproseResultMode mode) {
getGlobalMethods().addMethod(methodName, obj, aliasName, mode);
return this;
}
public final HproseService add(String methodName, Object obj, String aliasName, boolean simple) {
getGlobalMethods().addMethod(methodName, obj, aliasName, simple);
return this;
}
public final HproseService add(String methodName, Object obj, String aliasName, HproseResultMode mode, boolean simple) {
getGlobalMethods().addMethod(methodName, obj, aliasName, mode, simple);
return this;
}
public final HproseService add(String methodName, Object obj, String aliasName, HproseResultMode mode, boolean simple, boolean oneway) {
getGlobalMethods().addMethod(methodName, obj, aliasName, mode, simple, oneway);
return this;
}
public final HproseService add(String methodName, Class> type, String aliasName) {
getGlobalMethods().addMethod(methodName, type, aliasName);
return this;
}
public final HproseService add(String methodName, Class> type, String aliasName, HproseResultMode mode) {
getGlobalMethods().addMethod(methodName, type, aliasName, mode);
return this;
}
public final HproseService add(String methodName, Class> type, String aliasName, boolean simple) {
getGlobalMethods().addMethod(methodName, type, aliasName, simple);
return this;
}
public final HproseService add(String methodName, Class> type, String aliasName, HproseResultMode mode, boolean simple) {
getGlobalMethods().addMethod(methodName, type, aliasName, mode, simple);
return this;
}
public final HproseService add(String methodName, Class> type, String aliasName, HproseResultMode mode, boolean simple, boolean oneway) {
getGlobalMethods().addMethod(methodName, type, aliasName, mode, simple, oneway);
return this;
}
public final HproseService add(String methodName, Object obj) {
getGlobalMethods().addMethod(methodName, obj);
return this;
}
public final HproseService add(String methodName, Object obj, HproseResultMode mode) {
getGlobalMethods().addMethod(methodName, obj, mode);
return this;
}
public final HproseService add(String methodName, Object obj, boolean simple) {
getGlobalMethods().addMethod(methodName, obj, simple);
return this;
}
public final HproseService add(String methodName, Object obj, HproseResultMode mode, boolean simple) {
getGlobalMethods().addMethod(methodName, obj, mode, simple);
return this;
}
public final HproseService add(String methodName, Object obj, HproseResultMode mode, boolean simple, boolean oneway) {
getGlobalMethods().addMethod(methodName, obj, mode, simple, oneway);
return this;
}
public final HproseService add(String methodName, Class> type) {
getGlobalMethods().addMethod(methodName, type);
return this;
}
public final HproseService add(String methodName, Class> type, HproseResultMode mode) {
getGlobalMethods().addMethod(methodName, type, mode);
return this;
}
public final HproseService add(String methodName, Class> type, boolean simple) {
getGlobalMethods().addMethod(methodName, type, simple);
return this;
}
public final HproseService add(String methodName, Class> type, HproseResultMode mode, boolean simple) {
getGlobalMethods().addMethod(methodName, type, mode, simple);
return this;
}
public final HproseService add(String methodName, Class> type, HproseResultMode mode, boolean simple, boolean oneway) {
getGlobalMethods().addMethod(methodName, type, mode, simple, oneway);
return this;
}
public final HproseService add(String[] methodNames, Object obj, String[] aliasNames) {
getGlobalMethods().addMethods(methodNames, obj, aliasNames);
return this;
}
public final HproseService add(String[] methodNames, Object obj, String[] aliasNames, HproseResultMode mode) {
getGlobalMethods().addMethods(methodNames, obj, aliasNames, mode);
return this;
}
public final HproseService add(String[] methodNames, Object obj, String[] aliasNames, boolean simple) {
getGlobalMethods().addMethods(methodNames, obj, aliasNames, simple);
return this;
}
public final HproseService add(String[] methodNames, Object obj, String[] aliasNames, HproseResultMode mode, boolean simple) {
getGlobalMethods().addMethods(methodNames, obj, aliasNames, mode, simple);
return this;
}
public final HproseService add(String[] methodNames, Object obj, String[] aliasNames, HproseResultMode mode, boolean simple, boolean oneway) {
getGlobalMethods().addMethods(methodNames, obj, aliasNames, mode, simple, oneway);
return this;
}
public final HproseService add(String[] methodNames, Object obj, String aliasPrefix) {
getGlobalMethods().addMethods(methodNames, obj, aliasPrefix);
return this;
}
public final HproseService add(String[] methodNames, Object obj, String aliasPrefix, HproseResultMode mode) {
getGlobalMethods().addMethods(methodNames, obj, aliasPrefix, mode);
return this;
}
public final HproseService add(String[] methodNames, Object obj, String aliasPrefix, boolean simple) {
getGlobalMethods().addMethods(methodNames, obj, aliasPrefix, simple);
return this;
}
public final HproseService add(String[] methodNames, Object obj, String aliasPrefix, HproseResultMode mode, boolean simple) {
getGlobalMethods().addMethods(methodNames, obj, aliasPrefix, mode, simple);
return this;
}
public final HproseService add(String[] methodNames, Object obj, String aliasPrefix, HproseResultMode mode, boolean simple, boolean oneway) {
getGlobalMethods().addMethods(methodNames, obj, aliasPrefix, mode, simple, oneway);
return this;
}
public final HproseService add(String[] methodNames, Object obj) {
getGlobalMethods().addMethods(methodNames, obj);
return this;
}
public final HproseService add(String[] methodNames, Object obj, HproseResultMode mode) {
getGlobalMethods().addMethods(methodNames, obj, mode);
return this;
}
public final HproseService add(String[] methodNames, Object obj, boolean simple) {
getGlobalMethods().addMethods(methodNames, obj, simple);
return this;
}
public final HproseService add(String[] methodNames, Object obj, HproseResultMode mode, boolean simple) {
getGlobalMethods().addMethods(methodNames, obj, mode, simple);
return this;
}
public final HproseService add(String[] methodNames, Object obj, HproseResultMode mode, boolean simple, boolean oneway) {
getGlobalMethods().addMethods(methodNames, obj, mode, simple, oneway);
return this;
}
public final HproseService add(String[] methodNames, Class> type, String[] aliasNames) {
getGlobalMethods().addMethods(methodNames, type, aliasNames);
return this;
}
public final HproseService add(String[] methodNames, Class> type, String[] aliasNames, HproseResultMode mode) {
getGlobalMethods().addMethods(methodNames, type, aliasNames, mode);
return this;
}
public final HproseService add(String[] methodNames, Class> type, String[] aliasNames, boolean simple) {
getGlobalMethods().addMethods(methodNames, type, aliasNames, simple);
return this;
}
public final HproseService add(String[] methodNames, Class> type, String[] aliasNames, HproseResultMode mode, boolean simple) {
getGlobalMethods().addMethods(methodNames, type, aliasNames, mode, simple);
return this;
}
public final HproseService add(String[] methodNames, Class> type, String[] aliasNames, HproseResultMode mode, boolean simple, boolean oneway) {
getGlobalMethods().addMethods(methodNames, type, aliasNames, mode, simple, oneway);
return this;
}
public final HproseService add(String[] methodNames, Class> type, String aliasPrefix) {
getGlobalMethods().addMethods(methodNames, type, aliasPrefix);
return this;
}
public final HproseService add(String[] methodNames, Class> type, String aliasPrefix, HproseResultMode mode) {
getGlobalMethods().addMethods(methodNames, type, aliasPrefix, mode);
return this;
}
public final HproseService add(String[] methodNames, Class> type, String aliasPrefix, boolean simple) {
getGlobalMethods().addMethods(methodNames, type, aliasPrefix, simple);
return this;
}
public final HproseService add(String[] methodNames, Class> type, String aliasPrefix, HproseResultMode mode, boolean simple) {
getGlobalMethods().addMethods(methodNames, type, aliasPrefix, mode, simple);
return this;
}
public final HproseService add(String[] methodNames, Class> type, String aliasPrefix, HproseResultMode mode, boolean simple, boolean oneway) {
getGlobalMethods().addMethods(methodNames, type, aliasPrefix, mode, simple, oneway);
return this;
}
public final HproseService add(String[] methodNames, Class> type) {
getGlobalMethods().addMethods(methodNames, type);
return this;
}
public final HproseService add(String[] methodNames, Class> type, HproseResultMode mode) {
getGlobalMethods().addMethods(methodNames, type, mode);
return this;
}
public final HproseService add(String[] methodNames, Class> type, boolean simple) {
getGlobalMethods().addMethods(methodNames, type, simple);
return this;
}
public final HproseService add(String[] methodNames, Class> type, HproseResultMode mode, boolean simple) {
getGlobalMethods().addMethods(methodNames, type, mode, simple);
return this;
}
public final HproseService add(String[] methodNames, Class> type, HproseResultMode mode, boolean simple, boolean oneway) {
getGlobalMethods().addMethods(methodNames, type, mode, simple, oneway);
return this;
}
public final HproseService add(Object obj, Class> type, String aliasPrefix) {
getGlobalMethods().addInstanceMethods(obj, type, aliasPrefix);
return this;
}
public final HproseService add(Object obj, Class> type, String aliasPrefix, HproseResultMode mode) {
getGlobalMethods().addInstanceMethods(obj, type, aliasPrefix, mode);
return this;
}
public final HproseService add(Object obj, Class> type, String aliasPrefix, boolean simple) {
getGlobalMethods().addInstanceMethods(obj, type, aliasPrefix, simple);
return this;
}
public final HproseService add(Object obj, Class> type, String aliasPrefix, HproseResultMode mode, boolean simple) {
getGlobalMethods().addInstanceMethods(obj, type, aliasPrefix, mode, simple);
return this;
}
public final HproseService add(Object obj, Class> type, String aliasPrefix, HproseResultMode mode, boolean simple, boolean oneway) {
getGlobalMethods().addInstanceMethods(obj, type, aliasPrefix, mode, simple, oneway);
return this;
}
public final HproseService add(Object obj, Class> type) {
getGlobalMethods().addInstanceMethods(obj, type);
return this;
}
public final HproseService add(Object obj, Class> type, HproseResultMode mode) {
getGlobalMethods().addInstanceMethods(obj, type, mode);
return this;
}
public final HproseService add(Object obj, Class> type, boolean simple) {
getGlobalMethods().addInstanceMethods(obj, type, simple);
return this;
}
public final HproseService add(Object obj, Class> type, HproseResultMode mode, boolean simple) {
getGlobalMethods().addInstanceMethods(obj, type, mode, simple);
return this;
}
public final HproseService add(Object obj, Class> type, HproseResultMode mode, boolean simple, boolean oneway) {
getGlobalMethods().addInstanceMethods(obj, type, mode, simple, oneway);
return this;
}
public final HproseService add(Object obj, String aliasPrefix) {
getGlobalMethods().addInstanceMethods(obj, aliasPrefix);
return this;
}
public final HproseService add(Object obj, String aliasPrefix, HproseResultMode mode) {
getGlobalMethods().addInstanceMethods(obj, aliasPrefix, mode);
return this;
}
public final HproseService add(Object obj, String aliasPrefix, boolean simple) {
getGlobalMethods().addInstanceMethods(obj, aliasPrefix, simple);
return this;
}
public final HproseService add(Object obj, String aliasPrefix, HproseResultMode mode, boolean simple) {
getGlobalMethods().addInstanceMethods(obj, aliasPrefix, mode, simple);
return this;
}
public final HproseService add(Object obj, String aliasPrefix, HproseResultMode mode, boolean simple, boolean oneway) {
getGlobalMethods().addInstanceMethods(obj, aliasPrefix, mode, simple, oneway);
return this;
}
public final HproseService add(Object obj) {
getGlobalMethods().addInstanceMethods(obj);
return this;
}
public final HproseService add(Object obj, HproseResultMode mode) {
getGlobalMethods().addInstanceMethods(obj, mode);
return this;
}
public final HproseService add(Object obj, boolean simple) {
getGlobalMethods().addInstanceMethods(obj, simple);
return this;
}
public final HproseService add(Object obj, HproseResultMode mode, boolean simple) {
getGlobalMethods().addInstanceMethods(obj, mode, simple);
return this;
}
public final HproseService add(Object obj, HproseResultMode mode, boolean simple, boolean oneway) {
getGlobalMethods().addInstanceMethods(obj, mode, simple, oneway);
return this;
}
public final HproseService add(Class> type, String aliasPrefix) {
getGlobalMethods().addStaticMethods(type, aliasPrefix);
return this;
}
public final HproseService add(Class> type, String aliasPrefix, HproseResultMode mode) {
getGlobalMethods().addStaticMethods(type, aliasPrefix, mode);
return this;
}
public final HproseService add(Class> type, String aliasPrefix, boolean simple) {
getGlobalMethods().addStaticMethods(type, aliasPrefix, simple);
return this;
}
public final HproseService add(Class> type, String aliasPrefix, HproseResultMode mode, boolean simple) {
getGlobalMethods().addStaticMethods(type, aliasPrefix, mode, simple);
return this;
}
public final HproseService add(Class> type, String aliasPrefix, HproseResultMode mode, boolean simple, boolean oneway) {
getGlobalMethods().addStaticMethods(type, aliasPrefix, mode, simple, oneway);
return this;
}
public final HproseService add(Class> type) {
getGlobalMethods().addStaticMethods(type);
return this;
}
public final HproseService add(Class> type, HproseResultMode mode) {
getGlobalMethods().addStaticMethods(type, mode);
return this;
}
public final HproseService add(Class> type, boolean simple) {
getGlobalMethods().addStaticMethods(type, simple);
return this;
}
public final HproseService add(Class> type, HproseResultMode mode, boolean simple) {
getGlobalMethods().addStaticMethods(type, mode, simple);
return this;
}
public final HproseService add(Class> type, HproseResultMode mode, boolean simple, boolean oneway) {
getGlobalMethods().addStaticMethods(type, mode, simple, oneway);
return this;
}
public final HproseService addMissingMethod(String methodName, Object obj) throws NoSuchMethodException {
getGlobalMethods().addMissingMethod(methodName, obj);
return this;
}
public final HproseService addMissingMethod(String methodName, Object obj, HproseResultMode mode) throws NoSuchMethodException {
getGlobalMethods().addMissingMethod(methodName, obj, mode);
return this;
}
public final HproseService addMissingMethod(String methodName, Object obj, boolean simple) throws NoSuchMethodException {
getGlobalMethods().addMissingMethod(methodName, obj, simple);
return this;
}
public final HproseService addMissingMethod(String methodName, Object obj, HproseResultMode mode, boolean simple) throws NoSuchMethodException {
getGlobalMethods().addMissingMethod(methodName, obj, mode, simple);
return this;
}
public final HproseService addMissingMethod(String methodName, Object obj, HproseResultMode mode, boolean simple, boolean oneway) throws NoSuchMethodException {
getGlobalMethods().addMissingMethod(methodName, obj, mode, simple, oneway);
return this;
}
public final HproseService addMissingMethod(String methodName, Class> type) throws NoSuchMethodException {
getGlobalMethods().addMissingMethod(methodName, type);
return this;
}
public final HproseService addMissingMethod(String methodName, Class> type, HproseResultMode mode) throws NoSuchMethodException {
getGlobalMethods().addMissingMethod(methodName, type, mode);
return this;
}
public final HproseService addMissingMethod(String methodName, Class> type, boolean simple) throws NoSuchMethodException {
getGlobalMethods().addMissingMethod(methodName, type, simple);
return this;
}
public final HproseService addMissingMethod(String methodName, Class> type, HproseResultMode mode, boolean simple) throws NoSuchMethodException {
getGlobalMethods().addMissingMethod(methodName, type, mode, simple);
return this;
}
public final HproseService addMissingMethod(String methodName, Class> type, HproseResultMode mode, boolean simple, boolean oneway) throws NoSuchMethodException {
getGlobalMethods().addMissingMethod(methodName, type, mode, simple, oneway);
return this;
}
public final HproseService remove(String alias) {
getGlobalMethods().remove(alias);
return this;
}
private ByteBuffer outputFilter(ByteBuffer response, ServiceContext context) {
if (response.position() != 0) {
response.flip();
}
for (int i = 0, n = filters.size(); i < n; ++i) {
response = filters.get(i).outputFilter(response, context);
if (response.position() != 0) {
response.flip();
}
}
return response;
}
private ByteBuffer inputFilter(ByteBuffer request, ServiceContext context) {
if (request.position() != 0) {
request.flip();
}
for (int i = filters.size() - 1; i >= 0; --i) {
request = filters.get(i).inputFilter(request, context);
if (request.position() != 0) {
request.flip();
}
}
return request;
}
private String getErrorMessage(Throwable e) {
if (debugEnabled) {
StackTraceElement[] st = e.getStackTrace();
StringBuffer es = new StringBuffer(e.toString()).append("\r\n");
for (int i = 0, n = st.length; i < n; ++i) {
es.append(st[i].toString()).append("\r\n");
}
return es.toString();
}
return e.toString();
}
private ByteBuffer sendError(Throwable e, ServiceContext context) {
try {
if (event != null) {
Throwable ex = event.onSendError(e, context);
if (ex != null) {
e = ex;
}
}
}
catch (Throwable ex) {
e = ex;
}
try {
ByteBufferStream data = new ByteBufferStream();
Writer writer = new Writer(data.getOutputStream(), mode, true);
data.write(TagError);
writer.writeString(getErrorMessage(e));
data.flip();
return data.buffer;
}
catch (IOException ex) {
fireErrorEvent(ex, context);
}
return null;
}
private ByteBuffer endError(Throwable e, ServiceContext context) {
ByteBufferStream data = new ByteBufferStream();
data.write(sendError(e, context));
data.write(TagEnd);
data.flip();
return data.buffer;
}
protected Object[] fixArguments(Type[] argumentTypes, Object[] arguments, ServiceContext context) {
int count = arguments.length;
if (argumentTypes.length != count) {
Object[] args = new Object[argumentTypes.length];
System.arraycopy(arguments, 0, args, 0, count);
Class> argType = (Class>) argumentTypes[count];
if (argType.equals(HproseContext.class) || argType.equals(ServiceContext.class)) {
args[count] = context;
}
return args;
}
return arguments;
}
private ByteBuffer doOutput(Object[] args, Object result, ServiceContext context) throws IOException, InterruptedException, ExecutionException {
ByteBufferStream data = new ByteBufferStream();
HproseMethod remoteMethod = context.getRemoteMethod();
if (result instanceof Future) {
result = ((Future)result).get();
}
switch (remoteMethod.mode) {
case RawWithEndTag:
data.write((byte[])result);
data.flip();
return data.buffer;
case Raw:
data.write((byte[])result);
break;
default: {
data.write(TagResult);
boolean simple = remoteMethod.simple;
Writer writer = new Writer(data.getOutputStream(), mode, simple);
if (remoteMethod.mode == HproseResultMode.Serialized) {
data.write((byte[])result);
}
else {
writer.serialize(result);
}
if (context.isByref()) {
data.write(TagArgument);
writer.reset();
writer.writeArray(args);
}
break;
}
}
data.flip();
return data.buffer;
}
private Object beforeInvoke(final String name, final Object[] args, final ServiceContext context) {
try {
if (event != null) {
event.onBeforeInvoke(name, args, context.isByref(), context);
}
return invokeHandler.handle(name, args, context).then(new Func() {
public ByteBuffer call(Object result) throws Throwable {
if (result instanceof Throwable) {
throw (Throwable)result;
}
if (event != null) {
event.onAfterInvoke(name, args, context.isByref(), result, context);
}
return doOutput(args, result, context);
}
}).catchError(new Func() {
public ByteBuffer call(Throwable e) throws Throwable {
return sendError(e, context);
}
});
}
catch (Throwable e) {
return sendError(e, context);
}
}
private Object callService(String name, Object[] args, ServiceContext context) throws Throwable {
HproseMethod remoteMethod = context.getRemoteMethod();
try {
if (context.isMissingMethod()) {
return remoteMethod.method.invoke(remoteMethod.obj, new Object[]{name, args});
}
else {
Object[] arguments = fixArguments(remoteMethod.paramTypes, args, context);
Object result = remoteMethod.method.invoke(remoteMethod.obj, arguments);
if (context.isByref()) {
System.arraycopy(arguments, 0, args, 0, args.length);
}
return result;
}
}
catch (Throwable ex) {
Throwable e = ex.getCause();
if (e != null) {
throw e;
}
throw ex;
}
}
@Override
protected Promise