00001 package com.quadcap.server;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.BufferedInputStream;
00042
import java.io.BufferedReader;
00043
import java.io.FileInputStream;
00044
import java.io.FileReader;
00045
import java.io.IOException;
00046
00047
import java.util.ArrayList;
00048
import java.util.Collections;
00049
import java.util.Comparator;
00050
import java.util.HashMap;
00051
import java.util.Properties;
00052
00053
import org.xml.sax.AttributeList;
00054
import org.xml.sax.DocumentHandler;
00055
import org.xml.sax.ErrorHandler;
00056
import org.xml.sax.InputSource;
00057
import org.xml.sax.Parser;
00058
import org.xml.sax.Locator;
00059
import org.xml.sax.SAXException;
00060
import org.xml.sax.SAXParseException;
00061
00062
import org.xml.sax.helpers.ParserFactory;
00063
00064
import com.quadcap.util.ConfigString;
00065
import com.quadcap.util.Debug;
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 public class ServerConfigParser implements DocumentHandler, ErrorHandler {
00115 ServiceContainer container;
00116 Parser
parser;
00117 Locator
locator;
00118 StringBuffer
data =
new StringBuffer();
00119 ArrayList
services =
new ArrayList();
00120 ServiceWrapper service = null;
00121 Properties
props = null;
00122 String
paramName = null;
00123 String
paramValue = null;
00124
00125 static final int INIT = 0;
00126 static final int SERVER = 1;
00127 static final int SERVICE = 2;
00128 static final int SERVICE_NAME = 3;
00129 static final int SERVICE_CLASS = 4;
00130 static final int SERVICE_CONFIG = 5;
00131 static final int LOAD_ON_STARTUP = 6;
00132 static final int INIT_PARAMETER = 7;
00133 static final int PARAM_NAME = 8;
00134 static final int PARAM_VALUE = 9;
00135
00136
00137 static HashMap
elemMap =
new HashMap();
00138
static {
00139
elemMap.put(
"server",
new Integer(SERVER));
00140
elemMap.put(
"service",
new Integer(SERVICE));
00141
elemMap.put(
"service-name",
new Integer(SERVICE_NAME));
00142
elemMap.put(
"service-class",
new Integer(SERVICE_CLASS));
00143
elemMap.put(
"service-config",
new Integer(SERVICE_CONFIG));
00144
elemMap.put(
"load-on-startup",
new Integer(LOAD_ON_STARTUP));
00145
elemMap.put(
"init-parameter",
new Integer(INIT_PARAMETER));
00146
elemMap.put(
"param-name",
new Integer(PARAM_NAME));
00147
elemMap.put(
"param-value",
new Integer(PARAM_VALUE));
00148 }
00149
00150 static final int mapElement(String name) {
00151 Integer i = (Integer)
elemMap.get(name);
00152
if (i == null)
return -1;
00153
return i.intValue();
00154 }
00155
00156
00157
00158
00159
00160
00161
00162
00163 public ServerConfigParser(
ServiceContainer c)
throws Exception {
00164
this.container = c;
00165
ConfigString ps =
ConfigString.find(
"xml.sax.parser",
00166
"com.quadcap.text.sax.Parser");
00167
00168
try {
00169 ClassLoader cl = ClassLoader.getSystemClassLoader();
00170
this.parser =
00171 (Parser)(Class.forName(ps.
toString(),
true, cl).newInstance());
00172 }
catch (Throwable t) {
00173
Debug.print(t);
00174 }
00175
parser.setDocumentHandler(
this);
00176
parser.setErrorHandler(
this);
00177 }
00178
00179
00180
00181
00182
00183
00184 public void parse(String fileName)
throws Exception {
00185 FileReader f =
new FileReader(fileName);
00186
try {
00187 BufferedReader r =
new BufferedReader(f);
00189
parser.parse(is);
00190 } finally {
00191 f.close();
00192 }
00193 }
00194
00195
00196
00197
00198
00199
00200
00201
00203 System.err.println(
"error");
00204 exception.printStackTrace(System.err);
00205 }
00206
00207
00208
00209
00210
00211
00212
00213
00215 System.err.println(
"fatal error");
00216 exception.printStackTrace(System.err);
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00227 System.err.println(
"warning");
00228 exception.printStackTrace(System.err);
00229 }
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 public void characters(
char[] ch,
int start,
int length)
00246 {
00247
data.append(ch, start, length);
00248 }
00249
00250
00251
00252
00253
00254 public void endDocument() {
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00269
switch (mapElement(name)) {
00270
case SERVER:
00271
addServices();
00272
break;
00273
case SERVICE:
00274
if (
props != null) {
00275
service.
setServiceProperties(
props);
00276
props = null;
00277 }
00278
services.add(
service);
00279
break;
00280
case SERVICE_NAME:
00281
service.
setServiceName(
consume());
00282
break;
00283
case SERVICE_CLASS:
00284
service.
setServiceClass(
consume());
00285
break;
00286
case LOAD_ON_STARTUP:
00287
service.
setLoadOnStartup(Integer.parseInt(
consume()));
00288
break;
00289
case SERVICE_CONFIG:
00290 {
00291 String cname =
consume();
00292
getProps().setProperty(
"service.config", cname);
00293
try {
00294
getProps().putAll(
parseProps(cname));
00295 }
catch (IOException ex) {
00296 }
00297 }
00298
break;
00299
case PARAM_NAME:
00300
paramName =
consume();
00301
break;
00302
case PARAM_VALUE:
00303
paramValue =
consume();
00304
break;
00305
case INIT_PARAMETER:
00306
if (
paramName != null &&
paramValue != null) {
00307
getProps().put(
paramName,
paramValue);
00308
paramName = null;
00309
paramValue = null;
00310 }
00311
break;
00312
default:
00314 }
00315 }
00316
00317 final private Properties
getProps() {
00318
if (
props == null)
props =
new Properties();
00319
return props;
00320 }
00321
00322 final public static Properties
parseProps(String fileName)
00323
throws IOException
00324 {
00325 FileInputStream f =
new FileInputStream(fileName);
00326 Properties p =
new Properties();
00327
try {
00328 BufferedInputStream b =
new BufferedInputStream(f);
00329 p.load(b);
00330 } finally {
00331 f.close();
00332 }
00333
return p;
00334 }
00335
00336
00337
00338
00339
00340
00341
00342
00343 public void ignorableWhitespace(
char[] ch,
int start,
int length) {
00344 }
00345
00346
00347
00348
00349
00350
00351
00352
00353 public void processingInstruction(String target, String data) {
00354 }
00355
00356
00357
00358
00359
00360
00361 public void setDocumentLocator(Locator locator) {
00362
this.locator = locator;
00363 }
00364
00365
00366
00367
00368
00369 public void startDocument() {
00370 }
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00388 {
00389
data.setLength(0);
00390
switch (mapElement(name)) {
00391
case SERVICE:
00392
service =
new ServiceWrapper();
00393
break;
00394
default:
00395
break;
00396
case -1:
00398 }
00399 }
00400
00401
00402
00403
00404
00405 final String
consume() {
00406 String s =
data.toString().trim();
00407
data.setLength(0);
00408
return s;
00409 }
00410
00411
00412
00413
00415 Exception ex = null;
00416
00417 Collections.sort(
services,
new Comparator() {
00418
public int compare(Object a, Object b) {
00419
ServiceWrapper wa = (
ServiceWrapper)a;
00420 ServiceWrapper wb = (ServiceWrapper)b;
00421
if (wa.getLoadOnStartup() < wb.getLoadOnStartup())
return -1;
00422
if (wa.getLoadOnStartup() > wb.getLoadOnStartup())
return 1;
00423
return 0;
00424 }
00425 });
00426
for (
int i = 0; i < services.size(); i++) {
00427 ServiceWrapper w = (ServiceWrapper)services.get(i);
00428
try {
00429 container.addService(w.getServiceName(),
00430 w.getServiceClass(),
00431 w.getServiceProperties());
00432 }
catch (Exception e) {
00433 ex = e;
00434 Debug.print(e);
00435 }
00436 }
00437
00438
if (ex != null)
throw new SAXException(ex);
00439 }
00440 }