com.alibaba.otter.shared.communication.core.impl.AbstractCommunicationEndpoint.java Source code

Java tutorial

Introduction

Here is the source code for com.alibaba.otter.shared.communication.core.impl.AbstractCommunicationEndpoint.java

Source

/*
 * Copyright (C) 2010-2101 Alibaba Group Holding Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.otter.shared.communication.core.impl;

import java.lang.reflect.Method;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ReflectionUtils;

import com.alibaba.otter.shared.communication.core.CommunicationEndpoint;
import com.alibaba.otter.shared.communication.core.CommunicationRegistry;
import com.alibaba.otter.shared.communication.core.exception.CommunicationException;
import com.alibaba.otter.shared.communication.core.model.Event;
import com.alibaba.otter.shared.communication.core.model.heart.HeartEvent;

/**
 * endpoint
 * 
 * @author jianghang 2011-9-9 ?07:01:49
 */
public abstract class AbstractCommunicationEndpoint implements CommunicationEndpoint {

    // ??
    private static final Logger logger = LoggerFactory.getLogger(CommunicationEndpoint.class);

    private static final String DEFAULT_METHOD = "handleEvent";

    /**
     * ?
     */
    public Object acceptEvent(Event event) {
        if (event instanceof HeartEvent) {
            return event; // ??
        }

        try {
            Object action = CommunicationRegistry.getAction(event.getType());
            if (action != null) {

                // ???
                String methodName = "on" + StringUtils.capitalize(event.getType().toString());
                Method method = ReflectionUtils.findMethod(action.getClass(), methodName,
                        new Class[] { event.getClass() });
                if (method == null) {
                    methodName = DEFAULT_METHOD; // ?
                    method = ReflectionUtils.findMethod(action.getClass(), methodName,
                            new Class[] { event.getClass() });

                    if (method == null) { // ??Event?
                        method = ReflectionUtils.findMethod(action.getClass(), methodName,
                                new Class[] { Event.class });
                    }
                }
                // ?,????
                if (method != null) {
                    try {
                        ReflectionUtils.makeAccessible(method);
                        return method.invoke(action, new Object[] { event });
                    } catch (Throwable e) {
                        throw new CommunicationException("method_invoke_error:" + methodName, e);
                    }
                } else {
                    throw new CommunicationException(
                            "no_method_error for[" + StringUtils.capitalize(event.getType().toString())
                                    + "] in Class[" + action.getClass().getName() + "]");
                }

            }

            throw new CommunicationException("eventType_no_action", event.getType().name());
        } catch (RuntimeException e) {
            logger.error("endpoint_error", e);
            throw e;
        } catch (Exception e) {
            logger.error("endpoint_error", e);
            throw new CommunicationException(e);
        }
    }
}