uidserver.Config.java Source code

Java tutorial

Introduction

Here is the source code for uidserver.Config.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package uidserver;

import java.io.File;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.ElementHandler;
import org.dom4j.ElementPath;
import org.dom4j.io.SAXReader;

/**
 *
 * @author ghfan
 */
public class Config {
    public static String port = null;
    public static File uidFile = null;
    public static File logPath = null;
    public static int timeOut = 10;

    public Config(String configFilePath) {
        File configFile = new File(configFilePath);
        if (configFile.exists()) {
            try {
                SAXReader reader = new SAXReader();
                reader.addHandler("/config", new ElementHandler() {
                    @Override
                    public void onStart(ElementPath elementPath) {

                    }

                    @Override
                    public void onEnd(ElementPath elementPath) {
                        Element row = elementPath.getCurrent();
                        readElement(row);
                        row.detach();
                    }

                    private void readElement(Element row) {
                        List<Element> nodes = row.elements();
                        if (!nodes.isEmpty()) {
                            for (Element node : nodes) {
                                String name = node.getName().toLowerCase();
                                String value = node.getText();
                                switch (name) {
                                case "logpath":
                                    logPath = new File(value);
                                    break;
                                case "port":
                                    port = value;
                                    break;
                                case "timeout":
                                    timeOut = Integer.valueOf(value);
                                    break;
                                case "uidfile":
                                    uidFile = new File(value);
                                    break;
                                }

                            }

                        } else {
                            System.out.println("Error: empty elements in config file, please add correct setup");
                            System.exit(0);
                        }

                    }

                });

                reader.setValidation(false);
                Document document = reader.read(configFile);
                if (logPath != null && port != null && uidFile != null) {
                    if (!logPath.exists()) {
                        if (!logPath.mkdirs()) {
                            System.out.println("Failed to create log file: " + logPath.getAbsoluteFile());
                            System.out.println("Please setup correct log file path");
                            System.exit(0);
                        }
                    }

                } else {
                    System.out.println("Please set up correct Port/LogFile/UidFile");
                    System.exit(0);
                }
            } catch (DocumentException ex) {
                Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("Error during reading xml config file, please double check file content");
                System.exit(0);
            }
        } else {
            System.out.println("The specified config file: " + configFile.getAbsolutePath() + " doesn't exist");
            System.out.println("Please key in correct config file path");
            System.exit(0);
        }
    }

}