com.backendless.Invoker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of android Show documentation
Show all versions of android Show documentation
Android SDK used by developers to provide Backendless API in apps.
/*
* ********************************************************************************************************************
*
* BACKENDLESS.COM CONFIDENTIAL
*
* ********************************************************************************************************************
*
* Copyright 2012 BACKENDLESS.COM. All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains the property of Backendless.com and its suppliers,
* if any. The intellectual and technical concepts contained herein are proprietary to Backendless.com and its
* suppliers and may be covered by U.S. and Foreign Patents, patents in process, and are protected by trade secret
* or copyright law. Dissemination of this information or reproduction of this material is strictly forbidden
* unless prior written permission is obtained from Backendless.com.
*
* ********************************************************************************************************************
*/
package com.backendless;
import com.backendless.async.callback.AsyncCallback;
import com.backendless.async.message.AsyncMessage;
import com.backendless.core.ResponseCarrier;
import com.backendless.core.responder.AdaptingResponder;
import com.backendless.exceptions.BackendlessException;
import com.backendless.exceptions.BackendlessFault;
import com.backendless.exceptions.ExceptionMessage;
import weborb.client.Fault;
import weborb.client.IChainedResponder;
import weborb.client.IResponder;
import weborb.client.WeborbClient;
@SuppressWarnings("unchecked")
public class Invoker
{
private static final String URL_ENDING = "/" + Backendless.getVersion() + "/binary";
private static final String DESTINATION = "GenericDestination";
private static final int DEFAULT_TIMEOUT = 100500;
private static final Object webOrbClientLock = new Object();
private static WeborbClient weborbClient;
public static WeborbClient getWebOrbClient()
{
if( weborbClient == null )
{
synchronized( webOrbClientLock )
{
if( weborbClient == null )
{
weborbClient = new WeborbClient( Backendless.getUrl() + URL_ENDING, DEFAULT_TIMEOUT, DESTINATION );
weborbClient.setCookiesDateFormat( "EEE, dd-MMM-yy HH:mm:ss z" );
if( Backendless.isAndroid() )
InvokerAndroidAdds.updateClient( weborbClient );
}
}
}
return weborbClient;
}
public static void invokeAsync( String className, String methodName, Object[] args, AsyncCallback callback )
{
invokeAsync( className, methodName, args, callback, null );
}
public static void invokeAsync( final String className, final String methodName, final Object[] args,
final AsyncCallback callback, final IChainedResponder responder )
{
ThreadPoolService.getPoolExecutor().execute( new Runnable()
{
@Override
public void run()
{
try
{
T result = (T) Invoker.invokeSync( className, methodName, args, responder );
ResponseCarrier.getInstance().deliverMessage( new AsyncMessage( result, callback ) );
}
catch( BackendlessException e )
{
ResponseCarrier.getInstance().deliverMessage( new AsyncMessage( new BackendlessFault( e ), callback ) );
}
}
} );
}
public static T invokeSync( String className, String methodName, Object[] args,
IChainedResponder chainedResponder ) throws BackendlessException
{
SyncResponder invokeResponder = new SyncResponder();
if( chainedResponder == null )
chainedResponder = new AdaptingResponder();
chainedResponder.setNextResponder( invokeResponder );
try
{
getWebOrbClient().invoke( className, methodName, args, null, null, HeadersManager.getInstance().getHeaders(), chainedResponder );
}
catch( Exception e )
{
throw new BackendlessException( ExceptionMessage.SERVER_ERROR, e.getMessage() );
}
return (T) invokeResponder.getResult();
}
public static T invokeSync( String className, String methodName, Object[] args ) throws BackendlessException
{
return (T) invokeSync( className, methodName, args, null );
}
static class SyncResponder implements IResponder
{
private Object result;
private BackendlessException exception;
public void responseHandler( Object o )
{
result = o;
}
public void errorHandler( Fault fault )
{
this.exception = new BackendlessException( new BackendlessFault( fault ) );
}
public Object getResult() throws BackendlessException
{
if( exception == null )
return result;
throw exception;
}
}
}