1 54 55 package org.objectweb.jonas.ant; 56 57 import java.io.File ; 58 import java.io.FileInputStream ; 59 import java.io.FileNotFoundException ; 60 import java.io.IOException ; 61 import java.io.InputStream ; 62 import java.net.URL ; 63 import java.util.Hashtable ; 64 65 import org.apache.tools.ant.Project; 66 import org.apache.tools.ant.Task; 67 import org.xml.sax.Attributes ; 68 import org.xml.sax.InputSource ; 69 import org.xml.sax.SAXException ; 70 71 79 public class DescriptorHandler extends org.xml.sax.helpers.DefaultHandler { 80 private static final int STATE_LOOKING_EJBJAR = 1; 81 private static final int STATE_IN_EJBJAR = 2; 82 private static final int STATE_IN_BEANS = 3; 83 private static final int STATE_IN_SESSION = 4; 84 private static final int STATE_IN_ENTITY = 5; 85 private static final int STATE_IN_MESSAGE = 6; 86 87 private static final int STATE_IN_SERVICE_REF = 7; 88 private static final int STATE_IN_PORT_COMPONENT_REF = 8; 89 private static final int STATE_IN_HANDLER = 9; 90 91 92 private Task owningTask; 93 94 private String publicId = null; 95 96 100 private static final String EJB_REF = "ejb-ref"; 101 private static final String EJB_LOCAL_REF = "ejb-local-ref"; 102 private static final String HOME_INTERFACE = "home"; 103 private static final String REMOTE_INTERFACE = "remote"; 104 private static final String LOCAL_HOME_INTERFACE = "local-home"; 105 private static final String LOCAL_INTERFACE = "local"; 106 private static final String ENDPOINT_INTERFACE = "service-endpoint"; 107 private static final String BEAN_CLASS = "ejb-class"; 108 private static final String PK_CLASS = "prim-key-class"; 109 private static final String EJB_NAME = "ejb-name"; 110 private static final String EJB_JAR = "ejb-jar"; 111 private static final String ENTERPRISE_BEANS = "enterprise-beans"; 112 private static final String ENTITY_BEAN = "entity"; 113 private static final String SESSION_BEAN = "session"; 114 private static final String MESSAGE_BEAN = "message-driven"; 115 116 119 private static final String SERVICE_REF = "service-ref"; 120 private static final String SERVICE_INTERFACE = "service-interface"; 121 private static final String WSDL_FILE = "wsdl-file"; 122 private static final String JAXRPC_MAPPING_FILE = "jaxrpc-mapping-file"; 123 private static final String PORT_COMPONENT_REF = "port-component-ref"; 124 private static final String SERVICE_ENDPOINT_INTERFACE = "service-endpoint-interface"; 125 private static final String HANDLER = "handler"; 126 private static final String HANDLER_CLASS = "handler-class"; 127 128 131 private int parseState = STATE_LOOKING_EJBJAR; 132 133 136 private int oldParseState; 137 138 143 protected String currentElement = null; 144 145 148 protected String currentText = null; 149 150 155 protected Hashtable ejbFiles = null; 156 157 160 protected String ejbName = null; 161 162 private Hashtable fileDTDs = new Hashtable (); 163 164 private Hashtable resourceDTDs = new Hashtable (); 165 166 private boolean inEJBRef = false; 167 168 private Hashtable urlDTDs = new Hashtable (); 169 170 174 private File srcDir; 175 176 public DescriptorHandler(Task task, File srcDir) { 177 this.owningTask = task; 178 this.srcDir = srcDir; 179 } 180 181 public void registerDTD(String publicId, String location) { 182 if (location == null) { 183 return; 184 } 185 186 File fileDTD = new File (location); 187 if (!fileDTD.exists()) { 188 fileDTD = owningTask.getProject().resolveFile(location); 190 } 191 192 if (fileDTD.exists()) { 193 if (publicId != null) { 194 fileDTDs.put(publicId, fileDTD); 195 owningTask.log("Mapped publicId " + publicId + " to file " 196 + fileDTD, Project.MSG_VERBOSE); 197 } 198 return; 199 } 200 201 if (getClass().getResource(location) != null) { 202 if (publicId != null) { 203 resourceDTDs.put(publicId, location); 204 owningTask.log("Mapped publicId " + publicId + " to resource " 205 + location, Project.MSG_VERBOSE); 206 } 207 } 208 209 try { 210 if (publicId != null) { 211 URL urldtd = new URL (location); 212 urlDTDs.put(publicId, urldtd); 213 } 214 } catch (java.net.MalformedURLException e) { 215 } 217 218 } 219 220 public InputSource resolveEntity(String publicId, String systemId) 221 throws SAXException { 222 this.publicId = publicId; 223 224 File dtdFile = (File ) fileDTDs.get(publicId); 225 if (dtdFile != null) { 226 try { 227 owningTask.log("Resolved " + publicId + " to local file " 228 + dtdFile, Project.MSG_VERBOSE); 229 return new InputSource (new FileInputStream (dtdFile)); 230 } catch (FileNotFoundException ex) { 231 } 233 } 234 235 String dtdResourceName = (String ) resourceDTDs.get(publicId); 236 if (dtdResourceName != null) { 237 InputStream is = this.getClass().getResourceAsStream(dtdResourceName); 238 if (is != null) { 239 owningTask.log("Resolved " + publicId + " to local resource " 240 + dtdResourceName, Project.MSG_VERBOSE); 241 return new InputSource (is); 242 } 243 } 244 245 URL dtdUrl = (URL ) urlDTDs.get(publicId); 246 if (dtdUrl != null) { 247 try { 248 InputStream is = dtdUrl.openStream(); 249 owningTask.log("Resolved " + publicId + " to url " 250 + dtdUrl, Project.MSG_VERBOSE); 251 return new InputSource (is); 252 } catch (IOException ioe) { 253 } 255 } 256 257 owningTask.log("Could not resolve ( publicId: " + publicId 258 + ", systemId: " + systemId + ") to a local entity", Project.MSG_INFO); 259 260 return null; 261 } 262 263 266 public Hashtable getFiles() { 267 return (ejbFiles == null) ? new Hashtable () : ejbFiles; 268 } 269 270 273 public String getPublicId() { 274 return publicId; 275 } 276 277 280 public String getEjbName() { 281 return ejbName; 282 } 283 284 288 public void startDocument() throws SAXException { 289 this.ejbFiles = new Hashtable (10, 1); 290 this.currentElement = null; 291 inEJBRef = false; 292 } 293 294 295 304 public void startElement(String uri, String localname, String qname, Attributes attrs) 305 throws SAXException { 306 307 String name = qname; 308 309 this.currentElement = name; 310 currentText = ""; 311 if (name.equals(EJB_REF) || name.equals(EJB_LOCAL_REF)) { 312 inEJBRef = true; 313 } else if (parseState == STATE_LOOKING_EJBJAR && name.equals(EJB_JAR)) { 314 parseState = STATE_IN_EJBJAR; 315 } else if (parseState == STATE_IN_EJBJAR && name.equals(ENTERPRISE_BEANS)) { 316 parseState = STATE_IN_BEANS; 317 } else if (parseState == STATE_IN_BEANS && name.equals(SESSION_BEAN)) { 318 parseState = STATE_IN_SESSION; 319 } else if (parseState == STATE_IN_BEANS && name.equals(ENTITY_BEAN)) { 320 parseState = STATE_IN_ENTITY; 321 } else if (parseState == STATE_IN_BEANS && name.equals(MESSAGE_BEAN)) { 322 parseState = STATE_IN_MESSAGE; 323 } else if ( ((parseState == STATE_IN_SESSION) 324 || (parseState == STATE_IN_ENTITY) 325 || (parseState == STATE_IN_MESSAGE)) 326 && (name.equals(SERVICE_REF))) { 327 oldParseState = parseState; 328 parseState = STATE_IN_SERVICE_REF; 329 } else if ((parseState == STATE_IN_SERVICE_REF) 330 && (name.equals(PORT_COMPONENT_REF))) { 331 parseState = STATE_IN_PORT_COMPONENT_REF; 332 } else if ((parseState == STATE_IN_SERVICE_REF) 333 && (name.equals(HANDLER))) { 334 parseState = STATE_IN_HANDLER; 335 } 336 } 337 338 339 348 public void endElement(String uri, String localname, String qname) throws SAXException { 349 String name = qname; 350 351 processElement(); 352 currentText = ""; 353 this.currentElement = ""; 354 if (name.equals(EJB_REF) || name.equals(EJB_LOCAL_REF)) { 355 inEJBRef = false; 356 } else if (parseState == STATE_IN_HANDLER && name.equals(HANDLER)) { 357 parseState = STATE_IN_SERVICE_REF; 358 } else if (parseState == STATE_IN_PORT_COMPONENT_REF && name.equals(PORT_COMPONENT_REF)) { 359 parseState = STATE_IN_SERVICE_REF; 360 } else if (parseState == STATE_IN_SERVICE_REF && name.equals(SERVICE_REF)) { 361 parseState = oldParseState; 362 } else if (parseState == STATE_IN_ENTITY && name.equals(ENTITY_BEAN)) { 363 parseState = STATE_IN_BEANS; 364 } else if (parseState == STATE_IN_SESSION && name.equals(SESSION_BEAN)) { 365 parseState = STATE_IN_BEANS; 366 } else if (parseState == STATE_IN_MESSAGE && name.equals(MESSAGE_BEAN)) { 367 parseState = STATE_IN_BEANS; 368 } else if (parseState == STATE_IN_BEANS && name.equals(ENTERPRISE_BEANS)) { 369 parseState = STATE_IN_EJBJAR; 370 } else if (parseState == STATE_IN_EJBJAR && name.equals(EJB_JAR)) { 371 parseState = STATE_LOOKING_EJBJAR; 372 } 373 } 374 375 390 public void characters(char[] ch, int start, int length) 391 throws SAXException { 392 393 currentText += new String (ch, start, length); 394 } 395 396 397 protected void processElement() { 398 if (inEJBRef 399 || (parseState != STATE_IN_ENTITY 400 && parseState != STATE_IN_SESSION 401 && parseState != STATE_IN_MESSAGE 402 && parseState != STATE_IN_SERVICE_REF 403 && parseState != STATE_IN_PORT_COMPONENT_REF 404 && parseState != STATE_IN_HANDLER)) { 405 return; 406 } 407 408 if (currentElement.equals(HOME_INTERFACE) 409 || currentElement.equals(REMOTE_INTERFACE) 410 || currentElement.equals(LOCAL_INTERFACE) 411 || currentElement.equals(ENDPOINT_INTERFACE) 412 || currentElement.equals(LOCAL_HOME_INTERFACE) 413 || currentElement.equals(BEAN_CLASS) 414 || currentElement.equals(PK_CLASS) 415 || currentElement.equals(SERVICE_INTERFACE) 416 || currentElement.equals(SERVICE_ENDPOINT_INTERFACE) 417 || currentElement.equals(HANDLER_CLASS)) { 418 419 File classFile = null; 421 String className = currentText.trim(); 422 423 if (!className.startsWith("java.") 426 && !className.startsWith("javax.")) { 427 className = className.replace('.', File.separatorChar); 430 className += ".class"; 431 classFile = new File (srcDir, className); 432 ejbFiles.put(className, classFile); 433 } 434 } 435 436 if (currentElement.equals(WSDL_FILE) 438 || currentElement.equals(JAXRPC_MAPPING_FILE)) { 439 String filename = currentText.trim(); 441 File file = new File (srcDir, filename); 442 ejbFiles.put(filename, file); 443 } 444 445 if (currentElement.equals(EJB_NAME)) { 447 if (ejbName == null) { 448 ejbName = currentText.trim(); 449 } 450 } 451 } 452 } 453 | Popular Tags |