New test.
[mono.git] / mcs / class / System.ServiceProcess / System.ServiceProcess / ServiceBase.cs
1 //
2 // System.ServiceProcess.ServiceBase.cs
3 //
4 // Authors:
5 //      Cesar Octavio Lopez Nataren (cesar@ciencias.unam.mx)
6 //      Duncan Mak (duncan@ximian.com)
7 //      Joerg Rosenkranz (joergr@voelcker.com)
8 //
9 // (C) 2003, Ximian Inc and Cesar Octavio Lopez Nataren.
10 // (C) 2005, Voelcker Informatik AG
11 //
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.ComponentModel;
35 using System.Globalization;
36 using System.Diagnostics;
37
38 namespace System.ServiceProcess
39 {
40 #if ONLY_1_1
41         [Designer ("Microsoft.VisualStudio.Install.UserNTServiceDesigner, " + Consts.AssemblyMicrosoft_VisualStudio, "System.ComponentModel.Design.IRootDesigner")]
42 #endif
43         [InstallerType (typeof (ServiceProcessInstaller))]
44         public class ServiceBase : System.ComponentModel.Component
45         {
46                 internal delegate void RunServiceCallback (ServiceBase [] services);
47
48                 // This member is used for interoperation with mono-service
49                 internal static RunServiceCallback RunService = null;
50
51                 public const int MaxNameLength = 80;
52
53                 bool hasStarted;
54                 bool auto_log = true;
55                 bool can_handle_power_event;
56                 bool can_pause_and_continue;
57                 bool can_shutdown;
58                 bool can_stop = true;
59                 EventLog event_log;
60                 string service_name;
61
62                 public ServiceBase ()
63                 {
64                 }
65
66                 [DefaultValue (true)]
67                 [ServiceProcessDescription ("Whether the service should automatically write to the event log on common events such as Install and Start.")]
68                 public bool AutoLog {
69                         get { return auto_log; }
70                         set { auto_log = value; }
71                 }
72
73                 [DefaultValue (false)]
74                 public bool CanHandlePowerEvent {
75                         get { return can_handle_power_event; }
76                         set {
77                                 if (hasStarted)
78                                         throw new InvalidOperationException (
79                                                         Locale.GetText ("Cannot modify this property " +
80                                                                                         "after the service has started."));
81
82                                 can_handle_power_event = value;
83                         }
84                 }
85
86                 [DefaultValue (false)]
87                 public bool CanPauseAndContinue {
88                         get { return can_pause_and_continue; }
89                         set {
90                                 if (hasStarted)
91                                         throw new InvalidOperationException (
92                                                         Locale.GetText ("Cannot modify this property " +
93                                                                                         "after the service has started."));
94
95                                 can_pause_and_continue = value;
96                         }
97                 }
98
99                 [DefaultValue (false)]
100                 public bool CanShutdown {
101                         get { return can_shutdown; }
102                         set {
103                                 if (hasStarted)
104                                         throw new InvalidOperationException (
105                                                         Locale.GetText ("Cannot modify this property " +
106                                                                                         "after the service has started."));
107
108                                 can_shutdown = value;
109                         }
110                 }
111
112                 [DefaultValue (true)]
113                 public bool CanStop {
114                         get { return can_stop; }
115                         set {
116                                 if (hasStarted)
117                                         throw new InvalidOperationException (
118                                                         Locale.GetText ("Cannot modify this property " +
119                                                                                         "after the service has started."));
120
121                                 can_stop = value;
122                         }
123                 }
124
125                 [Browsable (false)]
126                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
127                 public virtual EventLog EventLog {
128                         get {
129                                 if (event_log == null)
130                                         event_log = new EventLog ("Application", ".", service_name);
131                                 return event_log;
132                         }
133                 }
134
135                 [ServiceProcessDescription ("The name by which the service is identified to the system.")]
136                 [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
137                 public string ServiceName {
138                         get { return service_name; }
139                         set {
140                                 if (hasStarted)
141                                         throw new InvalidOperationException (
142                                                         Locale.GetText ("Cannot modify this property " +
143                                                                                         "after the service has started."));
144
145                                 service_name = value;
146                         }
147                 }
148
149                 protected override void Dispose (bool disposing)
150                 {
151                 }
152
153                 protected virtual void OnStart (string [] args)
154                 {
155                 }
156
157                 protected virtual void OnStop ()
158                 {
159                 }
160
161                 protected virtual void OnContinue ()
162                 {
163                 }
164
165                 protected virtual void OnCustomCommand (int command)
166                 {
167                 }
168
169                 protected virtual void OnPause ()
170                 {
171                 }
172
173                 protected virtual bool OnPowerEvent (PowerBroadcastStatus powerStatus)
174                 {
175                         return true;
176                 }
177
178                 protected virtual void OnShutdown ()
179                 {
180                 }
181
182                 public static void Run (ServiceBase service)
183                 {
184                         Run (new ServiceBase [] { service });
185                 }
186
187                 public static void Run (ServiceBase [] servicesToRun)
188                 {
189                         if (RunService != null)
190                                 RunService (servicesToRun);
191                 }
192         }
193 }