Search in sources :

Example 1 with ZooKeeperRetry

use of org.apache.zookeeper.retry.ZooKeeperRetry in project zookeeper by apache.

the class ZooInspectorManagerImpl method connect.

/*
     * (non-Javadoc)
     * 
     * @see
     * org.apache.zookeeper.inspector.manager.ZooInspectorManager#connect(java
     * .util.Properties)
     */
public boolean connect(Properties connectionProps) {
    try {
        if (this.zooKeeper == null) {
            String connectString = connectionProps.getProperty(CONNECT_STRING);
            String sessionTimeout = connectionProps.getProperty(SESSION_TIMEOUT);
            String encryptionManager = connectionProps.getProperty(DATA_ENCRYPTION_MANAGER);
            String authScheme = connectionProps.getProperty(AUTH_SCHEME_KEY);
            String authData = connectionProps.getProperty(AUTH_DATA_KEY);
            if (connectString == null || sessionTimeout == null) {
                throw new IllegalArgumentException("Both connect string and session timeout are required.");
            }
            if (encryptionManager == null) {
                this.encryptionManager = new BasicDataEncryptionManager();
            } else {
                Class<?> clazz = Class.forName(encryptionManager);
                if (Arrays.asList(clazz.getInterfaces()).contains(DataEncryptionManager.class)) {
                    this.encryptionManager = (DataEncryptionManager) Class.forName(encryptionManager).newInstance();
                } else {
                    throw new IllegalArgumentException("Data encryption manager must implement DataEncryptionManager interface");
                }
            }
            this.connectString = connectString;
            this.sessionTimeout = Integer.valueOf(sessionTimeout);
            this.zooKeeper = new ZooKeeperRetry(connectString, Integer.valueOf(sessionTimeout), new Watcher() {

                public void process(WatchedEvent event) {
                    if (event.getState() == KeeperState.Expired) {
                        connected = false;
                    }
                }
            });
            if (authData != null && authData.length() > 0) {
                this.zooKeeper.addAuthInfo(authScheme, authData.getBytes());
            }
            ((ZooKeeperRetry) this.zooKeeper).setRetryLimit(10);
            connected = ((ZooKeeperRetry) this.zooKeeper).testConnection();
        }
    } catch (Exception e) {
        connected = false;
        e.printStackTrace();
    }
    if (!connected) {
        disconnect();
    } else {
        this.nodesCache = new NodesCache(zooKeeper);
    }
    return connected;
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeperRetry(org.apache.zookeeper.retry.ZooKeeperRetry) Watcher(org.apache.zookeeper.Watcher) BasicDataEncryptionManager(org.apache.zookeeper.inspector.encryption.BasicDataEncryptionManager) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 KeeperException (org.apache.zookeeper.KeeperException)1 WatchedEvent (org.apache.zookeeper.WatchedEvent)1 Watcher (org.apache.zookeeper.Watcher)1 BasicDataEncryptionManager (org.apache.zookeeper.inspector.encryption.BasicDataEncryptionManager)1 ZooKeeperRetry (org.apache.zookeeper.retry.ZooKeeperRetry)1