You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by ra...@apache.org on 2016/05/03 17:20:53 UTC
[01/16] curator git commit: initial work on schema validation
Repository: curator
Updated Branches:
refs/heads/CURATOR-322 [created] ae6cb4be4
initial work on schema validation
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/ef1658c1
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/ef1658c1
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/ef1658c1
Branch: refs/heads/CURATOR-322
Commit: ef1658c17e3f991968932c0bef94d547ecea790d
Parents: e76eb59
Author: randgalt <ra...@apache.org>
Authored: Mon May 2 14:06:15 2016 -0500
Committer: randgalt <ra...@apache.org>
Committed: Mon May 2 14:06:15 2016 -0500
----------------------------------------------------------------------
.../framework/CuratorFrameworkFactory.java | 20 +++
.../framework/imps/CreateBuilderImpl.java | 5 +-
.../framework/imps/CuratorFrameworkImpl.java | 9 ++
.../curator/framework/schema/DataValidator.java | 6 +
.../framework/schema/DefaultDataValidator.java | 10 ++
.../apache/curator/framework/schema/Schema.java | 144 +++++++++++++++++++
.../curator/framework/schema/SchemaBuilder.java | 61 ++++++++
.../curator/framework/schema/SchemaSet.java | 77 ++++++++++
.../framework/schema/SchemaViolation.java | 9 ++
9 files changed, 339 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/ef1658c1/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 fedafa4..5aed199 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
@@ -34,6 +34,7 @@ import org.apache.curator.framework.imps.CuratorFrameworkImpl;
import org.apache.curator.framework.imps.CuratorTempFrameworkImpl;
import org.apache.curator.framework.imps.DefaultACLProvider;
import org.apache.curator.framework.imps.GzipCompressionProvider;
+import org.apache.curator.framework.schema.SchemaSet;
import org.apache.curator.framework.state.ConnectionStateErrorPolicy;
import org.apache.curator.framework.state.StandardConnectionStateErrorPolicy;
import org.apache.curator.framework.state.ConnectionState;
@@ -143,6 +144,7 @@ public class CuratorFrameworkFactory
private boolean useContainerParentsIfAvailable = true;
private ConnectionStateErrorPolicy connectionStateErrorPolicy = new StandardConnectionStateErrorPolicy();
private ConnectionHandlingPolicy connectionHandlingPolicy = Boolean.getBoolean("curator-use-classic-connection-handling") ? new ClassicConnectionHandlingPolicy() : new StandardConnectionHandlingPolicy();
+ private SchemaSet schemaSet = new SchemaSet();
/**
* Apply the current values and build a new CuratorFramework
@@ -430,6 +432,19 @@ public class CuratorFrameworkFactory
return this;
}
+ /**
+ * Add an enforced schema set
+ *
+ * @param schemaSet the schema set
+ * @return this
+ * @since 3.2.0
+ */
+ public Builder schemaSet(SchemaSet schemaSet)
+ {
+ this.schemaSet = schemaSet;
+ return this;
+ }
+
public ACLProvider getAclProvider()
{
return aclProvider;
@@ -495,6 +510,11 @@ public class CuratorFrameworkFactory
return connectionHandlingPolicy;
}
+ public SchemaSet getSchemaSet()
+ {
+ return schemaSet;
+ }
+
@Deprecated
public String getAuthScheme()
{
http://git-wip-us.apache.org/repos/asf/curator/blob/ef1658c1/curator-framework/src/main/java/org/apache/curator/framework/imps/CreateBuilderImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/CreateBuilderImpl.java b/curator-framework/src/main/java/org/apache/curator/framework/imps/CreateBuilderImpl.java
index 9e8f7e6..facabe7 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/CreateBuilderImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/CreateBuilderImpl.java
@@ -35,7 +35,6 @@ import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.Op;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;
-
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Callable;
@@ -481,6 +480,8 @@ class CreateBuilderImpl implements CreateBuilder, BackgroundOperation<PathAndByt
@Override
public String forPath(final String givenPath, byte[] data) throws Exception
{
+ client.getSchemaSet().getSchema(givenPath).validateCreate(createMode, data);
+
if ( compress )
{
data = client.getCompressionProvider().compress(givenPath, data);
@@ -714,7 +715,7 @@ class CreateBuilderImpl implements CreateBuilder, BackgroundOperation<PathAndByt
client.queueOperation(mainOperationAndData);
}
};
- OperationAndData<T> parentOperation = new OperationAndData<T>(operation, mainOperationAndData.getData(), null, null, backgrounding.getContext(), null);
+ OperationAndData<T> parentOperation = new OperationAndData<>(operation, mainOperationAndData.getData(), null, null, backgrounding.getContext(), null);
client.queueOperation(parentOperation);
}
http://git-wip-us.apache.org/repos/asf/curator/blob/ef1658c1/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 4e1aefc..739f07e 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
@@ -37,6 +37,7 @@ import org.apache.curator.framework.api.transaction.CuratorTransaction;
import org.apache.curator.framework.api.transaction.TransactionOp;
import org.apache.curator.framework.listen.Listenable;
import org.apache.curator.framework.listen.ListenerContainer;
+import org.apache.curator.framework.schema.SchemaSet;
import org.apache.curator.framework.state.ConnectionState;
import org.apache.curator.framework.state.ConnectionStateErrorPolicy;
import org.apache.curator.framework.state.ConnectionStateListener;
@@ -89,6 +90,7 @@ public class CuratorFrameworkImpl implements CuratorFramework
private final AtomicLong currentInstanceIndex = new AtomicLong(-1);
private final InternalConnectionHandler internalConnectionHandler;
private final EnsembleTracker ensembleTracker;
+ private final SchemaSet schemaSet;
private volatile ExecutorService executorService;
private final AtomicBoolean logAsErrorConnectionErrors = new AtomicBoolean(false);
@@ -143,6 +145,7 @@ public class CuratorFrameworkImpl implements CuratorFramework
state = new AtomicReference<CuratorFrameworkState>(CuratorFrameworkState.LATENT);
useContainerParentsIfAvailable = builder.useContainerParentsIfAvailable();
connectionStateErrorPolicy = Preconditions.checkNotNull(builder.getConnectionStateErrorPolicy(), "errorPolicy cannot be null");
+ schemaSet = Preconditions.checkNotNull(builder.getSchemaSet(), "schemaSet cannot be null");
byte[] builderDefaultData = builder.getDefaultData();
defaultData = (builderDefaultData != null) ? Arrays.copyOf(builderDefaultData, builderDefaultData.length) : new byte[0];
@@ -226,6 +229,7 @@ public class CuratorFrameworkImpl implements CuratorFramework
useContainerParentsIfAvailable = parent.useContainerParentsIfAvailable;
connectionStateErrorPolicy = parent.connectionStateErrorPolicy;
internalConnectionHandler = parent.internalConnectionHandler;
+ schemaSet = parent.schemaSet;
ensembleTracker = null;
}
@@ -548,6 +552,11 @@ public class CuratorFrameworkImpl implements CuratorFramework
return namespace.newNamespaceAwareEnsurePath(path);
}
+ SchemaSet getSchemaSet()
+ {
+ return schemaSet;
+ }
+
ACLProvider getAclProvider()
{
return aclProvider;
http://git-wip-us.apache.org/repos/asf/curator/blob/ef1658c1/curator-framework/src/main/java/org/apache/curator/framework/schema/DataValidator.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/DataValidator.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/DataValidator.java
new file mode 100644
index 0000000..855f2bb
--- /dev/null
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/DataValidator.java
@@ -0,0 +1,6 @@
+package org.apache.curator.framework.schema;
+
+public interface DataValidator
+{
+ boolean isValid(byte[] data);
+}
http://git-wip-us.apache.org/repos/asf/curator/blob/ef1658c1/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java
new file mode 100644
index 0000000..05e1f7c
--- /dev/null
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java
@@ -0,0 +1,10 @@
+package org.apache.curator.framework.schema;
+
+public class DefaultDataValidator implements DataValidator
+{
+ @Override
+ public boolean isValid(byte[] data)
+ {
+ return true;
+ }
+}
http://git-wip-us.apache.org/repos/asf/curator/blob/ef1658c1/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
new file mode 100644
index 0000000..f10542d
--- /dev/null
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
@@ -0,0 +1,144 @@
+package org.apache.curator.framework.schema;
+
+import com.google.common.base.Preconditions;
+import org.apache.zookeeper.CreateMode;
+import java.util.regex.Pattern;
+
+public class Schema
+{
+ private final Pattern path;
+ private final String documentation;
+ private final DataValidator dataValidator;
+ private final Allowance ephemeral;
+ private final Allowance sequential;
+ private final boolean canBeWatched;
+ private final boolean canHaveChildren;
+
+ public enum Allowance
+ {
+ CAN,
+ MUST,
+ CANNOT
+ }
+
+ public static SchemaBuilder builder(String path)
+ {
+ return new SchemaBuilder(Pattern.compile(path));
+ }
+
+ public Schema(Pattern path, String documentation, DataValidator dataValidator, Allowance ephemeral, Allowance sequential, boolean canBeWatched, boolean canHaveChildren)
+ {
+ this.path = Preconditions.checkNotNull(path, "path cannot be null");
+ this.documentation = Preconditions.checkNotNull(documentation, "documentation cannot be null");
+ this.dataValidator = Preconditions.checkNotNull(dataValidator, "dataValidator cannot be null");
+ this.ephemeral = Preconditions.checkNotNull(ephemeral, "ephemeral cannot be null");
+ this.sequential = Preconditions.checkNotNull(sequential, "sequential cannot be null");
+ this.canBeWatched = canBeWatched;
+ this.canHaveChildren = canHaveChildren;
+ }
+
+ public void validateCreate(CreateMode mode, byte[] data)
+ {
+ if ( mode.isEphemeral() && (ephemeral == Allowance.CANNOT) )
+ {
+ throw new SchemaViolation(this, "Cannot be ephemeral");
+ }
+
+ if ( !mode.isEphemeral() && (ephemeral == Allowance.MUST) )
+ {
+ throw new SchemaViolation(this, "Must be ephemeral");
+ }
+
+ validateData(data);
+ }
+
+ public void validateData(byte[] data)
+ {
+ if ( !dataValidator.isValid(data) )
+ {
+ throw new SchemaViolation(this, "Data is not valid");
+ }
+ }
+
+ public Pattern getPath()
+ {
+ return path;
+ }
+
+ public String getDocumentation()
+ {
+ return documentation;
+ }
+
+ public DataValidator getDataValidator()
+ {
+ return dataValidator;
+ }
+
+ public Allowance getEphemeral()
+ {
+ return ephemeral;
+ }
+
+ public Allowance getSequential()
+ {
+ return sequential;
+ }
+
+ public boolean isCanBeWatched()
+ {
+ return canBeWatched;
+ }
+
+ public boolean isCanHaveChildren()
+ {
+ return canHaveChildren;
+ }
+
+ @Override
+ public boolean equals(Object o)
+ {
+ if ( this == o )
+ {
+ return true;
+ }
+ if ( o == null || getClass() != o.getClass() )
+ {
+ return false;
+ }
+
+ Schema schema = (Schema)o;
+
+ return path.equals(schema.path);
+
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return path.hashCode();
+ }
+
+ @Override
+ public String toString()
+ {
+ return "Schema{" +
+ "path=" + path +
+ ", documentation='" + documentation + '\'' +
+ ", dataValidator=" + dataValidator +
+ ", isEphemeral=" + ephemeral +
+ ", isSequential=" + sequential +
+ ", canBeWatched=" + canBeWatched +
+ ", canHaveChildren=" + canHaveChildren +
+ '}';
+ }
+
+ public String toDocumentation()
+ {
+ return path.pattern() + '\n'
+ + documentation + '\n'
+ + "Validator: " + dataValidator.getClass().getSimpleName() + '\n'
+ + String.format("ephemeral: %s | sequential: %s | canBeWatched: %s | canHaveChildren: %s", ephemeral, sequential, canBeWatched, canHaveChildren) + '\n'
+ ;
+ }
+}
http://git-wip-us.apache.org/repos/asf/curator/blob/ef1658c1/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
new file mode 100644
index 0000000..053ccfc
--- /dev/null
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
@@ -0,0 +1,61 @@
+package org.apache.curator.framework.schema;
+
+import com.google.common.base.Preconditions;
+import java.util.regex.Pattern;
+
+public class SchemaBuilder
+{
+ private Pattern path;
+ private String documentation = "";
+ private DataValidator dataValidator = new DefaultDataValidator();
+ private boolean isEphemeral = false;
+ private boolean isSequential = false;
+ private boolean canBeWatched = true;
+ private boolean canHaveChildren = true;
+
+ public Schema build()
+ {
+ return new Schema(path, documentation, dataValidator, isEphemeral, isSequential, canBeWatched, canHaveChildren);
+ }
+
+ public SchemaBuilder documentation(String documentation)
+ {
+ this.documentation = Preconditions.checkNotNull(documentation, "documentation cannot be null");
+ return this;
+ }
+
+ public SchemaBuilder dataValidator(DataValidator dataValidator)
+ {
+ this.dataValidator = Preconditions.checkNotNull(dataValidator, "dataValidator cannot be null");
+ return this;
+ }
+
+ public SchemaBuilder isEphemeral(boolean isEphemeral)
+ {
+ this.isEphemeral = isEphemeral;
+ return this;
+ }
+
+ public SchemaBuilder isSequential(boolean isSequential)
+ {
+ this.isSequential = isSequential;
+ return this;
+ }
+
+ public SchemaBuilder canBeWatched(boolean canBeWatched)
+ {
+ this.canBeWatched = canBeWatched;
+ return this;
+ }
+
+ public SchemaBuilder canHaveChildren(boolean canHaveChildren)
+ {
+ this.canHaveChildren = canHaveChildren;
+ return this;
+ }
+
+ SchemaBuilder(Pattern path)
+ {
+ this.path = Preconditions.checkNotNull(path, "path cannot be null");
+ }
+}
http://git-wip-us.apache.org/repos/asf/curator/blob/ef1658c1/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
new file mode 100644
index 0000000..0c3dfd5
--- /dev/null
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
@@ -0,0 +1,77 @@
+package org.apache.curator.framework.schema;
+
+import com.google.common.base.Preconditions;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import com.google.common.collect.ImmutableSet;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.concurrent.ExecutionException;
+import java.util.regex.Pattern;
+
+public class SchemaSet
+{
+ private final Logger log = LoggerFactory.getLogger(getClass());
+ private final Collection<Schema> schemas;
+ private final CacheLoader<String, Schema> cacheLoader = new CacheLoader<String, Schema>()
+ {
+ @Override
+ public Schema load(String path) throws Exception
+ {
+ for ( Schema schema : schemas )
+ {
+ if ( schema.getPath().matcher(path).matches() )
+ {
+ log.debug("path -> {}", schema);
+ return schema;
+ }
+ }
+ return defaultSchema;
+ }
+ };
+ private final LoadingCache<String, Schema> cache = CacheBuilder
+ .newBuilder()
+ .softValues()
+ .build(cacheLoader);
+
+ private static final Schema defaultSchema = new Schema(Pattern.compile(".*"), "__default__", new DefaultDataValidator(), Schema.Allowance.CAN, Schema.Allowance.CAN, true, true);
+
+ public SchemaSet()
+ {
+ this(Collections.<Schema>emptySet());
+ }
+
+ public SchemaSet(Collection<Schema> schemas)
+ {
+ this.schemas = ImmutableSet.copyOf(Preconditions.checkNotNull(schemas, "schemas cannot be null"));
+ }
+
+ public Schema getSchema(String path)
+ {
+ if ( schemas.size() == 0 )
+ {
+ return defaultSchema;
+ }
+ try
+ {
+ return cache.get(path);
+ }
+ catch ( ExecutionException e )
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public String toDocumentation()
+ {
+ StringBuilder str = new StringBuilder("Curator Schemas:\n\n");
+ for ( Schema schema : schemas )
+ {
+ str.append(schema).append('\n');
+ }
+ return str.toString();
+ }
+}
http://git-wip-us.apache.org/repos/asf/curator/blob/ef1658c1/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaViolation.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaViolation.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaViolation.java
new file mode 100644
index 0000000..6935094
--- /dev/null
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaViolation.java
@@ -0,0 +1,9 @@
+package org.apache.curator.framework.schema;
+
+public class SchemaViolation extends RuntimeException
+{
+ public SchemaViolation(Schema schema, String violation)
+ {
+ super(String.format("Schema violation: %s for schema: %s", violation, schema));
+ }
+}
[02/16] curator git commit: forgot sequential
Posted by ra...@apache.org.
forgot sequential
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/da760f96
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/da760f96
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/da760f96
Branch: refs/heads/CURATOR-322
Commit: da760f96cc5e553e7475505f282bce60a5da3056
Parents: ef1658c
Author: randgalt <ra...@apache.org>
Authored: Mon May 2 14:16:48 2016 -0500
Committer: randgalt <ra...@apache.org>
Committed: Mon May 2 14:16:48 2016 -0500
----------------------------------------------------------------------
.../java/org/apache/curator/framework/schema/Schema.java | 10 ++++++++++
1 file changed, 10 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/da760f96/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
index f10542d..7b8f1d5 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
@@ -49,6 +49,16 @@ public class Schema
throw new SchemaViolation(this, "Must be ephemeral");
}
+ if ( mode.isSequential() && (sequential == Allowance.CANNOT) )
+ {
+ throw new SchemaViolation(this, "Cannot be sequential");
+ }
+
+ if ( !mode.isSequential() && (sequential == Allowance.MUST) )
+ {
+ throw new SchemaViolation(this, "Must be sequential");
+ }
+
validateData(data);
}
[15/16] curator git commit: removed SchemaKey - it was overkill
Posted by ra...@apache.org.
removed SchemaKey - it was overkill
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/923a2d8b
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/923a2d8b
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/923a2d8b
Branch: refs/heads/CURATOR-322
Commit: 923a2d8b93d348bcacc6541be5f4013423ce43d2
Parents: 450976f
Author: randgalt <ra...@apache.org>
Authored: Tue May 3 09:40:55 2016 -0500
Committer: randgalt <ra...@apache.org>
Committed: Tue May 3 09:40:55 2016 -0500
----------------------------------------------------------------------
.../curator/framework/schema/SchemaKey.java | 77 --------------------
.../curator/framework/schema/SchemaSet.java | 12 +--
.../framework/schema/SchemaSetLoader.java | 10 +--
.../curator/framework/schema/TestSchema.java | 4 +-
4 files changed, 14 insertions(+), 89 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/923a2d8b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaKey.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaKey.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaKey.java
deleted file mode 100644
index a6ab23e..0000000
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaKey.java
+++ /dev/null
@@ -1,77 +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.framework.schema;
-
-import com.google.common.base.Preconditions;
-import java.util.UUID;
-
-public class SchemaKey
-{
- private final String key;
-
- public static SchemaKey named(String name)
- {
- return new SchemaKey(name);
- }
-
- public SchemaKey()
- {
- this(UUID.randomUUID().toString());
- }
-
- public SchemaKey(String key)
- {
- this.key = Preconditions.checkNotNull(key, "key cannot be null");
- }
-
- public String getKey()
- {
- return key;
- }
-
- @Override
- public boolean equals(Object o)
- {
- if ( this == o )
- {
- return true;
- }
- if ( o == null || getClass() != o.getClass() )
- {
- return false;
- }
-
- SchemaKey schemaKey = (SchemaKey)o;
-
- return key.equals(schemaKey.key);
-
- }
-
- @Override
- public int hashCode()
- {
- return key.hashCode();
- }
-
- @Override
- public String toString()
- {
- return key;
- }
-}
http://git-wip-us.apache.org/repos/asf/curator/blob/923a2d8b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
index 3131862..d33519a 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
@@ -35,7 +35,7 @@ import java.util.concurrent.ExecutionException;
public class SchemaSet
{
private final Logger log = LoggerFactory.getLogger(getClass());
- private final Map<SchemaKey, Schema> schemas;
+ private final Map<String, Schema> schemas;
private final Map<String, Schema> pathSchemas;
private final CacheLoader<String, Schema> cacheLoader = new CacheLoader<String, Schema>()
{
@@ -68,7 +68,7 @@ public class SchemaSet
*/
public static SchemaSet getDefaultSchemaSet()
{
- return new SchemaSet(Collections.<SchemaKey, Schema>emptyMap(), true)
+ return new SchemaSet(Collections.<String, Schema>emptyMap(), true)
{
@Override
public String toDocumentation()
@@ -80,10 +80,10 @@ public class SchemaSet
/**
* @param schemas the schemas for the set. The key of the map is a key/name for the schema that can be
- * used when calling {@link #getNamedSchema(SchemaKey)}
+ * used when calling {@link #getNamedSchema(String)}
* @param useDefaultSchema if true, return a default schema when there is no match. Otherwise, an exception is thrown
*/
- public SchemaSet(Map<SchemaKey, Schema> schemas, boolean useDefaultSchema)
+ public SchemaSet(Map<String, Schema> schemas, boolean useDefaultSchema)
{
this.useDefaultSchema = useDefaultSchema;
this.schemas = ImmutableMap.copyOf(Preconditions.checkNotNull(schemas, "schemas cannot be null"));
@@ -139,7 +139,7 @@ public class SchemaSet
* @param name name
* @return schema or null
*/
- public Schema getNamedSchema(SchemaKey name)
+ public Schema getNamedSchema(String name)
{
return schemas.get(name);
}
@@ -152,7 +152,7 @@ public class SchemaSet
public String toDocumentation()
{
StringBuilder str = new StringBuilder("Curator Schemas:\n\n");
- for ( Map.Entry<SchemaKey, Schema> schemaEntry : schemas.entrySet() )
+ for ( Map.Entry<String, Schema> schemaEntry : schemas.entrySet() )
{
str.append(schemaEntry.getKey()).append('\n').append(schemaEntry.getValue().toDocumentation()).append('\n');
}
http://git-wip-us.apache.org/repos/asf/curator/blob/923a2d8b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java
index 3c4a0f8..c7db916 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java
@@ -57,7 +57,7 @@ import java.util.regex.Pattern;
*/
public class SchemaSetLoader
{
- private final Map<SchemaKey, Schema> schemas;
+ private final Map<String, Schema> schemas;
/**
* Called to map a data validator name in the JSON stream to an actual data validator
@@ -78,7 +78,7 @@ public class SchemaSetLoader
public SchemaSetLoader(Reader in, DataValidatorMapper dataValidatorMapper)
{
- ImmutableMap.Builder<SchemaKey, Schema> builder = ImmutableMap.builder();
+ ImmutableMap.Builder<String, Schema> builder = ImmutableMap.builder();
try
{
JsonNode root = new ObjectMapper().readTree(in);
@@ -96,7 +96,7 @@ public class SchemaSetLoader
return new SchemaSet(schemas, useDefaultSchema);
}
- private void read(ImmutableMap.Builder<SchemaKey, Schema> builder, JsonNode node, DataValidatorMapper dataValidatorMapper)
+ private void read(ImmutableMap.Builder<String, Schema> builder, JsonNode node, DataValidatorMapper dataValidatorMapper)
{
for ( JsonNode child : node )
{
@@ -104,7 +104,7 @@ public class SchemaSetLoader
}
}
- private void readNode(ImmutableMap.Builder<SchemaKey, Schema> builder, JsonNode node, DataValidatorMapper dataValidatorMapper)
+ private void readNode(ImmutableMap.Builder<String, Schema> builder, JsonNode node, DataValidatorMapper dataValidatorMapper)
{
String name = getText(node, "name", null);
String path = getText(node, "path", null);
@@ -136,7 +136,7 @@ public class SchemaSetLoader
.watched(getAllowance(node, "watched"))
.canBeDeleted(getBoolean(node, "canBeDeleted"))
.build();
- builder.put(new SchemaKey(name), schema);
+ builder.put(name, schema);
}
private String getText(JsonNode node, String name, String defaultValue)
http://git-wip-us.apache.org/repos/asf/curator/blob/923a2d8b/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java b/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
index eadae80..f3754f5 100644
--- a/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
+++ b/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
@@ -43,7 +43,9 @@ public class TestSchema extends BaseClassForTests
try
{
- client.create().creatingParentsIfNeeded().forPath(schemaSet.getNamedSchema(SchemaKey.named("test")).getRawPath());
+ String rawPath = schemaSet.getNamedSchema("test").getRawPath();
+ Assert.assertEquals(rawPath, "/a/b/c");
+ client.create().creatingParentsIfNeeded().forPath(rawPath);
Assert.fail("Should've violated schema");
}
catch ( SchemaViolation dummy )
[04/16] curator git commit: Log the schema at start
Posted by ra...@apache.org.
Log the schema at start
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/df3c1c4b
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/df3c1c4b
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/df3c1c4b
Branch: refs/heads/CURATOR-322
Commit: df3c1c4b059fc9c930716f91230dbe8a466fef08
Parents: f25a812
Author: randgalt <ra...@apache.org>
Authored: Mon May 2 16:12:47 2016 -0500
Committer: randgalt <ra...@apache.org>
Committed: Mon May 2 16:12:47 2016 -0500
----------------------------------------------------------------------
.../apache/curator/framework/CuratorFrameworkFactory.java | 9 ++++++++-
.../apache/curator/framework/imps/CuratorFrameworkImpl.java | 2 ++
2 files changed, 10 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/df3c1c4b/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 5aed199..c17534f 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
@@ -144,7 +144,14 @@ public class CuratorFrameworkFactory
private boolean useContainerParentsIfAvailable = true;
private ConnectionStateErrorPolicy connectionStateErrorPolicy = new StandardConnectionStateErrorPolicy();
private ConnectionHandlingPolicy connectionHandlingPolicy = Boolean.getBoolean("curator-use-classic-connection-handling") ? new ClassicConnectionHandlingPolicy() : new StandardConnectionHandlingPolicy();
- private SchemaSet schemaSet = new SchemaSet();
+ private SchemaSet schemaSet = new SchemaSet()
+ {
+ @Override
+ public String toDocumentation()
+ {
+ return "Default schema";
+ }
+ };
/**
* Apply the current values and build a new CuratorFramework
http://git-wip-us.apache.org/repos/asf/curator/blob/df3c1c4b/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 739f07e..f5ae213 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
@@ -317,6 +317,8 @@ public class CuratorFrameworkImpl implements CuratorFramework
});
ensembleTracker.start();
+
+ log.info(schemaSet.toDocumentation());
}
catch ( Exception e )
{
[08/16] curator git commit: Make regex optional and add a key to each
schema
Posted by ra...@apache.org.
Make regex optional and add a key to each schema
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/f83c8efc
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/f83c8efc
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/f83c8efc
Branch: refs/heads/CURATOR-322
Commit: f83c8efcfbd4781d64ef4e7ef37c12ff68de12a1
Parents: 3f47f6a
Author: randgalt <ra...@apache.org>
Authored: Mon May 2 17:03:54 2016 -0500
Committer: randgalt <ra...@apache.org>
Committed: Mon May 2 17:03:54 2016 -0500
----------------------------------------------------------------------
.../curator/framework/schema/SchemaKey.java | 59 ++++++++++++++++++++
1 file changed, 59 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/f83c8efc/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaKey.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaKey.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaKey.java
new file mode 100644
index 0000000..0faabb1
--- /dev/null
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaKey.java
@@ -0,0 +1,59 @@
+package org.apache.curator.framework.schema;
+
+import com.google.common.base.Preconditions;
+import java.util.UUID;
+
+public class SchemaKey
+{
+ private final String key;
+
+ public static SchemaKey named(String name)
+ {
+ return new SchemaKey(name);
+ }
+
+ public SchemaKey()
+ {
+ this(UUID.randomUUID().toString());
+ }
+
+ public SchemaKey(String key)
+ {
+ this.key = Preconditions.checkNotNull(key, "key cannot be null");
+ }
+
+ public String getKey()
+ {
+ return key;
+ }
+
+ @Override
+ public boolean equals(Object o)
+ {
+ if ( this == o )
+ {
+ return true;
+ }
+ if ( o == null || getClass() != o.getClass() )
+ {
+ return false;
+ }
+
+ SchemaKey schemaKey = (SchemaKey)o;
+
+ return key.equals(schemaKey.key);
+
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return key.hashCode();
+ }
+
+ @Override
+ public String toString()
+ {
+ return key;
+ }
+}
[16/16] curator git commit: test transactions
Posted by ra...@apache.org.
test transactions
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/ae6cb4be
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/ae6cb4be
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/ae6cb4be
Branch: refs/heads/CURATOR-322
Commit: ae6cb4be4ee56215bef26e8d7fb5139d9b81d026
Parents: 923a2d8
Author: randgalt <ra...@apache.org>
Authored: Tue May 3 10:19:46 2016 -0500
Committer: randgalt <ra...@apache.org>
Committed: Tue May 3 10:19:46 2016 -0500
----------------------------------------------------------------------
.../curator/framework/schema/TestSchema.java | 83 ++++++++++++++++++++
.../src/test/resources/schema4.json | 14 ++++
2 files changed, 97 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/ae6cb4be/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java b/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
index f3754f5..e094c0e 100644
--- a/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
+++ b/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
@@ -22,6 +22,7 @@ import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
+import org.apache.curator.framework.api.transaction.CuratorOp;
import org.apache.curator.retry.RetryOneTime;
import org.apache.curator.test.BaseClassForTests;
import org.apache.curator.utils.CloseableUtils;
@@ -139,6 +140,88 @@ public class TestSchema extends BaseClassForTests
}
}
+ @Test
+ public void testTransaction() throws Exception
+ {
+ final DataValidator dataValidator = new DataValidator()
+ {
+ @Override
+ public boolean isValid(String path, byte[] data)
+ {
+ return data.length > 0;
+ }
+ };
+ SchemaSetLoader.DataValidatorMapper dataValidatorMapper = new SchemaSetLoader.DataValidatorMapper()
+ {
+ @Override
+ public DataValidator getDataValidator(String name)
+ {
+ return dataValidator;
+ }
+ };
+ SchemaSet schemaSet = loadSchemaSet("schema4.json", dataValidatorMapper);
+ CuratorFramework client = newClient(schemaSet);
+ try
+ {
+ client.start();
+
+ CuratorOp createAPersistent = client.transactionOp().create().forPath("/a");
+ CuratorOp createAEphemeral = client.transactionOp().create().withMode(CreateMode.EPHEMERAL).forPath("/a");
+ CuratorOp deleteA = client.transactionOp().delete().forPath("/a");
+ CuratorOp createBEmptyData = client.transactionOp().create().forPath("/b", new byte[0]);
+ CuratorOp createBWithData = client.transactionOp().create().forPath("/b", new byte[10]);
+ CuratorOp setBEmptyData = client.transactionOp().setData().forPath("/b", new byte[0]);
+ CuratorOp setBWithData = client.transactionOp().setData().forPath("/b", new byte[10]);
+
+ try
+ {
+ client.transaction().forOperations(createAPersistent, createAEphemeral);
+ Assert.fail("Should've violated schema");
+ }
+ catch ( SchemaViolation dummy )
+ {
+ // expected
+ }
+ client.transaction().forOperations(createAEphemeral);
+
+ try
+ {
+ client.transaction().forOperations(deleteA);
+ Assert.fail("Should've violated schema");
+ }
+ catch ( SchemaViolation dummy )
+ {
+ // expected
+ }
+
+ try
+ {
+ client.transaction().forOperations(createBEmptyData);
+ Assert.fail("Should've violated schema");
+ }
+ catch ( SchemaViolation dummy )
+ {
+ // expected
+ }
+ client.transaction().forOperations(createBWithData);
+
+ try
+ {
+ client.transaction().forOperations(setBEmptyData);
+ Assert.fail("Should've violated schema");
+ }
+ catch ( SchemaViolation dummy )
+ {
+ // expected
+ }
+ client.transaction().forOperations(setBWithData);
+ }
+ finally
+ {
+ CloseableUtils.closeQuietly(client);
+ }
+ }
+
@Override
protected boolean enabledSessionExpiredStateAware()
{
http://git-wip-us.apache.org/repos/asf/curator/blob/ae6cb4be/curator-framework/src/test/resources/schema4.json
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/resources/schema4.json b/curator-framework/src/test/resources/schema4.json
new file mode 100644
index 0000000..6fec36d
--- /dev/null
+++ b/curator-framework/src/test/resources/schema4.json
@@ -0,0 +1,14 @@
+[
+ {
+ "name": "testa",
+ "path": "/a",
+ "ephemeral": "must",
+ "canBeDeleted": false
+ },
+
+ {
+ "name": "testb",
+ "path": "/b",
+ "dataValidator": "test"
+ }
+]
\ No newline at end of file
[03/16] curator git commit: Further implementation of schema support
Posted by ra...@apache.org.
Further implementation of schema support
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/f25a8126
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/f25a8126
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/f25a8126
Branch: refs/heads/CURATOR-322
Commit: f25a81263a5cfb4895f4aa147eec8374dbbb510e
Parents: da760f9
Author: randgalt <ra...@apache.org>
Authored: Mon May 2 16:07:26 2016 -0500
Committer: randgalt <ra...@apache.org>
Committed: Mon May 2 16:07:26 2016 -0500
----------------------------------------------------------------------
.../imps/CuratorMultiTransactionImpl.java | 22 +++++++++++++-
.../framework/imps/DeleteBuilderImpl.java | 2 ++
.../framework/imps/ExistsBuilderImpl.java | 2 ++
.../framework/imps/GetChildrenBuilderImpl.java | 2 ++
.../framework/imps/GetDataBuilderImpl.java | 2 ++
.../framework/imps/SetDataBuilderImpl.java | 2 ++
.../apache/curator/framework/imps/Watching.java | 5 +++
.../apache/curator/framework/schema/Schema.java | 32 +++++++++++++++++---
.../curator/framework/schema/SchemaBuilder.java | 21 ++++++++-----
9 files changed, 78 insertions(+), 12 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/f25a8126/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java b/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java
index da37a06..49824da 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java
@@ -32,13 +32,17 @@ import org.apache.curator.framework.api.transaction.CuratorMultiTransaction;
import org.apache.curator.framework.api.transaction.CuratorMultiTransactionMain;
import org.apache.curator.framework.api.transaction.CuratorOp;
import org.apache.curator.framework.api.transaction.CuratorTransactionResult;
+import org.apache.curator.framework.schema.Schema;
import org.apache.zookeeper.AsyncCallback;
+import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.OpResult;
+import org.apache.zookeeper.ZooDefs;
+import org.apache.zookeeper.proto.CreateRequest;
+import org.apache.zookeeper.proto.SetDataRequest;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
-import java.util.concurrent.ThreadFactory;
public class CuratorMultiTransactionImpl implements
CuratorMultiTransaction,
@@ -119,7 +123,23 @@ public class CuratorMultiTransactionImpl implements
CuratorMultiTransactionRecord record = new CuratorMultiTransactionRecord();
for ( CuratorOp curatorOp : operations )
{
+ Schema schema = client.getSchemaSet().getSchema(curatorOp.getTypeAndPath().getForPath());
record.add(curatorOp.get(), curatorOp.getTypeAndPath().getType(), curatorOp.getTypeAndPath().getForPath());
+ if ( (curatorOp.get().getType() == ZooDefs.OpCode.create) || (curatorOp.get().getType() == ZooDefs.OpCode.createContainer) )
+ {
+ CreateRequest createRequest = (CreateRequest)curatorOp.get().toRequestRecord();
+ CreateMode createMode = CreateMode.fromFlag(createRequest.getFlags(), CreateMode.PERSISTENT);
+ schema.validateCreate(createMode, createRequest.getData());
+ }
+ else if ( (curatorOp.get().getType() == ZooDefs.OpCode.delete) || (curatorOp.get().getType() == ZooDefs.OpCode.deleteContainer) )
+ {
+ schema.validateDeletion();
+ }
+ else if ( curatorOp.get().getType() == ZooDefs.OpCode.setData )
+ {
+ SetDataRequest setDataRequest = (SetDataRequest)curatorOp.get().toRequestRecord();
+ schema.validateData(setDataRequest.getData());
+ }
}
if ( backgrounding.inBackground() )
http://git-wip-us.apache.org/repos/asf/curator/blob/f25a8126/curator-framework/src/main/java/org/apache/curator/framework/imps/DeleteBuilderImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/DeleteBuilderImpl.java b/curator-framework/src/main/java/org/apache/curator/framework/imps/DeleteBuilderImpl.java
index 678b0cb..54aac54 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/DeleteBuilderImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/DeleteBuilderImpl.java
@@ -213,6 +213,8 @@ class DeleteBuilderImpl implements DeleteBuilder, BackgroundOperation<String>, E
@Override
public Void forPath(String path) throws Exception
{
+ client.getSchemaSet().getSchema(path).validateDeletion();
+
final String unfixedPath = path;
path = client.fixForNamespace(path);
http://git-wip-us.apache.org/repos/asf/curator/blob/f25a8126/curator-framework/src/main/java/org/apache/curator/framework/imps/ExistsBuilderImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/ExistsBuilderImpl.java b/curator-framework/src/main/java/org/apache/curator/framework/imps/ExistsBuilderImpl.java
index cead168..11721de 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/ExistsBuilderImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/ExistsBuilderImpl.java
@@ -158,6 +158,8 @@ class ExistsBuilderImpl implements ExistsBuilder, BackgroundOperation<String>, E
{
path = client.fixForNamespace(path);
+ client.getSchemaSet().getSchema(path).validateWatcher(watching.isWatched() || watching.hasWatcher());
+
Stat returnStat = null;
if ( backgrounding.inBackground() )
{
http://git-wip-us.apache.org/repos/asf/curator/blob/f25a8126/curator-framework/src/main/java/org/apache/curator/framework/imps/GetChildrenBuilderImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/GetChildrenBuilderImpl.java b/curator-framework/src/main/java/org/apache/curator/framework/imps/GetChildrenBuilderImpl.java
index 8c7efa4..e88f58f 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/GetChildrenBuilderImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/GetChildrenBuilderImpl.java
@@ -195,6 +195,8 @@ class GetChildrenBuilderImpl implements GetChildrenBuilder, BackgroundOperation<
@Override
public List<String> forPath(String path) throws Exception
{
+ client.getSchemaSet().getSchema(path).validateWatcher(watching.isWatched() || watching.hasWatcher());
+
path = client.fixForNamespace(path);
List<String> children = null;
http://git-wip-us.apache.org/repos/asf/curator/blob/f25a8126/curator-framework/src/main/java/org/apache/curator/framework/imps/GetDataBuilderImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/GetDataBuilderImpl.java b/curator-framework/src/main/java/org/apache/curator/framework/imps/GetDataBuilderImpl.java
index d937d00..78bfd39 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/GetDataBuilderImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/GetDataBuilderImpl.java
@@ -275,6 +275,8 @@ class GetDataBuilderImpl implements GetDataBuilder, BackgroundOperation<String>,
@Override
public byte[] forPath(String path) throws Exception
{
+ client.getSchemaSet().getSchema(path).validateWatcher(watching.isWatched() || watching.hasWatcher());
+
path = client.fixForNamespace(path);
byte[] responseData = null;
http://git-wip-us.apache.org/repos/asf/curator/blob/f25a8126/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java b/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java
index ee51b9e..1f9b335 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java
@@ -241,6 +241,8 @@ class SetDataBuilderImpl implements SetDataBuilder, BackgroundOperation<PathAndB
@Override
public Stat forPath(String path, byte[] data) throws Exception
{
+ client.getSchemaSet().getSchema(path).validateData(data);
+
if ( compress )
{
data = client.getCompressionProvider().compress(path, data);
http://git-wip-us.apache.org/repos/asf/curator/blob/f25a8126/curator-framework/src/main/java/org/apache/curator/framework/imps/Watching.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/Watching.java b/curator-framework/src/main/java/org/apache/curator/framework/imps/Watching.java
index 2058c3b..568f308 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/Watching.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/Watching.java
@@ -88,6 +88,11 @@ class Watching
return namespaceWatcher;
}
+ boolean hasWatcher()
+ {
+ return (watcher != null) || (curatorWatcher != null);
+ }
+
boolean isWatched()
{
return watched;
http://git-wip-us.apache.org/repos/asf/curator/blob/f25a8126/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
index 7b8f1d5..ed8fecb 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
@@ -13,6 +13,7 @@ public class Schema
private final Allowance sequential;
private final boolean canBeWatched;
private final boolean canHaveChildren;
+ private final boolean canBeDeleted;
public enum Allowance
{
@@ -26,7 +27,7 @@ public class Schema
return new SchemaBuilder(Pattern.compile(path));
}
- public Schema(Pattern path, String documentation, DataValidator dataValidator, Allowance ephemeral, Allowance sequential, boolean canBeWatched, boolean canHaveChildren)
+ public Schema(Pattern path, String documentation, DataValidator dataValidator, Allowance ephemeral, Allowance sequential, boolean canBeWatched, boolean canHaveChildren, boolean canBeDeleted)
{
this.path = Preconditions.checkNotNull(path, "path cannot be null");
this.documentation = Preconditions.checkNotNull(documentation, "documentation cannot be null");
@@ -35,6 +36,23 @@ public class Schema
this.sequential = Preconditions.checkNotNull(sequential, "sequential cannot be null");
this.canBeWatched = canBeWatched;
this.canHaveChildren = canHaveChildren;
+ this.canBeDeleted = canBeDeleted;
+ }
+
+ public void validateDeletion()
+ {
+ if ( !canBeDeleted )
+ {
+ throw new SchemaViolation(this, "Cannot be deleted");
+ }
+ }
+
+ public void validateWatcher(boolean isWatching)
+ {
+ if ( isWatching && !canBeWatched )
+ {
+ throw new SchemaViolation(this, "Cannot be watched");
+ }
}
public void validateCreate(CreateMode mode, byte[] data)
@@ -95,16 +113,21 @@ public class Schema
return sequential;
}
- public boolean isCanBeWatched()
+ public boolean canBeWatched()
{
return canBeWatched;
}
- public boolean isCanHaveChildren()
+ public boolean canHaveChildren()
{
return canHaveChildren;
}
+ public boolean canBeDeleted()
+ {
+ return canBeDeleted;
+ }
+
@Override
public boolean equals(Object o)
{
@@ -140,6 +163,7 @@ public class Schema
", isSequential=" + sequential +
", canBeWatched=" + canBeWatched +
", canHaveChildren=" + canHaveChildren +
+ ", canBeDeleted=" + canBeDeleted +
'}';
}
@@ -148,7 +172,7 @@ public class Schema
return path.pattern() + '\n'
+ documentation + '\n'
+ "Validator: " + dataValidator.getClass().getSimpleName() + '\n'
- + String.format("ephemeral: %s | sequential: %s | canBeWatched: %s | canHaveChildren: %s", ephemeral, sequential, canBeWatched, canHaveChildren) + '\n'
+ + String.format("ephemeral: %s | sequential: %s | canBeWatched: %s | canHaveChildren: %s | canBeDeleted: %s", ephemeral, sequential, canBeWatched, canHaveChildren, canBeDeleted) + '\n'
;
}
}
http://git-wip-us.apache.org/repos/asf/curator/blob/f25a8126/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
index 053ccfc..383a16b 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
@@ -8,14 +8,15 @@ public class SchemaBuilder
private Pattern path;
private String documentation = "";
private DataValidator dataValidator = new DefaultDataValidator();
- private boolean isEphemeral = false;
- private boolean isSequential = false;
+ private Schema.Allowance ephemeral = Schema.Allowance.CAN;
+ private Schema.Allowance sequential = Schema.Allowance.CAN;
private boolean canBeWatched = true;
private boolean canHaveChildren = true;
+ private boolean canBeDeleted = true;
public Schema build()
{
- return new Schema(path, documentation, dataValidator, isEphemeral, isSequential, canBeWatched, canHaveChildren);
+ return new Schema(path, documentation, dataValidator, ephemeral, sequential, canBeWatched, canHaveChildren, canBeDeleted);
}
public SchemaBuilder documentation(String documentation)
@@ -30,15 +31,15 @@ public class SchemaBuilder
return this;
}
- public SchemaBuilder isEphemeral(boolean isEphemeral)
+ public SchemaBuilder ephemeral(Schema.Allowance ephemeral)
{
- this.isEphemeral = isEphemeral;
+ this.ephemeral = ephemeral;
return this;
}
- public SchemaBuilder isSequential(boolean isSequential)
+ public SchemaBuilder sequential(Schema.Allowance sequential)
{
- this.isSequential = isSequential;
+ this.sequential = sequential;
return this;
}
@@ -54,6 +55,12 @@ public class SchemaBuilder
return this;
}
+ public SchemaBuilder canBeDeleted(boolean canBeDeleted)
+ {
+ this.canBeDeleted = canBeDeleted;
+ return this;
+ }
+
SchemaBuilder(Pattern path)
{
this.path = Preconditions.checkNotNull(path, "path cannot be null");
[07/16] curator git commit: Make regex optional and add a key to each
schema
Posted by ra...@apache.org.
Make regex optional and add a key to each schema
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/3f47f6a3
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/3f47f6a3
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/3f47f6a3
Branch: refs/heads/CURATOR-322
Commit: 3f47f6a384d5a7459ec088b3c621a48c04731ebc
Parents: 33534a0
Author: randgalt <ra...@apache.org>
Authored: Mon May 2 17:03:49 2016 -0500
Committer: randgalt <ra...@apache.org>
Committed: Mon May 2 17:03:49 2016 -0500
----------------------------------------------------------------------
.../apache/curator/framework/schema/Schema.java | 63 +++++++-------------
.../curator/framework/schema/SchemaBuilder.java | 10 ++--
.../curator/framework/schema/SchemaSet.java | 57 +++++++++++++-----
3 files changed, 69 insertions(+), 61 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/3f47f6a3/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
index 05aa6f1..16d335a 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
@@ -9,7 +9,8 @@ import java.util.regex.Pattern;
*/
public class Schema
{
- private final Pattern path;
+ private final Pattern pathRegex;
+ private final String path;
private final String documentation;
private final DataValidator dataValidator;
private final Allowance ephemeral;
@@ -25,30 +26,33 @@ public class Schema
}
/**
- * Start a builder for the given path pattern.
+ * Start a builder for the given path pattern. Note: full path schemas
+ * take precedence over regex path schemas.
*
- * @param pathRegex regex for the path. This schema applies to all matching paths
+ * @param path full ZNode path. This schema only applies to an exact match
* @return builder
*/
- public static SchemaBuilder builder(String pathRegex)
+ public static SchemaBuilder builder(String path)
{
- return builder(Pattern.compile(pathRegex));
+ return new SchemaBuilder(null, path);
}
/**
* Start a builder for the given path pattern.
*
- * @param pathRegex regex for the path. This schema applies to all matching paths
+ * @param pathRegex regex for the path. This schema applies to any matching paths
* @return builder
*/
public static SchemaBuilder builder(Pattern pathRegex)
{
- return new SchemaBuilder(pathRegex);
+ return new SchemaBuilder(pathRegex, null);
}
- Schema(Pattern path, String documentation, DataValidator dataValidator, Allowance ephemeral, Allowance sequential, Allowance watched, boolean canBeDeleted)
+ Schema(Pattern pathRegex, String path, String documentation, DataValidator dataValidator, Allowance ephemeral, Allowance sequential, Allowance watched, boolean canBeDeleted)
{
- this.path = Preconditions.checkNotNull(path, "path cannot be null");
+ Preconditions.checkNotNull((pathRegex != null) || (path != null), "pathRegex and path cannot both be null");
+ this.pathRegex = pathRegex;
+ this.path = path;
this.documentation = Preconditions.checkNotNull(documentation, "documentation cannot be null");
this.dataValidator = Preconditions.checkNotNull(dataValidator, "dataValidator cannot be null");
this.ephemeral = Preconditions.checkNotNull(ephemeral, "ephemeral cannot be null");
@@ -111,39 +115,14 @@ public class Schema
}
}
- public Pattern getPath()
- {
- return path;
- }
-
- public String getDocumentation()
- {
- return documentation;
- }
-
- public DataValidator getDataValidator()
- {
- return dataValidator;
- }
-
- public Allowance getEphemeral()
- {
- return ephemeral;
- }
-
- public Allowance getSequential()
+ public Pattern getPathRegex()
{
- return sequential;
+ return pathRegex;
}
- public Schema.Allowance getWatched()
+ public String getPath()
{
- return watched;
- }
-
- public boolean canBeDeleted()
- {
- return canBeDeleted;
+ return path;
}
@Override
@@ -160,21 +139,21 @@ public class Schema
Schema schema = (Schema)o;
- return path.equals(schema.path);
+ return pathRegex.equals(schema.pathRegex);
}
@Override
public int hashCode()
{
- return path.hashCode();
+ return pathRegex.hashCode();
}
@Override
public String toString()
{
return "Schema{" +
- "path=" + path +
+ "path=" + pathRegex +
", documentation='" + documentation + '\'' +
", dataValidator=" + dataValidator +
", isEphemeral=" + ephemeral +
@@ -186,7 +165,7 @@ public class Schema
public String toDocumentation()
{
- return path.pattern() + '\n'
+ return pathRegex.pattern() + '\n'
+ documentation + '\n'
+ "Validator: " + dataValidator.getClass().getSimpleName() + '\n'
+ String.format("ephemeral: %s | sequential: %s | watched: %s | | canBeDeleted: %s", ephemeral, sequential, watched, canBeDeleted) + '\n'
http://git-wip-us.apache.org/repos/asf/curator/blob/3f47f6a3/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
index 33b42ce..64a6b73 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
@@ -5,7 +5,8 @@ import java.util.regex.Pattern;
public class SchemaBuilder
{
- private Pattern path;
+ private final Pattern pathRegex;
+ private final String path;
private String documentation = "";
private DataValidator dataValidator = new DefaultDataValidator();
private Schema.Allowance ephemeral = Schema.Allowance.CAN;
@@ -20,7 +21,7 @@ public class SchemaBuilder
*/
public Schema build()
{
- return new Schema(path, documentation, dataValidator, ephemeral, sequential, watched, canBeDeleted);
+ return new Schema(pathRegex, path, documentation, dataValidator, ephemeral, sequential, watched, canBeDeleted);
}
/**
@@ -83,8 +84,9 @@ public class SchemaBuilder
return this;
}
- SchemaBuilder(Pattern path)
+ SchemaBuilder(Pattern pathRegex, String path)
{
- this.path = Preconditions.checkNotNull(path, "path cannot be null");
+ this.pathRegex = pathRegex;
+ this.path = path;
}
}
http://git-wip-us.apache.org/repos/asf/curator/blob/3f47f6a3/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
index 113e42a..1014dc8 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
@@ -4,13 +4,12 @@ import com.google.common.base.Preconditions;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
-import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.util.Collection;
import java.util.Collections;
+import java.util.Map;
import java.util.concurrent.ExecutionException;
-import java.util.regex.Pattern;
/**
* Collection of all schemas for a Curator instance
@@ -18,15 +17,16 @@ import java.util.regex.Pattern;
public class SchemaSet
{
private final Logger log = LoggerFactory.getLogger(getClass());
- private final Collection<Schema> schemas;
+ private final Map<SchemaKey, Schema> schemas;
+ private final Map<String, Schema> pathSchemas;
private final CacheLoader<String, Schema> cacheLoader = new CacheLoader<String, Schema>()
{
@Override
public Schema load(String path) throws Exception
{
- for ( Schema schema : schemas )
+ for ( Schema schema : schemas.values() )
{
- if ( schema.getPath().matcher(path).matches() )
+ if ( (schema.getPathRegex() != null) && schema.getPathRegex().matcher(path).matches() )
{
log.debug("path -> {}", schema);
return schema;
@@ -35,12 +35,12 @@ public class SchemaSet
return defaultSchema;
}
};
- private final LoadingCache<String, Schema> cache = CacheBuilder
+ private final LoadingCache<String, Schema> regexCache = CacheBuilder
.newBuilder()
.softValues()
.build(cacheLoader);
- private static final Schema defaultSchema = new Schema(Pattern.compile(".*"), "Default schema", new DefaultDataValidator(), Schema.Allowance.CAN, Schema.Allowance.CAN, Schema.Allowance.CAN, true);
+ private static final Schema defaultSchema = new Schema(null, "", "Default schema", new DefaultDataValidator(), Schema.Allowance.CAN, Schema.Allowance.CAN, Schema.Allowance.CAN, true);
/**
* Return the default (empty) schema set
@@ -49,7 +49,7 @@ public class SchemaSet
*/
public static SchemaSet getDefaultSchemaSet()
{
- return new SchemaSet(Collections.<Schema>emptySet())
+ return new SchemaSet(Collections.<SchemaKey, Schema>emptyMap())
{
@Override
public String toDocumentation()
@@ -60,11 +60,21 @@ public class SchemaSet
}
/**
- * @param schemas the schemas for the set
+ * @param schemas the schemas for the set. The key of the map is a key/name for the schema that can be
+ * used when calling {@link #getNamedSchema(SchemaKey)}
*/
- public SchemaSet(Collection<Schema> schemas)
+ public SchemaSet(Map<SchemaKey, Schema> schemas)
{
- this.schemas = ImmutableSet.copyOf(Preconditions.checkNotNull(schemas, "schemas cannot be null"));
+ this.schemas = ImmutableMap.copyOf(Preconditions.checkNotNull(schemas, "schemas cannot be null"));
+ ImmutableMap.Builder<String, Schema> builder = ImmutableMap.builder();
+ for ( Schema schema : schemas.values() )
+ {
+ if ( schema.getPath() != null )
+ {
+ builder.put(schema.getPath(), schema);
+ }
+ }
+ pathSchemas = builder.build();
}
/**
@@ -79,9 +89,15 @@ public class SchemaSet
{
return defaultSchema;
}
+ Schema schema = pathSchemas.get(path);
+ if ( schema != null )
+ {
+ return schema;
+ }
+
try
{
- return cache.get(path);
+ return regexCache.get(path);
}
catch ( ExecutionException e )
{
@@ -90,6 +106,17 @@ public class SchemaSet
}
/**
+ * Return the schema with the given key/name
+ *
+ * @param name name
+ * @return schema or null
+ */
+ public Schema getNamedSchema(SchemaKey name)
+ {
+ return schemas.get(name);
+ }
+
+ /**
* Build a user displayable documentation string for the schemas in this set
*
* @return documentation
@@ -97,9 +124,9 @@ public class SchemaSet
public String toDocumentation()
{
StringBuilder str = new StringBuilder("Curator Schemas:\n\n");
- for ( Schema schema : schemas )
+ for ( Map.Entry<SchemaKey, Schema> schemaEntry : schemas.entrySet() )
{
- str.append(schema).append('\n');
+ str.append(schemaEntry.getKey()).append('\n').append(schemaEntry.getValue()).append('\n');
}
return str.toString();
}
[13/16] curator git commit: moved test to better package
Posted by ra...@apache.org.
moved test to better package
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/30d83b87
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/30d83b87
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/30d83b87
Branch: refs/heads/CURATOR-322
Commit: 30d83b87e9ddcee78d21615d45995f13ef1832ff
Parents: e3ec920
Author: randgalt <ra...@apache.org>
Authored: Mon May 2 23:41:26 2016 -0500
Committer: randgalt <ra...@apache.org>
Committed: Mon May 2 23:41:26 2016 -0500
----------------------------------------------------------------------
.../curator/framework/imps/TestSchema.java | 115 -------------------
.../curator/framework/schema/TestSchema.java | 111 ++++++++++++++++++
2 files changed, 111 insertions(+), 115 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/30d83b87/curator-framework/src/test/java/org/apache/curator/framework/imps/TestSchema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestSchema.java b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestSchema.java
deleted file mode 100644
index c908fd5..0000000
--- a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestSchema.java
+++ /dev/null
@@ -1,115 +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.framework.imps;
-
-import com.google.common.base.Charsets;
-import com.google.common.io.Resources;
-import org.apache.curator.framework.CuratorFramework;
-import org.apache.curator.framework.CuratorFrameworkFactory;
-import org.apache.curator.framework.schema.SchemaKey;
-import org.apache.curator.framework.schema.SchemaSet;
-import org.apache.curator.framework.schema.SchemaSetLoader;
-import org.apache.curator.framework.schema.SchemaViolation;
-import org.apache.curator.retry.RetryOneTime;
-import org.apache.curator.test.BaseClassForTests;
-import org.apache.curator.utils.CloseableUtils;
-import org.apache.zookeeper.CreateMode;
-import org.testng.Assert;
-import org.testng.annotations.Test;
-import java.io.IOException;
-
-public class TestSchema extends BaseClassForTests
-{
- @Test
- public void testBasics() throws Exception
- {
- SchemaSet schemaSet = loadSchemaSet("schema1.json");
- CuratorFramework client = newClient(schemaSet);
- try
- {
- client.start();
-
- try
- {
- client.create().creatingParentsIfNeeded().forPath(schemaSet.getNamedSchema(SchemaKey.named("test")).getRawPath());
- Assert.fail("Should've violated schema");
- }
- catch ( SchemaViolation dummy )
- {
- // expected
- }
-
- client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/a/b/c");
- }
- finally
- {
- CloseableUtils.closeQuietly(client);
- }
- }
-
- @Test
- public void testMulti() throws Exception
- {
- SchemaSet schemaSet = loadSchemaSet("schema2.json");
- CuratorFramework client = newClient(schemaSet);
- try
- {
- client.start();
-
- try
- {
- client.create().creatingParentsIfNeeded().forPath("/a/b/c");
- Assert.fail("Should've violated schema: test");
- }
- catch ( SchemaViolation dummy )
- {
- // expected
- }
-
- try
- {
- client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/a/b/c/d/e");
- Assert.fail("Should've violated schema: test2");
- }
- catch ( SchemaViolation dummy )
- {
- // expected
- }
- }
- finally
- {
- CloseableUtils.closeQuietly(client);
- }
- }
-
- private CuratorFramework newClient(SchemaSet schemaSet)
- {
- return CuratorFrameworkFactory.builder()
- .connectString(server.getConnectString())
- .retryPolicy(new RetryOneTime(1))
- .schemaSet(schemaSet)
- .build();
- }
-
- private SchemaSet loadSchemaSet(String name) throws IOException
- {
- String json = Resources.toString(Resources.getResource(name), Charsets.UTF_8);
- return new SchemaSetLoader(json, null).toSchemaSet(true);
- }
-}
http://git-wip-us.apache.org/repos/asf/curator/blob/30d83b87/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java b/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
new file mode 100644
index 0000000..7f29148
--- /dev/null
+++ b/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
@@ -0,0 +1,111 @@
+/**
+ * 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.framework.schema;
+
+import com.google.common.base.Charsets;
+import com.google.common.io.Resources;
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.curator.framework.CuratorFrameworkFactory;
+import org.apache.curator.retry.RetryOneTime;
+import org.apache.curator.test.BaseClassForTests;
+import org.apache.curator.utils.CloseableUtils;
+import org.apache.zookeeper.CreateMode;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+import java.io.IOException;
+
+public class TestSchema extends BaseClassForTests
+{
+ @Test
+ public void testBasics() throws Exception
+ {
+ SchemaSet schemaSet = loadSchemaSet("schema1.json");
+ CuratorFramework client = newClient(schemaSet);
+ try
+ {
+ client.start();
+
+ try
+ {
+ client.create().creatingParentsIfNeeded().forPath(schemaSet.getNamedSchema(SchemaKey.named("test")).getRawPath());
+ Assert.fail("Should've violated schema");
+ }
+ catch ( SchemaViolation dummy )
+ {
+ // expected
+ }
+
+ client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/a/b/c");
+ }
+ finally
+ {
+ CloseableUtils.closeQuietly(client);
+ }
+ }
+
+ @Test
+ public void testMulti() throws Exception
+ {
+ SchemaSet schemaSet = loadSchemaSet("schema2.json");
+ CuratorFramework client = newClient(schemaSet);
+ try
+ {
+ client.start();
+
+ try
+ {
+ client.create().creatingParentsIfNeeded().forPath("/a/b/c");
+ Assert.fail("Should've violated schema: test");
+ }
+ catch ( SchemaViolation dummy )
+ {
+ // expected
+ }
+
+ try
+ {
+ client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/a/b/c/d/e");
+ Assert.fail("Should've violated schema: test2");
+ }
+ catch ( SchemaViolation dummy )
+ {
+ // expected
+ }
+ }
+ finally
+ {
+ CloseableUtils.closeQuietly(client);
+ }
+ }
+
+ private CuratorFramework newClient(SchemaSet schemaSet)
+ {
+ return CuratorFrameworkFactory.builder()
+ .connectString(server.getConnectString())
+ .retryPolicy(new RetryOneTime(1))
+ .schemaSet(schemaSet)
+ .build();
+ }
+
+ private SchemaSet loadSchemaSet(String name) throws IOException
+ {
+ String json = Resources.toString(Resources.getResource(name), Charsets.UTF_8);
+ return new SchemaSetLoader(json, null).toSchemaSet(true);
+ }
+}
[06/16] curator git commit: documentation and cleanup
Posted by ra...@apache.org.
documentation and cleanup
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/33534a09
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/33534a09
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/33534a09
Branch: refs/heads/CURATOR-322
Commit: 33534a099884fc8336c85aaf518e10485de4efe1
Parents: 7f27631
Author: randgalt <ra...@apache.org>
Authored: Mon May 2 16:35:55 2016 -0500
Committer: randgalt <ra...@apache.org>
Committed: Mon May 2 16:35:55 2016 -0500
----------------------------------------------------------------------
.../framework/CuratorFrameworkFactory.java | 9 +---
.../framework/schema/DefaultDataValidator.java | 3 ++
.../apache/curator/framework/schema/Schema.java | 53 +++++++++++++-------
.../curator/framework/schema/SchemaBuilder.java | 48 +++++++++++++-----
.../curator/framework/schema/SchemaSet.java | 35 +++++++++++--
.../framework/schema/SchemaViolation.java | 18 +++++++
6 files changed, 124 insertions(+), 42 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/33534a09/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 c17534f..915aee1 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
@@ -144,14 +144,7 @@ public class CuratorFrameworkFactory
private boolean useContainerParentsIfAvailable = true;
private ConnectionStateErrorPolicy connectionStateErrorPolicy = new StandardConnectionStateErrorPolicy();
private ConnectionHandlingPolicy connectionHandlingPolicy = Boolean.getBoolean("curator-use-classic-connection-handling") ? new ClassicConnectionHandlingPolicy() : new StandardConnectionHandlingPolicy();
- private SchemaSet schemaSet = new SchemaSet()
- {
- @Override
- public String toDocumentation()
- {
- return "Default schema";
- }
- };
+ private SchemaSet schemaSet = SchemaSet.getDefaultSchemaSet();
/**
* Apply the current values and build a new CuratorFramework
http://git-wip-us.apache.org/repos/asf/curator/blob/33534a09/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java
index 05e1f7c..f28db57 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java
@@ -1,5 +1,8 @@
package org.apache.curator.framework.schema;
+/**
+ * The default data validator - always returns true
+ */
public class DefaultDataValidator implements DataValidator
{
@Override
http://git-wip-us.apache.org/repos/asf/curator/blob/33534a09/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
index ed8fecb..05aa6f1 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
@@ -4,6 +4,9 @@ import com.google.common.base.Preconditions;
import org.apache.zookeeper.CreateMode;
import java.util.regex.Pattern;
+/**
+ * Represents and documents operations allowed for a given path pattern
+ */
public class Schema
{
private final Pattern path;
@@ -11,8 +14,7 @@ public class Schema
private final DataValidator dataValidator;
private final Allowance ephemeral;
private final Allowance sequential;
- private final boolean canBeWatched;
- private final boolean canHaveChildren;
+ private final Schema.Allowance watched;
private final boolean canBeDeleted;
public enum Allowance
@@ -22,20 +24,36 @@ public class Schema
CANNOT
}
- public static SchemaBuilder builder(String path)
+ /**
+ * Start a builder for the given path pattern.
+ *
+ * @param pathRegex regex for the path. This schema applies to all matching paths
+ * @return builder
+ */
+ public static SchemaBuilder builder(String pathRegex)
{
- return new SchemaBuilder(Pattern.compile(path));
+ return builder(Pattern.compile(pathRegex));
}
- public Schema(Pattern path, String documentation, DataValidator dataValidator, Allowance ephemeral, Allowance sequential, boolean canBeWatched, boolean canHaveChildren, boolean canBeDeleted)
+ /**
+ * Start a builder for the given path pattern.
+ *
+ * @param pathRegex regex for the path. This schema applies to all matching paths
+ * @return builder
+ */
+ public static SchemaBuilder builder(Pattern pathRegex)
+ {
+ return new SchemaBuilder(pathRegex);
+ }
+
+ Schema(Pattern path, String documentation, DataValidator dataValidator, Allowance ephemeral, Allowance sequential, Allowance watched, boolean canBeDeleted)
{
this.path = Preconditions.checkNotNull(path, "path cannot be null");
this.documentation = Preconditions.checkNotNull(documentation, "documentation cannot be null");
this.dataValidator = Preconditions.checkNotNull(dataValidator, "dataValidator cannot be null");
this.ephemeral = Preconditions.checkNotNull(ephemeral, "ephemeral cannot be null");
this.sequential = Preconditions.checkNotNull(sequential, "sequential cannot be null");
- this.canBeWatched = canBeWatched;
- this.canHaveChildren = canHaveChildren;
+ this.watched = Preconditions.checkNotNull(watched, "watched cannot be null");
this.canBeDeleted = canBeDeleted;
}
@@ -49,10 +67,15 @@ public class Schema
public void validateWatcher(boolean isWatching)
{
- if ( isWatching && !canBeWatched )
+ if ( isWatching && (watched == Allowance.CANNOT) )
{
throw new SchemaViolation(this, "Cannot be watched");
}
+
+ if ( !isWatching && (watched == Allowance.MUST) )
+ {
+ throw new SchemaViolation(this, "Must be watched");
+ }
}
public void validateCreate(CreateMode mode, byte[] data)
@@ -113,14 +136,9 @@ public class Schema
return sequential;
}
- public boolean canBeWatched()
- {
- return canBeWatched;
- }
-
- public boolean canHaveChildren()
+ public Schema.Allowance getWatched()
{
- return canHaveChildren;
+ return watched;
}
public boolean canBeDeleted()
@@ -161,8 +179,7 @@ public class Schema
", dataValidator=" + dataValidator +
", isEphemeral=" + ephemeral +
", isSequential=" + sequential +
- ", canBeWatched=" + canBeWatched +
- ", canHaveChildren=" + canHaveChildren +
+ ", watched=" + watched +
", canBeDeleted=" + canBeDeleted +
'}';
}
@@ -172,7 +189,7 @@ public class Schema
return path.pattern() + '\n'
+ documentation + '\n'
+ "Validator: " + dataValidator.getClass().getSimpleName() + '\n'
- + String.format("ephemeral: %s | sequential: %s | canBeWatched: %s | canHaveChildren: %s | canBeDeleted: %s", ephemeral, sequential, canBeWatched, canHaveChildren, canBeDeleted) + '\n'
+ + String.format("ephemeral: %s | sequential: %s | watched: %s | | canBeDeleted: %s", ephemeral, sequential, watched, canBeDeleted) + '\n'
;
}
}
http://git-wip-us.apache.org/repos/asf/curator/blob/33534a09/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
index 383a16b..33b42ce 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
@@ -10,51 +10,73 @@ public class SchemaBuilder
private DataValidator dataValidator = new DefaultDataValidator();
private Schema.Allowance ephemeral = Schema.Allowance.CAN;
private Schema.Allowance sequential = Schema.Allowance.CAN;
- private boolean canBeWatched = true;
- private boolean canHaveChildren = true;
+ private Schema.Allowance watched = Schema.Allowance.CAN;
private boolean canBeDeleted = true;
+ /**
+ * Build a new schema from the currently set values
+ *
+ * @return new schema
+ */
public Schema build()
{
- return new Schema(path, documentation, dataValidator, ephemeral, sequential, canBeWatched, canHaveChildren, canBeDeleted);
+ return new Schema(path, documentation, dataValidator, ephemeral, sequential, watched, canBeDeleted);
}
+ /**
+ * @param documentation user displayable documentation for the schema
+ * @return this for chaining
+ */
public SchemaBuilder documentation(String documentation)
{
this.documentation = Preconditions.checkNotNull(documentation, "documentation cannot be null");
return this;
}
+ /**
+ * @param dataValidator a data validator - will be used to validate data set for the znode
+ * @return this for chaining
+ */
public SchemaBuilder dataValidator(DataValidator dataValidator)
{
this.dataValidator = Preconditions.checkNotNull(dataValidator, "dataValidator cannot be null");
return this;
}
+ /**
+ * @param ephemeral whether can, must or cannot be ephemeral
+ * @return this for chaining
+ */
public SchemaBuilder ephemeral(Schema.Allowance ephemeral)
{
- this.ephemeral = ephemeral;
+ this.ephemeral = Preconditions.checkNotNull(ephemeral, "ephemeral cannot be null");
return this;
}
+ /**
+ * @param sequential whether can, must or cannot be sequential
+ * @return this for chaining
+ */
public SchemaBuilder sequential(Schema.Allowance sequential)
{
- this.sequential = sequential;
+ this.sequential = Preconditions.checkNotNull(sequential, "sequential cannot be null");
return this;
}
- public SchemaBuilder canBeWatched(boolean canBeWatched)
+ /**
+ * @param watched whether can, must or cannot be watched
+ * @return this for chaining
+ */
+ public SchemaBuilder watched(Schema.Allowance watched)
{
- this.canBeWatched = canBeWatched;
- return this;
- }
-
- public SchemaBuilder canHaveChildren(boolean canHaveChildren)
- {
- this.canHaveChildren = canHaveChildren;
+ this.watched = watched;
return this;
}
+ /**
+ * @param canBeDeleted true if znode can be deleted
+ * @return this for chaining
+ */
public SchemaBuilder canBeDeleted(boolean canBeDeleted)
{
this.canBeDeleted = canBeDeleted;
http://git-wip-us.apache.org/repos/asf/curator/blob/33534a09/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
index 8a6e19c..113e42a 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
@@ -12,6 +12,9 @@ import java.util.Collections;
import java.util.concurrent.ExecutionException;
import java.util.regex.Pattern;
+/**
+ * Collection of all schemas for a Curator instance
+ */
public class SchemaSet
{
private final Logger log = LoggerFactory.getLogger(getClass());
@@ -37,18 +40,39 @@ public class SchemaSet
.softValues()
.build(cacheLoader);
- private static final Schema defaultSchema = new Schema(Pattern.compile(".*"), "Default schema", new DefaultDataValidator(), Schema.Allowance.CAN, Schema.Allowance.CAN, true, true, true);
+ private static final Schema defaultSchema = new Schema(Pattern.compile(".*"), "Default schema", new DefaultDataValidator(), Schema.Allowance.CAN, Schema.Allowance.CAN, Schema.Allowance.CAN, true);
- public SchemaSet()
+ /**
+ * Return the default (empty) schema set
+ *
+ * @return default schema set
+ */
+ public static SchemaSet getDefaultSchemaSet()
{
- this(Collections.<Schema>emptySet());
+ return new SchemaSet(Collections.<Schema>emptySet())
+ {
+ @Override
+ public String toDocumentation()
+ {
+ return "Default schema";
+ }
+ };
}
+ /**
+ * @param schemas the schemas for the set
+ */
public SchemaSet(Collection<Schema> schemas)
{
this.schemas = ImmutableSet.copyOf(Preconditions.checkNotNull(schemas, "schemas cannot be null"));
}
+ /**
+ * Find the first matching schema for the path and return it
+ *
+ * @param path ZNode full path
+ * @return matching schema or a default schema
+ */
public Schema getSchema(String path)
{
if ( schemas.size() == 0 )
@@ -65,6 +89,11 @@ public class SchemaSet
}
}
+ /**
+ * Build a user displayable documentation string for the schemas in this set
+ *
+ * @return documentation
+ */
public String toDocumentation()
{
StringBuilder str = new StringBuilder("Curator Schemas:\n\n");
http://git-wip-us.apache.org/repos/asf/curator/blob/33534a09/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaViolation.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaViolation.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaViolation.java
index 6935094..afd6bbf 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaViolation.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaViolation.java
@@ -1,9 +1,27 @@
package org.apache.curator.framework.schema;
+/**
+ * Thrown by the various <code>validation</code> methods in a Schema
+ */
public class SchemaViolation extends RuntimeException
{
+ private final Schema schema;
+ private final String violation;
+
public SchemaViolation(Schema schema, String violation)
{
super(String.format("Schema violation: %s for schema: %s", violation, schema));
+ this.schema = schema;
+ this.violation = violation;
+ }
+
+ public Schema getSchema()
+ {
+ return schema;
+ }
+
+ public String getViolation()
+ {
+ return violation;
}
}
[11/16] curator git commit: more tests, fixes
Posted by ra...@apache.org.
more tests, fixes
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/4e667686
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/4e667686
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/4e667686
Branch: refs/heads/CURATOR-322
Commit: 4e667686332fe43114b7e093569a988a1a85c819
Parents: 734f453
Author: randgalt <ra...@apache.org>
Authored: Mon May 2 23:36:48 2016 -0500
Committer: randgalt <ra...@apache.org>
Committed: Mon May 2 23:36:48 2016 -0500
----------------------------------------------------------------------
.../apache/curator/framework/schema/Schema.java | 6 ++--
.../curator/framework/schema/SchemaSet.java | 2 +-
.../curator/framework/imps/TestSchema.java | 38 +++++++++++++++++++-
.../src/test/resources/schema2.json | 17 +++++++++
4 files changed, 58 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/4e667686/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
index 7cb9d76..1f91397 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
@@ -200,10 +200,10 @@ public class Schema
public String toDocumentation()
{
- return pathRegex.pattern() + '\n'
- + documentation + '\n'
+ return "Path: " + getRawPath() + '\n'
+ + "Documentation: " + documentation + '\n'
+ "Validator: " + dataValidator.getClass().getSimpleName() + '\n'
- + String.format("ephemeral: %s | sequential: %s | watched: %s | | canBeDeleted: %s", ephemeral, sequential, watched, canBeDeleted) + '\n'
+ + String.format("ephemeral: %s | sequential: %s | watched: %s | canBeDeleted: %s", ephemeral, sequential, watched, canBeDeleted) + '\n'
;
}
}
http://git-wip-us.apache.org/repos/asf/curator/blob/4e667686/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
index 768f6c6..ff650f5 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
@@ -136,7 +136,7 @@ public class SchemaSet
StringBuilder str = new StringBuilder("Curator Schemas:\n\n");
for ( Map.Entry<SchemaKey, Schema> schemaEntry : schemas.entrySet() )
{
- str.append(schemaEntry.getKey()).append('\n').append(schemaEntry.getValue()).append('\n');
+ str.append(schemaEntry.getKey()).append('\n').append(schemaEntry.getValue().toDocumentation()).append('\n');
}
return str.toString();
}
http://git-wip-us.apache.org/repos/asf/curator/blob/4e667686/curator-framework/src/test/java/org/apache/curator/framework/imps/TestSchema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestSchema.java b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestSchema.java
index a55ad8e..9359fff 100644
--- a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestSchema.java
+++ b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestSchema.java
@@ -4,6 +4,7 @@ import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
+import org.apache.curator.framework.schema.SchemaKey;
import org.apache.curator.framework.schema.SchemaSet;
import org.apache.curator.framework.schema.SchemaSetLoader;
import org.apache.curator.framework.schema.SchemaViolation;
@@ -28,7 +29,7 @@ public class TestSchema extends BaseClassForTests
try
{
- client.create().creatingParentsIfNeeded().forPath("/a/b/c");
+ client.create().creatingParentsIfNeeded().forPath(schemaSet.getNamedSchema(SchemaKey.named("test")).getRawPath());
Assert.fail("Should've violated schema");
}
catch ( SchemaViolation dummy )
@@ -44,6 +45,41 @@ public class TestSchema extends BaseClassForTests
}
}
+ @Test
+ public void testMulti() throws Exception
+ {
+ SchemaSet schemaSet = loadSchemaSet("schema2.json");
+ CuratorFramework client = newClient(schemaSet);
+ try
+ {
+ client.start();
+
+ try
+ {
+ client.create().creatingParentsIfNeeded().forPath("/a/b/c");
+ Assert.fail("Should've violated schema: test");
+ }
+ catch ( SchemaViolation dummy )
+ {
+ // expected
+ }
+
+ try
+ {
+ client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/a/b/c/d/e");
+ Assert.fail("Should've violated schema: test2");
+ }
+ catch ( SchemaViolation dummy )
+ {
+ // expected
+ }
+ }
+ finally
+ {
+ CloseableUtils.closeQuietly(client);
+ }
+ }
+
private CuratorFramework newClient(SchemaSet schemaSet)
{
return CuratorFrameworkFactory.builder()
http://git-wip-us.apache.org/repos/asf/curator/blob/4e667686/curator-framework/src/test/resources/schema2.json
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/resources/schema2.json b/curator-framework/src/test/resources/schema2.json
new file mode 100644
index 0000000..28a9a92
--- /dev/null
+++ b/curator-framework/src/test/resources/schema2.json
@@ -0,0 +1,17 @@
+[
+ {
+ "name": "test",
+ "path": "/a/b/c",
+ "documentation": "This is a schema",
+ "ephemeral": "must",
+ "sequential": "cannot"
+ },
+
+ {
+ "name": "test2",
+ "path": "/a/.*",
+ "isRegex": true,
+ "ephemeral": "cannot",
+ "canBeDeleted": false
+ }
+]
\ No newline at end of file
[14/16] curator git commit: Include path in data validator - tests
for data validator
Posted by ra...@apache.org.
Include path in data validator - tests for data validator
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/450976f3
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/450976f3
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/450976f3
Branch: refs/heads/CURATOR-322
Commit: 450976f3d5c629b83d556b4f840ac844bb620830
Parents: 30d83b8
Author: randgalt <ra...@apache.org>
Authored: Mon May 2 23:51:51 2016 -0500
Committer: randgalt <ra...@apache.org>
Committed: Mon May 2 23:51:51 2016 -0500
----------------------------------------------------------------------
.../imps/CuratorMultiTransactionImpl.java | 2 +-
.../framework/imps/SetDataBuilderImpl.java | 2 +-
.../curator/framework/schema/DataValidator.java | 2 +-
.../framework/schema/DefaultDataValidator.java | 2 +-
.../apache/curator/framework/schema/Schema.java | 8 +--
.../curator/framework/schema/TestSchema.java | 57 ++++++++++++++++++--
.../src/test/resources/schema3.json | 7 +++
7 files changed, 69 insertions(+), 11 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/450976f3/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java b/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java
index 49824da..bfdf40f 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java
@@ -138,7 +138,7 @@ public class CuratorMultiTransactionImpl implements
else if ( curatorOp.get().getType() == ZooDefs.OpCode.setData )
{
SetDataRequest setDataRequest = (SetDataRequest)curatorOp.get().toRequestRecord();
- schema.validateData(setDataRequest.getData());
+ schema.validateData(setDataRequest.getPath(), setDataRequest.getData());
}
}
http://git-wip-us.apache.org/repos/asf/curator/blob/450976f3/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java b/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java
index 1f9b335..6a54146 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java
@@ -241,7 +241,7 @@ class SetDataBuilderImpl implements SetDataBuilder, BackgroundOperation<PathAndB
@Override
public Stat forPath(String path, byte[] data) throws Exception
{
- client.getSchemaSet().getSchema(path).validateData(data);
+ client.getSchemaSet().getSchema(path).validateData(path, data);
if ( compress )
{
http://git-wip-us.apache.org/repos/asf/curator/blob/450976f3/curator-framework/src/main/java/org/apache/curator/framework/schema/DataValidator.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/DataValidator.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/DataValidator.java
index 84e7933..8109f3b 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/DataValidator.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/DataValidator.java
@@ -20,5 +20,5 @@ package org.apache.curator.framework.schema;
public interface DataValidator
{
- boolean isValid(byte[] data);
+ boolean isValid(String path, byte[] data);
}
http://git-wip-us.apache.org/repos/asf/curator/blob/450976f3/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java
index afc38d5..404c389 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java
@@ -24,7 +24,7 @@ package org.apache.curator.framework.schema;
public class DefaultDataValidator implements DataValidator
{
@Override
- public boolean isValid(byte[] data)
+ public boolean isValid(String path, byte[] data)
{
return true;
}
http://git-wip-us.apache.org/repos/asf/curator/blob/450976f3/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
index d7e9be7..6d08244 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
@@ -140,18 +140,20 @@ public class Schema
throw new SchemaViolation(this, "Must be sequential");
}
- validateData(data);
+ validateData(path, data);
}
/**
* Validate that this schema validates the data
*
+ *
+ * @param path the znode full path
* @param data data being set
* @throws SchemaViolation if data is invalid
*/
- public void validateData(byte[] data)
+ public void validateData(String path, byte[] data)
{
- if ( !dataValidator.isValid(data) )
+ if ( !dataValidator.isValid(path, data) )
{
throw new SchemaViolation(this, "Data is not valid");
}
http://git-wip-us.apache.org/repos/asf/curator/blob/450976f3/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java b/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
index 7f29148..eadae80 100644
--- a/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
+++ b/curator-framework/src/test/java/org/apache/curator/framework/schema/TestSchema.java
@@ -35,7 +35,7 @@ public class TestSchema extends BaseClassForTests
@Test
public void testBasics() throws Exception
{
- SchemaSet schemaSet = loadSchemaSet("schema1.json");
+ SchemaSet schemaSet = loadSchemaSet("schema1.json", null);
CuratorFramework client = newClient(schemaSet);
try
{
@@ -60,9 +60,52 @@ public class TestSchema extends BaseClassForTests
}
@Test
+ public void testDataValidator() throws Exception
+ {
+ final DataValidator dataValidator = new DataValidator()
+ {
+ @Override
+ public boolean isValid(String path, byte[] data)
+ {
+ return data.length > 0;
+ }
+ };
+ SchemaSetLoader.DataValidatorMapper dataValidatorMapper = new SchemaSetLoader.DataValidatorMapper()
+ {
+ @Override
+ public DataValidator getDataValidator(String name)
+ {
+ return dataValidator;
+ }
+ };
+ SchemaSet schemaSet = loadSchemaSet("schema3.json", dataValidatorMapper);
+ CuratorFramework client = newClient(schemaSet);
+ try
+ {
+ client.start();
+
+ try
+ {
+ client.create().forPath("/test", new byte[0]);
+ Assert.fail("Should've violated schema");
+ }
+ catch ( SchemaViolation dummy )
+ {
+ // expected
+ }
+
+ client.create().forPath("/test", "good".getBytes());
+ }
+ finally
+ {
+ CloseableUtils.closeQuietly(client);
+ }
+ }
+
+ @Test
public void testMulti() throws Exception
{
- SchemaSet schemaSet = loadSchemaSet("schema2.json");
+ SchemaSet schemaSet = loadSchemaSet("schema2.json", null);
CuratorFramework client = newClient(schemaSet);
try
{
@@ -94,6 +137,12 @@ public class TestSchema extends BaseClassForTests
}
}
+ @Override
+ protected boolean enabledSessionExpiredStateAware()
+ {
+ return true;
+ }
+
private CuratorFramework newClient(SchemaSet schemaSet)
{
return CuratorFrameworkFactory.builder()
@@ -103,9 +152,9 @@ public class TestSchema extends BaseClassForTests
.build();
}
- private SchemaSet loadSchemaSet(String name) throws IOException
+ private SchemaSet loadSchemaSet(String name, SchemaSetLoader.DataValidatorMapper dataValidatorMapper) throws IOException
{
String json = Resources.toString(Resources.getResource(name), Charsets.UTF_8);
- return new SchemaSetLoader(json, null).toSchemaSet(true);
+ return new SchemaSetLoader(json, dataValidatorMapper).toSchemaSet(true);
}
}
http://git-wip-us.apache.org/repos/asf/curator/blob/450976f3/curator-framework/src/test/resources/schema3.json
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/resources/schema3.json b/curator-framework/src/test/resources/schema3.json
new file mode 100644
index 0000000..e777a11
--- /dev/null
+++ b/curator-framework/src/test/resources/schema3.json
@@ -0,0 +1,7 @@
+[
+ {
+ "name": "test",
+ "path": "/test",
+ "dataValidator": "test"
+ }
+]
\ No newline at end of file
[05/16] curator git commit: needs new arg
Posted by ra...@apache.org.
needs new arg
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/7f27631c
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/7f27631c
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/7f27631c
Branch: refs/heads/CURATOR-322
Commit: 7f27631cb8d11e7026deca5e6848d9a1e584f0d3
Parents: df3c1c4
Author: randgalt <ra...@apache.org>
Authored: Mon May 2 16:16:52 2016 -0500
Committer: randgalt <ra...@apache.org>
Committed: Mon May 2 16:16:52 2016 -0500
----------------------------------------------------------------------
.../main/java/org/apache/curator/framework/schema/SchemaSet.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/7f27631c/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
index 0c3dfd5..8a6e19c 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
@@ -37,7 +37,7 @@ public class SchemaSet
.softValues()
.build(cacheLoader);
- private static final Schema defaultSchema = new Schema(Pattern.compile(".*"), "__default__", new DefaultDataValidator(), Schema.Allowance.CAN, Schema.Allowance.CAN, true, true);
+ private static final Schema defaultSchema = new Schema(Pattern.compile(".*"), "Default schema", new DefaultDataValidator(), Schema.Allowance.CAN, Schema.Allowance.CAN, true, true, true);
public SchemaSet()
{
[09/16] curator git commit: more doc
Posted by ra...@apache.org.
more doc
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/8ccfdebd
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/8ccfdebd
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/8ccfdebd
Branch: refs/heads/CURATOR-322
Commit: 8ccfdebddfd9ce3d23d35f9ae94d5be77fa538a8
Parents: f83c8ef
Author: randgalt <ra...@apache.org>
Authored: Mon May 2 17:09:40 2016 -0500
Committer: randgalt <ra...@apache.org>
Committed: Mon May 2 17:09:40 2016 -0500
----------------------------------------------------------------------
.../apache/curator/framework/schema/Schema.java | 39 +++++++++++++++++++-
1 file changed, 37 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/8ccfdebd/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
index 16d335a..7cb9d76 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
@@ -61,6 +61,11 @@ public class Schema
this.canBeDeleted = canBeDeleted;
}
+ /**
+ * Validate that this schema allows znode deletion
+ *
+ * @throws SchemaViolation if schema does not allow znode deletion
+ */
public void validateDeletion()
{
if ( !canBeDeleted )
@@ -69,6 +74,12 @@ public class Schema
}
}
+ /**
+ * Validate that this schema's watching setting matches
+ *
+ * @param isWatching true if attempt is being made to watch node
+ * @throws SchemaViolation if schema's watching setting does not match
+ */
public void validateWatcher(boolean isWatching)
{
if ( isWatching && (watched == Allowance.CANNOT) )
@@ -82,6 +93,13 @@ public class Schema
}
}
+ /**
+ * Validate that this schema's create mode setting matches and that the data is valid
+ *
+ * @param mode CreateMode being used
+ * @param data data being set
+ * @throws SchemaViolation if schema's create mode setting does not match or data is invalid
+ */
public void validateCreate(CreateMode mode, byte[] data)
{
if ( mode.isEphemeral() && (ephemeral == Allowance.CANNOT) )
@@ -107,6 +125,12 @@ public class Schema
validateData(data);
}
+ /**
+ * Validate that this schema validates the data
+ *
+ * @param data data being set
+ * @throws SchemaViolation if data is invalid
+ */
public void validateData(byte[] data)
{
if ( !dataValidator.isValid(data) )
@@ -115,12 +139,23 @@ public class Schema
}
}
- public Pattern getPathRegex()
+ /**
+ * Return the raw path for this schema. If a full path was used, it is returned.
+ * If a regex was used, it is returned
+ *
+ * @return path
+ */
+ public String getRawPath()
+ {
+ return (path != null) ? path : pathRegex.pattern();
+ }
+
+ Pattern getPathRegex()
{
return pathRegex;
}
- public String getPath()
+ String getPath()
{
return path;
}
[12/16] curator git commit: licenses
Posted by ra...@apache.org.
licenses
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/e3ec9204
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/e3ec9204
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/e3ec9204
Branch: refs/heads/CURATOR-322
Commit: e3ec920454206493317ec9cc49aaa21be48ee0ad
Parents: 4e66768
Author: randgalt <ra...@apache.org>
Authored: Mon May 2 23:40:33 2016 -0500
Committer: randgalt <ra...@apache.org>
Committed: Mon May 2 23:40:33 2016 -0500
----------------------------------------------------------------------
.../framework/api/ErrorListenerEnsembleable.java | 18 ++++++++++++++++++
.../api/ErrorListenerMultiTransactionMain.java | 18 ++++++++++++++++++
.../api/ErrorListenerPathAndBytesable.java | 18 ++++++++++++++++++
.../framework/api/ErrorListenerPathable.java | 18 ++++++++++++++++++
.../api/ErrorListenerReconfigBuilderMain.java | 18 ++++++++++++++++++
.../curator/framework/schema/DataValidator.java | 18 ++++++++++++++++++
.../framework/schema/DefaultDataValidator.java | 18 ++++++++++++++++++
.../apache/curator/framework/schema/Schema.java | 18 ++++++++++++++++++
.../curator/framework/schema/SchemaBuilder.java | 18 ++++++++++++++++++
.../curator/framework/schema/SchemaKey.java | 18 ++++++++++++++++++
.../curator/framework/schema/SchemaSet.java | 18 ++++++++++++++++++
.../curator/framework/schema/SchemaSetLoader.java | 18 ++++++++++++++++++
.../curator/framework/schema/SchemaViolation.java | 18 ++++++++++++++++++
.../apache/curator/framework/imps/TestSchema.java | 18 ++++++++++++++++++
14 files changed, 252 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/e3ec9204/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerEnsembleable.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerEnsembleable.java b/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerEnsembleable.java
index 1072baa..8a1805e 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerEnsembleable.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerEnsembleable.java
@@ -1,3 +1,21 @@
+/**
+ * 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.framework.api;
public interface ErrorListenerEnsembleable<T> extends Ensembleable<T>
http://git-wip-us.apache.org/repos/asf/curator/blob/e3ec9204/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerMultiTransactionMain.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerMultiTransactionMain.java b/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerMultiTransactionMain.java
index a217004..83450cf 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerMultiTransactionMain.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerMultiTransactionMain.java
@@ -1,3 +1,21 @@
+/**
+ * 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.framework.api;
import org.apache.curator.framework.api.transaction.CuratorMultiTransactionMain;
http://git-wip-us.apache.org/repos/asf/curator/blob/e3ec9204/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerPathAndBytesable.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerPathAndBytesable.java b/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerPathAndBytesable.java
index 9882a3e..67f906a 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerPathAndBytesable.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerPathAndBytesable.java
@@ -1,3 +1,21 @@
+/**
+ * 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.framework.api;
public interface ErrorListenerPathAndBytesable<T> extends PathAndBytesable<T>
http://git-wip-us.apache.org/repos/asf/curator/blob/e3ec9204/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerPathable.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerPathable.java b/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerPathable.java
index 8245076..45380b8 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerPathable.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerPathable.java
@@ -1,3 +1,21 @@
+/**
+ * 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.framework.api;
public interface ErrorListenerPathable<T> extends Pathable<T>
http://git-wip-us.apache.org/repos/asf/curator/blob/e3ec9204/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerReconfigBuilderMain.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerReconfigBuilderMain.java b/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerReconfigBuilderMain.java
index 163ecc5..ef2f287 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerReconfigBuilderMain.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/api/ErrorListenerReconfigBuilderMain.java
@@ -1,3 +1,21 @@
+/**
+ * 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.framework.api;
public interface ErrorListenerReconfigBuilderMain extends ReconfigBuilderMain
http://git-wip-us.apache.org/repos/asf/curator/blob/e3ec9204/curator-framework/src/main/java/org/apache/curator/framework/schema/DataValidator.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/DataValidator.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/DataValidator.java
index 855f2bb..84e7933 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/DataValidator.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/DataValidator.java
@@ -1,3 +1,21 @@
+/**
+ * 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.framework.schema;
public interface DataValidator
http://git-wip-us.apache.org/repos/asf/curator/blob/e3ec9204/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java
index f28db57..afc38d5 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/DefaultDataValidator.java
@@ -1,3 +1,21 @@
+/**
+ * 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.framework.schema;
/**
http://git-wip-us.apache.org/repos/asf/curator/blob/e3ec9204/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
index 1f91397..d7e9be7 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
@@ -1,3 +1,21 @@
+/**
+ * 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.framework.schema;
import com.google.common.base.Preconditions;
http://git-wip-us.apache.org/repos/asf/curator/blob/e3ec9204/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
index 64a6b73..65d0d34 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
@@ -1,3 +1,21 @@
+/**
+ * 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.framework.schema;
import com.google.common.base.Preconditions;
http://git-wip-us.apache.org/repos/asf/curator/blob/e3ec9204/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaKey.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaKey.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaKey.java
index 0faabb1..a6ab23e 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaKey.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaKey.java
@@ -1,3 +1,21 @@
+/**
+ * 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.framework.schema;
import com.google.common.base.Preconditions;
http://git-wip-us.apache.org/repos/asf/curator/blob/e3ec9204/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
index ff650f5..3131862 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
@@ -1,3 +1,21 @@
+/**
+ * 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.framework.schema;
import com.google.common.base.Preconditions;
http://git-wip-us.apache.org/repos/asf/curator/blob/e3ec9204/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java
index 092b161..3c4a0f8 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java
@@ -1,3 +1,21 @@
+/**
+ * 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.framework.schema;
import com.fasterxml.jackson.databind.JsonNode;
http://git-wip-us.apache.org/repos/asf/curator/blob/e3ec9204/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaViolation.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaViolation.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaViolation.java
index 4953c65..47661d1 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaViolation.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaViolation.java
@@ -1,3 +1,21 @@
+/**
+ * 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.framework.schema;
/**
http://git-wip-us.apache.org/repos/asf/curator/blob/e3ec9204/curator-framework/src/test/java/org/apache/curator/framework/imps/TestSchema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestSchema.java b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestSchema.java
index 9359fff..c908fd5 100644
--- a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestSchema.java
+++ b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestSchema.java
@@ -1,3 +1,21 @@
+/**
+ * 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.framework.imps;
import com.google.common.base.Charsets;
[10/16] curator git commit: more doc, some testing, schema set loader
Posted by ra...@apache.org.
more doc, some testing, schema set loader
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/734f4533
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/734f4533
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/734f4533
Branch: refs/heads/CURATOR-322
Commit: 734f4533808f1a4b8b8086407476bca8862339ff
Parents: 8ccfdeb
Author: randgalt <ra...@apache.org>
Authored: Mon May 2 21:41:25 2016 -0500
Committer: randgalt <ra...@apache.org>
Committed: Mon May 2 21:41:25 2016 -0500
----------------------------------------------------------------------
curator-framework/pom.xml | 12 ++
.../curator/framework/schema/SchemaSet.java | 34 +++--
.../framework/schema/SchemaSetLoader.java | 148 +++++++++++++++++++
.../framework/schema/SchemaViolation.java | 7 +
.../curator/framework/imps/TestSchema.java | 61 ++++++++
.../src/test/resources/schema1.json | 9 ++
pom.xml | 13 ++
7 files changed, 272 insertions(+), 12 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/734f4533/curator-framework/pom.xml
----------------------------------------------------------------------
diff --git a/curator-framework/pom.xml b/curator-framework/pom.xml
index 32b2cdb..c47e265 100644
--- a/curator-framework/pom.xml
+++ b/curator-framework/pom.xml
@@ -57,6 +57,18 @@
</dependency>
<dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-core</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-databind</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
http://git-wip-us.apache.org/repos/asf/curator/blob/734f4533/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
index 1014dc8..768f6c6 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
@@ -41,6 +41,7 @@ public class SchemaSet
.build(cacheLoader);
private static final Schema defaultSchema = new Schema(null, "", "Default schema", new DefaultDataValidator(), Schema.Allowance.CAN, Schema.Allowance.CAN, Schema.Allowance.CAN, true);
+ private final boolean useDefaultSchema;
/**
* Return the default (empty) schema set
@@ -49,7 +50,7 @@ public class SchemaSet
*/
public static SchemaSet getDefaultSchemaSet()
{
- return new SchemaSet(Collections.<SchemaKey, Schema>emptyMap())
+ return new SchemaSet(Collections.<SchemaKey, Schema>emptyMap(), true)
{
@Override
public String toDocumentation()
@@ -62,9 +63,11 @@ public class SchemaSet
/**
* @param schemas the schemas for the set. The key of the map is a key/name for the schema that can be
* used when calling {@link #getNamedSchema(SchemaKey)}
+ * @param useDefaultSchema if true, return a default schema when there is no match. Otherwise, an exception is thrown
*/
- public SchemaSet(Map<SchemaKey, Schema> schemas)
+ public SchemaSet(Map<SchemaKey, Schema> schemas, boolean useDefaultSchema)
{
+ this.useDefaultSchema = useDefaultSchema;
this.schemas = ImmutableMap.copyOf(Preconditions.checkNotNull(schemas, "schemas cannot be null"));
ImmutableMap.Builder<String, Schema> builder = ImmutableMap.builder();
for ( Schema schema : schemas.values() )
@@ -85,24 +88,31 @@ public class SchemaSet
*/
public Schema getSchema(String path)
{
- if ( schemas.size() == 0 )
+ Schema schema = null;
+ if ( schemas.size() > 0 )
{
- return defaultSchema;
+ schema = pathSchemas.get(path);
+ if ( schema == null )
+ {
+ try
+ {
+ schema = regexCache.get(path);
+ }
+ catch ( ExecutionException e )
+ {
+ throw new RuntimeException(e);
+ }
+ }
}
- Schema schema = pathSchemas.get(path);
if ( schema != null )
{
return schema;
}
-
- try
+ if ( useDefaultSchema )
{
- return regexCache.get(path);
- }
- catch ( ExecutionException e )
- {
- throw new RuntimeException(e);
+ return defaultSchema;
}
+ throw new SchemaViolation("No schema found for: " + path);
}
/**
http://git-wip-us.apache.org/repos/asf/curator/blob/734f4533/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java
new file mode 100644
index 0000000..092b161
--- /dev/null
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java
@@ -0,0 +1,148 @@
+package org.apache.curator.framework.schema;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.ImmutableMap;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.Arrays;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+/**
+ * <p>
+ * Utility to load schems set from a JSON stream/file. <strong>NOTE:</strong>
+ * to avoid adding a new dependency to Curator, the Jackson library has been used
+ * with "provided" scope. You will need to add a dependency to <code>com.fasterxml.jackson.core:jackson-core:2.7.3</code>
+ * and <code>com.fasterxml.jackson.core:jackson-databind:2.7.3</code> to your project
+ * </p>
+ *
+ * <p>
+ * The JSON stream should be an array of named schemas:<br><br>
+ * <code><pre>
+ * [
+ * {
+ * "name": "name", required - name of the schema
+ * "path": "path or pattern", required - full path or regex pattern
+ * "isRegex": true/false, optional - true if path is a regular expression - default is false
+ * "dataValidator": "name", optional - name of a data validator - default is no validator
+ * "documentation": "docs", optional - user displayable docs - default is ""
+ * "ephemeral": "allowance", optional - "can", "must" or "cannot" - default is "can"
+ * "sequential": "allowance", optional - "can", "must" or "cannot" - default is "can"
+ * "watched": "allowance", optional - "can", "must" or "cannot" - default is "can"
+ * "canBeDeleted": "true/false optional - true if ZNode at path can be deleted - default is true
+ * }
+ * ]
+ * </pre></code>
+ * </p>
+ */
+public class SchemaSetLoader
+{
+ private final Map<SchemaKey, Schema> schemas;
+
+ /**
+ * Called to map a data validator name in the JSON stream to an actual data validator
+ */
+ public interface DataValidatorMapper
+ {
+ /**
+ * @param name name of the validator
+ * @return the validator
+ */
+ DataValidator getDataValidator(String name);
+ }
+
+ public SchemaSetLoader(String json, DataValidatorMapper dataValidatorMapper)
+ {
+ this(new StringReader(json), dataValidatorMapper);
+ }
+
+ public SchemaSetLoader(Reader in, DataValidatorMapper dataValidatorMapper)
+ {
+ ImmutableMap.Builder<SchemaKey, Schema> builder = ImmutableMap.builder();
+ try
+ {
+ JsonNode root = new ObjectMapper().readTree(in);
+ read(builder, root, dataValidatorMapper);
+ }
+ catch ( IOException e )
+ {
+ throw new RuntimeException(e);
+ }
+ schemas = builder.build();
+ }
+
+ public SchemaSet toSchemaSet(boolean useDefaultSchema)
+ {
+ return new SchemaSet(schemas, useDefaultSchema);
+ }
+
+ private void read(ImmutableMap.Builder<SchemaKey, Schema> builder, JsonNode node, DataValidatorMapper dataValidatorMapper)
+ {
+ for ( JsonNode child : node )
+ {
+ readNode(builder, child, dataValidatorMapper);
+ }
+ }
+
+ private void readNode(ImmutableMap.Builder<SchemaKey, Schema> builder, JsonNode node, DataValidatorMapper dataValidatorMapper)
+ {
+ String name = getText(node, "name", null);
+ String path = getText(node, "path", null);
+ boolean isRegex = getBoolean(node, "isRegex");
+ if ( name == null )
+ {
+ throw new RuntimeException("name is required at: " + node);
+ }
+ if ( path == null )
+ {
+ throw new RuntimeException("path is required at: " + node);
+ }
+
+ SchemaBuilder schemaBuilder = isRegex ? Schema.builder(Pattern.compile(path)) : Schema.builder(path);
+
+ String dataValidatorName = getText(node, "dataValidator", null);
+ if ( dataValidatorName != null )
+ {
+ if ( dataValidatorMapper == null )
+ {
+ throw new RuntimeException("No DataValidatorMapper provided but needed at: " + node);
+ }
+ schemaBuilder.dataValidator(dataValidatorMapper.getDataValidator(dataValidatorName));
+ }
+
+ Schema schema = schemaBuilder.documentation(getText(node, "documentation", ""))
+ .ephemeral(getAllowance(node, "ephemeral"))
+ .sequential(getAllowance(node, "sequential"))
+ .watched(getAllowance(node, "watched"))
+ .canBeDeleted(getBoolean(node, "canBeDeleted"))
+ .build();
+ builder.put(new SchemaKey(name), schema);
+ }
+
+ private String getText(JsonNode node, String name, String defaultValue)
+ {
+ JsonNode namedNode = node.get(name);
+ return (namedNode != null) ? namedNode.asText() : defaultValue;
+ }
+
+ private boolean getBoolean(JsonNode node, String name)
+ {
+ JsonNode namedNode = node.get(name);
+ return (namedNode != null) && namedNode.asBoolean();
+ }
+
+ private Schema.Allowance getAllowance(JsonNode node, String name)
+ {
+ JsonNode namedNode = node.get(name);
+ try
+ {
+ return (namedNode != null) ? Schema.Allowance.valueOf(namedNode.asText().toUpperCase()) : Schema.Allowance.CAN;
+ }
+ catch ( IllegalArgumentException ignore )
+ {
+ throw new RuntimeException("Must be one of: " + Arrays.toString(Schema.Allowance.values()) + " at " + node);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/curator/blob/734f4533/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaViolation.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaViolation.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaViolation.java
index afd6bbf..4953c65 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaViolation.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaViolation.java
@@ -8,6 +8,13 @@ public class SchemaViolation extends RuntimeException
private final Schema schema;
private final String violation;
+ public SchemaViolation(String violation)
+ {
+ super(String.format("Schema violation: %s", violation));
+ this.schema = null;
+ this.violation = violation;
+ }
+
public SchemaViolation(Schema schema, String violation)
{
super(String.format("Schema violation: %s for schema: %s", violation, schema));
http://git-wip-us.apache.org/repos/asf/curator/blob/734f4533/curator-framework/src/test/java/org/apache/curator/framework/imps/TestSchema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestSchema.java b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestSchema.java
new file mode 100644
index 0000000..a55ad8e
--- /dev/null
+++ b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestSchema.java
@@ -0,0 +1,61 @@
+package org.apache.curator.framework.imps;
+
+import com.google.common.base.Charsets;
+import com.google.common.io.Resources;
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.curator.framework.CuratorFrameworkFactory;
+import org.apache.curator.framework.schema.SchemaSet;
+import org.apache.curator.framework.schema.SchemaSetLoader;
+import org.apache.curator.framework.schema.SchemaViolation;
+import org.apache.curator.retry.RetryOneTime;
+import org.apache.curator.test.BaseClassForTests;
+import org.apache.curator.utils.CloseableUtils;
+import org.apache.zookeeper.CreateMode;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+import java.io.IOException;
+
+public class TestSchema extends BaseClassForTests
+{
+ @Test
+ public void testBasics() throws Exception
+ {
+ SchemaSet schemaSet = loadSchemaSet("schema1.json");
+ CuratorFramework client = newClient(schemaSet);
+ try
+ {
+ client.start();
+
+ try
+ {
+ client.create().creatingParentsIfNeeded().forPath("/a/b/c");
+ Assert.fail("Should've violated schema");
+ }
+ catch ( SchemaViolation dummy )
+ {
+ // expected
+ }
+
+ client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/a/b/c");
+ }
+ finally
+ {
+ CloseableUtils.closeQuietly(client);
+ }
+ }
+
+ private CuratorFramework newClient(SchemaSet schemaSet)
+ {
+ return CuratorFrameworkFactory.builder()
+ .connectString(server.getConnectString())
+ .retryPolicy(new RetryOneTime(1))
+ .schemaSet(schemaSet)
+ .build();
+ }
+
+ private SchemaSet loadSchemaSet(String name) throws IOException
+ {
+ String json = Resources.toString(Resources.getResource(name), Charsets.UTF_8);
+ return new SchemaSetLoader(json, null).toSchemaSet(true);
+ }
+}
http://git-wip-us.apache.org/repos/asf/curator/blob/734f4533/curator-framework/src/test/resources/schema1.json
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/resources/schema1.json b/curator-framework/src/test/resources/schema1.json
new file mode 100644
index 0000000..5491059
--- /dev/null
+++ b/curator-framework/src/test/resources/schema1.json
@@ -0,0 +1,9 @@
+[
+ {
+ "name": "test",
+ "path": "/a/b/c",
+ "documentation": "This is a schema",
+ "ephemeral": "must",
+ "sequential": "cannot"
+ }
+]
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/curator/blob/734f4533/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index f946f59..d9d33fa 100644
--- a/pom.xml
+++ b/pom.xml
@@ -67,6 +67,7 @@
<maven-license-plugin-version>1.9.0</maven-license-plugin-version>
<commons-math-version>2.2</commons-math-version>
<jackson-mapper-asl-version>1.9.13</jackson-mapper-asl-version>
+ <jackson-version>2.7.3</jackson-version>
<jersey-version>1.18.1</jersey-version>
<jsr311-api-version>1.1.1</jsr311-api-version>
<jetty-version>6.1.26</jetty-version>
@@ -356,6 +357,18 @@
</dependency>
<dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-core</artifactId>
+ <version>${jackson-version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-databind</artifactId>
+ <version>${jackson-version}</version>
+ </dependency>
+
+ <dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey-version}</version>