Click here to Skip to main content
15,884,966 members
Articles / Web Development / IIS

BooProd.Core - Context sensitive URL

Rate me:
Please Sign up or sign in to vote.
3.80/5 (3 votes)
22 Dec 20047 min read 47.6K   22  
Helps you create context sensitive dynamic URLs: dynamically computed URLs, depending on which server the page is generated on.
using System;
using System.Collections;
using System.Net;
using System.Xml;
using System.Xml.Schema;
using System.Reflection;
using System.Reflection.Emit;
using System.Web.SessionState;

namespace BooProd.Core
{

	/// <summary>
	/// v1.1	/ 2004-11-19 / CB => Modification of ExeDB. XML file is now in version 1.2
	/// v1.0	/ 2004-08-21 / CB => Creation
	/// </summary>

	public class ExeContext
	{

		#region HostName
		/// <summary>
		/// My HostName
		/// </summary>
		private static string myHostName= (Dns.GetHostName()).ToLower();
		public static string HostName {
			get {	return myHostName;}
		}
		#endregion

		#region IsLocal
		/// <summary>
		/// Check if execution is on local machine.
		/// Local machine is considered running on ip range 10.0.x.x
		/// </summary>
		private bool	_IsLocal=						false;
		private bool	_IsLocalIsComputed=	false;
		public  bool getIsLocal() {
			if (_IsLocalIsComputed==false) {
				//' On ne fait le calcul qu'une fois
				_IsLocal= false;
				IPHostEntry ipEntry = Dns.GetHostByName(HostName);
				IPAddress [] IpAddr = ipEntry.AddressList;
				for (int i = 0; i < IpAddr.Length; i++) {
					if (getConfig().isLocalIP(IpAddr[i].ToString())) {
						_IsLocal= true;
						break;
					}
				}
				_IsLocalIsComputed= true;
			}
			return _IsLocal;
		}

		/// <summary>
		/// IsLocal value associated with the current context.
		/// </summary>
		/// <returns></returns>
		static public bool IsLocal {
			get {
				if (ExeContext.Current==null) {
					Console.WriteLine("Current ExeContext not initialized!");
					return false;
				}
				return ExeContext.Current.getIsLocal();
			}
		}

		#endregion

		#region ExeContext
		/// <summary>
		/// Current ExeContext
		/// </summary>
		private static ExeContext _Current= null;
		public static ExeContext Current {
			get { return _Current; }
			set { _Current= value; }
		}
		#endregion

		#region ExeConfig
		private ExeConfig _ExeConfig=			null;
		/// <summary>
		/// ExeConfig object associated with value.
		/// </summary>
		/// <returns>ExeConfig</returns>
		public ExeConfig getConfig() {
				return _ExeConfig;
		}
		/// <summary>
		/// ExeConfig object associated with the current context of this value.
		/// </summary>
		/// <returns>ExeConfig</returns>
		static public ExeConfig Config() {
			if (ExeContext.Current==null) {
				Console.WriteLine("Current ExeContext not initialized!");
				return null;
			}
			return ExeContext.Current.getConfig();
		}
		#endregion

		#region ExeDB
		private Hashtable _ExeDBList=			null;
		/// <summary>
		/// ExeDB object associated with value.
		/// </summary>
		/// <param name="value"></param>
		/// <returns>ExeDB</returns>
		public ExeDB getDB(string pDBAlias) {
			try {
				return ((ExeDB)(_ExeDBList[pDBAlias]));
			} catch {
				Console.WriteLine("ExeDB '{0}' not found!", pDBAlias);
				return null;
			}
		}
		/// <summary>
		/// ExeDB object associated with the current context of this value.
		/// </summary>
		/// <param name="value"></param>
		/// <returns>ExeDB</returns>
		static public ExeDB DB(string pDBAlias) {
			if (ExeContext.Current==null) {
				Console.WriteLine("Current ExeContext not initialized!");
				return null;
			}
			return ExeContext.Current.getDB(pDBAlias);
		}
		#endregion

		#region ExeWebSite
		private Hashtable _ExeWebSiteList= null;
		/// <summary>
		/// ExeWebSite object associated with value.
		/// </summary>
		public ExeWebSite getWebSite(string pWebSiteAlias) {
			try {
				return ((ExeWebSite)(_ExeWebSiteList[pWebSiteAlias]));
			} catch {
				Console.WriteLine("ExeWebSite '{0}' not found!", pWebSiteAlias);
				return null;
			}
		}
		/// <summary>
		/// ExeWebSite object associated with the current context of this value.
		/// </summary>
		/// <param name="value"></param>
		/// <returns>ExeWebSite</returns>
		static public ExeWebSite WebSite(string pWebSiteAlias) {
			if (ExeContext.Current==null) {
				Console.WriteLine("Current ExeContext not initialized!");
				return null;
			}
			return ExeContext.Current.getWebSite(pWebSiteAlias);
		}
		#endregion

		#region DELEGATE

		/// From:
		/// http://www.codeproject.com/managedcpp/csdeleg01.asp
		
		/// <summary>
		/// Delegate cryp method
		/// </summary>
		public delegate String CryptDelegate(string pString);
		private static CryptDelegate _CryptMethod;
		public static CryptDelegate CryptMethod {
			set { _CryptMethod= value; }
		}

		/// <summary>
		/// Delegate uncryp method
		/// </summary>
		public delegate String UnCryptDelegate(string pString);
		private static UnCryptDelegate _UnCryptMethod;
		public static UnCryptDelegate UnCryptMethod {
			set { _UnCryptMethod= value; }
		}

		#endregion

		#region INIT

		/// <summary>
		/// Read the config file in the entry assembly.
		/// </summary>
		private void readFromAssembly(System.Reflection.Assembly pAs, string pManifestRessourceFile) {
			Console.WriteLine("XMLConfig.readDefault: {0}");

			System.IO.Stream vStream= pAs.GetManifestResourceStream(pManifestRessourceFile);

			XmlDocument doc= new XmlDocument();
			doc.Load(vStream);

			XmlNodeReader reader= new XmlNodeReader(doc);

			//Moves the reader to the root element.
			reader.MoveToContent();
			reader.ReadStartElement("BooProd.Core.ExeContext");

			_ExeConfig=				ExeConfig.newFromXML(reader);
			_ExeDBList=				ExeDB.newAllFromXML(reader);
			_ExeWebSiteList=	ExeWebSite.newAllFromXML(reader);

			reader.ReadEndElement();
			reader.Close();
		}

		/// <summary>
		/// Initialize the execution context using the entry assembly.
		/// The xml file must be in the current application assembly.
		/// This king of init does not work inside a Global.asax.
		/// For a Web Application, use initFromCallingAssembly.
		/// </summary>
		/// <param name="pManifestRessourceFile"></param>
		/// <returns></returns>
		static public ExeContext initFromEntryAssembly(string pManifestRessourceFile) {
			System.Reflection.Assembly vAs = System.Reflection.Assembly.GetEntryAssembly();
			ExeContext vEC=			new ExeContext();
			vEC.readFromAssembly(vAs, pManifestRessourceFile);
			ExeContext.Current= vEC;
			return vEC;
		}
		/// <summary>
		/// Initialize the execution context using the calling assembly.
		/// The xml file must be in the current Web application assembly.
		/// For a Windows application, use initFromEntryAssembly.
		/// Put the ExeContext in a session variable.
		/// Apparently Not working on VS2005
		/// </summary>
		/// <param name="pManifestRessourceFile"></param>
		/// <param name="pSession"></param>
		/// <returns></returns>
		static public ExeContext initFromCallingAssembly(string pManifestRessourceFile, HttpSessionState pSession) {
			System.Reflection.Assembly vAs = System.Reflection.Assembly.GetCallingAssembly();
			ExeContext vEC=			new ExeContext();
			vEC.readFromAssembly(vAs, pManifestRessourceFile);
			ExeContext.Current= vEC;
			pSession["ExeContext"]= vEC;
			return vEC;
		}

		/// <summary>
		/// Initialize the executiong context using the executing assembly.
		/// The xml file must be in BooProd.Core assembly.
		/// </summary>
		/// <param name="pManifestRessourceFile"></param>
		/// <returns></returns>
		static internal ExeContext initFromExecutingAssembly(string pManifestRessourceFile) {
			System.Reflection.Assembly vAs = System.Reflection.Assembly.GetExecutingAssembly();
			ExeContext vEC=			new ExeContext();
			vEC.readFromAssembly(vAs, pManifestRessourceFile);
			ExeContext.Current= vEC;
			return vEC;
		}
		/// <summary>
		/// Same as initFromExecutingAssembly, and put the ExeContext in a session variable.
		/// </summary>
		/// <param name="pManifestRessourceFile"></param>
		/// <param name="pSession"></param>
		/// <returns></returns>
		static internal ExeContext initFromExecutingAssembly(string pManifestRessourceFile, HttpSessionState pSession) {
			ExeContext vEC= ExeContext.initFromExecutingAssembly(pManifestRessourceFile);
			pSession["ExeContext"]= vEC;
			return vEC;
		}

		static internal ExeContext initFromSession(HttpSessionState pSession) {
			ExeContext vEC= (ExeContext)(pSession["ExeContext"]);
			ExeContext.Current= vEC;
			return vEC;
		}

		/// <summary>
		/// Constructor
		/// </summary>
		public ExeContext()		{	}

		#endregion

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
France France
I work on a leading European telecom provider regarding on-line real time account management for B2B and B2C customers. Before this position, I worked in one of the leading European council providers of economic forecasts analyses.

I jump into software development in 1985 and never stop! I work with a lot of systems like Apple, NeXT, Unix, Windows. I develop with a lot of languages like Assembler, Pascal, C, C++, Java and C#. I play with databases like Oracle and SQL Server. I love networks and like to make systems working and cooperate themselves.

I'm very interested in MAS: Multi Agent System and really hope that computer will be human in the future. I work on BDI architecture extensions on this purpose, but this is the project of my life!

Comments and Discussions