|
| 1 | +package io.ably.lib.object; |
| 2 | + |
| 3 | +import io.ably.lib.object.adapter.AblyClientAdapter; |
| 4 | +import io.ably.lib.object.adapter.Adapter; |
| 5 | +import io.ably.lib.objects.RealtimeObjects; |
| 6 | +import io.ably.lib.realtime.AblyRealtime; |
| 7 | +import io.ably.lib.realtime.ChannelState; |
| 8 | +import io.ably.lib.types.ProtocolMessage; |
| 9 | +import io.ably.lib.util.Log; |
| 10 | +import org.jetbrains.annotations.NotNull; |
| 11 | +import org.jetbrains.annotations.Nullable; |
| 12 | + |
| 13 | +import java.lang.reflect.InvocationTargetException; |
| 14 | + |
| 15 | +/** |
| 16 | + * The LiveObjectsPlugin interface provides a mechanism for managing and interacting with |
| 17 | + * live data objects in a real-time environment. It allows for the retrieval, disposal, and |
| 18 | + * management of Objects instances associated with specific channel names. |
| 19 | + */ |
| 20 | +public interface LiveObjectsPlugin { |
| 21 | + |
| 22 | + /** |
| 23 | + * Retrieves an instance of RealtimeObjects associated with the specified channel name. |
| 24 | + * This method ensures that a RealtimeObjects instance is available for the given channel, |
| 25 | + * creating one if it does not already exist. |
| 26 | + * |
| 27 | + * @param channelName the name of the channel for which the RealtimeObjects instance is to be retrieved. |
| 28 | + * @return the RealtimeObjects instance associated with the specified channel name. |
| 29 | + */ |
| 30 | + @NotNull |
| 31 | + RealtimeObjects getInstance(@NotNull String channelName); |
| 32 | + |
| 33 | + /** |
| 34 | + * Handles a protocol message. |
| 35 | + * This method is invoked whenever a protocol message is received, allowing the implementation |
| 36 | + * to process the message and take appropriate actions. |
| 37 | + * |
| 38 | + * @param message the protocol message to handle. |
| 39 | + */ |
| 40 | + void handle(@NotNull ProtocolMessage message); |
| 41 | + |
| 42 | + /** |
| 43 | + * Handles state changes for a specific channel. |
| 44 | + * This method is invoked whenever a channel's state changes, allowing the implementation |
| 45 | + * to update the RealtimeObjects instances accordingly based on the new state and presence of objects. |
| 46 | + * |
| 47 | + * @param channelName the name of the channel whose state has changed. |
| 48 | + * @param state the new state of the channel. |
| 49 | + * @param hasObjects flag indicates whether the channel has any associated objects. |
| 50 | + */ |
| 51 | + void handleStateChange(@NotNull String channelName, @NotNull ChannelState state, boolean hasObjects); |
| 52 | + |
| 53 | + /** |
| 54 | + * Disposes of the RealtimeObjects instance associated with the specified channel name. |
| 55 | + * This method removes the RealtimeObjects instance for the given channel, releasing any |
| 56 | + * resources associated with it. |
| 57 | + * This is invoked when ablyRealtimeClient.channels.release(channelName) is called |
| 58 | + * |
| 59 | + * @param channelName the name of the channel whose RealtimeObjects instance is to be removed. |
| 60 | + */ |
| 61 | + void dispose(@NotNull String channelName); |
| 62 | + |
| 63 | + /** |
| 64 | + * Disposes of the plugin instance and all underlying resources. |
| 65 | + * This is invoked when ablyRealtimeClient.close() is called |
| 66 | + */ |
| 67 | + void dispose(); |
| 68 | + |
| 69 | + /** |
| 70 | + * Attempts to initialize the LiveObjects plugin by reflectively loading its implementation |
| 71 | + * from the classpath. Returns a new plugin instance on every successful invocation, or |
| 72 | + * {@code null} if the LiveObjects plugin is not present in the classpath. |
| 73 | + * |
| 74 | + * @param ablyRealtime the AblyRealtime client used to build the adapter the plugin runs against. |
| 75 | + * @return a new {@link LiveObjectsPlugin} instance, or {@code null} if the plugin is unavailable. |
| 76 | + */ |
| 77 | + @Nullable |
| 78 | + static LiveObjectsPlugin tryInitialize(@NotNull AblyRealtime ablyRealtime) { |
| 79 | + return Factory.create(ablyRealtime); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Reflectively constructs the LiveObjects plugin implementation. Lives in a nested class so the |
| 84 | + * implementation-class name stays {@code private} (interface fields are forced {@code public}), |
| 85 | + * mirroring {@link io.ably.lib.object.serialization.ObjectSerializer.Holder}. Unlike {@code Holder} |
| 86 | + * this is stateless: {@link #create} returns a new instance on every call. |
| 87 | + */ |
| 88 | + final class Factory { |
| 89 | + private static final String TAG = LiveObjectsPlugin.Factory.class.getName(); |
| 90 | + private static final String IMPLEMENTATION_CLASS = "io.ably.lib.object.DefaultLiveObjectsPlugin"; |
| 91 | + |
| 92 | + private Factory() {} |
| 93 | + |
| 94 | + @Nullable |
| 95 | + static LiveObjectsPlugin create(@NotNull AblyRealtime ablyRealtime) { |
| 96 | + try { |
| 97 | + Class<?> objectsImplementation = Class.forName(IMPLEMENTATION_CLASS); |
| 98 | + AblyClientAdapter adapter = new Adapter(ablyRealtime); |
| 99 | + return (LiveObjectsPlugin) objectsImplementation |
| 100 | + .getDeclaredConstructor(AblyClientAdapter.class) |
| 101 | + .newInstance(adapter); |
| 102 | + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | |
| 103 | + InvocationTargetException e) { |
| 104 | + Log.i(TAG, "LiveObjects plugin not found in classpath. LiveObjects functionality will not be available.", e); |
| 105 | + return null; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments