com.eincs.decanter.container.simple.route.SimpleRouteService.java Source code

Java tutorial

Introduction

Here is the source code for com.eincs.decanter.container.simple.route.SimpleRouteService.java

Source

/*
 * Copyright 2012 The Decanter Project
 *
 * The Decanter Project licenses this file to you 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.eincs.decanter.container.simple.route;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;

import net.sf.cglib.reflect.FastClass;
import net.sf.cglib.reflect.FastMethod;

import org.apache.commons.lang.ArrayUtils;

import com.eincs.decanter.container.simple.route.MethodExtractor.MethodRouteInfo;
import com.eincs.decanter.message.DecanterRequest;
import com.eincs.decanter.message.DecanterResponse;
import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

/**
 * @author Jung-Haeng Lee
 */
public class SimpleRouteService implements RouteService {

    private static final Class<?>[] METHOD_PARAMTER = new Class[] { DecanterRequest.class, DecanterResponse.class };

    /**
     * 
     * @param serviceObj
     * @return
     * @throws RouteReflectException
     */
    public static Multimap<SimpleRouteServiceId, SimpleRouteService> createServices(Object serviceObj)
            throws RouteReflectException {
        Multimap<SimpleRouteServiceId, SimpleRouteService> result = ArrayListMultimap.create();
        FastClass clazz = FastClass.create(serviceObj.getClass());

        // extract serviceId and method from serviceObj
        for (MethodRouteInfo methodInfo : MethodExtractor.extract(serviceObj)) {
            FastMethod method = clazz.getMethod(methodInfo.getMethod());
            Class<?>[] parameters = method.getParameterTypes();

            // check paramters of the method
            if (!ArrayUtils.isEquals(METHOD_PARAMTER, parameters)) {
                throw new RouteReflectException(String.format("method(%s) is not applicable for SimpleRouteService",
                        Arrays.asList(parameters)));
            }

            // create service object and add to the result multimap
            SimpleRouteService service = new SimpleRouteService(serviceObj, method);
            List<SimpleRouteServiceId> serviceIds = SimpleRouteServiceId.create(methodInfo);
            for (SimpleRouteServiceId serviceId : serviceIds) {
                result.put(serviceId, service);
            }
        }
        return result;
    }

    private final Object target;
    private final FastMethod method;

    /**
     * 
     * @param target
     * @param method
     */
    private SimpleRouteService(Object target, FastMethod method) {
        this.target = target;
        this.method = method;
    }

    @Override
    public void doServe(DecanterRequest request, DecanterResponse response) throws Exception {
        try {
            method.invoke(target, new Object[] { request, response });
        } catch (InvocationTargetException e) {
            Throwable t = e.getCause();
            if (t instanceof Exception) {
                throw (Exception) t;
            } else {
                throw e;
            }
        }
    }

    @Override
    public String toString() {
        ToStringHelper tsh = Objects.toStringHelper(getClass());
        tsh.add("target", target.getClass().getSimpleName());
        tsh.add("method", method.getName());
        return tsh.toString();
    }

}