11package io .ably .lib .realtime ;
22
3+ import java .lang .reflect .InvocationTargetException ;
34import java .util .ArrayList ;
45import java .util .HashMap ;
56import java .util .List ;
67import java .util .Map ;
78
9+ import io .ably .lib .network .HttpEngineFactory ;
10+ import io .ably .lib .objects .LiveObjects ;
11+ import io .ably .lib .objects .LiveObjectsPlugin ;
812import io .ably .lib .rest .AblyRest ;
913import io .ably .lib .rest .Auth ;
1014import io .ably .lib .transport .ConnectionManager ;
@@ -40,6 +44,13 @@ public class AblyRealtime extends AblyRest {
4044 */
4145 public final Channels channels ;
4246
47+ /**
48+ * A nullable reference to the LiveObjects plugin.
49+ * <p>
50+ * This field is initialized only if the LiveObjects plugin is present in the classpath.
51+ */
52+ private final LiveObjectsPlugin liveObjectsPlugin ;
53+
4354 /**
4455 * Constructs a Realtime client object using an Ably API key or token string.
4556 * <p>
@@ -72,6 +83,8 @@ public AblyRealtime(ClientOptions options) throws AblyException {
7283 }
7384 }
7485
86+ liveObjectsPlugin = tryInitializeLiveObjectsPlugin ();
87+
7588 if (options .autoConnect ) connection .connect ();
7689 }
7790
@@ -168,6 +181,16 @@ public interface Channels extends ReadOnlyMap<String, Channel> {
168181 void release (String channelName );
169182 }
170183
184+ private LiveObjectsPlugin tryInitializeLiveObjectsPlugin () {
185+ try {
186+ Class <?> liveObjectsImplementation = Class .forName ("io.ably.lib.objects.DefaultLiveObjectsPlugin" );
187+ return (LiveObjectsPlugin ) liveObjectsImplementation .getDeclaredConstructor ().newInstance ();
188+ } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException |
189+ InvocationTargetException e ) {
190+ return null ;
191+ }
192+ }
193+
171194 private class InternalChannels extends InternalMap <String , Channel > implements Channels , ConnectionManager .Channels {
172195 /**
173196 * Get the named channel; if it does not already exist,
@@ -187,7 +210,7 @@ public Channel get(final String channelName, final ChannelOptions channelOptions
187210 // We're not using computeIfAbsent because that requires Java 1.8.
188211 // Hence there's the slight inefficiency of creating newChannel when it may not be
189212 // needed because there is an existingChannel.
190- final Channel newChannel = new Channel (AblyRealtime .this , channelName , channelOptions );
213+ final Channel newChannel = new Channel (AblyRealtime .this , channelName , channelOptions , liveObjectsPlugin );
191214 final Channel existingChannel = map .putIfAbsent (channelName , newChannel );
192215
193216 if (existingChannel != null ) {
0 commit comments