/*
 * 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.dubbo.registrycenter;

import org.apache.curator.test.TestingServer;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.RpcException;

import java.io.IOException;

/**
 * The default single registry center.
 */
public class DefaultSingleRegistryCenter implements SingleRegistryCenter {

    /**
     * Initialize {@link RegistryCenter} instance.
     */
    public DefaultSingleRegistryCenter() {
        this(DEFAULT_PORT);
    }

    /**
     * Initialize {@link RegistryCenter} instance.
     *
     * @param port the zookeeper server's port.
     */
    public DefaultSingleRegistryCenter(int port) {
        this.port = port;
    }

    private static final Logger logger = LoggerFactory.getLogger(DefaultSingleRegistryCenter.class);

    /**
     * The zookeeper server's default port.
     */
    private static final int DEFAULT_PORT = 2181;
    /**
     * Define a zookeeper server.
     */
    private volatile TestingServer zookeeperServer;

    /**
     * The zookeeper server's port.
     */
    private int port;

    /**
     * {@inheritDoc}
     */
    @Override
    public void startup() throws RpcException {
        try {
            logger.info("The DefaultSingleRegistryCenter is starting...");
            this.zookeeperServer = new TestingServer(this.port, true);
            logger.info("The DefaultSingleRegistryCenter is started successfully");
        } catch (Exception exception) {
            throw new RpcException("Failed to initialize DefaultSingleRegistryCenter instance", exception);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public RegistryConfig getRegistryConfig() {
        return new RegistryConfig("zookeeper://" + this.zookeeperServer.getConnectString());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void shutdown() throws RpcException {
        try {
            logger.info("The DefaultSingleRegistryCenter is stopping...");
            if (this.zookeeperServer != null) {
                this.zookeeperServer.close();
            }
            logger.info("The DefaultSingleRegistryCenter is shutdown successfully");
        } catch (IOException exception) {
            throw new RpcException("Failed to close DefaultSingleRegistryCenter instance", exception);
        }
    }
}
