DescriptorHandler.java

00001 /*
00002  * The Apache Software License, Version 1.1
00003  *
00004  * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
00005  * reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  *
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  *
00014  * 2. Redistributions in binary form must reproduce the above copyright
00015  *    notice, this list of conditions and the following disclaimer in
00016  *    the documentation and/or other materials provided with the
00017  *    distribution.
00018  *
00019  * 3. The end-user documentation included with the redistribution, if
00020  *    any, must include the following acknowlegement:
00021  *       "This product includes software developed by the
00022  *        Apache Software Foundation (http://www.apache.org/)."
00023  *    Alternately, this acknowlegement may appear in the software itself,
00024  *    if and wherever such third-party acknowlegements normally appear.
00025  *
00026  * 4. The names "Ant" and "Apache Software
00027  *    Foundation" must not be used to endorse or promote products derived
00028  *    from this software without prior written permission. For written
00029  *    permission, please contact apache@apache.org.
00030  *
00031  * 5. Products derived from this software may not be called "Apache"
00032  *    nor may "Apache" appear in their names without prior written
00033  *    permission of the Apache Group.
00034  *
00035  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
00036  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00037  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00038  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
00039  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00040  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00041  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
00042  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00043  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00044  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00045  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00046  * SUCH DAMAGE.
00047  * ====================================================================
00048  *
00049  * This software consists of voluntary contributions made by many
00050  * individuals on behalf of the Apache Software Foundation.  For more
00051  * information on the Apache Software Foundation, please see
00052  * <http://www.apache.org/>.
00053  */
00054 
00055 package org.objectweb.jonas.ant;
00056 
00057 import java.io.File;
00058 import java.io.FileInputStream;
00059 import java.io.FileNotFoundException;
00060 import java.io.IOException;
00061 import java.io.InputStream;
00062 import java.net.URL;
00063 import java.util.Hashtable;
00064 
00065 import org.apache.tools.ant.Project;
00066 import org.apache.tools.ant.Task;
00067 import org.xml.sax.Attributes;
00068 import org.xml.sax.InputSource;
00069 import org.xml.sax.SAXException;
00070 
00079 public class DescriptorHandler extends org.xml.sax.helpers.DefaultHandler {
00080     private static final int STATE_LOOKING_EJBJAR = 1;
00081     private static final int STATE_IN_EJBJAR = 2;
00082     private static final int STATE_IN_BEANS = 3;
00083     private static final int STATE_IN_SESSION = 4;
00084     private static final int STATE_IN_ENTITY = 5;
00085     private static final int STATE_IN_MESSAGE = 6;
00086 
00087     private static final int STATE_IN_SERVICE_REF = 7;
00088     private static final int STATE_IN_PORT_COMPONENT_REF = 8;
00089     private static final int STATE_IN_HANDLER = 9;
00090 
00091 
00092     private Task owningTask;
00093 
00094     private String publicId = null;
00095 
00100     private static final String EJB_REF               = "ejb-ref";
00101     private static final String EJB_LOCAL_REF         = "ejb-local-ref";
00102     private static final String HOME_INTERFACE        = "home";
00103     private static final String REMOTE_INTERFACE      = "remote";
00104     private static final String LOCAL_HOME_INTERFACE  = "local-home";
00105     private static final String LOCAL_INTERFACE       = "local";
00106     private static final String ENDPOINT_INTERFACE    = "service-endpoint";
00107     private static final String BEAN_CLASS            = "ejb-class";
00108     private static final String PK_CLASS              = "prim-key-class";
00109     private static final String EJB_NAME              = "ejb-name";
00110     private static final String EJB_JAR               = "ejb-jar";
00111     private static final String ENTERPRISE_BEANS      = "enterprise-beans";
00112     private static final String ENTITY_BEAN           = "entity";
00113     private static final String SESSION_BEAN          = "session";
00114     private static final String MESSAGE_BEAN          = "message-driven";
00115 
00119     private static final String SERVICE_REF                = "service-ref";
00120     private static final String SERVICE_INTERFACE          = "service-interface";
00121     private static final String WSDL_FILE                  = "wsdl-file";
00122     private static final String JAXRPC_MAPPING_FILE        = "jaxrpc-mapping-file";
00123     private static final String PORT_COMPONENT_REF         = "port-component-ref";
00124     private static final String SERVICE_ENDPOINT_INTERFACE = "service-endpoint-interface";
00125     private static final String HANDLER                    = "handler";
00126     private static final String HANDLER_CLASS              = "handler-class";
00127 
00131     private int parseState = STATE_LOOKING_EJBJAR;
00132 
00136     private int oldParseState;
00137 
00143     protected String currentElement = null;
00144 
00148     protected String currentText = null;
00149 
00155     protected Hashtable ejbFiles = null;
00156 
00160     protected String ejbName = null;
00161 
00162     private Hashtable fileDTDs = new Hashtable();
00163 
00164     private Hashtable resourceDTDs = new Hashtable();
00165 
00166     private boolean inEJBRef = false;
00167 
00168     private Hashtable urlDTDs = new Hashtable();
00169 
00174     private File srcDir;
00175 
00176     public DescriptorHandler(Task task, File srcDir) {
00177         this.owningTask = task;
00178         this.srcDir = srcDir;
00179     }
00180 
00181     public void registerDTD(String publicId, String location) {
00182         if (location == null) {
00183             return;
00184         }
00185 
00186         File fileDTD = new File(location);
00187         if (!fileDTD.exists()) {
00188             // resolve relative to project basedir
00189             fileDTD = owningTask.getProject().resolveFile(location);
00190         }
00191 
00192         if (fileDTD.exists()) {
00193             if (publicId != null) {
00194                 fileDTDs.put(publicId, fileDTD);
00195                 owningTask.log("Mapped publicId " + publicId + " to file "
00196                     + fileDTD, Project.MSG_VERBOSE);
00197             }
00198             return;
00199         }
00200 
00201         if (getClass().getResource(location) != null) {
00202             if (publicId != null) {
00203                 resourceDTDs.put(publicId, location);
00204                 owningTask.log("Mapped publicId " + publicId + " to resource "
00205                     + location, Project.MSG_VERBOSE);
00206             }
00207         }
00208 
00209         try {
00210             if (publicId != null) {
00211                 URL urldtd = new URL(location);
00212                 urlDTDs.put(publicId, urldtd);
00213             }
00214         } catch (java.net.MalformedURLException e) {
00215             //ignored
00216         }
00217 
00218     }
00219 
00220     public InputSource resolveEntity(String publicId, String systemId)
00221         throws SAXException {
00222         this.publicId = publicId;
00223 
00224         File dtdFile = (File) fileDTDs.get(publicId);
00225         if (dtdFile != null) {
00226             try {
00227                 owningTask.log("Resolved " + publicId + " to local file "
00228                     + dtdFile, Project.MSG_VERBOSE);
00229                 return new InputSource(new FileInputStream(dtdFile));
00230             } catch (FileNotFoundException ex) {
00231                 // ignore
00232             }
00233         }
00234 
00235         String dtdResourceName = (String) resourceDTDs.get(publicId);
00236         if (dtdResourceName != null) {
00237             InputStream is = this.getClass().getResourceAsStream(dtdResourceName);
00238             if (is != null) {
00239                 owningTask.log("Resolved " + publicId + " to local resource "
00240                     + dtdResourceName, Project.MSG_VERBOSE);
00241                 return new InputSource(is);
00242             }
00243         }
00244 
00245         URL dtdUrl = (URL) urlDTDs.get(publicId);
00246         if (dtdUrl != null) {
00247             try {
00248                 InputStream is = dtdUrl.openStream();
00249                 owningTask.log("Resolved " + publicId + " to url "
00250                     + dtdUrl, Project.MSG_VERBOSE);
00251                 return new InputSource(is);
00252             } catch (IOException ioe) {
00253                 //ignore
00254             }
00255         }
00256 
00257         owningTask.log("Could not resolve ( publicId: " + publicId
00258             + ", systemId: " + systemId + ") to a local entity", Project.MSG_INFO);
00259 
00260         return null;
00261     }
00262 
00266     public Hashtable getFiles() {
00267         return (ejbFiles == null) ? new Hashtable() : ejbFiles;
00268     }
00269 
00273     public String getPublicId() {
00274         return publicId;
00275     }
00276 
00280     public String getEjbName() {
00281         return ejbName;
00282     }
00283 
00288     public void startDocument() throws SAXException {
00289         this.ejbFiles = new Hashtable(10, 1);
00290         this.currentElement = null;
00291         inEJBRef = false;
00292     }
00293 
00294 
00304     public void startElement(String uri, String localname, String qname, Attributes attrs)
00305         throws SAXException {
00306 
00307         String name = qname;
00308 
00309         this.currentElement = name;
00310         currentText = "";
00311         if (name.equals(EJB_REF) || name.equals(EJB_LOCAL_REF)) {
00312             inEJBRef = true;
00313         } else if (parseState == STATE_LOOKING_EJBJAR && name.equals(EJB_JAR)) {
00314             parseState = STATE_IN_EJBJAR;
00315         } else if (parseState == STATE_IN_EJBJAR && name.equals(ENTERPRISE_BEANS)) {
00316             parseState = STATE_IN_BEANS;
00317         } else if (parseState == STATE_IN_BEANS && name.equals(SESSION_BEAN)) {
00318             parseState = STATE_IN_SESSION;
00319         } else if (parseState == STATE_IN_BEANS && name.equals(ENTITY_BEAN)) {
00320             parseState = STATE_IN_ENTITY;
00321         } else if (parseState == STATE_IN_BEANS && name.equals(MESSAGE_BEAN)) {
00322             parseState = STATE_IN_MESSAGE;
00323         } else if ( ((parseState == STATE_IN_SESSION)
00324                      || (parseState == STATE_IN_ENTITY)
00325                      || (parseState == STATE_IN_MESSAGE))
00326                     && (name.equals(SERVICE_REF))) {
00327             oldParseState = parseState;
00328             parseState = STATE_IN_SERVICE_REF;
00329         } else if ((parseState == STATE_IN_SERVICE_REF)
00330                    && (name.equals(PORT_COMPONENT_REF))) {
00331             parseState = STATE_IN_PORT_COMPONENT_REF;
00332         } else if ((parseState == STATE_IN_SERVICE_REF)
00333                    && (name.equals(HANDLER))) {
00334             parseState = STATE_IN_HANDLER;
00335         }
00336     }
00337 
00338 
00348     public void endElement(String uri, String localname, String qname) throws SAXException {
00349         String name = qname;
00350 
00351         processElement();
00352         currentText = "";
00353         this.currentElement = "";
00354         if (name.equals(EJB_REF) || name.equals(EJB_LOCAL_REF)) {
00355             inEJBRef = false;
00356         } else if (parseState == STATE_IN_HANDLER && name.equals(HANDLER)) {
00357             parseState = STATE_IN_SERVICE_REF;
00358         } else if (parseState == STATE_IN_PORT_COMPONENT_REF && name.equals(PORT_COMPONENT_REF)) {
00359             parseState = STATE_IN_SERVICE_REF;
00360         } else if (parseState == STATE_IN_SERVICE_REF && name.equals(SERVICE_REF)) {
00361             parseState = oldParseState;
00362         } else if (parseState == STATE_IN_ENTITY && name.equals(ENTITY_BEAN)) {
00363             parseState = STATE_IN_BEANS;
00364         } else if (parseState == STATE_IN_SESSION && name.equals(SESSION_BEAN)) {
00365             parseState = STATE_IN_BEANS;
00366         } else if (parseState == STATE_IN_MESSAGE && name.equals(MESSAGE_BEAN)) {
00367             parseState = STATE_IN_BEANS;
00368         } else if (parseState == STATE_IN_BEANS && name.equals(ENTERPRISE_BEANS)) {
00369             parseState = STATE_IN_EJBJAR;
00370         } else if (parseState == STATE_IN_EJBJAR && name.equals(EJB_JAR)) {
00371             parseState = STATE_LOOKING_EJBJAR;
00372         }
00373     }
00374 
00390     public void characters(char[] ch, int start, int length)
00391         throws SAXException {
00392 
00393         currentText += new String(ch, start, length);
00394     }
00395 
00396 
00397     protected void processElement() {
00398         if (inEJBRef
00399             || (parseState != STATE_IN_ENTITY
00400                 && parseState != STATE_IN_SESSION
00401                 && parseState != STATE_IN_MESSAGE
00402                 && parseState != STATE_IN_SERVICE_REF
00403                 && parseState != STATE_IN_PORT_COMPONENT_REF
00404                 && parseState != STATE_IN_HANDLER)) {
00405             return;
00406         }
00407 
00408         if (currentElement.equals(HOME_INTERFACE)
00409             || currentElement.equals(REMOTE_INTERFACE)
00410             || currentElement.equals(LOCAL_INTERFACE)
00411             || currentElement.equals(ENDPOINT_INTERFACE)
00412             || currentElement.equals(LOCAL_HOME_INTERFACE)
00413             || currentElement.equals(BEAN_CLASS)
00414             || currentElement.equals(PK_CLASS)
00415             || currentElement.equals(SERVICE_INTERFACE)
00416             || currentElement.equals(SERVICE_ENDPOINT_INTERFACE)
00417             || currentElement.equals(HANDLER_CLASS)) {
00418 
00419             // Get the filename into a String object
00420             File classFile = null;
00421             String className = currentText.trim();
00422 
00423             // If it's a primitive wrapper then we shouldn't try and put
00424             // it into the jar, so ignore it.
00425             if (!className.startsWith("java.")
00426                 && !className.startsWith("javax.")) {
00427                 // Translate periods into path separators, add .class to the
00428                 // name, create the File object and add it to the Hashtable.
00429                 className = className.replace('.', File.separatorChar);
00430                 className += ".class";
00431                 classFile = new File(srcDir, className);
00432                 ejbFiles.put(className, classFile);
00433             }
00434         }
00435 
00436         // service-ref support for simple file
00437         if (currentElement.equals(WSDL_FILE)
00438             || currentElement.equals(JAXRPC_MAPPING_FILE)) {
00439             //owningTask.log("***** element name : " + currentElement, Project.MSG_VERBOSE);
00440             String filename = currentText.trim();
00441             File file = new File(srcDir, filename);
00442             ejbFiles.put(filename, file);
00443         }
00444 
00445         // Get the value of the <ejb-name> tag.  Only the first occurrence.
00446         if (currentElement.equals(EJB_NAME)) {
00447             if (ejbName == null) {
00448                 ejbName = currentText.trim();
00449             }
00450         }
00451     }
00452 }

Generated on Tue Feb 15 15:05:13 2005 for JOnAS by  doxygen 1.3.9.1