Package org.saf.settings

Source Code of org.saf.settings.RemoteSettings

package org.saf.settings;

import java.util.ArrayList;
import java.util.StringTokenizer;
import org.apache.log4j.Logger;
import org.ini4j.Ini;
import org.ini4j.Ini.Section;
import org.saf.business.RemoteNode;

public class RemoteSettings extends ProducerConsumersSettings {
  static String SECTION = "remote";

  protected static final Logger logger =
    Logger.getLogger(RemoteSettings.class);
 
  protected static final String KEY_SERVER_PORT = "server_port";
  protected static final String KEY_NODES = "nodes";
  protected final static String KEY_ENABLED_SERVER = "enabled";

    private static final String DEFAULT_SERVER_PORT = "1099";
    private final static String DEFAULT_ENABLED = "N";
    private static final String UNUSABLE_SERVER_PORT = "0";
    private static final String UNUSABLE_NODE = "127.0.0.1:0";
   
  private static final String FIXED_QUEUE_CAPACITY = "50";
    private static final String FIXED_READ_TIMEOUT = "1";
    private static final String FIXED_WRITE_TIMEOUT = "1";
    private static final String FIXED_WRITE_MAX_RETRIES = "2";
    private static final String FIXED_CONSUMERS = "1"
 
    private int serverPort = 0;
    private boolean enabled = false;
    protected ArrayList<RemoteNode> nodes = null;
 
  public int getServerPort() {
    return this.serverPort;
  }
 
  public ArrayList<RemoteNode> getNodes() {
    return this.nodes;
  }
 
  public int remoteNodesSize() {
    return this.nodes.size();
  }
 
  public boolean isEnabled() {
    return this.enabled;
  }

  RemoteSettings() {
      super();
      init();
   

  @Override
  void createDefaults(Ini ini) {
        Ini.Section remoteSection = ini.add(SECTION);
        remoteSection.put(KEY_SERVER_PORT, UNUSABLE_SERVER_PORT);
        remoteSection.put(KEY_NODES, UNUSABLE_NODE);
  }

  @Override
  void load(Section remoteSection) {
    setQueueCapacity(FIXED_QUEUE_CAPACITY);
    setReadTimeout(FIXED_READ_TIMEOUT);
    setWriteTimeout(FIXED_WRITE_TIMEOUT);
    setWriteMaxRetries(FIXED_WRITE_MAX_RETRIES);
    setConsumers(FIXED_CONSUMERS);
   
    String sServerPort = DEFAULT_SERVER_PORT;
    String sNodes = "";
    String sEnabled = DEFAULT_ENABLED;
   
        if(remoteSection != null) {
            String tmp;
         
            tmp = remoteSection.get(KEY_SERVER_PORT);
            if(tmp != null){
              sServerPort = tmp;
            }
           
            tmp = remoteSection.get(KEY_ENABLED_SERVER);
            if(tmp != null) {
              sEnabled = tmp;
            }
           
            tmp = remoteSection.get(KEY_NODES);
            if(tmp != null){
              sNodes = tmp;
            }
           
            tmp = null;
        }   
       
        setServerPort(sServerPort);
        setNodes(sNodes);
        setEnabled(sEnabled);
       
        sServerPort = null;
        sNodes = null;
  }
 
  @Override
  void refresh(Section section) {
    // TODO Auto-generated method stub
   
 
 
  private void init() {
    setServerPort(DEFAULT_SERVER_PORT);
    nodes = new ArrayList<RemoteNode>();
    setEnabled(DEFAULT_ENABLED);
  }
 
    private void setServerPort(String value) {
        try {
          this.serverPort = Integer.parseInt(value);
        } catch (NumberFormatException e) {
          this.serverPort =
            Integer.parseInt(DEFAULT_SERVER_PORT);
          logger.error(KEY_SERVER_PORT + " no valid value: " + value +
                  ". Used: " + this.serverPort + " value.");        
        }
    } 
   
    private void setNodes (String value) {         
      StringTokenizer tokenizer = new StringTokenizer(value, ";");
      while(tokenizer.hasMoreElements() == true) {
        String node = tokenizer.nextToken();
        int idx = node.indexOf(":");
        if(idx > -1) {
          String address = node.substring(0, idx);
          int port = 0;
          try {
            port = Integer.parseInt(node.substring(idx + 1));
          } catch (NumberFormatException e) {
          }
          RemoteNode rm = new RemoteNode(address, port);
          if(rm.isValidNode() == true) {
            this.nodes.add(rm);
          } else {
            rm = null;
            logger.error("no valid node data for " + address);
          }
        } else {
          logger.error("no valid node for " + node +
              ". To be address:port");
        }
        node = null;
      }
      tokenizer = null;
   }
   
  private void setEnabled(String value) {
    if(value != null && value.compareTo("Y") == 0) {
      this.enabled = true;
    } else {
      this.enabled = false;
    }
  }
}
TOP

Related Classes of org.saf.settings.RemoteSettings

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.