summaryrefslogtreecommitdiff
path: root/java/src/ServiceRepository.java
blob: 8845fb52d0ab245489aa47c62e4e905a8d2ef742 (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
/*************************************************
 *
 * = PACKAGE
 *    JACE.ServiceConfigurator
 *
 * = FILENAME
 *    ServiceRepository.java
 *
 * The service repository stores the network services, allowing them to be removed, suspended,
 * resumed, etc.  To reload a service, the caller must remove it from the repository and then
 * call prepareForReload().
 *
 *@see JACE.ServiceConfigurator.ServiceRecord;
 *@see JACE.ServiceConfigurator.ServiceConfig;
 *
 *@author Everett Anderson
 *
 *************************************************/
package JACE.ServiceConfigurator;

import java.io.*;
import java.util.*;
import JACE.ServiceConfigurator.*;

public class ServiceRepository
{
  /**
   * Constructor
   */
  public ServiceRepository ()
  {
    this.serviceVector_ = new Vector();
  }

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

  /**
   *  Shut down all the services, closing them in reverse order of insertion
   *  
   *  Maybe should be called by finalize?
   */
  public int close() 
  { 
    for (int i = this.size() - 1; i >= 0; i--) {
      ServiceRecord  rec = (ServiceRecord)this.serviceVector_.elementAt(i);

      rec.fini();

      this.serviceVector_.removeElementAt(i);
    }

    return 0;
  }

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

    // Replace the service
    if (alreadyIn != null) {
      alreadyIn.fini();
      this.serviceVector_.removeElement(alreadyIn);
    }

    this.serviceVector_.addElement(srvRec);
  }

  /**
   * Finds the ServiceRecord associated with a given
   * service name.  Note -- the user should not try to
   * get a ServiceObject out of the ServiceRecord.
   * Same as saying ignoreSuspended is false on the
   * next method.
   *
   *@param name    Name of the service to find
   */
  public ServiceRecord find (String name)
  {
    return this.find(name, false);
  }

  /** Return the service record for the given service.  The caller
    * should NOT try to access a ServiceObject (or Module or Stream)
    * by taking it out of the ServiceRecord -- just make the calls
    * through the record!
    *
    *@param   name             Name of the service to find
    *@param   ignoreSuspended  Allow finding suspended services?
    */
  public ServiceRecord find (String name, boolean ignoreSuspended)
  {
    ServiceRecord rec;

    for (int i = 0; i < this.size(); i++) {
      rec = (ServiceRecord)this.serviceVector_.elementAt(i);

      if ((rec.name().equals(name)) && ((!ignoreSuspended) || (!rec.suspended())))
	return rec;
    }

    return null;
  }

  /** Take the given service out of the repository.  This also sets the
   * reference in the repository to null to ensure there are no
   * hidden references to the old ServiceObject.  To reload, the user must
   * still run prepareToReload on ServiceConfig if they don't want any
   * problems.
   */
  public int remove (String name)
  {
    ServiceRecord rec = this.find(name, false);

    if (rec == null)
      return -1;

    int index = this.serviceVector_.indexOf(rec);

    // Shut down the service
    rec.fini();

    // Make sure there are no hidden references left
    this.serviceVector_.setElementAt(null, index);

    this.serviceVector_.removeElementAt(index);

    return 0;
  }

  /**
   * Resume a suspended service
   *@param name    Name of the service to resume
   */
  public int resume (String name)
  {
    ServiceRecord rec = this.find(name, false);

    if (rec == null)
      return -1;

    return rec.resume();
  }

  /**
   * Suspend a service
   *@param name    Name of the service to suspend
   */
  public int suspend (String name)
  {
    ServiceRecord rec = this.find(name, false);

    if (rec == null)
      return -1;

    return rec.suspend();
  }

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

  // Vector representation
  Vector serviceVector_;
};