summaryrefslogtreecommitdiff
path: root/java/JACE/ServiceConfigurator/ServiceRepository.java
blob: 33f9a0880775080a0bb17f9a0ae6016e02123120 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*************************************************
 *
 * = PACKAGE
 *    JACE.ServiceConfigurator
 *
 * = FILENAME
 *    ServiceRepository.java
 *
 * The service repository stores the network services, allowing them to be 
 * removed, suspended, resumed, etc.  
 *
 *@see JACE.ServiceConfigurator.ServiceConfig;
 *@see JACE.ServiceConfigurator.Service;
 *
 *@author Everett Anderson
 *
 *************************************************/
package JACE.ServiceConfigurator;

import java.io.*;
import java.util.*;
 
/**
 * Stores Services, providing operations such as remove, suspend, resume, etc.
 */
public class ServiceRepository
{
  /**
   * Constructor
   */
  public ServiceRepository ()
  {
    this.serviceMap_ = new Hashtable ();
    this.serviceNames_ = new Vector ();
  }

  /**
   * Constructor
   *
   *@param initialSize    Initial vector size for the repository
   */
  public ServiceRepository (int initialSize)
  {
    this.serviceMap_ = new Hashtable (initialSize);
    this.serviceNames_ = new Vector (initialSize);
  }

  /**
   *  Shut down all the services, closing them in reverse order of insertion.
   *  This calls fini on each service.
   *@return -1 on failure, 0 on sucess
   */
  public int close() 
  { 
    int result = 0;

    for (int i = this.size() - 1; i >= 0; i--) {

      String name = (String)this.serviceNames_.elementAt (i);

      Service s = (Service)this.serviceMap_.get (name);

      result = (s.fini () == -1 ? -1 : result);
    }

    this.serviceMap_.clear ();
    this.serviceNames_.removeAllElements ();

    return result;
  }

  /**
   * Insert a Service into the repository.
   * (If already in, calls fini() and replaces)
   *
   *@param service      Service to add
   */
  public void insert (Service service)
  {
    String name = service.name ();
    Service alreadyIn = this.find (name);

    if (alreadyIn != null) {

      alreadyIn.fini ();
      this.remove (alreadyIn);

    } else {

      this.serviceMap_.put (name, service);
      this.serviceNames_.addElement (name);

    }
  }

  /**
   * Returns an enumeration of all the Service objects.
   *
   */
  public Enumeration services ()
  {
    return this.serviceMap_.elements ();
  }

  /**
   * Returns an enumeration of all the Service names
   */
  public Enumeration serviceNames ()
  {
    return this.serviceMap_.keys ();
  }

  /**
   * Convenience method that returns null when the service isn't
   * found.
   */
  public Service find (String name) 
  {
    Object serviceObj = this.serviceMap_.get (name);

    if (serviceObj == null)
      return null;

    return (Service)serviceObj;
  }

  /**
   * Finds the Service associated with a given
   * name.
   *
   *@param name    Name of the service to find
   *@exception NoSuchElementException if the given service is not found
   */
  protected Service findService (String name) throws NoSuchElementException
  {
    Object serviceObj = this.serviceMap_.get (name);

    if (serviceObj == null)
      throw new NoSuchElementException ("Service " + name + " not found.");

    return (Service)serviceObj;
  }

  /**
   * Removes the given Service and calls its fini () method.
   *@param service Service to remove
   *@return -1 on failure, 0 on success
   */
  protected int remove (Service service)
  {
    String name = service.name ();

    this.serviceMap_.remove (name);

    int index = this.serviceNames_.indexOf (name);

    this.serviceNames_.removeElementAt (index);

    return service.fini ();
  }

  /**
   * Shut down the specified Service.
   *
   *@param name name of the Service to shut down
   *@return -1 on failure, 0 on success
   */
  public int fini (String name)
  {
    Service service = this.findService (name);

    return service.fini ();
  }

  /**
   * Remove the specified Service, calling its fini () method.
   *
   *@param name name of the Service to remove
   *@return -1 on failure, 0 on success
   */
  public int remove (String name)
  {
    Service service = this.findService (name);

    return this.remove (service);
  }

  /**
   * Resume a suspended service
   *@param name    Name of the service to resume
   *@return -1 on failure, 0 on success
   */
  public int resume (String name)
  {
    Service service = this.findService (name);

    return service.resume();
  }

  /**
   * Suspend a service
   *@param name    Name of the service to suspend
   *@return -1 on failure, 0 on success
   */
  public int suspend (String name)
  {
    Service service = this.findService (name);
    
    if (service.suspended ())
      return 0;

    return service.suspend ();
  }

  /**
   * Returns status information about the specified Service.
   *
   *@param name name of the Service to query
   *@return String of information about the Service's status
   */
  public String info (String name)
  {
    Service service = this.findService (name);

    return service.info ();
  }

  /** 
   * Check to see if the specified Service is suspended or not
   */
  public boolean suspended (String name)
  {
    Service service = this.findService (name);

    return service.suspended ();
  }

  /**
   * Initialize the specified Service with the given command
   * line arguments.
   */
  public int init (String name, String [] args)
  {
    Service service = this.findService (name);

    return service.init (args);
  }

  /**
   * Returns the number of items in the repository
   */
  public int size ()
  {
    return this.serviceNames_.size();
  }

  /**
   * Stores the Service names in order of insertion
   */
  Vector serviceNames_;

  /**
   * A mapping between Service names and Service objects
   */
  Hashtable serviceMap_;
};