Use Guava
Project: http://git-wip-us.apache.org/repos/asf/curator/repo Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/7fc3e650 Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/7fc3e650 Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/7fc3e650 Branch: refs/heads/master Commit: 7fc3e6507d5bcbb16c696906bed529c23df88e34 Parents: 58ecd64 Author: randgalt <randg...@apache.org> Authored: Mon Jan 12 16:03:35 2015 -0500 Committer: randgalt <randg...@apache.org> Committed: Mon Jan 12 16:03:35 2015 -0500 ---------------------------------------------------------------------- .../framework/CuratorFrameworkFactory.java | 73 +++++++++---------- .../framework/imps/CuratorFrameworkImpl.java | 77 ++++++++++---------- 2 files changed, 76 insertions(+), 74 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/curator/blob/7fc3e650/curator-framework/src/main/java/org/apache/curator/framework/CuratorFrameworkFactory.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/main/java/org/apache/curator/framework/CuratorFrameworkFactory.java b/curator-framework/src/main/java/org/apache/curator/framework/CuratorFrameworkFactory.java index 38fe911..ae26f2c 100644 --- a/curator-framework/src/main/java/org/apache/curator/framework/CuratorFrameworkFactory.java +++ b/curator-framework/src/main/java/org/apache/curator/framework/CuratorFrameworkFactory.java @@ -16,8 +16,10 @@ * specific language governing permissions and limitations * under the License. */ + package org.apache.curator.framework; +import com.google.common.collect.ImmutableList; import org.apache.curator.RetryPolicy; import org.apache.curator.ensemble.EnsembleProvider; import org.apache.curator.ensemble.fixed.FixedEnsembleProvider; @@ -34,7 +36,6 @@ import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; import java.net.InetAddress; import java.net.UnknownHostException; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurrent.ThreadFactory; @@ -45,23 +46,23 @@ import java.util.concurrent.TimeUnit; */ public class CuratorFrameworkFactory { - private static final int DEFAULT_SESSION_TIMEOUT_MS = Integer.getInteger("curator-default-session-timeout", 60 * 1000); - private static final int DEFAULT_CONNECTION_TIMEOUT_MS = Integer.getInteger("curator-default-connection-timeout", 15 * 1000); + private static final int DEFAULT_SESSION_TIMEOUT_MS = Integer.getInteger("curator-default-session-timeout", 60 * 1000); + private static final int DEFAULT_CONNECTION_TIMEOUT_MS = Integer.getInteger("curator-default-connection-timeout", 15 * 1000); - private static final byte[] LOCAL_ADDRESS = getLocalAddress(); + private static final byte[] LOCAL_ADDRESS = getLocalAddress(); - private static final CompressionProvider DEFAULT_COMPRESSION_PROVIDER = new GzipCompressionProvider(); - private static final DefaultZookeeperFactory DEFAULT_ZOOKEEPER_FACTORY = new DefaultZookeeperFactory(); - private static final DefaultACLProvider DEFAULT_ACL_PROVIDER = new DefaultACLProvider(); - private static final long DEFAULT_INACTIVE_THRESHOLD_MS = (int)TimeUnit.MINUTES.toMillis(3); - private static final int DEFAULT_CLOSE_WAIT_MS = (int)TimeUnit.SECONDS.toMillis(1); + private static final CompressionProvider DEFAULT_COMPRESSION_PROVIDER = new GzipCompressionProvider(); + private static final DefaultZookeeperFactory DEFAULT_ZOOKEEPER_FACTORY = new DefaultZookeeperFactory(); + private static final DefaultACLProvider DEFAULT_ACL_PROVIDER = new DefaultACLProvider(); + private static final long DEFAULT_INACTIVE_THRESHOLD_MS = (int)TimeUnit.MINUTES.toMillis(3); + private static final int DEFAULT_CLOSE_WAIT_MS = (int)TimeUnit.SECONDS.toMillis(1); /** * Return a new builder that builds a CuratorFramework * * @return new builder */ - public static Builder builder() + public static Builder builder() { return new Builder(); } @@ -69,9 +70,8 @@ public class CuratorFrameworkFactory /** * Create a new client with default session timeout and default connection timeout * - * * @param connectString list of servers to connect to - * @param retryPolicy retry policy to use + * @param retryPolicy retry policy to use * @return client */ public static CuratorFramework newClient(String connectString, RetryPolicy retryPolicy) @@ -82,11 +82,10 @@ public class CuratorFrameworkFactory /** * Create a new client * - * - * @param connectString list of servers to connect to - * @param sessionTimeoutMs session timeout + * @param connectString list of servers to connect to + * @param sessionTimeoutMs session timeout * @param connectionTimeoutMs connection timeout - * @param retryPolicy retry policy to use + * @param retryPolicy retry policy to use * @return client */ public static CuratorFramework newClient(String connectString, int sessionTimeoutMs, int connectionTimeoutMs, RetryPolicy retryPolicy) @@ -101,21 +100,21 @@ public class CuratorFrameworkFactory public static class Builder { - private EnsembleProvider ensembleProvider; - private int sessionTimeoutMs = DEFAULT_SESSION_TIMEOUT_MS; - private int connectionTimeoutMs = DEFAULT_CONNECTION_TIMEOUT_MS; - private int maxCloseWaitMs = DEFAULT_CLOSE_WAIT_MS; - private RetryPolicy retryPolicy; - private ThreadFactory threadFactory = null; - private String namespace; - private String authScheme = null; - private byte[] authValue = null; - private List<AuthInfo> authInfos = null; - private byte[] defaultData = LOCAL_ADDRESS; + private EnsembleProvider ensembleProvider; + private int sessionTimeoutMs = DEFAULT_SESSION_TIMEOUT_MS; + private int connectionTimeoutMs = DEFAULT_CONNECTION_TIMEOUT_MS; + private int maxCloseWaitMs = DEFAULT_CLOSE_WAIT_MS; + private RetryPolicy retryPolicy; + private ThreadFactory threadFactory = null; + private String namespace; + private String authScheme = null; + private byte[] authValue = null; + private List<AuthInfo> authInfos = null; + private byte[] defaultData = LOCAL_ADDRESS; private CompressionProvider compressionProvider = DEFAULT_COMPRESSION_PROVIDER; - private ZookeeperFactory zookeeperFactory = DEFAULT_ZOOKEEPER_FACTORY; - private ACLProvider aclProvider = DEFAULT_ACL_PROVIDER; - private boolean canBeReadOnly = false; + private ZookeeperFactory zookeeperFactory = DEFAULT_ZOOKEEPER_FACTORY; + private ACLProvider aclProvider = DEFAULT_ACL_PROVIDER; + private boolean canBeReadOnly = false; /** * Apply the current values and build a new CuratorFramework @@ -147,7 +146,7 @@ public class CuratorFrameworkFactory * are limited. Further, the connection will be closed after <code>inactiveThresholdMs</code> milliseconds of inactivity. * * @param inactiveThreshold number of milliseconds of inactivity to cause connection close - * @param unit threshold unit + * @param unit threshold unit * @return temp instance */ public CuratorTempFramework buildTemp(long inactiveThreshold, TimeUnit unit) @@ -159,10 +158,10 @@ public class CuratorFrameworkFactory * Add connection authorization * * @param scheme the scheme - * @param auth the auth bytes + * @param auth the auth bytes * @return this */ - public Builder authorization(String scheme, byte[] auth) + public Builder authorization(String scheme, byte[] auth) { this.authScheme = scheme; this.authValue = (auth != null) ? Arrays.copyOf(auth, auth.length) : null; @@ -172,15 +171,15 @@ public class CuratorFrameworkFactory /** * Add connection authorization. The supplied authInfos are appended to those added via call to * {@link #authorization(java.lang.String, byte[])} for backward compatibility. - * + * <p/> * Subsequent calls to this method overwrite the prior calls. * * @param authInfos list of {@link AuthInfo} objects with scheme and auth * @return this */ - public Builder authorization(List<AuthInfo> authInfos) { - this.authInfos = new ArrayList<AuthInfo>(authInfos.size()); - this.authInfos.addAll(authInfos); + public Builder authorization(List<AuthInfo> authInfos) + { + this.authInfos = ImmutableList.copyOf(authInfos); return this; } http://git-wip-us.apache.org/repos/asf/curator/blob/7fc3e650/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java b/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java index c38617d..e8785be 100644 --- a/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java +++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java @@ -22,7 +22,7 @@ package org.apache.curator.framework.imps; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Function; import com.google.common.base.Preconditions; - +import com.google.common.collect.ImmutableList; import org.apache.curator.CuratorConnectionLossException; import org.apache.curator.CuratorZookeeperClient; import org.apache.curator.RetryLoop; @@ -47,8 +47,6 @@ import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurrent.BlockingQueue; @@ -122,19 +120,24 @@ public class CuratorFrameworkImpl implements CuratorFramework byte[] builderDefaultData = builder.getDefaultData(); defaultData = (builderDefaultData != null) ? Arrays.copyOf(builderDefaultData, builderDefaultData.length) : new byte[0]; + authInfos = buildAuths(builder); - authInfos = new ArrayList<AuthInfo>(); + failedDeleteManager = new FailedDeleteManager(this); + namespaceFacadeCache = new NamespaceFacadeCache(this); + } + + private List<AuthInfo> buildAuths(CuratorFrameworkFactory.Builder builder) + { + ImmutableList.Builder<AuthInfo> builder1 = ImmutableList.builder(); if ( builder.getAuthScheme() != null ) { - authInfos.add(new AuthInfo(builder.getAuthScheme(), builder.getAuthValue())); + builder1.add(new AuthInfo(builder.getAuthScheme(), builder.getAuthValue())); } if ( builder.getAuthInfos() != null ) { - authInfos.addAll(builder.getAuthInfos()); + builder1.addAll(builder.getAuthInfos()); } - - failedDeleteManager = new FailedDeleteManager(this); - namespaceFacadeCache = new NamespaceFacadeCache(this); + return builder1.build(); } private ZookeeperFactory makeZookeeperFactory(final ZookeeperFactory actualZookeeperFactory) @@ -145,7 +148,7 @@ public class CuratorFrameworkImpl implements CuratorFramework public ZooKeeper newZooKeeper(String connectString, int sessionTimeout, Watcher watcher, boolean canBeReadOnly) throws Exception { ZooKeeper zooKeeper = actualZookeeperFactory.newZooKeeper(connectString, sessionTimeout, watcher, canBeReadOnly); - for (AuthInfo auth : authInfos) + for ( AuthInfo auth : authInfos ) { zooKeeper.addAuthInfo(auth.getScheme(), auth.getAuth()); } @@ -251,14 +254,14 @@ public class CuratorFrameworkImpl implements CuratorFramework executorService = Executors.newFixedThreadPool(2, threadFactory); // 1 for listeners, 1 for background ops executorService.submit(new Callable<Object>() + { + @Override + public Object call() throws Exception { - @Override - public Object call() throws Exception - { - backgroundOperationsLoop(); - return null; - } - }); + backgroundOperationsLoop(); + return null; + } + }); } catch ( Exception e ) { @@ -273,22 +276,22 @@ public class CuratorFrameworkImpl implements CuratorFramework if ( state.compareAndSet(CuratorFrameworkState.STARTED, CuratorFrameworkState.STOPPED) ) { listeners.forEach(new Function<CuratorListener, Void>() + { + @Override + public Void apply(CuratorListener listener) { - @Override - public Void apply(CuratorListener listener) + CuratorEvent event = new CuratorEventImpl(CuratorFrameworkImpl.this, CuratorEventType.CLOSING, 0, null, null, null, null, null, null, null, null); + try { - CuratorEvent event = new CuratorEventImpl(CuratorFrameworkImpl.this, CuratorEventType.CLOSING, 0, null, null, null, null, null, null, null, null); - try - { - listener.eventReceived(CuratorFrameworkImpl.this, event); - } - catch ( Exception e ) - { - log.error("Exception while sending Closing event", e); - } - return null; + listener.eventReceived(CuratorFrameworkImpl.this, event); } - }); + catch ( Exception e ) + { + log.error("Exception while sending Closing event", e); + } + return null; + } + }); if ( executorService != null ) { @@ -550,14 +553,14 @@ public class CuratorFrameworkImpl implements CuratorFramework final String localReason = reason; unhandledErrorListeners.forEach(new Function<UnhandledErrorListener, Void>() + { + @Override + public Void apply(UnhandledErrorListener listener) { - @Override - public Void apply(UnhandledErrorListener listener) - { - listener.unhandledError(localReason, e); - return null; - } - }); + listener.unhandledError(localReason, e); + return null; + } + }); if ( debugUnhandledErrorListener != null ) {