All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.java.com.dragome.web.services.AbstractServicesInvocationHandler Maven / Gradle / Ivy

There is a newer version: 0.96-beta4
Show newest version
/*
 * Copyright (c) 2011-2014 Fernando Petrola
 *
 * 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.dragome.web.services;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dragome.commons.AbstractProxyRelatedInvocationHandler;
import com.dragome.services.ServiceInvocation;
import com.dragome.services.ServiceLocator;
import com.dragome.services.WebServiceLocator;
import com.dragome.services.interfaces.RequestExecutor;
import com.dragome.services.interfaces.SerializationService;

public abstract class AbstractServicesInvocationHandler extends AbstractProxyRelatedInvocationHandler
{
	public static final String INVOCATION= "invocation";
	public static final String SERVICE_INVOKER_PATH= "service-invoker";

	protected Class type;

	public AbstractServicesInvocationHandler(Class type)
	{
		this.type= type;
	}

	public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable
	{
		setProxy(proxy);

		final WebServiceLocator webServiceLocator= WebServiceLocator.getInstance();
		final ServiceLocator instance= ServiceLocator.getInstance();

		Map parameters= createParameters(type, method, args);
		RequestExecutor requestExecutor= webServiceLocator.getRequestExecutor();
		final SerializationService serializationService= instance.getSerializationService();

		return execute(parameters, requestExecutor, serializationService, !method.getReturnType().equals(void.class));
	}

	public static Map createParameters(Class type, Method method, Object[] args)
	{
		List asList= args != null ? Arrays.asList(args) : new ArrayList();
		ServiceInvocation serviceInvocation= new ServiceInvocation(type, method, asList);
		String serialize= ServiceLocator.getInstance().getSerializationService().serialize(serviceInvocation);
		HashMap parameters= new HashMap();
		parameters.put(INVOCATION, serialize);
		return parameters;
	}

	public abstract Object execute(Map parameters, RequestExecutor requestExecutor, SerializationService serializationService, boolean returnsValue);
}