summaryrefslogtreecommitdiff
path: root/java/src/ServiceRecord.java
blob: 6c598aa732f552ccebcf70fbef89eaa38c65b9d0 (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
/*************************************************
 *
 * = PACKAGE
 *    JACE.ServiceConfigurator
 *
 * = FILENAME
 *    ServiceRecord.java
 *
 * This class structure is used in the ServiceRepository.  Each service
 * object, module, or stream in the repository should be wrapped by a
 * type of ServiceRecord.  The contained object does the real work.
 *
 * Modules and Streams will require records with more functionality.
 *
 * The caller should never be allowed to access the Object within the
 * record -- casting will result in a ClassCastException because of
 * the problem with loading classes with a ClassLoader.  To get
 * around this, all the method calls are made on the Object via
 * reflection.
 *
 *@see JACE.ServiceConfigurator.ServiceObject
 *@see JACE.ServiceConfigurator.ServiceRepository
 *
 *@author Everett Anderson
 *
 *************************************************/
package JACE.ServiceConfigurator;

import java.io.*;
import java.lang.*;
import java.lang.reflect.*;
import JACE.ServiceConfigurator.*;
import JACE.OS.*;

public class ServiceRecord
{
  /** 
   * Constructor
   *
   *@param  service   A java Object, the service
   *@param  name      Name of this service
   */
  ServiceRecord (Object service, String name) 
  {
    this.service_ = service;
    this.name_ = name;
    this.suspended_ = false;
  }

  /**
   * Forward the call to suspend
   * @return -1 error
   */
  public int suspend()
  {
    this.setSuspend(true);

    Object result = this.invokeSimpleReflected("suspend");
    
    if (result == null)
      return -1;
    else
      return ((Integer)result).intValue();
  }


  /**
   * Forward the call to resume
   * @return -1 error
   */
  public int resume()
  {
    this.setSuspend(false);

    Object result = this.invokeSimpleReflected("resume");

    if (result == null)
      return -1;
    else
      return ((Integer)result).intValue();
  }


  /** 
   *  Initialize the service, provide the given command line args to it.
   *  
   */
  public int init(String [] args)
  {
    Class types[] = new Class[1];
    if (args == null)
      args = new String[0];

    types[0] = args.getClass();

    // Find the method we want to call
    Method m;
    try {
      m = this.object().getClass().getMethod("init", types);
    } catch (NoSuchMethodException e) {
      ACE.ERROR("" + e);
      return -1;
    } catch (SecurityException e) {
      ACE.ERROR("" + e);
      return -1;
    }

    Class ptypes[] = m.getParameterTypes();
    //for (int x = 0; x < ptypes.length; x++) 
    //  System.err.println(ptypes[x].getName());
    Object params[] = new Object[1];

    params[0] = args;

    int result = -1;
    try {
      result = ((Integer)m.invoke(this.object(), params)).intValue();
    } catch (IllegalAccessException e) {
      ACE.ERROR("" + e);
      return -1;
    } catch (IllegalArgumentException e) {
      ACE.ERROR("" + e);
      return -1;
    } catch (InvocationTargetException e) {
      ACE.ERROR("init(): " + e.getTargetException());
      return -1;
    }

    return result;
  }

  /**
   * Prepare to close it
   */
  public int fini()
  {
    Object result = this.invokeSimpleReflected("fini");

    if (result == null)
      return -1;
    else
      return ((Integer)result).intValue();
  }

  /** 
   * Obtain information about this service
   */
  public String info()
  {
    Object result = this.invokeSimpleReflected("info");

    System.err.println("info fin");

    if (result == null)
      return null;
    else
      return new String((String)result);  
  }

  /** Invokes the method with the given name on the ServiceObject.
   * The invoked method must take no parameters for this to work.
   * Could be adjusted to throw a generic exception.
   */
  private Object invokeSimpleReflected(String name) 
  {
    Method m;

    // find the desired method
    try {
      m = this.object().getClass().getMethod(name, null);
    } catch (NoSuchMethodException e) {
      ACE.ERROR("" + e);
      return null;
    } catch (SecurityException e) {
      ACE.ERROR("" + e);
      return null;
    }

    // Invoke it
    Object result = null;
    
    try {
      result = m.invoke(this.object(), null);
    } catch (IllegalAccessException e) {
      ACE.ERROR("" + e);
    } catch (IllegalArgumentException e) {
      ACE.ERROR("" + e);
    } catch (InvocationTargetException e) {
      ACE.ERROR(name + "(): " + e.getTargetException());
    }

    return result;
  }

  /**
   * Accessor for the name
   */
  public String name()
  {
    return new String(this.name_);
  }

  /** Set the name of the service
   */
  public void name(String name)
  {
    this.name_ = name;
  }

  /** Is this service suspended?
   */
  public boolean suspended() 
  {
    return this.suspended_;
  }

  /** Set the suspended flag
   */
  void setSuspend (boolean suspended)
  {
    this.suspended_ = suspended;
  }

  /** Accessor for the contained Object.  This should
   *  never be available to the end user since they
   *  might try casting the result.
   */
  Object object()
  {
    return this.service_;
  }

  /**
   * Set the contained object
   */
  void object(Object service)
  {
    this.service_ = service;
  }

  Object service_;
  String name_;
  boolean suspended_;
};


/*
class ModuleRecord extends ServiceRecord
{
};

class StreamRecord extends ServiceRecord
{
};
*/