// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.zookeeper;

import com.google.inject.Inject;
import com.yahoo.cloud.config.ZookeeperServerConfig;
import com.yahoo.component.AbstractComponent;
import com.yahoo.security.tls.TransportSecurityUtils;

import java.util.logging.Level;

import static com.yahoo.vespa.defaults.Defaults.getDefaults;
import static com.yahoo.vespa.zookeeper.Configurator.zookeeperServerHostnames;

/**
 * Writes zookeeper config and starts zookeeper server.
 *
 * @author Ulf Lilleengen
 * @author Harald Musum
 */
public class VespaZooKeeperServerImpl extends AbstractComponent implements Runnable, VespaZooKeeperServer {
    private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(VespaZooKeeperServerImpl.class.getName());
    private final Thread zkServerThread;
    private final ZookeeperServerConfig zookeeperServerConfig;

    @Inject
    public VespaZooKeeperServerImpl(ZookeeperServerConfig zookeeperServerConfig) {
        this.zookeeperServerConfig = zookeeperServerConfig;
        new Configurator(zookeeperServerConfig).writeConfigToDisk(TransportSecurityUtils.getOptions());
        zkServerThread = new Thread(this, "zookeeper server");
        zkServerThread.start();
    }

    private void shutdown() {
        zkServerThread.interrupt();
        try {
            zkServerThread.join();
        } catch (InterruptedException e) {
            log.log(Level.WARNING, "Error joining server thread on shutdown", e);
        }
    }

    @Override
    public void run() {
        String[] args = new String[]{getDefaults().underVespaHome(zookeeperServerConfig.zooKeeperConfigFile())};
        log.log(Level.INFO, "Starting ZooKeeper server with config file " + args[0] +
                            ". Trying to establish ZooKeeper quorum (members: " + zookeeperServerHostnames(zookeeperServerConfig) + ")");
        org.apache.zookeeper.server.quorum.QuorumPeerMain.main(args);
    }

    @Override
    public void deconstruct() {
        shutdown();
        super.deconstruct();
    }

}

@@ -146,41 +146,8 @@
 r;%0A%0A
-import com.google.inject.Inject;%0A
 impo
@@ -199,54 +199,8 @@
 ig;%0A
-import com.yahoo.component.AbstractComponent;%0A
 impo
@@ -491,34 +491,8 @@
 %0A *%0A
- * @author Ulf Lilleengen%0A
  * @
@@ -528,21 +528,16 @@
 c class 
-Vespa
 ZooKeepe
@@ -541,86 +541,34 @@
 eper
-ServerImpl extends AbstractComponent implements Runnable, VespaZooKeeperServer
+Runner implements Runnable
  %7B%0A 
@@ -657,21 +657,16 @@
 tLogger(
-Vespa
 ZooKeepe
@@ -666,26 +666,22 @@
 ooKeeper
-ServerImpl
+Runner
 .class.g
@@ -800,20 +800,8 @@
 g;%0A%0A
-    @Inject%0A
     
@@ -807,21 +807,16 @@
  public 
-Vespa
 ZooKeepe
@@ -820,18 +820,14 @@
 eper
-ServerImpl
+Runner
 (Zoo
@@ -1140,24 +1140,16 @@
  %7D%0A%0A    
-private 
 void shu
@@ -1845,104 +1845,106 @@
     
-@Override%0A    public void deconstruct() %7B%0A        shutdown();%0A        super.deconstruct()
+public ZookeeperServerConfig zookeeperServerConfig() %7B%0A        return zookeeperServerConfig
 ;%0A    %7D%0A
 %0A%7D%0A
@@ -1940,11 +1940,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Fix class names and shutdown