1 package com.quadcap.server; 2 3 40 41 import java.io.BufferedInputStream ; 42 import java.io.BufferedReader ; 43 import java.io.FileInputStream ; 44 import java.io.FileReader ; 45 import java.io.IOException ; 46 47 import java.util.ArrayList ; 48 import java.util.Collections ; 49 import java.util.Comparator ; 50 import java.util.HashMap ; 51 import java.util.Properties ; 52 53 import org.xml.sax.AttributeList ; 54 import org.xml.sax.DocumentHandler ; 55 import org.xml.sax.ErrorHandler ; 56 import org.xml.sax.InputSource ; 57 import org.xml.sax.Parser ; 58 import org.xml.sax.Locator ; 59 import org.xml.sax.SAXException ; 60 import org.xml.sax.SAXParseException ; 61 62 import org.xml.sax.helpers.ParserFactory ; 63 64 import com.quadcap.util.ConfigString; 65 import com.quadcap.util.Debug; 66 67 107 108 114 public class ServerConfigParser implements DocumentHandler , ErrorHandler { 115 ServiceContainer container; 116 Parser parser; 117 Locator locator; 118 StringBuffer data = new StringBuffer (); 119 ArrayList services = new ArrayList (); 120 ServiceWrapper service = null; 121 Properties props = null; 122 String paramName = null; 123 String paramValue = null; 124 125 static final int INIT = 0; 126 static final int SERVER = 1; 127 static final int SERVICE = 2; 128 static final int SERVICE_NAME = 3; 129 static final int SERVICE_CLASS = 4; 130 static final int SERVICE_CONFIG = 5; 131 static final int LOAD_ON_STARTUP = 6; 132 static final int INIT_PARAMETER = 7; 133 static final int PARAM_NAME = 8; 134 static final int PARAM_VALUE = 9; 135 136 137 static HashMap elemMap = new HashMap (); 138 static { 139 elemMap.put("server", new Integer (SERVER)); 140 elemMap.put("service", new Integer (SERVICE)); 141 elemMap.put("service-name", new Integer (SERVICE_NAME)); 142 elemMap.put("service-class", new Integer (SERVICE_CLASS)); 143 elemMap.put("service-config", new Integer (SERVICE_CONFIG)); 144 elemMap.put("load-on-startup", new Integer (LOAD_ON_STARTUP)); 145 elemMap.put("init-parameter", new Integer (INIT_PARAMETER)); 146 elemMap.put("param-name", new Integer (PARAM_NAME)); 147 elemMap.put("param-value", new Integer (PARAM_VALUE)); 148 } 149 150 static final int mapElement(String name) { 151 Integer i = (Integer )elemMap.get(name); 152 if (i == null) return -1; 153 return i.intValue(); 154 } 155 156 163 public ServerConfigParser(ServiceContainer c) throws Exception { 164 this.container = c; 165 ConfigString ps = ConfigString.find("xml.sax.parser", 166 "com.quadcap.text.sax.Parser"); 167 168 try { 169 ClassLoader cl = ClassLoader.getSystemClassLoader(); 170 this.parser = 171 (Parser )(Class.forName(ps.toString(), true, cl).newInstance()); 172 } catch (Throwable t) { 173 Debug.print(t); 174 } 175 parser.setDocumentHandler(this); 176 parser.setErrorHandler(this); 177 } 178 179 184 public void parse(String fileName) throws Exception { 185 FileReader f = new FileReader (fileName); 186 try { 187 BufferedReader r = new BufferedReader (f); 188 InputSource is = new InputSource (r); 189 parser.parse(is); 190 } finally { 191 f.close(); 192 } 193 } 194 195 202 public void error(SAXParseException exception) { 203 System.err.println("error"); 204 exception.printStackTrace(System.err); 205 } 206 207 214 public void fatalError(SAXParseException exception) { 215 System.err.println("fatal error"); 216 exception.printStackTrace(System.err); 217 } 218 219 226 public void warning(SAXParseException exception) { 227 System.err.println("warning"); 228 exception.printStackTrace(System.err); 229 } 230 231 244 public void characters(char[] ch, int start, int length) 245 throws SAXException 246 { 247 data.append(ch, start, length); 248 } 249 250 254 public void endDocument() { 255 } 256 257 268 public void endElement(String name) throws SAXException { 269 switch (mapElement(name)) { 270 case SERVER: 271 addServices(); 272 break; 273 case SERVICE: 274 if (props != null) { 275 service.setServiceProperties(props); 276 props = null; 277 } 278 services.add(service); 279 break; 280 case SERVICE_NAME: 281 service.setServiceName(consume()); 282 break; 283 case SERVICE_CLASS: 284 service.setServiceClass(consume()); 285 break; 286 case LOAD_ON_STARTUP: 287 service.setLoadOnStartup(Integer.parseInt(consume())); 288 break; 289 case SERVICE_CONFIG: 290 { 291 String cname = consume(); 292 getProps().setProperty("service.config", cname); 293 try { 294 getProps().putAll(parseProps(cname)); 295 } catch (IOException ex) { 296 } 297 } 298 break; 299 case PARAM_NAME: 300 paramName = consume(); 301 break; 302 case PARAM_VALUE: 303 paramValue = consume(); 304 break; 305 case INIT_PARAMETER: 306 if (paramName != null && paramValue != null) { 307 getProps().put(paramName, paramValue); 308 paramName = null; 309 paramValue = null; 310 } 311 break; 312 default: 313 throw new SAXException ("endElement: bad element: " + name); 314 } 315 } 316 317 final private Properties getProps() { 318 if (props == null) props = new Properties (); 319 return props; 320 } 321 322 final public static Properties parseProps(String fileName) 323 throws IOException 324 { 325 FileInputStream f = new FileInputStream (fileName); 326 Properties p = new Properties (); 327 try { 328 BufferedInputStream b = new BufferedInputStream (f); 329 p.load(b); 330 } finally { 331 f.close(); 332 } 333 return p; 334 } 335 336 343 public void ignorableWhitespace(char[] ch, int start, int length) { 344 } 345 346 353 public void processingInstruction(String target, String data) { 354 } 355 356 361 public void setDocumentLocator(Locator locator) { 362 this.locator = locator; 363 } 364 365 369 public void startDocument() { 370 } 371 372 386 public void startElement(String name, AttributeList attrs) 387 throws SAXException 388 { 389 data.setLength(0); 390 switch (mapElement(name)) { 391 case SERVICE: 392 service = new ServiceWrapper(); 393 break; 394 default: 395 break; 396 case -1: 397 throw new SAXException ("Unrecognized element: " + name); 398 } 399 } 400 401 405 final String consume() { 406 String s = data.toString().trim(); 407 data.setLength(0); 408 return s; 409 } 410 411 414 final void addServices() throws SAXException { 415 Exception ex = null; 416 417 Collections.sort(services, new Comparator () { 418 public int compare(Object a, Object b) { 419 ServiceWrapper wa = (ServiceWrapper)a; 420 ServiceWrapper wb = (ServiceWrapper)b; 421 if (wa.getLoadOnStartup() < wb.getLoadOnStartup()) return -1; 422 if (wa.getLoadOnStartup() > wb.getLoadOnStartup()) return 1; 423 return 0; 424 } 425 }); 426 for (int i = 0; i < services.size(); i++) { 427 ServiceWrapper w = (ServiceWrapper)services.get(i); 428 try { 429 container.addService(w.getServiceName(), 430 w.getServiceClass(), 431 w.getServiceProperties()); 432 } catch (Exception e) { 433 ex = e; 434 Debug.print(e); 435 } 436 } 437 438 if (ex != null) throw new SAXException (ex); 439 } 440 } 441 | Popular Tags |