public boolean isSocketInterceptorEnabled() {
   final SocketInterceptorConfig socketInterceptorConfig = ioService.getSocketInterceptorConfig();
   if (socketInterceptorConfig != null && socketInterceptorConfig.isEnabled()) {
     return true;
   }
   return false;
 }
  @Test
  public void testNetworkConfig() {
    final ClientNetworkConfig networkConfig = clientConfig.getNetworkConfig();
    assertEquals(2, networkConfig.getConnectionAttemptLimit());
    assertEquals(2, networkConfig.getAddresses().size());
    assertTrue(networkConfig.getAddresses().contains("127.0.0.1"));
    assertTrue(networkConfig.getAddresses().contains("127.0.0.2"));

    assertTrue(networkConfig.isSmartRouting());
    assertTrue(networkConfig.isRedoOperation());

    final SocketInterceptorConfig socketInterceptorConfig =
        networkConfig.getSocketInterceptorConfig();
    assertTrue(socketInterceptorConfig.isEnabled());
    assertEquals(
        "com.hazelcast.examples.MySocketInterceptor", socketInterceptorConfig.getClassName());
    assertEquals("bar", socketInterceptorConfig.getProperty("foo"));

    final ClientAwsConfig awsConfig = networkConfig.getAwsConfig();
    assertTrue(awsConfig.isEnabled());
    assertTrue(awsConfig.isInsideAws());
    assertEquals("TEST_ACCESS_KEY", awsConfig.getAccessKey());
    assertEquals("TEST_ACCESS_KEY", awsConfig.getAccessKey());
    assertEquals("TEST_SECRET_KEY", awsConfig.getSecretKey());
    assertEquals("us-east-1", awsConfig.getRegion());
    assertEquals("ec2.amazonaws.com", awsConfig.getHostHeader());
    assertEquals("type", awsConfig.getTagKey());
    assertEquals("hz-nodes", awsConfig.getTagValue());
    assertEquals(11, awsConfig.getConnectionTimeoutSeconds());
  }
 @Test
 public void testSocketInterceptorConfig() {
   SocketInterceptorConfig socketInterceptorConfig =
       config.getNetworkConfig().getSocketInterceptorConfig();
   assertNotNull(socketInterceptorConfig);
   assertFalse(socketInterceptorConfig.isEnabled());
   assertEquals(DummySocketInterceptor.class.getName(), socketInterceptorConfig.getClassName());
   assertEquals(socketInterceptor, socketInterceptorConfig.getImplementation());
 }
 @Test(expected = RuntimeException.class, timeout = 120000)
 public void testFailingSocketInterceptor() {
   Config config = new Config();
   config.setProperty(GroupProperties.PROP_MAX_JOIN_SECONDS, "3");
   SocketInterceptorConfig sic = new SocketInterceptorConfig();
   MySocketInterceptor mySocketInterceptor = new MySocketInterceptor(false);
   sic.setImplementation(mySocketInterceptor);
   config.getNetworkConfig().setSocketInterceptorConfig(sic);
   HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
   HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config);
 }
 @Test(timeout = 120000)
 public void testSuccessfulSocketInterceptor() {
   Config config = new Config();
   SocketInterceptorConfig sic = new SocketInterceptorConfig();
   MySocketInterceptor mySocketInterceptor = new MySocketInterceptor(true);
   sic.setImplementation(mySocketInterceptor);
   config.getNetworkConfig().setSocketInterceptorConfig(sic);
   HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
   HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config);
   HazelcastInstance h3 = Hazelcast.newHazelcastInstance(config);
   HazelcastInstance h4 = Hazelcast.newHazelcastInstance(config);
   int count = 1000;
   for (int i = 0; i < count; i++) {
     h1.getMap("default").put(i, "value" + i);
     h2.getMap("default").put(i, "value" + i);
     h3.getMap("default").put(i, "value" + i);
     h4.getMap("default").put(i, "value" + i);
   }
   assertEquals(4, h4.getCluster().getMembers().size());
   assertTrue(mySocketInterceptor.getAcceptCallCount() >= 6);
   assertTrue(mySocketInterceptor.getConnectCallCount() >= 6);
   assertEquals(4, mySocketInterceptor.getInitCallCount());
   assertEquals(0, mySocketInterceptor.getAcceptFailureCount());
   assertEquals(0, mySocketInterceptor.getConnectFailureCount());
   ClientConfig clientConfig = new ClientConfig();
   clientConfig.setGroupConfig(new GroupConfig("dev", "dev-pass")).addAddress("localhost");
   MySocketInterceptor myClientSocketInterceptor = new MySocketInterceptor(true);
   clientConfig.setSocketInterceptor(myClientSocketInterceptor);
   HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
   for (int i = 0; i < count; i++) {
     client.getMap("default").put(i, "value" + i);
   }
   assertTrue(mySocketInterceptor.getAcceptCallCount() >= 7);
   assertTrue(mySocketInterceptor.getConnectCallCount() >= 6);
   assertEquals(1, myClientSocketInterceptor.getConnectCallCount());
   assertEquals(0, myClientSocketInterceptor.getAcceptCallCount());
   assertEquals(0, mySocketInterceptor.getAcceptFailureCount());
   assertEquals(0, mySocketInterceptor.getConnectFailureCount());
   assertEquals(0, myClientSocketInterceptor.getAcceptFailureCount());
   assertEquals(0, myClientSocketInterceptor.getConnectFailureCount());
 }
  public SmartClientConnectionManager(
      HazelcastClient client, Authenticator authenticator, LoadBalancer loadBalancer) {
    this.authenticator = authenticator;
    this.client = client;
    ClientConfig config = client.getClientConfig();
    router = new Router(loadBalancer);

    // init socketInterceptor
    SocketInterceptorConfig sic = config.getSocketInterceptorConfig();
    if (sic != null && sic.isEnabled()) {
      SocketInterceptor implementation = (SocketInterceptor) sic.getImplementation();
      if (implementation == null && sic.getClassName() != null) {
        try {
          implementation = (SocketInterceptor) Class.forName(sic.getClassName()).newInstance();
        } catch (Throwable e) {
          logger.severe("SocketInterceptor class cannot be instantiated!" + sic.getClassName(), e);
        }
      }
      if (implementation != null) {
        if (!(implementation instanceof MemberSocketInterceptor)) {
          logger.severe(
              "SocketInterceptor must be instance of " + MemberSocketInterceptor.class.getName());
          implementation = null;
        } else {
          logger.info("SocketInterceptor is enabled");
        }
      }
      if (implementation != null) {
        socketInterceptor = implementation;
        socketInterceptor.init(sic.getProperties());
      } else {
        socketInterceptor = null;
      }
    } else {
      socketInterceptor = null;
    }

    poolSize = config.getConnectionPoolSize();
    int connectionTimeout = config.getConnectionTimeout();
    heartbeat =
        new HeartBeatChecker(
            connectionTimeout,
            client.getSerializationService(),
            client.getClientExecutionService());
    socketOptions = config.getSocketOptions();
  }
  private SocketInterceptor initSocketInterceptor(SocketInterceptorConfig sic) {
    SocketInterceptor implementation = null;
    if (sic != null && sic.isEnabled()) {
      implementation = (SocketInterceptor) sic.getImplementation();
      if (implementation == null && sic.getClassName() != null) {
        try {
          implementation = (SocketInterceptor) Class.forName(sic.getClassName()).newInstance();
        } catch (Throwable e) {
          LOGGER.severe("SocketInterceptor class cannot be instantiated!" + sic.getClassName(), e);
        }
      }
    }

    if (implementation != null) {
      implementation.init(sic.getProperties());
    }
    return implementation;
  }