Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *
   *  Copyright 2011 Netflix, Inc.
   *
   *     Licensed 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 com.netflix.curator.framework.imps;
 
 import java.util.List;
 import java.util.UUID;
 
 {
     private final CuratorFrameworkImpl      client;
     private CreateMode                      createMode;
     private Backgrounding                   backgrounding;
     private boolean                         createParentsIfNeeded;
     private boolean                         doProtectedEphemeralSequential;
     private String                          protectedEphemeralSequentialId;
     private ACLing                          acling;
 
     boolean failNextCreateForTesting = false;
 
     static final String         PROTECTED_PREFIX = "_c_";
 
     {
         this. = client;
          = .;
          = new Backgrounding();
          = new ACLing();
          = false;
          = false;
          = null;
     }
 
     TransactionCreateBuilder        asTransactionCreateBuilder(final CuratorTransactionImpl curatorTransactionfinal CuratorMultiTransactionRecord transaction)
     {
         return new TransactionCreateBuilder()
         {
             @Override
             public PathAndBytesable<CuratorTransactionBridgewithACL(List<ACLaclList)
             {
                 CreateBuilderImpl.this.withACL(aclList);
                 return this;
             }
 
             @Override
             public ACLPathAndBytesable<CuratorTransactionBridgewithMode(CreateMode mode)
             {
                 CreateBuilderImpl.this.withMode(mode);
                 return this;
             }
 
             @Override
             public CuratorTransactionBridge forPath(String paththrows Exception
             {
                 return forPath(path.getDefaultData());
             }
 
            @Override
            public CuratorTransactionBridge forPath(String pathbyte[] datathrows Exception
            {
                String      fixedPath = .fixForNamespace(path);
                transaction.add(Op.create(fixedPathdata.getAclList(), ), .path);
                return curatorTransaction;
            }
        };
    }
    @Override
    {
         = new ACLing(aclList);
        return new ACLBackgroundPathAndBytesable<String>()
        {
            @Override
            public BackgroundPathAndBytesable<StringwithACL(List<ACLaclList)
            {
                return CreateBuilderImpl.this.withACL(aclList);
            }
            @Override
            public PathAndBytesable<StringinBackground()
            {
                return CreateBuilderImpl.this.inBackground();
            }
            @Override
            public PathAndBytesable<StringinBackground(Object context)
            {
                return CreateBuilderImpl.this.inBackground(context);
            }
            @Override
            public PathAndBytesable<StringinBackground(BackgroundCallback callback)
            {
                return CreateBuilderImpl.this.inBackground(callback);
            }
            @Override
            public PathAndBytesable<StringinBackground(BackgroundCallback callbackExecutor executor)
            {
                return CreateBuilderImpl.this.inBackground(callbackexecutor);
            }
            @Override
            public String forPath(String pathbyte[] datathrows Exception
            {
                return CreateBuilderImpl.this.forPath(pathdata);
            }
            @Override
            public String forPath(String paththrows Exception
            {
                return CreateBuilderImpl.this.forPath(path);
            }
        };
    }
    @Override
    {
         = true;
        return new ACLCreateModePathAndBytesable<String>()
        {
            @Override
            public PathAndBytesable<StringwithACL(List<ACLaclList)
            {
                return CreateBuilderImpl.this.withACL(aclList);
            }
            @Override
            public ACLPathAndBytesable<StringwithMode(CreateMode mode)
            {
                 = mode;
                return new ACLPathAndBytesable<String>()
                {
                    @Override
                    public PathAndBytesable<StringwithACL(List<ACLaclList)
                    {
                        return CreateBuilderImpl.this.withACL(aclList);
                    }
                    @Override
                    public String forPath(String pathbyte[] datathrows Exception
                    {
                        return CreateBuilderImpl.this.forPath(pathdata);
                    }
                    @Override
                    public String forPath(String paththrows Exception
                    {
                        return CreateBuilderImpl.this.forPath(path);
                    }
                };
            }
            @Override
            public String forPath(String pathbyte[] datathrows Exception
            {
                return CreateBuilderImpl.this.forPath(pathdata);
            }
            @Override
            public String forPath(String paththrows Exception
            {
                return CreateBuilderImpl.this.forPath(path);
            }
        };
    }
    @Override
    {
         = true;
        return new ACLPathAndBytesable<String>()
        {
            @Override
            public PathAndBytesable<StringwithACL(List<ACLaclList)
            {
                return CreateBuilderImpl.this.withACL(aclList);
            }
            @Override
            public String forPath(String pathbyte[] datathrows Exception
            {
                return CreateBuilderImpl.this.forPath(pathdata);
            }
            @Override
            public String forPath(String paththrows Exception
            {
                return CreateBuilderImpl.this.forPath(path);
            }
        };
    }
    @Override
    {
         = mode;
        return this;
    }
    @Override
    {
         = new Backgrounding(callback);
        return this;
    }
    @Override
    public PathAndBytesable<StringinBackground(BackgroundCallback callbackExecutor executor)
    {
         = new Backgrounding(callbackexecutor);
        return this;
    }
    @Override
    {
         = new Backgrounding(true);
        return this;
    }
    @Override
    public PathAndBytesable<StringinBackground(Object context)
    {
         = new Backgrounding(context);
        return this;
    }
    @Override
    public String forPath(String paththrows Exception
    {
        return forPath(path.getDefaultData());
    }
    @Override
    public String forPath(String pathbyte[] datathrows Exception
    {
        path = .fixForNamespace(path);
        String  returnPath = null;
        if ( .inBackground() )
        {
            .processBackgroundOperation(new OperationAndData<PathAndBytes>(thisnew PathAndBytes(pathdata), .getCallback(), null), null);
        }
        else
        {
            returnPath = pathInForeground(pathdata);
            returnPath = .unfixForNamespace(returnPath);
        }
        return returnPath;
    }
    @Override
    public void performBackgroundOperation(final OperationAndData<PathAndBytesoperationAndDatathrows Exception
    {
        final TimeTrace   trace = .getZookeeperClient().startTracer("CreateBuilderImpl-Background");
        .getZooKeeper().create
        (
            operationAndData.getData().getPath(),
            operationAndData.getData().getData(),
            .getAclList(),
            ,
            new AsyncCallback.StringCallback()
            {
                @Override
                public void processResult(int rcString pathObject ctxString name)
                {
                    path = .unfixForNamespace(path);
                    name = .unfixForNamespace(name);
                    trace.commit();
                    CuratorEvent event = new CuratorEventImpl(.rcpathnamectxnullnullnullnullnull);
                    .processBackgroundOperation(operationAndDataevent);
                }
            },
            .getContext()
        );
    }
    private String pathInForeground(final String pathfinal byte[] datathrows Exception
    {
        TimeTrace               trace = .getZookeeperClient().startTracer("CreateBuilderImpl-Foreground");
        final AtomicBoolean     firstTime = new AtomicBoolean(true);
        String                  returnPath = RetryLoop.callWithRetry
        (
            .getZookeeperClient(),
            new Callable<String>()
            {
                @Override
                public String call() throws Exception
                {
                    boolean   localFirstTime = firstTime.getAndSet(false);
                    String    localPath = adjustPath(path);
                    if (  )
                    {
                        ZKPaths.mkdirs(.getZooKeeper(), localPathfalse);
                    }
                    String  createdPath = null;
                    if ( !localFirstTime &&  )
                    {
                        createdPath = findProtectedNodeInForeground(localPath);
                    }
                    if ( createdPath == null )
                    {
                        createdPath = .getZooKeeper().create(localPathdata.getAclList(), );
                    }
                    if (  )
                    {
                         = false;
                        throw new KeeperException.ConnectionLossException();
                    }
                    return createdPath;
                }
            }
        );
        
        trace.commit();
        return returnPath;
    }
    private String  findProtectedNodeInForeground(final String paththrows Exception
    {
        TimeTrace       trace = .getZookeeperClient().startTracer("CreateBuilderImpl-findProtectedNodeInForeground");
        String          returnPath = RetryLoop.callWithRetry
        (
            .getZookeeperClient(),
            new Callable<String>()
            {
                @Override
                public String call() throws Exception
                {
                    String foundNode = null;
                    try
                    {
                        final ZKPaths.PathAndNode   pathAndNode = ZKPaths.getPathAndNode(path);
                        List<String>                children = .getZooKeeper().getChildren(pathAndNode.getPath(), false);
                        final String                protectedPrefix = getProtectedPrefix();
                        foundNode = Iterables.find
                        (
                            children,
                            new Predicate<String>()
                            {
                                @Override
                                public boolean apply(String node)
                                {
                                    return node.startsWith(protectedPrefix);
                                }
                            },
                            null
                        );
                        if ( foundNode != null )
                        {
                            foundNode = ZKPaths.makePath(pathAndNode.getPath(), foundNode);
                        }
                    }
                    catch ( KeeperException.NoNodeException ignore )
                    {
                        // ignore
                    }
                    return foundNode;
                }
            }
        );
        trace.commit();
        return returnPath;
    }
    private String  adjustPath(String paththrows Exception
    {
        if (  )
        {
            ZKPaths.PathAndNode     pathAndNode = ZKPaths.getPathAndNode(path);
            String                  name = getProtectedPrefix() + pathAndNode.getNode();
            path = ZKPaths.makePath(pathAndNode.getPath(), name);
        }
        return path;
    }
    private String getProtectedPrefix() throws Exception
    {
        return  +  + "-";
    }
New to GrepCode? Check out our FAQ X