com.asuraiv.coordination.menu.MainMenu.java Source code

Java tutorial

Introduction

Here is the source code for com.asuraiv.coordination.menu.MainMenu.java

Source

/*
 * @(#)MainView.java $version 2016. 1. 4.
 *
 * Copyright 2007 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package com.asuraiv.coordination.menu;

import java.io.IOException;
import java.util.List;
import java.util.Scanner;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.springframework.util.CollectionUtils;

/**
 * @author Jupyo Hong
 */
public class MainMenu implements Menu, Watcher {

    private Menu nextMenu;

    @Override
    public void displayMenu() {

        boolean isContinue = true;

        while (isContinue) {

            System.out.println();
            System.out.println();
            System.out.println("### Main MENU ###");
            System.out.println("1.  ?");
            System.out.println("2.  ");
            System.out.println("3.  ");
            System.out.println("99.  ");
            System.out.println();
            System.out.print(" ? >  ");

            @SuppressWarnings("resource")
            Scanner scanner = new Scanner(System.in);

            switch (scanner.next()) {
            case "1":
                nextMenu = new RegistTaskMenu();
                isContinue = false;
                break;
            case "2":
                doTaskListUp();
                isContinue = true;
                break;
            case "3":
                doDeleteTasks();
                isContinue = true;
                break;
            case "99":
                //  
                isContinue = false;
                break;
            default:
                System.out.println("[WRONG!] .");
                isContinue = true;
                break;
            }
        }

        nextMenu.displayMenu();
    }

    /**
     * ? ? ().
     */
    private void doDeleteTasks() {
        try {

            ZooKeeper zk = new ZooKeeper("10.113.182.195:2181", 15000, this);

            System.out.println();
            System.out.println("###    ###");

            List<String> tasks = zk.getChildren("/tasks", false, null);

            if (CollectionUtils.isEmpty(tasks)) {
                System.out.println("[WARNNING]  ?? ? .");
                return;
            }

            for (String task : tasks) {
                zk.delete("/tasks/" + task, -1);
                System.out.println(task + " ? ");
            }

            zk.close();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     *  ? .
     */
    private void doTaskListUp() {
        try {

            ZooKeeper zk = new ZooKeeper("10.113.182.195:2181", 15000, this);

            System.out.println();
            System.out.println("###   ###");

            List<String> tasks = zk.getChildren("/tasks", false, null);

            if (CollectionUtils.isEmpty(tasks)) {
                System.out.println("[WARNNING]  ?? ? .");
            }

            for (String task : tasks) {
                String status = new String(zk.getData("/tasks/" + task, false, null));
                System.out.println(task + ": " + status);
            }

            zk.close();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param event
     * @see org.apache.zookeeper.Watcher#process(org.apache.zookeeper.WatchedEvent)
     */
    @Override
    public void process(WatchedEvent event) {
    }
}