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

main.java.com.dragome.web.services.ClientSideServiceFactory 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.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Map;

import com.dragome.services.interfaces.AsyncCallback;
import com.dragome.services.interfaces.AsyncServiceExecutor;
import com.dragome.services.interfaces.RequestExecutor;
import com.dragome.services.interfaces.SerializationService;
import com.dragome.services.interfaces.ServiceFactory;

public class ClientSideServiceFactory implements ServiceFactory
{
	public  T createSyncService(Class type)
	{
		return createProxy(type, new AbstractServicesInvocationHandler(type)
		{
			public Object execute(Map parameters, RequestExecutor requestExecutor, SerializationService serializationService, boolean returnsValue)
			{
				String result= requestExecutor.executeSynchronousRequest(AbstractServicesInvocationHandler.SERVICE_INVOKER_PATH, parameters);
				return serializationService.deserialize(result);
			}
		});
	}

	public  T createFixedSyncService(Class type)
	{
		return createProxy(type, new AbstractServicesInvocationHandler(type)
		{
			public Object execute(Map parameters, RequestExecutor requestExecutor, SerializationService serializationService, boolean returnsValue)
			{
				String result= requestExecutor.executeFixedSynchronousRequest(AbstractServicesInvocationHandler.SERVICE_INVOKER_PATH, parameters);
				return serializationService.deserialize(result);
			}
		});
	}

	public  AsyncServiceExecutor createAsyncService(final Class type)
	{
		return new AsyncServiceExecutor()
		{
			protected AsyncCallback finalAsyncCallback;
			protected AsyncCallback serviceAsyncCallback;

			protected RequestExecutor requestExecutor1;
			protected Map parameters1;

			final T createProxy= createProxy(type, new AbstractServicesInvocationHandler(type)
			{
				public Object execute(Map parameters, RequestExecutor requestExecutor, final SerializationService serializationService, boolean returnsValue)
				{
					parameters1= parameters;
					requestExecutor1= requestExecutor;

					AsyncCallback asyncCallback= new AsyncCallback()
					{
						public void onSuccess(String result)
						{
							if (result != null && result.trim().length() > 0 && finalAsyncCallback != null)
								finalAsyncCallback.onSuccess(serializationService.deserialize(result));
						}

						public void onError()
						{
							throw new RuntimeException("async call error");
						}
					};

					serviceAsyncCallback= RequestExecutorImpl.wrapCallback(Serializable.class, asyncCallback);

					if (!returnsValue)
						requestExecutor1.executeAsynchronousRequest(AbstractServicesInvocationHandler.SERVICE_INVOKER_PATH, parameters1, serviceAsyncCallback);

					return null;
				}
			});

			public T getService()
			{
				return createProxy;
			}

			public  void executeAsync(S value, AsyncCallback asyncCallback)
			{
				finalAsyncCallback= (AsyncCallback) asyncCallback;
				requestExecutor1.executeAsynchronousRequest(AbstractServicesInvocationHandler.SERVICE_INVOKER_PATH, parameters1, serviceAsyncCallback);
			}
		};

	}

	private  T createProxy(final Class type, InvocationHandler invocationHandler)
	{
		return (T) Proxy.newProxyInstance(ClientSideServiceFactory.class.getClassLoader(), new Class[] { type }, invocationHandler);
	}
}