Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) FuseSource, Inc. http://fusesource.com 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 io.fabric8.camel;
 
 import  org.apache.camel.impl.DefaultComponent;
 import  org.apache.commons.logging.Log;
 import  org.apache.commons.logging.LogFactory;
 import  org.apache.curator.framework.CuratorFramework;
 import  org.apache.curator.framework.CuratorFrameworkFactory;
 import  org.apache.curator.retry.RetryOneTime;
 import  io.fabric8.groups.Group;
 import  io.fabric8.groups.internal.ManagedGroupFactory;
 import  io.fabric8.groups.internal.ManagedGroupFactoryBuilder;
 
 
 public abstract class ZKComponentSupport extends DefaultComponent implements Callable<CuratorFramework> {
     private static final transient Log LOG = LogFactory.getLog(MasterComponent.class);
     private static final String ZOOKEEPER_URL = "zookeeper.url";
     private static final String ZOOKEEPER_PASSWORD = "zookeeper.password";
 
     private ManagedGroupFactory managedGroupFactory;
     private CuratorFramework curator;
     private boolean shouldCloseZkClient = false;
     private int maximumConnectionTimeout = 10 * 1000;
     private String zooKeeperUrl;
     private String zooKeeperPassword;
 
     public CuratorFramework getCurator() {
         if ( == null) {
             throw new IllegalStateException("Component is not started");
         }
         return .getCurator();
     }
 
     public Group<CamelNodeStatecreateGroup(String path) {
         if ( == null) {
             throw new IllegalStateException("Component is not started");
         }
         return .createGroup(pathCamelNodeState.class);
     }
 
 
     public void setCurator(CuratorFramework curator) {
         this. = curator;
     }
 
     public boolean isShouldCloseZkClient() {
         return ;
     }
 
     public void setShouldCloseZkClient(boolean shouldCloseZkClient) {
         this. = shouldCloseZkClient;
     }
 
     public int getMaximumConnectionTimeout() {
         return ;
     }
 
     public void setMaximumConnectionTimeout(int maximumConnectionTimeout) {
         this. = maximumConnectionTimeout;
     }
 
 
     public String getZooKeeperUrl() {
         return ;
     }
 
     public void setZooKeeperUrl(String zooKeeperUrl) {
         this. = zooKeeperUrl;
     }
 
     public String getZooKeeperPassword() {
         return ;
     }
 
     public void setZooKeeperPassword(String zooKeeperPassword) {
         this. = zooKeeperPassword;
     }
 
     @Override
     protected void doStart() throws Exception {
         super.doStart();
        if ( == null) {
            try {
                 = (CuratorFramework) getCamelContext().getRegistry().lookupByName("curator");
                if ( != null) {
                    .debug("Zookeeper client found in camel registry. " + );
                }
            } catch (Exception exception) {
            }
        }
         = ManagedGroupFactoryBuilder.create(getClass().getClassLoader(), this);
    }
    public CuratorFramework call() throws Exception {
        String connectString = getZooKeeperUrl();
        if (connectString == null) {
            connectString = System.getProperty("localhost:2181");
        }
        String password = getZooKeeperPassword();
        if (password == null) {
            System.getProperty();
        }
        .debug("CuratorFramework not found in camel registry, creating new with connection " + connectString);
        CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
                                                                         .connectString(connectString)
                                                                         .retryPolicy(new RetryOneTime(1000))
                                                                         .connectionTimeoutMs(getMaximumConnectionTimeout());
        if (password != null && !password.isEmpty()) {
            builder.authorization("digest", ("fabric:"+password).getBytes());
        }
         = builder.build();
        .debug("Starting curator " + );
        .start();
        return ;
    }
    @Override
    protected void doStop() throws Exception {
        super.doStop();
        if ( != null) {
            .close();
             = null;
        }
    }
New to GrepCode? Check out our FAQ X