View Javadoc

1   package org.apache.helix.examples;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  
25  import org.I0Itec.zkclient.IDefaultNameSpace;
26  import org.I0Itec.zkclient.ZkServer;
27  import org.apache.commons.io.FileUtils;
28  
29  public class ExampleHelper
30  {
31    
32    
33    public static ZkServer startZkServer(String zkAddr)
34    {
35      System.out.println("Start zookeeper at " + zkAddr + " in thread "
36          + Thread.currentThread().getName());
37  
38      String zkDir = zkAddr.replace(':', '_');
39      final String logDir = "/tmp/" + zkDir + "/logs";
40      final String dataDir = "/tmp/" + zkDir + "/dataDir";
41      try
42      {
43        FileUtils.deleteDirectory(new File(dataDir));
44        FileUtils.deleteDirectory(new File(logDir));
45      } catch (IOException e)
46      {
47        // TODO Auto-generated catch block
48        e.printStackTrace();
49      }
50  
51      IDefaultNameSpace defaultNameSpace = new IDefaultNameSpace() {
52        @Override
53        public void createDefaultNameSpace(org.I0Itec.zkclient.ZkClient zkClient)
54        {
55          // do nothing
56        }
57      };
58  
59      int port = Integer.parseInt(zkAddr.substring(zkAddr.lastIndexOf(':') + 1));
60      ZkServer zkServer = new ZkServer(dataDir, logDir, defaultNameSpace, port);
61      zkServer.start();
62  
63      return zkServer;
64    }
65    
66    public static void stopZkServer(ZkServer zkServer)
67    {
68      if (zkServer != null)
69      {
70        zkServer.shutdown();
71        System.out.println("Shut down zookeeper at port " + zkServer.getPort()
72            + " in thread " + Thread.currentThread().getName());
73      }
74    }
75  }