[Pkg-mono-svn-commits] rev 361 - in mono-nethostmanager/src: . doc

Pablo Fischer pabl0-guest@quantz.debian.org
Thu, 26 Feb 2004 05:58:58 +0100


Author: pabl0-guest
Date: 2004-02-26 05:58:57 +0100 (Thu, 26 Feb 2004)
New Revision: 361

Added:
   mono-nethostmanager/src/Makefile
   mono-nethostmanager/src/Manager.cs
   mono-nethostmanager/src/VirtualSchema.cs
   mono-nethostmanager/src/doc/nethostmanager.1
Log:
* Added:
 * doc/nethostmanager: Is the man page of nethostmanager.
 * VirtualSchema.cs: Is the source code (csharp) of the XML struct, in few words, 
   we use it to serialize and deserialize the XML file into variables, and also includes
   the main functions (add/delete/print hosts).
 * Makefile: More obvious?, contains the process to build/install the app and the 
   man page
 * Manager.cs: The main class. The main function is to check if the file exists, check if 
   the file (XML file) is valid, read the args from the command line.



Added: mono-nethostmanager/src/Makefile
===================================================================
--- mono-nethostmanager/src/Makefile	2004-02-26 03:47:59 UTC (rev 360)
+++ mono-nethostmanager/src/Makefile	2004-02-26 04:58:57 UTC (rev 361)
@@ -0,0 +1,37 @@
+#Makefile for mono-nethostmanager
+
+SHELL = /bin/sh
+MCS = mcs
+
+INSTALL = /usr/bin/install -c
+INSTALL_PROGRAM = ${INSTALL}
+INSTALL_DATA = ${INSTALL} -m 644
+INSTALL_DIR = ${INSTALL} -d
+
+srcdir = .
+prefix = /usr
+top_srcdir = ..
+mandir = ${prefix}/share/man
+bindir = ${prefix}/bin
+man1dir = $(mandir)/man1
+
+
+NAME = nethostmanager
+SOURCES = Manager.cs VirtualSchema.cs
+ 
+all: nethostmanager.exe
+ 
+nethostmanager.exe: $(SOURCES) 
+	$(MCS) -o $@ $(SOURCES) 
+
+install: nethostmanager.exe	
+	$(INSTALL) -m 755 $(NAME).exe $(DESTDIR)$(bindir)/$(NAME).exe
+	$(INSTALL_DIR) $(DESTDIR)$(man1dir)
+	$(INSTALL_DATA) doc/nethostmanager.1 $(DESTDIR)$(man1dir)/nethostmanager.1
+ 
+uninstall:
+	rm -Rf $(DESTDIR)/bin/$(NAME).exe
+	rm -Rf $(DESTDIR)/$(man1dir)/nethostmanager.1 
+ 
+clean:
+	rm -Rf $(NAME).exe *.bak *~

Added: mono-nethostmanager/src/Manager.cs
===================================================================
--- mono-nethostmanager/src/Manager.cs	2004-02-26 03:47:59 UTC (rev 360)
+++ mono-nethostmanager/src/Manager.cs	2004-02-26 04:58:57 UTC (rev 361)
@@ -0,0 +1,263 @@
+using System;
+using System.IO;
+using System.Xml;
+using System.Xml.Serialization;
+using System.Xml.XPath;
+using System.Collections;
+
+
+public class HostManager {
+
+  private static VirtualHosts hosts;
+  
+  private static string config_file;
+  private static string host_path;
+  private static string host_alias;
+  private static string action;
+    
+  ///<summary>
+  ///Verifies if the config file exists (the one that receives from the cmd line)
+  ///if the file is found return true, else, return false.
+  ///</summary>
+  private static bool ExistsConfig() {
+    if(System.IO.File.Exists(config_file)) {
+      return true;
+    }
+    else {
+      return false;
+    }
+  }
+
+  ///<summary>
+  ///Open the config file, read it, and look for errors, if there are no errors
+  ///we should return true, else, return false
+  ///</summary>
+  private static bool IsConfigValid() {
+    //Create the stream to check if it can be deserialized
+    System.Xml.XmlReader fStream = new System.Xml.XmlTextReader(config_file);
+    //Now the deserializer
+    try {
+      XmlSerializer xSerial = new XmlSerializer(typeof(VirtualHosts));
+      if(xSerial.CanDeserialize(fStream)) {
+	fStream.Close();
+	return true;
+      }
+      else {
+	fStream.Close();
+	return false;  
+      }
+    }catch(System.Xml.XmlException ex) {
+      fStream.Close();
+      return false;
+    }
+  }
+  
+  ///<summary>
+  ///Open the file and load it inside @hosts
+  ///</summary>
+  private static void LoadConfigInfo() {
+    //Create that object
+    hosts = new VirtualHosts();
+    //So, lets load it
+    XmlSerializer xSerial = new XmlSerializer(typeof(VirtualHosts));
+      //Lets open that file
+    FileStream fStream = new FileStream(config_file, FileMode.Open);
+    //Load, load!
+    hosts = (VirtualHosts)xSerial.Deserialize(fStream);
+    //CLose that file
+    fStream.Close();
+  }
+
+
+  ///<summary>
+  ///Save the file
+  ///</summary>
+  private static void Save() {
+    StreamWriter myWriter = new StreamWriter(config_file);
+    XmlSerializer xs = new XmlSerializer(typeof(VirtualHosts));
+    xs.Serialize(myWriter, hosts);
+    myWriter.Close();
+  }
+
+  ///<summary>
+  ///TODO AND TOFIX....
+  ///</summary>
+  private static void PrintHosts() {
+    VirtualHosts vh_tmp = new VirtualHosts();
+    XmlSerializer mySerializer = new XmlSerializer(typeof(VirtualHosts));
+    FileStream myFileStream = new FileStream(config_file, FileMode.Open);
+    vh_tmp = (VirtualHosts)mySerializer.Deserialize(myFileStream);
+  }
+
+  ///<summary>
+  ///Detects the option that is given from the command line
+  ///</summary>
+  private static void WhatIsIt(string option) {
+    
+    //We don't need this if we are just going to print or repair
+    if(action == "print") {
+      //Get the file name, after the '=' char
+      //Does the option has a value?
+      try {
+	config_file = option.Split('=')[1];
+      }catch(System.IndexOutOfRangeException ex) {
+	Console.WriteLine("No values for {0}", option.Split('=')[0]);
+	System.Environment.Exit(0);
+      }	
+      //If the file does not exists and the action depends of the file
+      if(!ExistsConfig()) {
+	Console.WriteLine("[FNE] The filename does not exists {0}", config_file);
+	System.Environment.Exit(0);
+      }
+      if(!IsConfigValid()) {
+	Console.WriteLine("[FNV] The file format is not valid {0}", config_file);
+	System.Environment.Exit(0);
+      }
+    }
+    else {
+      switch(option.Split('=')[0]) {
+      case "--config-file":
+	//Get the file name, after the '=' char
+	//Does the option has a value?
+	try {
+	  config_file = option.Split('=')[1];
+	}catch(System.IndexOutOfRangeException ex) {
+	  Console.WriteLine("No values for {0}", option.Split('=')[0]);
+	  System.Environment.Exit(0);
+	}	
+	//If the file does not exists and the action depends of the file
+	if(ExistsConfig()) {
+	  if(!IsConfigValid() && action != "add") {
+	    Console.WriteLine("[FNV] The file format is not valid {0}", config_file);
+	    System.Environment.Exit(0); 
+	  }
+	}
+	else {
+	  if(action != "add") {
+	    Console.WriteLine("[FNE] The filename does not exists {0}", config_file);
+	    System.Environment.Exit(0);
+	  }
+	}
+      	break;
+      case "--path":
+	//Get the full path of the web files
+	//Does the option has a value?
+	try {
+	  host_path = option.Split('=')[1];
+	}catch(System.IndexOutOfRangeException ex) {
+	  Console.WriteLine("No values for {0}", option.Split('=')[0]);
+	  System.Environment.Exit(0);
+	}	
+	if(!System.IO.Directory.Exists(host_path)) {
+	  Console.WriteLine("[DNE] The Path Directory does not exists");
+	  System.Environment.Exit(0);
+	}
+	break;
+    case "--alias":
+      try {
+	host_alias = option.Split('=')[1];
+      }catch(System.IndexOutOfRangeException ex) {
+	Console.WriteLine("No values for {0}", option.Split('=')[0]);
+	System.Environment.Exit(0);
+      }
+      //Remove the /      
+      host_alias.Replace("/", "");  
+      break;
+    default:
+      Console.WriteLine("[ONR] {0} Option not Recognized", option.Split('=')[0]);
+      System.Environment.Exit(0);
+      break;
+    }
+  }
+  }
+
+
+
+  ///<summary>
+  ///The main
+  ///</summary>
+  public static void Main(string[] args) {
+    try {
+      action = args[0];
+    }catch(System.IndexOutOfRangeException ex) {
+    }
+  
+    if(args.Length == 0 || args.Length < 4 && action != "print") {
+      Console.WriteLine("VirtualHost Manager");
+      Console.WriteLine("Use: netvirtualhost [action] [options]\n");
+      Console.WriteLine("Actions:");
+      Console.WriteLine(" add                         Add a VirtualHost");
+      Console.WriteLine(" del                         Delete a VirtualHost");
+      Console.WriteLine(" print                       Prints the file to screen in the xsp format (/alias:path)");
+      Console.WriteLine("Options:");
+      Console.WriteLine("   --config-file=<file>      The full path where the config file is");
+      Console.WriteLine("   --alias=<alias>           The alias that you want for the host");
+      Console.WriteLine("   --path=<path>             The path where your web files are");
+      Console.WriteLine("\nFor example:");
+      Console.WriteLine(" netvirtualhost add --config-file=/etc/xsp/files.conf --alias=/examples --path=/usr/share/foo/bar\n");
+      System.Environment.Exit(0);
+
+    }
+    //Ok, read the command line args
+    else {
+      action = args[0];
+      //Which action?
+      for(int i=1; i<args.Length; i++) {
+	WhatIsIt(args[i]);
+      }
+    }
+
+    //Now, what we need to do?
+    int pos;
+
+    switch(action) {
+    case "add":
+      if(!ExistsConfig()) {
+	hosts = new VirtualHosts();
+	hosts.AddHost(new Host(host_path, host_alias));
+	Save();
+      }
+      else {
+	LoadConfigInfo();      
+	//We already have a host with this path/alias?
+	if(hosts.GetHostPosition(host_alias, host_path) == -1) {
+	  hosts.AddHost(new Host(host_path, host_alias));
+	  Save();
+	}
+	else {
+	  Console.WriteLine("[VHE] The VirtualHost already exists");
+	  System.Environment.Exit(0);
+	}
+      }
+      break;
+    case "del":
+      LoadConfigInfo(); 
+      //We have this host?
+      //Save the value cause we will need it later..
+      pos = hosts.GetHostPosition(host_alias, host_path);
+      if(pos != -1) {
+	hosts.RemoveHost(pos);
+	Save();
+      }
+      else {
+	Console.WriteLine("[VHN] The VirtualHost does not exists");
+	System.Environment.Exit(0);
+      }
+      break;
+    case "print":      
+      LoadConfigInfo(); 
+      hosts.PrintHosts();
+      break;
+    }
+  }
+}
+
+      
+
+
+      
+      
+
+      
+
+	

Added: mono-nethostmanager/src/VirtualSchema.cs
===================================================================
--- mono-nethostmanager/src/VirtualSchema.cs	2004-02-26 03:47:59 UTC (rev 360)
+++ mono-nethostmanager/src/VirtualSchema.cs	2004-02-26 04:58:57 UTC (rev 361)
@@ -0,0 +1,71 @@
+using System;
+using System.IO;
+using System.Xml;
+using System.Xml.Serialization;
+using System.Collections;
+
+[XmlRoot("VirtualHosts")]
+public class VirtualHosts {
+  
+  [System.Xml.Serialization.XmlArray("Hosts")]
+  [System.Xml.Serialization.XmlArrayItem("Host", typeof(Host))]
+  public ArrayList hostArray;
+  private int position;
+
+  public VirtualHosts() {
+    hostArray = new ArrayList();
+  }
+
+  public int AddHost(Host new_host) {
+    return hostArray.Add(new_host);
+  }
+
+  public void RemoveHost(int host_position) {
+    hostArray.RemoveAt(host_position);
+  }
+
+  public int GetHostPosition(string alias_match, string path_match) {
+    for(position=0; position<hostArray.Count; position++) 
+      if( ((Host)hostArray[position]).path == path_match )
+	return position;
+    return -1;
+  }
+  
+  public void ModifyHost(int host_position, string alias, string path) {
+    ((Host)hostArray[position]).alias = alias;
+    ((Host)hostArray[position]).path  = path;
+  }
+
+  public void PrintHosts() {
+    if(position.hostArray.Count == 0) {
+      Console.Write("[NHF] Sorry, No Hosts Found");
+    }
+    else {
+      for(position=0; position<hostArray.Count; position++) 
+	if(position == hostArray.Count-1) {
+	  Console.Write("{0}:{1}", ((Host)hostArray[position]).alias, ((Host)hostArray[position]).path);
+	}
+	else {
+	  Console.Write("{0}:{1},", ((Host)hostArray[position]).alias, ((Host)hostArray[position]).path);
+	}			  
+      Console.WriteLine("");
+    }
+  }
+ 
+}
+
+
+public class Host {
+  [XmlElement("path")]  public string path;
+  [XmlElement("alias")] public string alias;
+  
+  public Host() {}
+
+  public Host(string new_path, string new_alias) {
+    path  = new_path;
+    alias = new_alias;
+  }
+
+}
+
+	

Added: mono-nethostmanager/src/doc/nethostmanager.1
===================================================================
--- mono-nethostmanager/src/doc/nethostmanager.1	2004-02-26 03:47:59 UTC (rev 360)
+++ mono-nethostmanager/src/doc/nethostmanager.1	2004-02-26 04:58:57 UTC (rev 361)
@@ -0,0 +1,70 @@
+.TH nethostmanager 1 "24 February 2004"
+.SH NAME 
+nethostmanager \- A XML VirtualHost manager, made in mono
+.SH SYNOPSIS
+.B mono nethostmanager.exe
+[options]
+.SH DESCRIPTION
+A XML VirtualHost manager, made in mono
+.PP
+The mono-hostmanager is a very little app to manage a 'host file' 
+for the XSP (or Mod-mono-server). 
+.PP
+What mono-hostmanger do, is add hosts (with their alias and path) 
+and remove them. The way that the config file is, is in XML format:
+.PP
+<VirtualHosts>
+.PP
+ <Hosts>
+.PP
+  <Host>
+.PP
+   <path>/path/of/my/foo/bar/aspapp</path>
+.PP
+   <alias>/mialias</alias>
+.PP
+  </Host>
+.PP
+ </Hosts>
+.PP
+</VirtualHosts>
+.PP
+Nethostmanager was wrote in C#, it uses Serialize and Deserializer 
+methods to work with the XML struct.
+.SH OPTIONS
+.TP
+.I add
+Add a VirtualHost
+.TP
+.I del
+Delete a VirtualHost
+.TP
+.I print
+Prints the file to screen in the xsp format (/alias:path)
+.TP
+.I \-\-config\-file
+The full path where the config file is
+.TP
+.I \-\-alias
+The alias that you want for the host
+.TP
+.I \-\-path
+The path where your web files are
+.TP
+Some examples:
+.TP
+.B add --config-file=/etc/xsp/files.conf --alias=examples --path=/usr/share/foo/bar
+Create a new VirtualHost (if no one is found) in the files.conf, with the alias: examples, 
+and the dir the the ASP.NET are is '/usr/share/foo/bar'.
+.PP
+.SH AUTHORS
+Mono NetHostManager was written by Pablo Fischer (pablo@pablo.com.mx)
+.SH SEE ALSO
+.BR mono (1),
+.PP
+http://www.pablo.com.mx is Pablo Fischer official site
+.SH MORE INFORMATION
+The Mono project (http://www.go-mono.com) is a collaborative effort
+led by Ximian (http://www.ximian.com) to implement an open source
+version of the .NET Framework.
+