view src/MPipeline2/src/mpipeline/ltpdapreferences/RepositoryPrefGroup.java @ 4:e3c5468b1bfe database-connection-manager

Integrate with LTPDAPreferences
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 16:20:06 +0100
parents f0afece42f48
children 5a49956df427
line wrap: on
line source

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mpipeline.ltpdapreferences;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import mpipeline.utils.MXMLUtils;
import mpipeline.utils.Utils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author hewitson
 */
public class RepositoryPrefGroup extends LTPDAPrefGroup {

  public static final int REPOSITORY_HOSTNAMES_CHANGED = 1;
  public static final int REPOSITORY_EXPIRY_CHANGED = 2;
  public static final int REPOSITORY_CACHE_PASSWORD_CHANGED = 3;
  public static final int REPOSITORY_MAX_CONNECTIONS_NUMBER_CHANGED = 4;
  
  private ArrayList<String> hostnames = new ArrayList<String>();
  private Integer expiry = 60;
  private Integer cachePassword = 2;
  private Integer maxConnectionsNumber = 10;

  public RepositoryPrefGroup() {
  }

  public RepositoryPrefGroup(Node node, float version) {

    NamedNodeMap nmap = node.getAttributes();
    expiry = MXMLUtils.getIntegerFromNode("expiry", nmap, version, version);

    NodeList nl = node.getChildNodes();
    for (int ii = 0; ii < nl.getLength(); ii++) {
      Node n = nl.item(ii);
      Utils.dmsg(" reading child node: " + n.getNodeName());
      if (n.getNodeName().equals("Hostname")) {
        NamedNodeMap wbnm = n.getAttributes();
        String path = MXMLUtils.getStringFromNode("Host", wbnm, version, version);
        this.addHostname(path);
      }
    }
  }

  public void attachToDom(Document doc, Element tnode) {

    Element pnode = doc.createElement("Repository");

    pnode.setAttribute("expiry", ""+expiry);

    Iterator it = hostnames.iterator();
    while (it.hasNext()) {
      String path = (String) it.next();
      Element snode = doc.createElement("Hostname");
      snode.setAttribute("Host", path);
      pnode.appendChild(snode);
    }

    tnode.appendChild(pnode);

  }

  public void addHostname(String host) {
    hostnames.add(host);
    this.setChanged();
    this.notifyObservers(REPOSITORY_HOSTNAMES_CHANGED);
  }

  public ArrayList<String> getHostnames() {
    return hostnames;
  }

  public Integer getExpiry() {
    return expiry;
  }

  public void setExpiry(Integer expiry) {
    this.expiry = expiry;
    setChanged();
    notifyObservers(REPOSITORY_EXPIRY_CHANGED);
  }

  public void setHostnames(ArrayList<String> hostnames) {
    this.hostnames = hostnames;
    setChanged();
    notifyObservers(REPOSITORY_HOSTNAMES_CHANGED);
  }

  public void removeHostnames(Object objs[]) {
    hostnames.removeAll(Arrays.asList(objs));
    this.setChanged();
    this.notifyObservers(REPOSITORY_HOSTNAMES_CHANGED);
  }
  
  public Integer getCachePassword() {
      return cachePassword;
  }
  
  public void setCachePassword(Integer value) {
      cachePassword = value;
      setChanged();
      notifyObservers(REPOSITORY_CACHE_PASSWORD_CHANGED);
  }
  
  public Integer getMaxConnectionsNumber() {
      return maxConnectionsNumber;
  }
  
  public void setMaxConnectionsNumber(Integer value) {
      maxConnectionsNumber = value;
      setChanged();
      notifyObservers(REPOSITORY_MAX_CONNECTIONS_NUMBER_CHANGED);
  }
  
  public void display() {
    System.out.printf(
        "RepositoryPrefGroup(expiry=%d, cachePassword=%d, maxConnectionsNumber=%d)\n",
        expiry, cachePassword, maxConnectionsNumber);
  }
}