Search in sources :

Example 1 with Promise

use of io.joynr.provider.Promise in project joynr by bmwcarit.

the class LocalCapabilitiesDirectoryImpl method lookup.

@Override
public Promise<Lookup2Deferred> lookup(String participantId) {
    Lookup2Deferred deferred = new Lookup2Deferred();
    DiscoveryEntryWithMetaInfo discoveryEntry = lookup(participantId, DiscoveryQos.NO_FILTER);
    deferred.resolve(discoveryEntry);
    return new Promise<>(deferred);
}
Also used : Promise(io.joynr.provider.Promise) DiscoveryEntryWithMetaInfo(joynr.types.DiscoveryEntryWithMetaInfo)

Example 2 with Promise

use of io.joynr.provider.Promise in project joynr by bmwcarit.

the class LocalCapabilitiesDirectoryImpl method remove.

@Override
public io.joynr.provider.Promise<io.joynr.provider.DeferredVoid> remove(String participantId) {
    DeferredVoid deferred = new DeferredVoid();
    DiscoveryEntry entryToRemove = localDiscoveryEntryStore.lookup(participantId, Long.MAX_VALUE);
    if (entryToRemove != null) {
        remove(entryToRemove);
        deferred.resolve();
    } else {
        deferred.reject(new ProviderRuntimeException("Failed to remove participantId: " + participantId));
    }
    return new Promise<>(deferred);
}
Also used : Promise(io.joynr.provider.Promise) DiscoveryEntry(joynr.types.DiscoveryEntry) GlobalDiscoveryEntry(joynr.types.GlobalDiscoveryEntry) DeferredVoid(io.joynr.provider.DeferredVoid) ProviderRuntimeException(joynr.exceptions.ProviderRuntimeException)

Example 3 with Promise

use of io.joynr.provider.Promise in project joynr by bmwcarit.

the class ProviderWrapperTest method testInvokeMultiOutMethod.

@Test
public void testInvokeMultiOutMethod() throws Throwable {
    ProviderWrapper subject = createSubject();
    JoynrProvider proxy = createProxy(subject);
    Method method = TestServiceProviderInterface.class.getMethod("testMultiOutMethod");
    Object result = subject.invoke(proxy, method, new Object[0]);
    assertTrue(result instanceof Promise);
    Promise<?> promise = (Promise<?>) result;
    assertTrue(promise.isFulfilled());
    promise.then(new PromiseListener() {

        @Override
        public void onFulfillment(Object... values) {
            assertArrayEquals(new Object[] { "one", "two" }, values);
        }

        @Override
        public void onRejection(JoynrException error) {
            fail("Shouldn't be here.");
        }
    });
}
Also used : Promise(io.joynr.provider.Promise) PromiseListener(io.joynr.provider.PromiseListener) JoynrException(io.joynr.exceptions.JoynrException) Method(java.lang.reflect.Method) JoynrProvider(io.joynr.provider.JoynrProvider) ProviderWrapper(io.joynr.jeeintegration.ProviderWrapper) Test(org.junit.Test)

Example 4 with Promise

use of io.joynr.provider.Promise in project joynr by bmwcarit.

the class ProviderWrapperTest method testInvokeMethodWithParams.

@Test
public void testInvokeMethodWithParams() throws Throwable {
    ProviderWrapper subject = createSubject();
    JoynrProvider proxy = createProxy(subject);
    Method method = TestServiceProviderInterface.class.getMethod("testServiceMethod", new Class[] { Integer.TYPE, String.class });
    Object result = subject.invoke(proxy, method, new Object[] { 1, "one" });
    assertTrue(result instanceof Promise);
    assertPromiseEquals(result, new TestServiceImpl().testServiceMethod(1, "one"));
}
Also used : Promise(io.joynr.provider.Promise) Method(java.lang.reflect.Method) JoynrProvider(io.joynr.provider.JoynrProvider) ProviderWrapper(io.joynr.jeeintegration.ProviderWrapper) Test(org.junit.Test)

Example 5 with Promise

use of io.joynr.provider.Promise in project joynr by bmwcarit.

the class ProviderWrapper method invoke.

/**
 * When a method is invoked via a joynr call, then it is delegated to an instance of the bean with which this
 * instance was initialised, if the method is part of the business interface and to this instance if it was part of
 * the {@link JoynrProvider} interface or the <code>Object</code> class.
 *
 * @param proxy
 *            the proxy object on which the method was called.
 * @param method
 *            the specific method which was called.
 * @param args
 *            the arguments with which the method was called.
 *
 * @return the result of the delegate method call on the EJB, but wrapped in a promise, as all the provider methods
 *         in joynr are declared that way.
 */
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    boolean isProviderMethod = matchesJoynrProviderMethod(method);
    Method delegateToMethod = getMethodFromInterfaces(bean.getBeanClass(), method, isProviderMethod);
    Object delegate = createDelegateForMethod(method, isProviderMethod);
    Object result = null;
    try {
        if (isProviderMethod(method, delegateToMethod)) {
            JoynrJeeMessageContext.getInstance().activate();
            copyMessageCreatorInfo();
            copyMessageContext();
        }
        JoynrException joynrException = null;
        try {
            result = delegateToMethod.invoke(delegate, args);
        } catch (InvocationTargetException e) {
            joynrException = getJoynrExceptionFromInvocationException(e);
        }
        if (delegate != this) {
            AbstractDeferred deferred = createAndResolveOrRejectDeferred(method, result, joynrException);
            Promise<AbstractDeferred> promiseResult = new Promise<>(deferred);
            return promiseResult;
        }
    } finally {
        if (isProviderMethod(method, delegateToMethod)) {
            JoynrJeeMessageContext.getInstance().deactivate();
        }
    }
    return result;
}
Also used : Promise(io.joynr.provider.Promise) JoynrException(io.joynr.exceptions.JoynrException) Method(java.lang.reflect.Method) AbstractDeferred(io.joynr.provider.AbstractDeferred) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

Promise (io.joynr.provider.Promise)79 DeferredVoid (io.joynr.provider.DeferredVoid)36 ProviderRuntimeException (joynr.exceptions.ProviderRuntimeException)27 MapStringString (joynr.interlanguagetest.namedTypeCollection2.MapStringString)13 Method (java.lang.reflect.Method)9 ProviderWrapper (io.joynr.jeeintegration.ProviderWrapper)6 JoynrProvider (io.joynr.provider.JoynrProvider)6 Test (org.junit.Test)6 JoynrException (io.joynr.exceptions.JoynrException)5 Deferred (io.joynr.provider.Deferred)4 PromiseListener (io.joynr.provider.PromiseListener)4 ExtendedExtendedBaseStruct (joynr.interlanguagetest.namedTypeCollection2.ExtendedExtendedBaseStruct)4 GlobalDiscoveryEntry (joynr.types.GlobalDiscoveryEntry)4 StructWithStringArray (joynr.interlanguagetest.namedTypeCollection1.StructWithStringArray)3 ExtendedTypeCollectionEnumerationInTypeCollection (joynr.interlanguagetest.namedTypeCollection2.ExtendedTypeCollectionEnumerationInTypeCollection)3 DiscoveryEntry (joynr.types.DiscoveryEntry)3 DiscoveryEntryWithMetaInfo (joynr.types.DiscoveryEntryWithMetaInfo)3 DiscoveryQos (io.joynr.arbitration.DiscoveryQos)2 DiscoveryScope (io.joynr.arbitration.DiscoveryScope)2 ProviderContainer (io.joynr.provider.ProviderContainer)2