EJBInvocationHandler<T> getAsyncHandler() { return async ? this : new EJBInvocationHandler<T>(this); }
/** * Compare and change the strong affinity of a proxy. All subsequent invocations against the proxy will use the new affinity. * Subsequent calls to {@link #getLocatorFor(Object)} for the given proxy will return the updated locator. If the * affinity is not equal to the expected value, {@code false} is returned and no change is made. * * @param proxy the proxy (may not be {@code null}) * @param newAffinity the new affinity (may not be {@code null}) * @throws IllegalArgumentException if the given proxy is not a valid client proxy instance * @throws SecurityException if a security manager is present and the caller does not have the {@code changeStrongAffinity} {@link EJBClientPermission} */ public static boolean compareAndSetStrongAffinity(Object proxy, Affinity expectedAffinity, Affinity newAffinity) throws IllegalArgumentException, SecurityException { Assert.checkNotNullParam("proxy", proxy); Assert.checkNotNullParam("expectedAffinity", expectedAffinity); Assert.checkNotNullParam("newAffinity", newAffinity); final EJBInvocationHandler<?> invocationHandler = EJBInvocationHandler.forProxy(proxy); final Affinity existing = invocationHandler.getLocator().getAffinity(); if (! expectedAffinity.equals(existing)) { return false; } final SecurityManager sm = System.getSecurityManager(); if (sm != null) { sm.checkPermission(EJBClientPermission.CHANGE_STRONG_AFFINITY); } return invocationHandler.compareAndSetStrongAffinity(expectedAffinity, newAffinity); }
/** * Determine whether this object is equal to another. * * @param other the other object * @return {@code true} if they are equal, {@code false} otherwise */ public boolean equals(Object other) { return other instanceof EJBInvocationHandler && equals((EJBInvocationHandler<?>)other); }
/** * Perform an asynchronous invocation by method locator on a proxy, returning the future result. * * @param proxy the EJB proxy * @param methodLocator the method locator * @param args the invocation arguments * @param <T> the view type * @throws Exception if the invocation failed for some reason */ public static <T> Future<?> invokeAsync(T proxy, EJBMethodLocator methodLocator, Object... args) throws Exception { final EJBInvocationHandler<? extends T> invocationHandler = EJBInvocationHandler.forProxy(proxy); final EJBProxyInformation.ProxyMethodInfo proxyMethodInfo = invocationHandler.getProxyMethodInfo(methodLocator); return (Future<?>) invocationHandler.invoke(proxy, proxyMethodInfo, args); }
/** * Get the current weak affinity of a proxy. * * @param proxy the proxy (must not be {@code null}) * @return the affinity (not {@code null}) * @throws IllegalArgumentException if the given proxy is not a valid client proxy instance */ public static Affinity getWeakAffinity(Object proxy) throws IllegalArgumentException { Assert.checkNotNullParam("proxy", proxy); return EJBInvocationHandler.forProxy(proxy).getWeakAffinity(); }
/** * Get the locator for a proxy, if it has one. * * @param proxy the proxy (may not be {@code null}) * @param <T> the proxy type * @return the locator * @throws IllegalArgumentException if the given proxy is not a valid client proxy instance */ public static <T> EJBLocator<? extends T> getLocatorFor(T proxy) throws IllegalArgumentException { Assert.checkNotNullParam("proxy", proxy); return EJBInvocationHandler.forProxy(proxy).getLocator(); }
/** * Clear the per-proxy invocation timeout, causing it to use the globally configured timeout. * * @param proxy the proxy to change (must not be {@code null}, must be a valid EJB proxy) * @throws IllegalArgumentException if the proxy is {@code null} or is not valid */ public static void clearInvocationTimeout(Object proxy) throws IllegalArgumentException { Assert.checkNotNullParam("proxy", proxy); EJBInvocationHandler.forProxy(proxy).setInvocationTimeout(-1L); }
/** * Remove a proxy attachment. * * @param proxy the proxy (must not be {@code null}) * @param attachmentKey the attachment key to use (must not be {@code null}) * @param <T> the value type * @return the previous attachment value or {@code null} if the attachment previously did not exist * @throws IllegalArgumentException if a required parameter is {@code null} or if the object is not a valid EJB client proxy */ public static <T> T removeProxyAttachment(Object proxy, AttachmentKey<T> attachmentKey) throws IllegalArgumentException { Assert.checkNotNullParam("proxy", proxy); Assert.checkNotNullParam("attachmentKey", attachmentKey); return EJBInvocationHandler.forProxy(proxy).removeAttachment(attachmentKey); }
/** * Replace a proxy attachment if it is already present. * * @param proxy the proxy (must not be {@code null}) * @param attachmentKey the attachment key to use (must not be {@code null}) * @param newValue the new value to set (must not be {@code null}) * @param <T> the value type * @return the previous attachment value or {@code null} if the attachment previously did not exist * @throws IllegalArgumentException if a required parameter is {@code null} or if the object is not a valid EJB client proxy */ public static <T> T replaceProxyAttachment(Object proxy, AttachmentKey<T> attachmentKey, T newValue) throws IllegalArgumentException { Assert.checkNotNullParam("proxy", proxy); Assert.checkNotNullParam("attachmentKey", attachmentKey); Assert.checkNotNullParam("newValue", newValue); return EJBInvocationHandler.forProxy(proxy).replaceAttachment(attachmentKey, newValue); }
/** * Get a proxy attachment. * * @param proxy the proxy (must not be {@code null}) * @param attachmentKey the attachment key to use (must not be {@code null}) * @param <T> the value type * @return the attachment value or {@code null} if the attachment is not present * @throws IllegalArgumentException if a required parameter is {@code null} or if the object is not a valid EJB client proxy */ public static <T> T getProxyAttachment(Object proxy, AttachmentKey<T> attachmentKey) throws IllegalArgumentException { Assert.checkNotNullParam("proxy", proxy); Assert.checkNotNullParam("attachmentKey", attachmentKey); return EJBInvocationHandler.forProxy(proxy).getAttachment(attachmentKey); }
EJBClientInvocationContext(final EJBInvocationHandler<?> invocationHandler, final EJBClientContext ejbClientContext, final Object invokedProxy, final Object[] parameters, final EJBProxyInformation.ProxyMethodInfo methodInfo, final int allowedRetries, final Supplier<AuthenticationContext> authenticationContextSupplier) { super(invocationHandler.getLocator(), ejbClientContext); this.invocationHandler = invocationHandler; authenticationContext = authenticationContextSupplier != null ? authenticationContextSupplier.get() : AuthenticationContext.captureCurrent(); this.invokedProxy = invokedProxy; this.parameters = parameters; this.methodInfo = methodInfo; long timeout = invocationHandler.getInvocationTimeout(); if (timeout == -1) { timeout = ejbClientContext.getInvocationTimeout(); } this.timeout = timeout; remainingRetries = allowedRetries; interceptorList = getClientContext().getInterceptors(getViewClass(), getInvokedMethod()); }
/** * Convert a non-stateful proxy to be stateful. If the proxy was already stateful and the session ID matches, the * proxy is unchanged. If the proxy was otherwise already stateful, an exception is thrown. Subsequent calls to * {@link #getLocatorFor(Object)} for the given proxy will return the updated locator. * * @param proxy the proxy to convert (must not be {@code null}) * @param sessionID the session ID to use for the stateful locator (must not be {@code null}) * @throws IllegalArgumentException if the given proxy is not a valid client proxy instance, or the proxy is already * stateful with a different session ID */ public static void convertToStateful(Object proxy, SessionID sessionID) throws IllegalArgumentException { Assert.checkNotNullParam("proxy", proxy); Assert.checkNotNullParam("sessionID", sessionID); EJBInvocationHandler.forProxy(proxy).setSessionID(sessionID); }
/** * Set a proxy attachment. * * @param proxy the proxy (must not be {@code null}) * @param attachmentKey the attachment key to use (must not be {@code null}) * @param newValue the new value to set (must not be {@code null}) * @param <T> the value type * @return the previous attachment value or {@code null} if the attachment previously did not exist * @throws IllegalArgumentException if a required parameter is {@code null} or if the object is not a valid EJB client proxy */ public static <T> T putProxyAttachment(Object proxy, AttachmentKey<T> attachmentKey, T newValue) throws IllegalArgumentException { Assert.checkNotNullParam("proxy", proxy); Assert.checkNotNullParam("attachmentKey", attachmentKey); Assert.checkNotNullParam("newValue", newValue); return EJBInvocationHandler.forProxy(proxy).putAttachment(attachmentKey, newValue); }
/** * Set a proxy attachment if it is not already set. * * @param proxy the proxy (must not be {@code null}) * @param attachmentKey the attachment key to use (must not be {@code null}) * @param newValue the new value to set (must not be {@code null}) * @param <T> the value type * @return the previous attachment value or {@code null} if the attachment previously did not exist * @throws IllegalArgumentException if a required parameter is {@code null} or if the object is not a valid EJB client proxy */ public static <T> T putProxyAttachmentIfAbsent(Object proxy, AttachmentKey<T> attachmentKey, T newValue) throws IllegalArgumentException { Assert.checkNotNullParam("proxy", proxy); Assert.checkNotNullParam("attachmentKey", attachmentKey); Assert.checkNotNullParam("newValue", newValue); return EJBInvocationHandler.forProxy(proxy).putAttachmentIfAbsent(attachmentKey, newValue); }
/** * Change the weak affinity of a proxy. All subsequent invocations against the proxy will use the new affinity. * * @param proxy the proxy (may not be {@code null}) * @param newAffinity the new affinity (may not be {@code null}) * @throws IllegalArgumentException if the given proxy is not a valid client proxy instance * @throws SecurityException if a security manager is present and the caller does not have the {@code changeWeakAffinity} {@link EJBClientPermission} */ public static void setWeakAffinity(Object proxy, Affinity newAffinity) throws IllegalArgumentException, SecurityException { Assert.checkNotNullParam("proxy", proxy); Assert.checkNotNullParam("newAffinity", newAffinity); final SecurityManager sm = System.getSecurityManager(); if (sm != null) { sm.checkPermission(EJBClientPermission.CHANGE_WEAK_AFFINITY); } EJBInvocationHandler.forProxy(proxy).setWeakAffinity(newAffinity); }
/** * Change the strong affinity of a proxy. All subsequent invocations against the proxy will use the new affinity. * Subsequent calls to {@link #getLocatorFor(Object)} for the given proxy will return the updated locator. * * @param proxy the proxy (may not be {@code null}) * @param newAffinity the new affinity (may not be {@code null}) * @throws IllegalArgumentException if the given proxy is not a valid client proxy instance * @throws SecurityException if a security manager is present and the caller does not have the {@code changeStrongAffinity} {@link EJBClientPermission} */ public static void setStrongAffinity(Object proxy, Affinity newAffinity) throws IllegalArgumentException, SecurityException { Assert.checkNotNullParam("proxy", proxy); Assert.checkNotNullParam("newAffinity", newAffinity); final SecurityManager sm = System.getSecurityManager(); if (sm != null) { sm.checkPermission(EJBClientPermission.CHANGE_STRONG_AFFINITY); } EJBInvocationHandler.forProxy(proxy).setStrongAffinity(newAffinity); }
/** * Get an asynchronous view of a proxy. Any {@code void} method on the proxy will be invoked fully asynchronously * without a server round-trip delay. Any method which returns a {@link java.util.concurrent.Future Future} will * continue to be asynchronous. Any other method invoked on the returned proxy will return {@code null} (the future * result can be acquired by wrapping the remote call with {@link #getFutureResult(Object)} or by using {@link #getFutureResult()}). * If an asynchronous view is passed in, the same view is returned. * * @param proxy the proxy interface instance * @param <T> the proxy type * @return the asynchronous view * @throws IllegalArgumentException if the given object is not a valid proxy */ @SuppressWarnings("unchecked") public static <T> T asynchronous(final T proxy) throws IllegalArgumentException { final InvocationHandler invocationHandler = Proxy.getInvocationHandler(proxy); if (invocationHandler instanceof EJBInvocationHandler) { final EJBInvocationHandler<?> remoteInvocationHandler = (EJBInvocationHandler<?>) invocationHandler; // determine proxy "type", return existing instance if it's already async if (remoteInvocationHandler.isAsyncHandler()) { return proxy; } else { return (T) Proxy.newProxyInstance(proxy.getClass().getClassLoader(), proxy.getClass().getInterfaces(), remoteInvocationHandler.getAsyncHandler()); } } else { throw log.unknownProxy(proxy); } }
final InvocationHandler handler = Proxy.getInvocationHandler(args[0]); if (handler instanceof EJBInvocationHandler) { return Boolean.valueOf(equals(handler)); invocationContext.setLocator(locatorRef.get()); invocationContext.setBlockingCaller(true); invocationContext.setWeakAffinity(getWeakAffinity());
/** * Get a value attached to the proxy. * * @param key the attachment key * @param <T> the value type * @return the value, or {@code null} if there is none */ public <T> T getProxyAttachment(AttachmentKey<T> key) { return invocationHandler.getAttachment(key); }
/** * Perform a one-way asynchronous invocation by method locator on a proxy. Any return value is ignored. * * @param proxy the EJB proxy * @param methodLocator the method locator * @param args the invocation arguments * @param <T> the view type * @throws Exception if the invocation failed for some reason */ public static <T> void invokeOneWay(T proxy, EJBMethodLocator methodLocator, Object... args) throws Exception { final EJBInvocationHandler<? extends T> invocationHandler = EJBInvocationHandler.forProxy(proxy); final EJBProxyInformation.ProxyMethodInfo proxyMethodInfo = invocationHandler.getProxyMethodInfo(methodLocator); invocationHandler.invoke(proxy, proxyMethodInfo, args); }