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
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
343
  
344
  
345
  
346
  
347
  
348
  
349
  
350
  
351
  
352
  
353
  
354
  
355
  
356
  
357
  
358
  
359
  
360
  
361
  
362
  
363
  
364
  
365
  
366
  
367
  
/* 
 * $Id$ 
 * 
 */ 
 
package com.roxen.roxen; 
 
import java.util.Vector; 
 
/** 
 * The base class for Roxen modules.  All modules must inherit this 
 * class, directly or indirectly. 
 * <P> 
 * Each module should also implement one or more of the specific module 
 * type interfaces. 
 * 
 * @see LocationModule 
 * @see ParserModule 
 * @see FileExtensionModule 
 * @see LastResortModule 
 * @see ProviderModule 
 * @see ExperimentalModule 
 * @see SecurityModule 
 * @see UniqueModule 
 * 
 * @version     $Version$ 
 * @author      marcus 
 */ 
 
public abstract class Module { 
 
  /* Module variable types */ 
 
  /** Constant for string type module variables */ 
  public static final int TYPE_STRING = 1; 
  /** Constant for file name type module variables */ 
  public static final int TYPE_FILE = 2; 
  /** Constant for integer type module variables */ 
  public static final int TYPE_INT = 3; 
  /** Constant for directory name type module variables */ 
  public static final int TYPE_DIR = 4; 
 
  /** Constant for string list type module variables */ 
  public static final int TYPE_STRING_LIST = 5; 
  /** The same as TYPE_STRING_LIST */ 
  public static final int TYPE_MULTIPLE_STRING = 5; 
 
  /** Constant for integer list type module variables */ 
  public static final int TYPE_INT_LIST = 6; 
  /** The same as TYPE_INT_LIST */ 
  public static final int TYPE_MULTIPLE_INT = 6; 
 
  /** Constant for boolean type module variables */ 
  public static final int TYPE_FLAG = 7; 
  /** The same as TYPE_FLAG */ 
  public static final int TYPE_TOGGLE = 7; 
 
  // public static final int TYPE_ERROR = 8;  /* not used anymore */ 
 
  /** Constant for directory name list type module variables */ 
  public static final int TYPE_DIR_LIST = 9; 
 
  /** Constant for file name list type module variables */ 
  public static final int TYPE_FILE_LIST = 10; 
 
  /** Constant for URL path type module variables */ 
  public static final int TYPE_LOCATION = 11; 
 
  // public static final int TYPE_COLOR = 12;  /* not implemented yet */ 
 
  /** Constant for free format text type module variables */ 
  public static final int TYPE_TEXT_FIELD = 13; 
  /** The same as TYPE_TEXT_FIELD */ 
  public static final int TYPE_TEXT = 13; 
 
  /** Constant for string type module variables */ 
  public static final int TYPE_PASSWORD = 14; 
 
  /** Constant for floating point type module variables */ 
  public static final int TYPE_FLOAT = 15; 
 
  // public static final int TYPE_PORTS = 16;  /* not used anymore */ 
 
  /** Constant for module type module variables */ 
  public static final int TYPE_MODULE = 17; 
  // public static final int TYPE_MODULE_LIST = 18; /* somewhat buggy.. */ 
  // public static final int TYPE_MULTIPLE_MODULE = 18; /* somewhat buggy.. */ 
 
  /** Constant for font name type module variables */ 
  public static final int TYPE_FONT = 19; 
 
  // public static final int TYPE_CUSTOM = 20;  /* not used anymore */ 
  // public static final int TYPE_NODE = 21;  /* not used anymore */ 
 
  /** 
   * Constant for expert mode variable flag. 
   * Set this flag to make the variable show only in expert mode. 
   */ 
  public static final int VAR_EXPERT = 256; 
  /** 
   * Constant for advanced option variable flag. 
   * Set this flag to make the variable show only when advanced options 
   * are enabled. 
   */ 
  public static final int VAR_MORE = 512; 
  /** 
   * Constant for developer option variable flag. 
   * Set this flag to make the variable show only when developer options 
   * are enabled. 
   */ 
  public static final int VAR_DEVELOPER = 1024; 
  /** 
   * Constant for initial configuration variable flag. 
   * Set this flag for variables that need to be configured when 
   * the module is created. 
   */ 
  public static final int VAR_INITIAL = 2048; 
 
  static final int MODULE_EXTENSION       =  (1 << 0); 
  static final int MODULE_LOCATION        =  (1 << 1); 
  static final int MODULE_URL                =  (1 << 2); 
  static final int MODULE_FILE_EXTENSION  =  (1 << 3); 
  static final int MODULE_PARSER          =  (1 << 4); 
  static final int MODULE_LAST            =  (1 << 5); 
  static final int MODULE_FIRST           =  (1 << 6); 
   
  static final int MODULE_AUTH            =  (1 << 7); 
  static final int MODULE_MAIN_PARSER     =  (1 << 8); 
  static final int MODULE_TYPES           =  (1 << 9); 
  static final int MODULE_DIRECTORIES     =  (1 << 10); 
   
  static final int MODULE_PROXY           =  (1 << 11); 
  static final int MODULE_LOGGER          =  (1 << 12); 
  static final int MODULE_FILTER          =  (1 << 13); 
 
  // A module which can be called from other modules, protocols, scripts etc. 
  static final int MODULE_PROVIDER   =  (1 << 15); 
  // The module implements a protocol. 
  static final int MODULE_PROTOCOL        =  (1 << 16); 
 
  // An administration interface module 
  static final int MODULE_CONFIG          =  (1 << 17); 
 
  // Flags. 
  static final int MODULE_SECURITY        =  (1 << 29); 
  static final int MODULE_EXPERIMENTAL    =  (1 << 30); 
 
 
  private RoxenConfiguration configuration; 
 
  private Vector defvars = null; 
 
  /** 
   * Returns the name of the module 
   * 
   * @return  the module's name 
   */ 
  public abstract String queryName(); 
 
  /** 
   * Returns the documentation for the module 
   * 
   * @return  an HTML string containing brief online documentation 
   */ 
  public abstract String info(); 
 
  final int queryType() 
  { 
    return (this instanceof LocationModule? MODULE_LOCATION : 0) | 
      (this instanceof ParserModule? MODULE_PARSER : 0) | 
      (this instanceof FileExtensionModule? MODULE_FILE_EXTENSION : 0) | 
      (this instanceof SecurityModule? MODULE_SECURITY : 0) | 
      (this instanceof ExperimentalModule? MODULE_EXPERIMENTAL : 0) | 
      (this instanceof ProviderModule? MODULE_PROVIDER : 0) | 
      (this instanceof LastResortModule? MODULE_LAST : 0); 
  } 
 
  final boolean queryUnique() 
  { 
    return this instanceof UniqueModule; 
  } 
 
  /** 
   * Returns the configuration object of the virtual server in which 
   * this module is enabled 
   * 
   * @return  the configuration 
   */ 
  public RoxenConfiguration myConfiguration() 
  { 
    return configuration; 
  } 
 
  /** 
   * Returns the URL path of the internal mount point that has been 
   * created for this module. 
   * 
   * @return  the URL path 
   */ 
  protected String queryInternalLocation() 
  { 
    return configuration.queryInternalLocation(this); 
  } 
 
  /** 
   * Request an internal resource from this module. 
   * 
   * @param  f   the path of the resource relative to the location of 
   *             this module 
   * @param  id  the request object 
   * @return     a response, or <code>null</code> if no such 
   *             file exists. 
   */ 
  protected RoxenResponse findInternal(String f, RoxenRequest id) 
  { 
    return null; 
  } 
 
  /** 
   * Produce information about the current status of the module 
   * 
   * @return  a status message, or <code>null</code> if no 
   *          information is available 
   */ 
  public String status() 
  { 
    return null; 
  } 
 
  /** 
   * Prepare the module for servicing requests 
   * 
   */ 
  protected void start() 
  {     
  } 
 
  /** 
   * Inform the module that it is about to be taken out of service 
   * 
   */ 
  protected void stop() 
  { 
  } 
 
  private void addDefvar(Defvar dv) 
  { 
    if(defvars == null) 
      defvars = new Vector(); 
    defvars.add(dv); 
  } 
 
  Defvar[] getDefvars() 
  { 
    if(defvars == null) 
      return new Defvar[0]; 
    Defvar[] dvs = new Defvar[defvars.size()]; 
    dvs = (Defvar[])defvars.toArray(dvs); 
    defvars = null; 
    return dvs; 
  } 
 
  /** 
   * Create a module varible. 
   * This method must be called from the modules constructor. 
   * 
   * @param  var   a name by which this variables is identified internally 
   * @param  value the default value for this variable 
   * @param  name  a human-readable name for this variable 
   * @param  type  a <code>TYPE_</code>-constant selecting the type of 
   *               this variable, optionally ORed with any <code>VAR_</code> 
   *               flags 
   * @param  doc   an HTML string containing brief documentation for the 
   *               variable 
   */ 
  protected void defvar(String var, Object value, String name, int type, 
                        String doc) 
  { 
    addDefvar(new Defvar(var, value, name, type, doc)); 
  } 
 
  /** 
   * Create an undocumented module varible. 
   * This method must be called from the modules constructor. 
   * 
   * @param  var   a name by which this variables is identified internally 
   * @param  value the default value for this variable 
   * @param  name  a human-readable name for this variable 
   * @param  type  a <code>TYPE_</code>-constant selecting the type of 
   *               this variable, optionally ORed with any <code>VAR_</code> 
   *               flags 
   */ 
  protected void defvar(String var, Object value, String name, int type) 
  { 
    defvar(var, value, name, type, null); 
  } 
 
  /** 
   * Get the current value of a module variable 
   * 
   * @param  name  the internal name of the variable 
   * @return       the value of the variable 
   */ 
  public native Object query(String name); 
 
  /** 
   * Get the current value of a <code>TYPE_INT</code> or 
   * <code>TYPE_FLAG</code> module variable 
   * 
   * @param  name  the internal name of the variable 
   * @return       the value of the variable 
   */ 
  public int queryInt(String name) 
  { 
    Object n = query(name); 
    return (n==null? 0 : ((Integer)n).intValue()); 
  } 
 
  /** 
   * Get the current value of a module variable of one of 
   * the string types. 
   * 
   * @param  name  the internal name of the variable 
   * @return       the value of the variable 
   */ 
  public String queryString(String name) 
  { 
    return (String)query(name); 
  } 
 
  /** 
   * Set the contents of a module variable to a new value 
   * 
   * @param  name  the internal name of the variable 
   * @param  value the new value 
   */ 
  protected native void set(String name, Object value); 
 
  /** 
   * Set the contents of a <code>TYPE_INT</code> or <code>TYPE_FLAG</code> 
   * module variable to a new value 
   * 
   * @param  name  the internal name of the variable 
   * @param  value the new value 
   */ 
  protected void set(String name, int value) 
  { 
    set(name, new Integer(value)); 
  } 
 
  /** 
   * Check whether a new value is suitable for a module variable 
   * 
   * @param  name  the internal name of the variable 
   * @param  value the new value 
   * @return       <code>null</code> if the new value is OK, an error 
   *               message otherwise. 
   */ 
  String checkVariable(String name, Object value) 
  { 
    return null; 
  } 
 
}