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


using System; 
using System.Diagnostics; 
using System.Globalization; 
using System.Reflection; 
using System.ServiceProcess; 
using System.Threading; 
using System.Windows.Forms; 
using kae.Configuration; 
using kae.ServiceStatePublisher; 
 
namespace kae.OracleServiceManager 
{ 
	///  
	/// Summary description for OracleServiceManager. 
	///  
	public class OracleServiceManager : ApplicationContext 
	{ 
		private const string OPTIONS_STR = "options"; 
		private const string POLLING_INTERVAL_STR = "pollingInterval"; 
		private const string VERIFY_ACTIONS_STR = "verifyActions"; 
 
		private static SingleInstance _singleInstance; 
		private static OracleServiceManager _instance; 
		private ServiceSubject _subject; 
		private SystemTrayObserver _trayObserver; 
		private DialogObserver _dialogObserver; 
		private TimerObserver _timerObserver; 
		private ConfigSettings _config; 
		private OsmResources _resources; 
		private double _pollingInterval; 
		private bool _verifyActions; 
		private string _title; 
 
		static OracleServiceManager() 
		{ 
			Type type = typeof(OracleServiceManager); 
			_singleInstance = new SingleInstance( type.GUID); 
			_instance = new OracleServiceManager(); 
		} 
 
		protected OracleServiceManager() 
		{ 
		} 
 
		internal static OracleServiceManager Instance 
		{ 
			get { return _instance; } 
		} 
 
		private void InitializeApplication() 
		{ 
			LoadConfig(); 
			_resources = new OsmResources(); 
 
			_title = GetAssemblyTitle(); 
			_subject = new ServiceSubject(); 
 
			_dialogObserver = new DialogObserver( _subject);			 
			_trayObserver = new SystemTrayObserver( _subject); 
			_timerObserver = new TimerObserver( _subject, _pollingInterval, _dialogObserver.SynchronizingObject); 
 
			Subject.ServerName = Environment.MachineName; 
		} 
 
		private void LoadConfig() 
		{ 
			_config = new ConfigSettings(); 
			if (_config.FileExists) 
			{ 
				_config.SectionName = OPTIONS_STR; 
				_pollingInterval = Convert.ToDouble( _config.GetValue( POLLING_INTERVAL_STR)); 
				_verifyActions = Convert.ToBoolean( _config.GetValue( VERIFY_ACTIONS_STR)); 
			} 
			else 
			{ 
				_pollingInterval = 5000; 
				_verifyActions = false; 
			} 
		} 
 
		internal string Title 
		{ 
			get { return _title; } 
		} 
 
		internal ServiceSubject Subject 
		{ 
			get { return _subject; } 
		} 
 
		internal OsmResources Resources 
		{ 
			get { return (_resources != null ? _resources : new OsmResources()); } 
		} 
 
		internal double PollingInterval 
		{ 
			get { return _pollingInterval; } 
			set { _pollingInterval = Convert.ToDouble( value); } 
		} 
 
		internal bool VerifyServiceActions 
		{ 
			get { return _verifyActions; } 
			set { _verifyActions = value; } 
		} 
 
		internal bool VerifyUserAction( string action, string serviceName, string serverName) 
		{ 
			if (VerifyServiceActions) 
			{ 
				string message = String.Format( Resources.GetAppString( "VerifyUserAction"), action.ToUpper(), serviceName.ToUpper(), serverName.ToUpper()); 
			 
				DialogResult result = MessageBox.Show( message, this.Title, MessageBoxButtons.YesNo, MessageBoxIcon.Question); 
				if (DialogResult.Yes == result) 
					return true; 
				else 
					return false; 
			} 
			return true; 
		} 
 
		private string GetAssemblyTitle() 
		{ 
			Assembly asm = Assembly.GetExecutingAssembly(); 
			object[] atts = asm.GetCustomAttributes( false); 
 
			for (int n = 0; n < atts.Length; n++) 
			{ 
				if (atts[n] is AssemblyTitleAttribute) 
				{ 
					AssemblyTitleAttribute ata = (AssemblyTitleAttribute) atts[n]; 
					return ata.Title; 
				} 
			} 
			return String.Empty; 
		} 
 
		internal void OpenServiceManagerDialog() 
		{ 
			if (_dialogObserver != null) 
			{ 
				_dialogObserver.Show(); 
				_dialogObserver.Activate(); 
			} 
		} 
 
		internal void ShowAboutDialog() 
		{ 
			AboutDialog about = new AboutDialog(); 
			about.ShowDialog(); 
		} 
 
		internal void ConfigureOptions() 
		{ 
			int interval = Convert.ToInt32( _pollingInterval / 1000); 
			OptionsDialog options = new OptionsDialog( interval, _verifyActions); 
			if (options.ShowDialog() == DialogResult.OK) 
			{ 
				_pollingInterval = Convert.ToDouble( options.PollingInterval * 1000); 
				_verifyActions = options.VerifyServiceActions; 
				_timerObserver.Interval = _pollingInterval; 
				SaveConfig(); 
			} 
		} 
 
		private void SaveConfig() 
		{ 
			if (!_config.FileExists) 
			{ 
				_config.CreateConfigFile( OPTIONS_STR); 
			} 
 
			_config.SectionName = OPTIONS_STR; 
			_config.SetValue( POLLING_INTERVAL_STR, _pollingInterval.ToString()); 
			_config.SetValue( VERIFY_ACTIONS_STR, _verifyActions.ToString()); 
		} 
 
		private bool IsCultureValid( string cultureName) 
		{ 
			CultureInfo[] cultures = CultureInfo.GetCultures( CultureTypes.AllCultures); 
			foreach( CultureInfo culture in cultures) 
			{ 
				if (culture.Name.Equals( cultureName)) 
					return true; 
			} 
 
			return false; 
		} 
 
		private void SetThreadUICulture( string cultureName) 
		{ 
			if (IsCultureValid( cultureName)) 
				Thread.CurrentThread.CurrentUICulture = new CultureInfo( cultureName); 
		} 
 
		///  
		/// The main entry point for the application. 
		///  
		[STAThread] 
		static void Main( string[] args) 
		{ 
			if (!_singleInstance.IsRunning) 
			{ 
				if ( args.Length > 0) 
					Instance.SetThreadUICulture( args[0]); 
 
				Instance.InitializeApplication(); 
				Application.Run( OracleServiceManager.Instance); 
				 
				_singleInstance.Dispose(); 
			} 
			else 
				MessageBox.Show( Instance.Resources.GetAppString( "AlreadyRunning"), 
									  Instance.Resources.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information); 
		} 
	} 
}