www.pudn.com > OracleServiceManager.zip > ServiceContext.cs


using System; 
using System.Diagnostics; 
using System.ServiceProcess; 
 
namespace kae.ServiceStatePublisher 
{ 
	///  
	/// Establishes	the	client contract	or interface.  The context delegates state specific	requests to	appropriate	concrete state object. 
	///  
	///  
	/// ServiceContext is part of the State Pattern implementation for the ServiceStatePublisher library. 
	/// For	more information on	the	state pattern see Design	Patterns: Elements of Reusable Code	by Gamma, et al. 
	///  
	public class	ServiceContext 
	{ 
		protected ServiceController _controller; 
		protected ServiceState _state; 
 
		public ServiceContext() 
		{ 
			State = ServiceStateUnknown.Instance; 
			Controller = null; 
		} 
 
		public ServiceContext( ServiceController service) 
		{ 
			Controller = service; 
		} 
 
		public virtual ServiceController Controller 
		{ 
			get { return _controller; } 
			set 
			{ 
				if (_controller != value) 
				{ 
					_controller = value; 
					State = QueryServiceState(); 
				} 
			} 
		} 
 
		public virtual ServiceState State 
		{ 
			get { return _state; } 
			set 
			{ 
				if (_state != value) 
					_state = value; 
			} 
		} 
 
		public void Start() 
		{ 
			State.Start( this); 
		} 
 
		public void Stop() 
		{ 
			State.Stop( this); 
		} 
 
		public void Pause() 
		{ 
			State.Pause( this); 
		} 
 
		public void Continue() 
		{ 
			State.Continue( this); 
		} 
 
		public bool CanStart 
		{ 
			get { return State.CanStart( this); } 
		} 
 
		public bool CanPause 
		{ 
			get { return State.CanPause( this); } 
		} 
 
		public bool CanStop 
		{ 
			get { return State.CanStop( this); } 
		} 
 
		protected ServiceState QueryServiceState() 
		{ 
			ServiceState state; 
 
			if (Controller != null) 
			{ 
				switch (Controller.Status) 
				{ 
					case ServiceControllerStatus.ContinuePending: 
						state = ServicePendingContinue.Instance; 
						break; 
					case ServiceControllerStatus.Paused: 
						state = ServicePaused.Instance; 
						break; 
					case ServiceControllerStatus.PausePending: 
						state = ServicePendingPause.Instance; 
						break; 
					case ServiceControllerStatus.Running: 
						state = ServiceRunning.Instance; 
						break; 
					case ServiceControllerStatus.StartPending: 
						state = ServicePendingStart.Instance; 
						break; 
					case ServiceControllerStatus.Stopped: 
						state = ServiceStopped.Instance; 
						break; 
					case ServiceControllerStatus.StopPending: 
						state = ServicePendingStop.Instance; 
						break; 
					default: 
						Debug.Assert( false); 
						state = ServiceStateUnknown.Instance; 
						break; 
				} 
			} 
			else 
				state = ServiceStateUnknown.Instance; 
 
			return state; 
		} 
	} 
}