merge -r 61110:61111
[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 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34
35 using System;
36 using System.Globalization;
37 using System.Diagnostics;
38
39 namespace System.ServiceProcess
40 {
41         public class ServiceBase : System.ComponentModel.Component
42         {
43                 internal delegate void RunServiceCallback (ServiceBase [] services);
44                 
45                 // This member is used for interoperation with mono-service
46                 internal static RunServiceCallback RunService = null;
47                 
48                 public ServiceBase() { }
49
50                 public const int MaxNameLength = 80;
51
52                 bool hasStarted = false;
53                 
54                 bool auto_log = true;
55                 bool can_handle_power_event = false;
56                 bool can_pause_and_continue = false;
57                 bool can_shutdown = false;
58                 bool can_stop = true;
59                 EventLog event_log = null;
60                 string service_name;
61
62                 public bool AutoLog {
63
64                         get { return auto_log; }
65
66                         set { auto_log = value; }
67                 }
68
69                 public bool CanHandlePowerEvent {
70
71                         get { return can_handle_power_event; }
72
73                         set {
74                                 if (hasStarted)
75                                         throw new InvalidOperationException (
76                                                 Locale.GetText ("Cannot modify this property " +
77                                                                 "after the service has started."));
78
79                                 can_handle_power_event = value;
80                         }
81                 }
82
83                 public bool CanPauseAndContinue {
84
85                         get { return can_pause_and_continue; }
86
87                         set {
88                                 if (hasStarted)
89                                         throw new InvalidOperationException (
90                                                 Locale.GetText ("Cannot modify this property " +
91                                                                 "after the service has started."));
92
93                                 can_pause_and_continue = value;
94                         }
95                 }
96
97                 public bool CanShutdown {
98
99                         get { return can_shutdown; }
100
101                         set {
102                                 if (hasStarted)
103                                         throw new InvalidOperationException (
104                                                 Locale.GetText ("Cannot modify this property " +
105                                                                 "after the service has started."));
106
107                                 can_shutdown = value;
108                         }
109                 }
110
111                 public bool CanStop {
112
113                         get { return can_stop; }
114
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                 public virtual EventLog EventLog {
126                         get { 
127                                                         if (event_log == null)
128                                                                 event_log = new EventLog ("Application", ".", service_name);
129                                                         return event_log; 
130                                                 }
131                 }
132
133                 public string ServiceName {
134
135                         get { return service_name; }
136
137                         set {
138                                 if (hasStarted)
139                                         throw new InvalidOperationException (
140                                                 Locale.GetText ("Cannot modify this property " +
141                                                                 "after the service has started."));
142
143                                 service_name = value;
144                         }
145                 }
146                                 
147                 protected override void Dispose (bool disposing) { }
148
149                 protected virtual void OnStart (string [] args) { }
150
151                 protected virtual void OnStop () { }
152                 
153                 protected virtual void OnContinue () { }
154
155                 protected virtual void OnCustomCommand () { }
156
157                 protected virtual void OnPause () { }
158
159                 protected virtual void OnPowerEvent () { }
160
161                 protected virtual void OnShutdown () { }
162
163         public static void Run (ServiceBase service) 
164                 {
165                         Run (new ServiceBase [] {service});
166                 }
167
168                 public static void Run (ServiceBase [] servicesToRun) 
169                 {
170                         if (RunService != null)
171                                 RunService (servicesToRun);
172                 }
173
174         }
175 }