Repository: curator Updated Branches: refs/heads/CURATOR-397 34c594a8e -> 792aef209
renamed ModeledCuratorFramework so it isn't so long Project: http://git-wip-us.apache.org/repos/asf/curator/repo Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/792aef20 Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/792aef20 Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/792aef20 Branch: refs/heads/CURATOR-397 Commit: 792aef2094e215edf77bf836b0f4dc43bb2cb232 Parents: 34c594a Author: randgalt <[email protected]> Authored: Sun Apr 9 14:27:15 2017 -0500 Committer: randgalt <[email protected]> Committed: Sun Apr 9 14:27:15 2017 -0500 ---------------------------------------------------------------------- .../org/apache/curator/x/async/AsyncStage.java | 6 +- .../modeled/ModeledAsyncCuratorFramework.java | 166 ---------------- .../ModeledAsyncCuratorFrameworkBuilder.java | 192 ------------------- .../async/modeled/ModeledCuratorFramework.java | 166 ++++++++++++++++ .../modeled/ModeledCuratorFrameworkBuilder.java | 192 +++++++++++++++++++ .../ModeledAsyncCuratorFrameworkImpl.java | 186 ------------------ .../details/ModeledCuratorFrameworkImpl.java | 186 ++++++++++++++++++ .../src/site/confluence/index.confluence | 2 +- .../src/site/confluence/modeled.confluence | 10 +- .../TestModeledAsyncCuratorFramework.java | 102 ---------- .../modeled/TestModeledCuratorFramework.java | 102 ++++++++++ .../modeled/recipes/TestModeledCaches.java | 6 +- 12 files changed, 658 insertions(+), 658 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/curator/blob/792aef20/curator-x-async/src/main/java/org/apache/curator/x/async/AsyncStage.java ---------------------------------------------------------------------- diff --git a/curator-x-async/src/main/java/org/apache/curator/x/async/AsyncStage.java b/curator-x-async/src/main/java/org/apache/curator/x/async/AsyncStage.java index 9b63309..331cd16 100644 --- a/curator-x-async/src/main/java/org/apache/curator/x/async/AsyncStage.java +++ b/curator-x-async/src/main/java/org/apache/curator/x/async/AsyncStage.java @@ -34,9 +34,9 @@ public interface AsyncStage<T> extends CompletionStage<T> * </p> * * <p> - * Also, applies to {@link org.apache.curator.x.async.modeled.ModeledAsyncCuratorFramework} - * when {@link org.apache.curator.x.async.modeled.ModeledAsyncCuratorFrameworkBuilder#watched(WatchMode)} - * or {@link org.apache.curator.x.async.modeled.ModeledAsyncCuratorFrameworkBuilder#watched(WatchMode, java.util.function.UnaryOperator)} + * Also, applies to {@link org.apache.curator.x.async.modeled.ModeledCuratorFramework} + * when {@link org.apache.curator.x.async.modeled.ModeledCuratorFrameworkBuilder#watched(WatchMode)} + * or {@link org.apache.curator.x.async.modeled.ModeledCuratorFrameworkBuilder#watched(WatchMode, java.util.function.UnaryOperator)} * is used. * </p> * http://git-wip-us.apache.org/repos/asf/curator/blob/792aef20/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/ModeledAsyncCuratorFramework.java ---------------------------------------------------------------------- diff --git a/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/ModeledAsyncCuratorFramework.java b/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/ModeledAsyncCuratorFramework.java deleted file mode 100644 index 4b7e8e8..0000000 --- a/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/ModeledAsyncCuratorFramework.java +++ /dev/null @@ -1,166 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.curator.x.async.modeled; - -import com.google.common.collect.ImmutableSet; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.x.async.AsyncStage; -import org.apache.curator.x.async.api.CreateOption; -import org.apache.curator.x.async.api.DeleteOption; -import org.apache.zookeeper.data.Stat; -import java.util.Set; - -public interface ModeledAsyncCuratorFramework<T> -{ - Set<CreateOption> defaultCreateOptions = ImmutableSet.of(CreateOption.createParentsAsContainers, CreateOption.setDataIfExists); - Set<DeleteOption> defaultDeleteOptions = ImmutableSet.of(DeleteOption.guaranteed); - - /** - * Return a new ModeledAsyncCuratorFramework for the given path and serializer. The returned ModeledAsyncCuratorFramework - * is set to not watch ZNodes and uses {@link #defaultCreateOptions} and {@link #defaultDeleteOptions}. - * - * @param client Curator client - * @param path path to model - * @param serializer the model's serializer - * @return new Modeled Curator instance - */ - static <T> ModeledAsyncCuratorFramework<T> wrap(CuratorFramework client, ZPath path, ModelSerializer<T> serializer) - { - return builder(client, path, serializer).build(); - } - - /** - * Start a new ModeledAsyncCuratorFrameworkBuilder for the given path and serializer. The returned ModeledAsyncCuratorFrameworkBuilder - * is set to not watch ZNodes and uses {@link #defaultCreateOptions} and {@link #defaultDeleteOptions}, but you can change these - * with builder methods. - * - * @param client Curator client - * @param path path to model - * @param serializer the model's serializer - * @return builder - */ - static <T> ModeledAsyncCuratorFrameworkBuilder<T> builder(CuratorFramework client, ZPath path, ModelSerializer<T> serializer) - { - return new ModeledAsyncCuratorFrameworkBuilder<>(client, path, serializer) - .withCreateOptions(defaultCreateOptions) - .withDeleteOptions(defaultDeleteOptions); - } - - /** - * Returns the client that was originally passed to {@link #wrap(org.apache.curator.framework.CuratorFramework, ZPath, ModelSerializer)} or - * the builder. - * - * @return original client - */ - CuratorFramework unwrap(); - - /** - * Return a new Modeled Curator instance with all the same options but applying to the given child node of this Modeled Curator's - * path. E.g. if this Modeled Curator instance applies to "/a/b", calling <code>modeled.at("c")</code> returns an instance that applies to - * "/a/b/c". - * - * @param child child node. - * @return new Modeled Curator instance - */ - ModeledAsyncCuratorFramework<T> at(String child); - - /** - * Create (or update depending on build options) a ZNode at this instance's path with a serialized - * version of the given model - * - * @param model model to write - * @return AsyncStage - * @see org.apache.curator.x.async.AsyncStage - */ - AsyncStage<String> create(T model); - - /** - * Create (or update depending on build options) a ZNode at this instance's path with a serialized - * form of the given model - * - * @param model model to write - * @param storingStatIn the stat for the new ZNode is stored here - * @return AsyncStage - * @see org.apache.curator.x.async.AsyncStage - */ - AsyncStage<String> create(T model, Stat storingStatIn); - - /** - * Read the ZNode at this instance's path and deserialize into a model - * - * @return AsyncStage - * @see org.apache.curator.x.async.AsyncStage - */ - AsyncStage<T> read(); - - /** - * Read the ZNode at this instance's path and deserialize into a model - * - * @param storingStatIn the stat for the new ZNode is stored here - * @return AsyncStage - * @see org.apache.curator.x.async.AsyncStage - */ - AsyncStage<T> read(Stat storingStatIn); - - /** - * Update the ZNode at this instance's path with a serialized - * form of the given model passing "-1" for the update version - * - * @param model model to write - * @return AsyncStage - * @see org.apache.curator.x.async.AsyncStage - */ - AsyncStage<Stat> update(T model); - - /** - * Update the ZNode at this instance's path with a serialized - * form of the given model passing the given update version - * - * @param model model to write - * @param version update version to use - * @return AsyncStage - * @see org.apache.curator.x.async.AsyncStage - */ - AsyncStage<Stat> update(T model, int version); - - /** - * Check to see if the ZNode at this instance's path exists - * - * @return AsyncStage - * @see org.apache.curator.x.async.AsyncStage - */ - AsyncStage<Stat> checkExists(); - - /** - * Delete the ZNode at this instance's path passing -1 for the delete version - * - * @return AsyncStage - * @see org.apache.curator.x.async.AsyncStage - */ - AsyncStage<Void> delete(); - - /** - * Delete the ZNode at this instance's path passing the given delete version - * - * @param version update version to use - * @return AsyncStage - * @see org.apache.curator.x.async.AsyncStage - */ - AsyncStage<Void> delete(int version); -} http://git-wip-us.apache.org/repos/asf/curator/blob/792aef20/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/ModeledAsyncCuratorFrameworkBuilder.java ---------------------------------------------------------------------- diff --git a/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/ModeledAsyncCuratorFrameworkBuilder.java b/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/ModeledAsyncCuratorFrameworkBuilder.java deleted file mode 100644 index b7e83d3..0000000 --- a/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/ModeledAsyncCuratorFrameworkBuilder.java +++ /dev/null @@ -1,192 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.curator.x.async.modeled; - -import com.google.common.collect.ImmutableSet; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.api.CuratorEvent; -import org.apache.curator.framework.api.UnhandledErrorListener; -import org.apache.curator.x.async.WatchMode; -import org.apache.curator.x.async.api.CreateOption; -import org.apache.curator.x.async.api.DeleteOption; -import org.apache.curator.x.async.modeled.details.ModeledAsyncCuratorFrameworkImpl; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.WatchedEvent; -import org.apache.zookeeper.data.ACL; -import java.util.List; -import java.util.Objects; -import java.util.Set; -import java.util.function.UnaryOperator; - -public class ModeledAsyncCuratorFrameworkBuilder<T> -{ - private final CuratorFramework client; - private final ZPath path; - private final ModelSerializer<T> serializer; - private WatchMode watchMode; - private UnaryOperator<WatchedEvent> watcherFilter; - private UnhandledErrorListener unhandledErrorListener; - private UnaryOperator<CuratorEvent> resultFilter; - private CreateMode createMode; - private List<ACL> aclList; - private Set<CreateOption> createOptions; - private Set<DeleteOption> deleteOptions; - - /** - * Build a new ModeledAsyncCuratorFramework instance - * - * @return new ModeledAsyncCuratorFramework instance - */ - public ModeledAsyncCuratorFramework<T> build() - { - return new ModeledAsyncCuratorFrameworkImpl<>( - client, - path.fullPath(), - serializer, - watchMode, - watcherFilter, - unhandledErrorListener, - resultFilter, - createMode, - aclList, - createOptions, - deleteOptions - ); - } - - /** - * Add watchers as appropriate to the Modeled Curator's ZNode using - * {@link org.apache.curator.x.async.WatchMode#stateChangeAndSuccess} - * - * @return this for chaining - * @see org.apache.curator.x.async.AsyncStage#event() - */ - public ModeledAsyncCuratorFrameworkBuilder<T> watched() - { - this.watchMode = WatchMode.stateChangeAndSuccess; - return this; - } - - /** - * Add watchers as appropriate using the given watchMode to the Modeled Curator's ZNode - * - * @param watchMode watcher style - * @return this for chaining - * @see org.apache.curator.x.async.AsyncStage#event() - */ - public ModeledAsyncCuratorFrameworkBuilder<T> watched(WatchMode watchMode) - { - this.watchMode = watchMode; - return this; - } - - /** - * Add watchers as appropriate using the given watchMode and filter to the Modeled Curator's ZNode - * - * @param watchMode watcher style - * @param watcherFilter filter - * @return this for chaining - * @see org.apache.curator.x.async.AsyncStage#event() - */ - public ModeledAsyncCuratorFrameworkBuilder<T> watched(WatchMode watchMode, UnaryOperator<WatchedEvent> watcherFilter) - { - this.watchMode = watchMode; - this.watcherFilter = watcherFilter; - return this; - } - - /** - * Use the given unhandledErrorListener for operations on the Modeled Curator's ZNode - * - * @param unhandledErrorListener listener - * @return this for chaining - */ - public ModeledAsyncCuratorFrameworkBuilder<T> withUnhandledErrorListener(UnhandledErrorListener unhandledErrorListener) - { - this.unhandledErrorListener = unhandledErrorListener; - return this; - } - - /** - * Use the given result filter for operations on the Modeled Curator's ZNode - * - * @param resultFilter filter - * @return this for chaining - */ - public ModeledAsyncCuratorFrameworkBuilder<T> withResultFilter(UnaryOperator<CuratorEvent> resultFilter) - { - this.resultFilter = resultFilter; - return this; - } - - /** - * Use the given createMode for create operations on the Modeled Curator's ZNode - * - * @param createMode create mode - * @return this for chaining - */ - public ModeledAsyncCuratorFrameworkBuilder<T> withCreateMode(CreateMode createMode) - { - this.createMode = createMode; - return this; - } - - /** - * Use the given aclList for create operations on the Modeled Curator's ZNode - * - * @param aclList ACLs - * @return this for chaining - */ - public ModeledAsyncCuratorFrameworkBuilder<T> withAclList(List<ACL> aclList) - { - this.aclList = aclList; - return this; - } - - /** - * Use the given create options on the Modeled Curator's ZNode - * - * @param createOptions options - * @return this for chaining - */ - public ModeledAsyncCuratorFrameworkBuilder<T> withCreateOptions(Set<CreateOption> createOptions) - { - this.createOptions = (createOptions != null) ? ImmutableSet.copyOf(createOptions) : null; - return this; - } - - /** - * Use the given delete options on the Modeled Curator's ZNode - * - * @param deleteOptions options - * @return this for chaining - */ - public ModeledAsyncCuratorFrameworkBuilder<T> withDeleteOptions(Set<DeleteOption> deleteOptions) - { - this.deleteOptions = (deleteOptions != null) ? ImmutableSet.copyOf(deleteOptions) : null; - return this; - } - - ModeledAsyncCuratorFrameworkBuilder(CuratorFramework client, ZPath path, ModelSerializer<T> serializer) - { - this.client = Objects.requireNonNull(client, "client cannot be null"); - this.path = Objects.requireNonNull(path, "path cannot be null"); - this.serializer = Objects.requireNonNull(serializer, "serializer cannot be null"); - } -} http://git-wip-us.apache.org/repos/asf/curator/blob/792aef20/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/ModeledCuratorFramework.java ---------------------------------------------------------------------- diff --git a/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/ModeledCuratorFramework.java b/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/ModeledCuratorFramework.java new file mode 100644 index 0000000..249e505 --- /dev/null +++ b/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/ModeledCuratorFramework.java @@ -0,0 +1,166 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.curator.x.async.modeled; + +import com.google.common.collect.ImmutableSet; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.x.async.AsyncStage; +import org.apache.curator.x.async.api.CreateOption; +import org.apache.curator.x.async.api.DeleteOption; +import org.apache.zookeeper.data.Stat; +import java.util.Set; + +public interface ModeledCuratorFramework<T> +{ + Set<CreateOption> defaultCreateOptions = ImmutableSet.of(CreateOption.createParentsAsContainers, CreateOption.setDataIfExists); + Set<DeleteOption> defaultDeleteOptions = ImmutableSet.of(DeleteOption.guaranteed); + + /** + * Return a new ModeledCuratorFramework for the given path and serializer. The returned ModeledCuratorFramework + * is set to not watch ZNodes and uses {@link #defaultCreateOptions} and {@link #defaultDeleteOptions}. + * + * @param client Curator client + * @param path path to model + * @param serializer the model's serializer + * @return new Modeled Curator instance + */ + static <T> ModeledCuratorFramework<T> wrap(CuratorFramework client, ZPath path, ModelSerializer<T> serializer) + { + return builder(client, path, serializer).build(); + } + + /** + * Start a new ModeledCuratorFrameworkBuilder for the given path and serializer. The returned ModeledCuratorFrameworkBuilder + * is set to not watch ZNodes and uses {@link #defaultCreateOptions} and {@link #defaultDeleteOptions}, but you can change these + * with builder methods. + * + * @param client Curator client + * @param path path to model + * @param serializer the model's serializer + * @return builder + */ + static <T> ModeledCuratorFrameworkBuilder<T> builder(CuratorFramework client, ZPath path, ModelSerializer<T> serializer) + { + return new ModeledCuratorFrameworkBuilder<>(client, path, serializer) + .withCreateOptions(defaultCreateOptions) + .withDeleteOptions(defaultDeleteOptions); + } + + /** + * Returns the client that was originally passed to {@link #wrap(org.apache.curator.framework.CuratorFramework, ZPath, ModelSerializer)} or + * the builder. + * + * @return original client + */ + CuratorFramework unwrap(); + + /** + * Return a new Modeled Curator instance with all the same options but applying to the given child node of this Modeled Curator's + * path. E.g. if this Modeled Curator instance applies to "/a/b", calling <code>modeled.at("c")</code> returns an instance that applies to + * "/a/b/c". + * + * @param child child node. + * @return new Modeled Curator instance + */ + ModeledCuratorFramework<T> at(String child); + + /** + * Create (or update depending on build options) a ZNode at this instance's path with a serialized + * version of the given model + * + * @param model model to write + * @return AsyncStage + * @see org.apache.curator.x.async.AsyncStage + */ + AsyncStage<String> create(T model); + + /** + * Create (or update depending on build options) a ZNode at this instance's path with a serialized + * form of the given model + * + * @param model model to write + * @param storingStatIn the stat for the new ZNode is stored here + * @return AsyncStage + * @see org.apache.curator.x.async.AsyncStage + */ + AsyncStage<String> create(T model, Stat storingStatIn); + + /** + * Read the ZNode at this instance's path and deserialize into a model + * + * @return AsyncStage + * @see org.apache.curator.x.async.AsyncStage + */ + AsyncStage<T> read(); + + /** + * Read the ZNode at this instance's path and deserialize into a model + * + * @param storingStatIn the stat for the new ZNode is stored here + * @return AsyncStage + * @see org.apache.curator.x.async.AsyncStage + */ + AsyncStage<T> read(Stat storingStatIn); + + /** + * Update the ZNode at this instance's path with a serialized + * form of the given model passing "-1" for the update version + * + * @param model model to write + * @return AsyncStage + * @see org.apache.curator.x.async.AsyncStage + */ + AsyncStage<Stat> update(T model); + + /** + * Update the ZNode at this instance's path with a serialized + * form of the given model passing the given update version + * + * @param model model to write + * @param version update version to use + * @return AsyncStage + * @see org.apache.curator.x.async.AsyncStage + */ + AsyncStage<Stat> update(T model, int version); + + /** + * Check to see if the ZNode at this instance's path exists + * + * @return AsyncStage + * @see org.apache.curator.x.async.AsyncStage + */ + AsyncStage<Stat> checkExists(); + + /** + * Delete the ZNode at this instance's path passing -1 for the delete version + * + * @return AsyncStage + * @see org.apache.curator.x.async.AsyncStage + */ + AsyncStage<Void> delete(); + + /** + * Delete the ZNode at this instance's path passing the given delete version + * + * @param version update version to use + * @return AsyncStage + * @see org.apache.curator.x.async.AsyncStage + */ + AsyncStage<Void> delete(int version); +} http://git-wip-us.apache.org/repos/asf/curator/blob/792aef20/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/ModeledCuratorFrameworkBuilder.java ---------------------------------------------------------------------- diff --git a/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/ModeledCuratorFrameworkBuilder.java b/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/ModeledCuratorFrameworkBuilder.java new file mode 100644 index 0000000..ec15d99 --- /dev/null +++ b/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/ModeledCuratorFrameworkBuilder.java @@ -0,0 +1,192 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.curator.x.async.modeled; + +import com.google.common.collect.ImmutableSet; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.api.CuratorEvent; +import org.apache.curator.framework.api.UnhandledErrorListener; +import org.apache.curator.x.async.WatchMode; +import org.apache.curator.x.async.api.CreateOption; +import org.apache.curator.x.async.api.DeleteOption; +import org.apache.curator.x.async.modeled.details.ModeledCuratorFrameworkImpl; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.data.ACL; +import java.util.List; +import java.util.Objects; +import java.util.Set; +import java.util.function.UnaryOperator; + +public class ModeledCuratorFrameworkBuilder<T> +{ + private final CuratorFramework client; + private final ZPath path; + private final ModelSerializer<T> serializer; + private WatchMode watchMode; + private UnaryOperator<WatchedEvent> watcherFilter; + private UnhandledErrorListener unhandledErrorListener; + private UnaryOperator<CuratorEvent> resultFilter; + private CreateMode createMode; + private List<ACL> aclList; + private Set<CreateOption> createOptions; + private Set<DeleteOption> deleteOptions; + + /** + * Build a new ModeledCuratorFramework instance + * + * @return new ModeledCuratorFramework instance + */ + public ModeledCuratorFramework<T> build() + { + return new ModeledCuratorFrameworkImpl<>( + client, + path.fullPath(), + serializer, + watchMode, + watcherFilter, + unhandledErrorListener, + resultFilter, + createMode, + aclList, + createOptions, + deleteOptions + ); + } + + /** + * Add watchers as appropriate to the Modeled Curator's ZNode using + * {@link org.apache.curator.x.async.WatchMode#stateChangeAndSuccess} + * + * @return this for chaining + * @see org.apache.curator.x.async.AsyncStage#event() + */ + public ModeledCuratorFrameworkBuilder<T> watched() + { + this.watchMode = WatchMode.stateChangeAndSuccess; + return this; + } + + /** + * Add watchers as appropriate using the given watchMode to the Modeled Curator's ZNode + * + * @param watchMode watcher style + * @return this for chaining + * @see org.apache.curator.x.async.AsyncStage#event() + */ + public ModeledCuratorFrameworkBuilder<T> watched(WatchMode watchMode) + { + this.watchMode = watchMode; + return this; + } + + /** + * Add watchers as appropriate using the given watchMode and filter to the Modeled Curator's ZNode + * + * @param watchMode watcher style + * @param watcherFilter filter + * @return this for chaining + * @see org.apache.curator.x.async.AsyncStage#event() + */ + public ModeledCuratorFrameworkBuilder<T> watched(WatchMode watchMode, UnaryOperator<WatchedEvent> watcherFilter) + { + this.watchMode = watchMode; + this.watcherFilter = watcherFilter; + return this; + } + + /** + * Use the given unhandledErrorListener for operations on the Modeled Curator's ZNode + * + * @param unhandledErrorListener listener + * @return this for chaining + */ + public ModeledCuratorFrameworkBuilder<T> withUnhandledErrorListener(UnhandledErrorListener unhandledErrorListener) + { + this.unhandledErrorListener = unhandledErrorListener; + return this; + } + + /** + * Use the given result filter for operations on the Modeled Curator's ZNode + * + * @param resultFilter filter + * @return this for chaining + */ + public ModeledCuratorFrameworkBuilder<T> withResultFilter(UnaryOperator<CuratorEvent> resultFilter) + { + this.resultFilter = resultFilter; + return this; + } + + /** + * Use the given createMode for create operations on the Modeled Curator's ZNode + * + * @param createMode create mode + * @return this for chaining + */ + public ModeledCuratorFrameworkBuilder<T> withCreateMode(CreateMode createMode) + { + this.createMode = createMode; + return this; + } + + /** + * Use the given aclList for create operations on the Modeled Curator's ZNode + * + * @param aclList ACLs + * @return this for chaining + */ + public ModeledCuratorFrameworkBuilder<T> withAclList(List<ACL> aclList) + { + this.aclList = aclList; + return this; + } + + /** + * Use the given create options on the Modeled Curator's ZNode + * + * @param createOptions options + * @return this for chaining + */ + public ModeledCuratorFrameworkBuilder<T> withCreateOptions(Set<CreateOption> createOptions) + { + this.createOptions = (createOptions != null) ? ImmutableSet.copyOf(createOptions) : null; + return this; + } + + /** + * Use the given delete options on the Modeled Curator's ZNode + * + * @param deleteOptions options + * @return this for chaining + */ + public ModeledCuratorFrameworkBuilder<T> withDeleteOptions(Set<DeleteOption> deleteOptions) + { + this.deleteOptions = (deleteOptions != null) ? ImmutableSet.copyOf(deleteOptions) : null; + return this; + } + + ModeledCuratorFrameworkBuilder(CuratorFramework client, ZPath path, ModelSerializer<T> serializer) + { + this.client = Objects.requireNonNull(client, "client cannot be null"); + this.path = Objects.requireNonNull(path, "path cannot be null"); + this.serializer = Objects.requireNonNull(serializer, "serializer cannot be null"); + } +} http://git-wip-us.apache.org/repos/asf/curator/blob/792aef20/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/ModeledAsyncCuratorFrameworkImpl.java ---------------------------------------------------------------------- diff --git a/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/ModeledAsyncCuratorFrameworkImpl.java b/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/ModeledAsyncCuratorFrameworkImpl.java deleted file mode 100644 index 40a3be8..0000000 --- a/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/ModeledAsyncCuratorFrameworkImpl.java +++ /dev/null @@ -1,186 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.curator.x.async.modeled.details; - -import com.google.common.collect.ImmutableSet; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.api.CuratorEvent; -import org.apache.curator.framework.api.UnhandledErrorListener; -import org.apache.curator.utils.ZKPaths; -import org.apache.curator.x.async.AsyncCuratorFramework; -import org.apache.curator.x.async.AsyncStage; -import org.apache.curator.x.async.WatchMode; -import org.apache.curator.x.async.api.AsyncCuratorFrameworkDsl; -import org.apache.curator.x.async.api.AsyncPathAndBytesable; -import org.apache.curator.x.async.api.AsyncPathable; -import org.apache.curator.x.async.api.CreateOption; -import org.apache.curator.x.async.api.DeleteOption; -import org.apache.curator.x.async.api.WatchableAsyncCuratorFramework; -import org.apache.curator.x.async.modeled.ModelSerializer; -import org.apache.curator.x.async.modeled.ModeledAsyncCuratorFramework; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.WatchedEvent; -import org.apache.zookeeper.data.ACL; -import org.apache.zookeeper.data.Stat; -import java.util.Collections; -import java.util.List; -import java.util.Objects; -import java.util.Set; -import java.util.concurrent.CompletionStage; -import java.util.function.UnaryOperator; - -public class ModeledAsyncCuratorFrameworkImpl<T> implements ModeledAsyncCuratorFramework<T> -{ - private final AsyncCuratorFramework client; - private final WatchableAsyncCuratorFramework watchableClient; - private final String path; - private final ModelSerializer<T> serializer; - private final WatchMode watchMode; - private final UnaryOperator<WatchedEvent> watcherFilter; - private final UnhandledErrorListener unhandledErrorListener; - private final UnaryOperator<CuratorEvent> resultFilter; - private final CreateMode createMode; - private final List<ACL> aclList; - private final Set<CreateOption> createOptions; - private final Set<DeleteOption> deleteOptions; - private final AsyncCuratorFrameworkDsl dslClient; - - public ModeledAsyncCuratorFrameworkImpl(CuratorFramework client, String path, ModelSerializer<T> serializer, WatchMode watchMode, UnaryOperator<WatchedEvent> watcherFilter, UnhandledErrorListener unhandledErrorListener, UnaryOperator<CuratorEvent> resultFilter, CreateMode createMode, List<ACL> aclList, Set<CreateOption> createOptions, Set<DeleteOption> deleteOptions) - { - boolean localIsWatched = (watchMode != null); - - this.client = AsyncCuratorFramework.wrap(client); - this.path = Objects.requireNonNull(path, "path cannot be null"); - this.serializer = Objects.requireNonNull(serializer, "serializer cannot be null"); - this.watchMode = (watchMode != null) ? watchMode : WatchMode.stateChangeAndSuccess; - this.watcherFilter = watcherFilter; - this.unhandledErrorListener = unhandledErrorListener; - this.resultFilter = resultFilter; - this.createMode = (createMode != null) ? createMode : CreateMode.PERSISTENT; - this.aclList = aclList; - this.createOptions = (createOptions != null) ? ImmutableSet.copyOf(createOptions) : Collections.emptySet(); - this.deleteOptions = (deleteOptions != null) ? ImmutableSet.copyOf(deleteOptions) : Collections.emptySet(); - - dslClient = this.client.with(this.watchMode, unhandledErrorListener, resultFilter, watcherFilter); - watchableClient = localIsWatched ? dslClient.watched() : dslClient; - } - - @Override - public CuratorFramework unwrap() - { - return client.unwrap(); - } - - @Override - public AsyncStage<String> create(T model) - { - return create(model, null); - } - - @Override - public AsyncStage<String> create(T model, Stat storingStatIn) - { - byte[] bytes = serializer.serialize(model); - return dslClient.create().withOptions(createOptions, createMode, aclList, storingStatIn).forPath(path, bytes); - } - - @Override - public AsyncStage<T> read() - { - return read(null); - } - - @Override - public AsyncStage<T> read(Stat storingStatIn) - { - AsyncPathable<AsyncStage<byte[]>> next; - if ( isCompressed() ) - { - next = (storingStatIn != null) ? watchableClient.getData().decompressedStoringStatIn(storingStatIn) : watchableClient.getData().decompressed(); - } - else - { - next = (storingStatIn != null) ? watchableClient.getData().storingStatIn(storingStatIn) : watchableClient.getData(); - } - AsyncStage<byte[]> asyncStage = next.forPath(path); - ModelStage<T> modelStage = new ModelStage<T>() - { - @Override - public CompletionStage<WatchedEvent> event() - { - return asyncStage.event(); - } - }; - asyncStage.whenComplete((value, e) -> { - if ( e != null ) - { - modelStage.completeExceptionally(e); - } - else - { - modelStage.complete(serializer.deserialize(value)); - } - }); - return modelStage; - } - - @Override - public AsyncStage<Stat> update(T model) - { - return update(model, -1); - } - - @Override - public AsyncStage<Stat> update(T model, int version) - { - byte[] bytes = serializer.serialize(model); - AsyncPathAndBytesable<AsyncStage<Stat>> next = isCompressed() ? dslClient.setData().compressedWithVersion(version) : dslClient.setData(); - return next.forPath(path, bytes); - } - - @Override - public AsyncStage<Stat> checkExists() - { - return watchableClient.checkExists().forPath(path); - } - - @Override - public AsyncStage<Void> delete() - { - return delete(-1); - } - - @Override - public AsyncStage<Void> delete(int version) - { - return dslClient.delete().withVersion(-1).forPath(path); - } - - @Override - public ModeledAsyncCuratorFramework<T> at(String child) - { - String childPath = ZKPaths.makePath(path, child); - return new ModeledAsyncCuratorFrameworkImpl<>(client.unwrap(), childPath, serializer, watchMode, watcherFilter, unhandledErrorListener, resultFilter, createMode, aclList, createOptions, deleteOptions); - } - - private boolean isCompressed() - { - return createOptions.contains(CreateOption.compress); - } -} http://git-wip-us.apache.org/repos/asf/curator/blob/792aef20/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/ModeledCuratorFrameworkImpl.java ---------------------------------------------------------------------- diff --git a/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/ModeledCuratorFrameworkImpl.java b/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/ModeledCuratorFrameworkImpl.java new file mode 100644 index 0000000..1dd04db --- /dev/null +++ b/curator-x-async/src/main/java/org/apache/curator/x/async/modeled/details/ModeledCuratorFrameworkImpl.java @@ -0,0 +1,186 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.curator.x.async.modeled.details; + +import com.google.common.collect.ImmutableSet; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.api.CuratorEvent; +import org.apache.curator.framework.api.UnhandledErrorListener; +import org.apache.curator.utils.ZKPaths; +import org.apache.curator.x.async.AsyncCuratorFramework; +import org.apache.curator.x.async.AsyncStage; +import org.apache.curator.x.async.WatchMode; +import org.apache.curator.x.async.api.AsyncCuratorFrameworkDsl; +import org.apache.curator.x.async.api.AsyncPathAndBytesable; +import org.apache.curator.x.async.api.AsyncPathable; +import org.apache.curator.x.async.api.CreateOption; +import org.apache.curator.x.async.api.DeleteOption; +import org.apache.curator.x.async.api.WatchableAsyncCuratorFramework; +import org.apache.curator.x.async.modeled.ModelSerializer; +import org.apache.curator.x.async.modeled.ModeledCuratorFramework; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.CompletionStage; +import java.util.function.UnaryOperator; + +public class ModeledCuratorFrameworkImpl<T> implements ModeledCuratorFramework<T> +{ + private final AsyncCuratorFramework client; + private final WatchableAsyncCuratorFramework watchableClient; + private final String path; + private final ModelSerializer<T> serializer; + private final WatchMode watchMode; + private final UnaryOperator<WatchedEvent> watcherFilter; + private final UnhandledErrorListener unhandledErrorListener; + private final UnaryOperator<CuratorEvent> resultFilter; + private final CreateMode createMode; + private final List<ACL> aclList; + private final Set<CreateOption> createOptions; + private final Set<DeleteOption> deleteOptions; + private final AsyncCuratorFrameworkDsl dslClient; + + public ModeledCuratorFrameworkImpl(CuratorFramework client, String path, ModelSerializer<T> serializer, WatchMode watchMode, UnaryOperator<WatchedEvent> watcherFilter, UnhandledErrorListener unhandledErrorListener, UnaryOperator<CuratorEvent> resultFilter, CreateMode createMode, List<ACL> aclList, Set<CreateOption> createOptions, Set<DeleteOption> deleteOptions) + { + boolean localIsWatched = (watchMode != null); + + this.client = AsyncCuratorFramework.wrap(client); + this.path = Objects.requireNonNull(path, "path cannot be null"); + this.serializer = Objects.requireNonNull(serializer, "serializer cannot be null"); + this.watchMode = (watchMode != null) ? watchMode : WatchMode.stateChangeAndSuccess; + this.watcherFilter = watcherFilter; + this.unhandledErrorListener = unhandledErrorListener; + this.resultFilter = resultFilter; + this.createMode = (createMode != null) ? createMode : CreateMode.PERSISTENT; + this.aclList = aclList; + this.createOptions = (createOptions != null) ? ImmutableSet.copyOf(createOptions) : Collections.emptySet(); + this.deleteOptions = (deleteOptions != null) ? ImmutableSet.copyOf(deleteOptions) : Collections.emptySet(); + + dslClient = this.client.with(this.watchMode, unhandledErrorListener, resultFilter, watcherFilter); + watchableClient = localIsWatched ? dslClient.watched() : dslClient; + } + + @Override + public CuratorFramework unwrap() + { + return client.unwrap(); + } + + @Override + public AsyncStage<String> create(T model) + { + return create(model, null); + } + + @Override + public AsyncStage<String> create(T model, Stat storingStatIn) + { + byte[] bytes = serializer.serialize(model); + return dslClient.create().withOptions(createOptions, createMode, aclList, storingStatIn).forPath(path, bytes); + } + + @Override + public AsyncStage<T> read() + { + return read(null); + } + + @Override + public AsyncStage<T> read(Stat storingStatIn) + { + AsyncPathable<AsyncStage<byte[]>> next; + if ( isCompressed() ) + { + next = (storingStatIn != null) ? watchableClient.getData().decompressedStoringStatIn(storingStatIn) : watchableClient.getData().decompressed(); + } + else + { + next = (storingStatIn != null) ? watchableClient.getData().storingStatIn(storingStatIn) : watchableClient.getData(); + } + AsyncStage<byte[]> asyncStage = next.forPath(path); + ModelStage<T> modelStage = new ModelStage<T>() + { + @Override + public CompletionStage<WatchedEvent> event() + { + return asyncStage.event(); + } + }; + asyncStage.whenComplete((value, e) -> { + if ( e != null ) + { + modelStage.completeExceptionally(e); + } + else + { + modelStage.complete(serializer.deserialize(value)); + } + }); + return modelStage; + } + + @Override + public AsyncStage<Stat> update(T model) + { + return update(model, -1); + } + + @Override + public AsyncStage<Stat> update(T model, int version) + { + byte[] bytes = serializer.serialize(model); + AsyncPathAndBytesable<AsyncStage<Stat>> next = isCompressed() ? dslClient.setData().compressedWithVersion(version) : dslClient.setData(); + return next.forPath(path, bytes); + } + + @Override + public AsyncStage<Stat> checkExists() + { + return watchableClient.checkExists().forPath(path); + } + + @Override + public AsyncStage<Void> delete() + { + return delete(-1); + } + + @Override + public AsyncStage<Void> delete(int version) + { + return dslClient.delete().withVersion(-1).forPath(path); + } + + @Override + public ModeledCuratorFramework<T> at(String child) + { + String childPath = ZKPaths.makePath(path, child); + return new ModeledCuratorFrameworkImpl<>(client.unwrap(), childPath, serializer, watchMode, watcherFilter, unhandledErrorListener, resultFilter, createMode, aclList, createOptions, deleteOptions); + } + + private boolean isCompressed() + { + return createOptions.contains(CreateOption.compress); + } +} http://git-wip-us.apache.org/repos/asf/curator/blob/792aef20/curator-x-async/src/site/confluence/index.confluence ---------------------------------------------------------------------- diff --git a/curator-x-async/src/site/confluence/index.confluence b/curator-x-async/src/site/confluence/index.confluence index a5d8072..8788b76 100644 --- a/curator-x-async/src/site/confluence/index.confluence +++ b/curator-x-async/src/site/confluence/index.confluence @@ -38,7 +38,7 @@ opposed to raw byte arrays. For example: {code} // let "client" be a CuratorFramework instance -ModeledAsyncCuratorFramework<MyModel> modeled = ModeledAsyncCuratorFramework.wrap(client, path, serializer); +ModeledCuratorFramework<MyModel> modeled = ModeledCuratorFramework.wrap(client, path, serializer); modeled.create(new MyModel()); {code} http://git-wip-us.apache.org/repos/asf/curator/blob/792aef20/curator-x-async/src/site/confluence/modeled.confluence ---------------------------------------------------------------------- diff --git a/curator-x-async/src/site/confluence/modeled.confluence b/curator-x-async/src/site/confluence/modeled.confluence index 118a731..3623728 100644 --- a/curator-x-async/src/site/confluence/modeled.confluence +++ b/curator-x-async/src/site/confluence/modeled.confluence @@ -5,7 +5,7 @@ opposed to raw byte arrays. For example: {code} // let "client" be a CuratorFramework instance -ModeledAsyncCuratorFramework<MyModel> modeled = ModeledAsyncCuratorFramework.wrap(client, path, serializer); +ModeledCuratorFramework<MyModel> modeled = ModeledCuratorFramework.wrap(client, path, serializer); modeled.create(new MyModel()); {code} @@ -35,7 +35,7 @@ ZooKeeper paths. h4. Building -You build a {{ModeledAsyncCuratorFramework}} instance using either the builder or helper wrapper. All +You build a {{ModeledCuratorFramework}} instance using either the builder or helper wrapper. All options needed to use the ZPath are specified at build time: * whether and/or how to watch the ZNode @@ -45,7 +45,7 @@ options needed to use the ZPath are specified at build time: * any ACLs * etc. -These options are bound into the {{ModeledAsyncCuratorFramework}} instance and applied as needed. +These options are bound into the {{ModeledCuratorFramework}} instance and applied as needed. h2. Example @@ -75,14 +75,14 @@ public class Person { } {code} -We can now build a {{ModeledAsyncCuratorFramework}} that manages {{Person}} instances at a given path: +We can now build a {{ModeledCuratorFramework}} that manages {{Person}} instances at a given path: {code} // let "client" be a CuratorFramework instance ZPath path = ZPath.parse(...); // whatever path you need JacksonModelSerializer<Person> serializer = JacksonModelSerializer.build(Person.class); -ModeledAsyncCuratorFramework<MyModel> modeled = ModeledAsyncCuratorFramework.wrap(client, path, serializer); +ModeledCuratorFramework<MyModel> modeled = ModeledCuratorFramework.wrap(client, path, serializer); ... http://git-wip-us.apache.org/repos/asf/curator/blob/792aef20/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestModeledAsyncCuratorFramework.java ---------------------------------------------------------------------- diff --git a/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestModeledAsyncCuratorFramework.java b/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestModeledAsyncCuratorFramework.java deleted file mode 100644 index 03d6d7d..0000000 --- a/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestModeledAsyncCuratorFramework.java +++ /dev/null @@ -1,102 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.curator.x.async.modeled; - -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.CuratorFrameworkFactory; -import org.apache.curator.retry.RetryOneTime; -import org.apache.curator.utils.CloseableUtils; -import org.apache.curator.x.async.AsyncStage; -import org.apache.curator.x.async.CompletableBaseClassForTests; -import org.apache.curator.x.async.modeled.models.TestModel; -import org.apache.curator.x.async.modeled.models.TestNewerModel; -import org.testng.Assert; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; -import java.math.BigInteger; -import java.util.concurrent.CountDownLatch; - -public class TestModeledAsyncCuratorFramework extends CompletableBaseClassForTests -{ - private static final ZPath path = ZPath.parse("/test/path"); - private CuratorFramework rawClient; - private JacksonModelSerializer<TestModel> serializer; - private JacksonModelSerializer<TestNewerModel> newSerializer; - - @BeforeMethod - @Override - public void setup() throws Exception - { - super.setup(); - - rawClient = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); - rawClient.start(); - - serializer = new JacksonModelSerializer<>(TestModel.class); - newSerializer = new JacksonModelSerializer<>(TestNewerModel.class); - } - - @AfterMethod - @Override - public void teardown() throws Exception - { - CloseableUtils.closeQuietly(rawClient); - super.teardown(); - } - - @Test - public void testCrud() - { - TestModel rawModel = new TestModel("John", "Galt", "1 Galt's Gulch", 42, BigInteger.valueOf(1)); - TestModel rawModel2 = new TestModel("Wayne", "Rooney", "Old Trafford", 10, BigInteger.valueOf(1)); - ModeledAsyncCuratorFramework<TestModel> client = ModeledAsyncCuratorFramework.wrap(rawClient, path, serializer); - AsyncStage<String> stage = client.create(rawModel); - Assert.assertNull(stage.event()); - complete(stage, (s, e) -> Assert.assertNotNull(s)); - complete(client.read(), (model, e) -> Assert.assertEquals(model, rawModel)); - complete(client.update(rawModel2)); - complete(client.read(), (model, e) -> Assert.assertEquals(model, rawModel2)); - complete(client.delete()); - complete(client.checkExists(), (stat, e) -> Assert.assertNull(stat)); - } - - @Test - public void testBackwardCompatibility() - { - TestNewerModel rawNewModel = new TestNewerModel("John", "Galt", "1 Galt's Gulch", 42, BigInteger.valueOf(1), 100); - ModeledAsyncCuratorFramework<TestNewerModel> clientForNew = ModeledAsyncCuratorFramework.wrap(rawClient, path, newSerializer); - complete(clientForNew.create(rawNewModel), (s, e) -> Assert.assertNotNull(s)); - - ModeledAsyncCuratorFramework<TestModel> clientForOld = ModeledAsyncCuratorFramework.wrap(rawClient, path, serializer); - complete(clientForOld.read(), (model, e) -> Assert.assertTrue(rawNewModel.equalsOld(model))); - } - - @Test - public void testWatched() throws InterruptedException - { - CountDownLatch latch = new CountDownLatch(1); - ModeledAsyncCuratorFramework<TestModel> client = ModeledAsyncCuratorFramework.builder(rawClient, path, serializer).watched().build(); - client.checkExists().event().whenComplete((event, ex) -> latch.countDown()); - timing.sleepABit(); - Assert.assertEquals(latch.getCount(), 1); - client.create(new TestModel()); - Assert.assertTrue(timing.awaitLatch(latch)); - } -} http://git-wip-us.apache.org/repos/asf/curator/blob/792aef20/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestModeledCuratorFramework.java ---------------------------------------------------------------------- diff --git a/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestModeledCuratorFramework.java b/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestModeledCuratorFramework.java new file mode 100644 index 0000000..8e76536 --- /dev/null +++ b/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestModeledCuratorFramework.java @@ -0,0 +1,102 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.curator.x.async.modeled; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.RetryOneTime; +import org.apache.curator.utils.CloseableUtils; +import org.apache.curator.x.async.AsyncStage; +import org.apache.curator.x.async.CompletableBaseClassForTests; +import org.apache.curator.x.async.modeled.models.TestModel; +import org.apache.curator.x.async.modeled.models.TestNewerModel; +import org.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; +import java.math.BigInteger; +import java.util.concurrent.CountDownLatch; + +public class TestModeledCuratorFramework extends CompletableBaseClassForTests +{ + private static final ZPath path = ZPath.parse("/test/path"); + private CuratorFramework rawClient; + private JacksonModelSerializer<TestModel> serializer; + private JacksonModelSerializer<TestNewerModel> newSerializer; + + @BeforeMethod + @Override + public void setup() throws Exception + { + super.setup(); + + rawClient = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); + rawClient.start(); + + serializer = new JacksonModelSerializer<>(TestModel.class); + newSerializer = new JacksonModelSerializer<>(TestNewerModel.class); + } + + @AfterMethod + @Override + public void teardown() throws Exception + { + CloseableUtils.closeQuietly(rawClient); + super.teardown(); + } + + @Test + public void testCrud() + { + TestModel rawModel = new TestModel("John", "Galt", "1 Galt's Gulch", 42, BigInteger.valueOf(1)); + TestModel rawModel2 = new TestModel("Wayne", "Rooney", "Old Trafford", 10, BigInteger.valueOf(1)); + ModeledCuratorFramework<TestModel> client = ModeledCuratorFramework.wrap(rawClient, path, serializer); + AsyncStage<String> stage = client.create(rawModel); + Assert.assertNull(stage.event()); + complete(stage, (s, e) -> Assert.assertNotNull(s)); + complete(client.read(), (model, e) -> Assert.assertEquals(model, rawModel)); + complete(client.update(rawModel2)); + complete(client.read(), (model, e) -> Assert.assertEquals(model, rawModel2)); + complete(client.delete()); + complete(client.checkExists(), (stat, e) -> Assert.assertNull(stat)); + } + + @Test + public void testBackwardCompatibility() + { + TestNewerModel rawNewModel = new TestNewerModel("John", "Galt", "1 Galt's Gulch", 42, BigInteger.valueOf(1), 100); + ModeledCuratorFramework<TestNewerModel> clientForNew = ModeledCuratorFramework.wrap(rawClient, path, newSerializer); + complete(clientForNew.create(rawNewModel), (s, e) -> Assert.assertNotNull(s)); + + ModeledCuratorFramework<TestModel> clientForOld = ModeledCuratorFramework.wrap(rawClient, path, serializer); + complete(clientForOld.read(), (model, e) -> Assert.assertTrue(rawNewModel.equalsOld(model))); + } + + @Test + public void testWatched() throws InterruptedException + { + CountDownLatch latch = new CountDownLatch(1); + ModeledCuratorFramework<TestModel> client = ModeledCuratorFramework.builder(rawClient, path, serializer).watched().build(); + client.checkExists().event().whenComplete((event, ex) -> latch.countDown()); + timing.sleepABit(); + Assert.assertEquals(latch.getCount(), 1); + client.create(new TestModel()); + Assert.assertTrue(timing.awaitLatch(latch)); + } +} http://git-wip-us.apache.org/repos/asf/curator/blob/792aef20/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/recipes/TestModeledCaches.java ---------------------------------------------------------------------- diff --git a/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/recipes/TestModeledCaches.java b/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/recipes/TestModeledCaches.java index 51114f0..6b168a4 100644 --- a/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/recipes/TestModeledCaches.java +++ b/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/recipes/TestModeledCaches.java @@ -27,7 +27,7 @@ import org.apache.curator.retry.RetryOneTime; import org.apache.curator.utils.CloseableUtils; import org.apache.curator.x.async.CompletableBaseClassForTests; import org.apache.curator.x.async.modeled.JacksonModelSerializer; -import org.apache.curator.x.async.modeled.ModeledAsyncCuratorFramework; +import org.apache.curator.x.async.modeled.ModeledCuratorFramework; import org.apache.curator.x.async.modeled.ZPath; import org.apache.curator.x.async.modeled.models.TestModel; import org.apache.zookeeper.data.Stat; @@ -45,7 +45,7 @@ public class TestModeledCaches extends CompletableBaseClassForTests private CuratorFramework client; private JacksonModelSerializer<TestModel> serializer; private ZPath path; - private ModeledAsyncCuratorFramework<TestModel> modeled; + private ModeledCuratorFramework<TestModel> modeled; @BeforeMethod @Override @@ -59,7 +59,7 @@ public class TestModeledCaches extends CompletableBaseClassForTests serializer = JacksonModelSerializer.build(TestModel.class); path = ZPath.parse("/test/path"); - modeled = ModeledAsyncCuratorFramework.wrap(client, path, serializer); + modeled = ModeledCuratorFramework.wrap(client, path, serializer); } @AfterMethod
