|
| 1 | +package io.getstream.chat.java.services.framework; |
| 2 | + |
| 3 | +import retrofit2.Retrofit; |
| 4 | +import java.util.concurrent.atomic.AtomicReference; |
| 5 | + |
| 6 | +/** |
| 7 | + * Smart user-aware service factory with automatic fallback mechanism. |
| 8 | + * <p> |
| 9 | + * This implementation attempts to use {@link UserServiceFactoryProxy} (more efficient) |
| 10 | + * and automatically falls back to {@link UserServiceFactoryTagging} if reflection fails |
| 11 | + * or the Retrofit API has changed. |
| 12 | + * <p> |
| 13 | + * <b>Fallback Strategy:</b> |
| 14 | + * <ol> |
| 15 | + * <li>First attempt: Use proxy-based approach (reuses Retrofit instance)</li> |
| 16 | + * <li>On failure: Switch to tagging-based approach (more compatible)</li> |
| 17 | + * <li>Once switched: All subsequent calls use the fallback implementation</li> |
| 18 | + * </ol> |
| 19 | + * <p> |
| 20 | + * <b>Thread-safety:</b> Thread-safe. Uses atomic reference for fallback state tracking. |
| 21 | + * Multiple threads may attempt fallback simultaneously, but only one will win. |
| 22 | + */ |
| 23 | +final class UserServiceFactorySelector implements UserServiceFactory { |
| 24 | + |
| 25 | + private final UserServiceFactory proxyFactory; |
| 26 | + private final UserServiceFactory taggingFactory; |
| 27 | + private final AtomicReference<UserServiceFactory> activeFactory; |
| 28 | + |
| 29 | + /** |
| 30 | + * Constructs a new smart factory with fallback capability. |
| 31 | + * |
| 32 | + * @param retrofit the Retrofit instance to create services from |
| 33 | + */ |
| 34 | + public UserServiceFactorySelector(Retrofit retrofit) { |
| 35 | + this.proxyFactory = new UserServiceFactoryProxy(retrofit); |
| 36 | + this.taggingFactory = new UserServiceFactoryTagging(retrofit); |
| 37 | + |
| 38 | + // Verify proxy approach is viable before setting default |
| 39 | + UserServiceFactory defaultFactory = proxyFactory; |
| 40 | + try { |
| 41 | + // Check if we can access the rawCall field that UserTokenCallRewriter needs |
| 42 | + Class<?> retrofitCallClass = Class.forName("retrofit2.OkHttpCall"); |
| 43 | + retrofitCallClass.getDeclaredField("rawCall"); |
| 44 | + // If we get here, proxy should work |
| 45 | + } catch (Throwable e) { |
| 46 | + // Proxy approach won't work, use tagging as default |
| 47 | + defaultFactory = taggingFactory; |
| 48 | + } |
| 49 | + |
| 50 | + this.activeFactory = new AtomicReference<>(defaultFactory); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Creates a user-aware service instance with automatic fallback. |
| 55 | + * <p> |
| 56 | + * Attempts to use the proxy implementation first. If it fails (due to reflection issues, |
| 57 | + * API changes, or other errors), automatically switches to the tagging implementation |
| 58 | + * and retries. |
| 59 | + * |
| 60 | + * @param svcClass the Retrofit service interface class |
| 61 | + * @param userToken the user token to inject into all requests from this service |
| 62 | + * @param <TService> the service type |
| 63 | + * @return a service instance that injects the user token |
| 64 | + * @throws RuntimeException if both implementations fail |
| 65 | + */ |
| 66 | + @Override |
| 67 | + public <TService> TService create(Class<TService> svcClass, UserToken userToken) { |
| 68 | + UserServiceFactory factory = activeFactory.get(); |
| 69 | + |
| 70 | + try { |
| 71 | + return factory.create(svcClass, userToken); |
| 72 | + } catch (Throwable e) { |
| 73 | + // If we're already using the fallback, propagate the error |
| 74 | + if (factory == taggingFactory) { |
| 75 | + throw new RuntimeException("Failed to create service using fallback implementation", e); |
| 76 | + } |
| 77 | + |
| 78 | + // Switch to fallback and retry |
| 79 | + activeFactory.compareAndSet(proxyFactory, taggingFactory); |
| 80 | + |
| 81 | + // Retry with fallback |
| 82 | + try { |
| 83 | + return taggingFactory.create(svcClass, userToken); |
| 84 | + } catch (Throwable fallbackException) { |
| 85 | + throw new RuntimeException("Failed to create service with both implementations", fallbackException); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | +} |
| 91 | + |
0 commit comments