FileDocCategorySizeDatePackage
DeploymentDescriptorFile.javaAPI DocGlassfish v2 API20050Mon Jul 02 09:41:00 BST 2007com.sun.enterprise.deployment.io

DeploymentDescriptorFile

public abstract class DeploymentDescriptorFile extends Object
This abstract class defines common behaviour for classes responsibles for loading/saving XML deployment descriptors
author
Jerome Dochez

Fields Summary
public static final String
FULL_VALIDATION
public static final String
PARSING_VALIDATION
private boolean
xmlValidation
private String
validationLevel
private String
errorReportingString
private static com.sun.enterprise.util.LocalStringManagerImpl
localStrings
Constructors Summary
public DeploymentDescriptorFile()
Creates a new instance of DeploymentDescriptorFile

        
    
           
      
    
Methods Summary
protected java.lang.StringgetDefaultSchemaSource()

return
the default schema source for this deployment descriptors

	RootXMLNode node = getRootXMLNode(null);
	if (node!=null) {
	    List<String> systemIDs = node.getSystemIDs();
        if (systemIDs != null) {
            String path = null;
            for (int i = 0; i < systemIDs.size(); i++) {
                if (path == null) {
                    path = systemIDs.get(i) + " ";
                } else {
                    path = path + systemIDs.get(i) + " ";
                }
            }
            return path.trim();
        }
	}
	return null;
    
public abstract java.lang.StringgetDeploymentDescriptorPath()

return
the location of the DeploymentDescriptor file for a particular type of J2EE Archive

public org.w3c.dom.DocumentgetDocument(com.sun.enterprise.deployment.Descriptor descriptor)

return
a Document for the passed descriptor
param
descriptor

        return J2EEDocumentBuilder.getDocument(descriptor, getRootXMLNode(descriptor));
    
public javax.xml.parsers.DocumentBuildergetDocumentBuilder(boolean validating)

return
a DOM parser to read XML File into a DOM tree
param
true if validation should happen

        try {
            // always use system default to parse DD
            System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            System.clearProperty("javax.xml.parsers.DocumentBuilderFactory"); 

            // set the namespace awareness
            dbf.setNamespaceAware(true);
            
	    // turn validation on for deployment descriptor XML files
            dbf.setValidating(validating);            

            // Validation part 2a: set the schema language if necessary
            try     {
                // put the default schema for this deployment file type
                String path = getDefaultSchemaSource();
                if (path!=null) {
                    dbf.setAttribute("http://apache.org/xml/properties/schema/external-schemaLocation",path);
                }
                return dbf.newDocumentBuilder();
            } catch (ParserConfigurationException x) {
                // This can happen if the parser does not support JAXP 1.2
                DOLUtils.getDefaultLogger().log(Level.SEVERE,
                    "Error: JAXP DOMParser property not recognized: "
                    + SaxParserHandler.JAXP_SCHEMA_LANGUAGE);
                DOLUtils.getDefaultLogger().log(Level.SEVERE,
                    "Check to see if parser conforms to JAXP 1.2 spec.");

            }            
        } catch (Exception e) {
            e.printStackTrace();
            DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.saxParserError",
                                new Object[]{e.getMessage()});
        }
        return null;
    
public abstract com.sun.enterprise.deployment.node.RootXMLNodegetRootXMLNode(com.sun.enterprise.deployment.Descriptor descriptor)

return
a RootXMLNode responsible for handling the deployment descriptors associated with this J2EE module
param
the descriptor for which we need the node

public javax.xml.parsers.SAXParsergetSAXParser()

return
a non validating SAX Parser to read an XML file (containing Deployment Descriptors) into DOL descriptors

        return getSAXParser(false);
    
public javax.xml.parsers.SAXParsergetSAXParser(boolean validating)

return
a SAX Parser to read an XML file (containing Deployment Descriptors) into DOL descriptors
param
validating true if the parser should excercise DTD validation

 
        try {
            // always use system SAXParser to parse DDs 
            System.setProperty("javax.xml.parsers.SAXParserFactory", "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
            SAXParserFactory spf = SAXParserFactory.newInstance();
            System.clearProperty("javax.xml.parsers.SAXParserFactory"); 

            // set the namespace awareness
            spf.setNamespaceAware(true);
            
	    // turn validation on for deployment descriptor XML files
            spf.setValidating(validating);    

	    // this feature is needed for backward compat with old DDs 
	    // constructed by J2EE1.2 which used Java encoding names
	    // such as ISO8859_1 etc.
            
            // this is a hack for a few days so people can continue runnning
            // with crimson
            if (spf.getClass().getName().indexOf("xerces")!=-1) {
                spf.setFeature(
                    "http://apache.org/xml/features/allow-java-encodings", true);
            } else {
                DOLUtils.getDefaultLogger().log(Level.WARNING, "modify your java command line to include the -Djava.endorsed.dirs option");
            }
	    
	    try {
		
		// Validation part 2a: set the schema language if necessary            
		spf.setFeature("http://apache.org/xml/features/validation/schema",validating);		
	    
            	SAXParser sp = spf.newSAXParser();
                
                // put the default schema for this deployment file type
                String path = getDefaultSchemaSource();
                if (path!=null) {
                    sp.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation",path);
                }

		// Set Xerces feature to allow dynamic validation. This prevents
		// SAX errors from being reported when no schemaLocation attr
		// is seen for a DTD based (J2EE1.3) XML descriptor.
		sp.getXMLReader().setFeature(
		    "http://apache.org/xml/features/validation/dynamic", validating);
		    
		return sp;
		
            } catch (SAXNotRecognizedException x) {
                // This can happen if the parser does not support JAXP 1.2
                DOLUtils.getDefaultLogger().log(Level.SEVERE,
                    "INFO: JAXP SAXParser property not recognized: "
                    + SaxParserHandler.JAXP_SCHEMA_LANGUAGE);
                 DOLUtils.getDefaultLogger().log(Level.SEVERE,
                    "Check to see if parser conforms to JAXP 1.2 spec.");

            }            
        } catch (Exception e) {
            e.printStackTrace();
            DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.saxParserError",
                                new Object[]{e.getMessage()});
        }
        return null;
    
protected booleangetXMLValidation()

return
true if XML validation should be performed at load time

        return xmlValidation;
    
public java.lang.StringgetXMLValidationLevel()

return
the xml validation reporting level

	return validationLevel;
    
public com.sun.enterprise.deployment.Descriptorread(java.io.InputStream is)
read and parse a J2EE Deployment Descriptor input file and return the constructed DOL descriptors for the J2EE Module

param
is the input stream for the XML file
return
the DOL descriptor for the J2EE Module

    
        return read(null, is);
    
public com.sun.enterprise.deployment.Descriptorread(com.sun.enterprise.deployment.Descriptor descriptor, java.io.File in)
read and parse a J2EE Deployment Descriptor input file and return the constructed DOL descriptors for the J2EE Module

param
if the read is incremental, the top node to apply the DDs to
param
is the input stream for the XML file
return
the DOL descriptor for the J2EE Module

        
        FileInputStream fis = new FileInputStream(in);
        try {
            return read(descriptor, fis);
        } finally {
            fis.close();
        }
    
public com.sun.enterprise.deployment.Descriptorread(com.sun.enterprise.deployment.Descriptor descriptor, com.sun.enterprise.deployment.deploy.shared.AbstractArchive in)
read and parse a J2EE Deployment Descriptor input file and return the constructed DOL descriptors for the J2EE Module

param
if the read is incremental, the top node to apply the DDs to
param
is the input archive abstraction for the XML file
return
the DOL descriptor for the J2EE Module

            
            InputStream is = in.getEntry(getDeploymentDescriptorPath());
            try {
                return read(descriptor, is);
            } finally {
                is.close();
            }
    
public com.sun.enterprise.deployment.Descriptorread(com.sun.enterprise.deployment.Descriptor descriptor, java.io.InputStream is)
read and parse a J2EE Deployment Descriptor input file and return the constructed DOL descriptors for the J2EE Module

param
if the read is incremental, the top node to apply the DDs to
param
is the input stream for the XML file
return
the DOL descriptor for the J2EE Module

        
        
        errorReportingString = FileUtils.revertFriendlyFilenameExtension(errorReportingString);
        String error = (errorReportingString == null)? errorReportingString:new File(errorReportingString).getName();
        String errorReporting = localStrings.getLocalString(
			"enterprise.deployment.io.errorcontext",
			"archive {0} and deployment descriptor file {1}", 
			new Object []{ error, getDeploymentDescriptorPath()});
        
        SAXParser sp = getSAXParser(getXMLValidation());
        SaxParserHandler dh = SaxParserHandlerFactory.newInstance();
	if (validationLevel.equals(FULL_VALIDATION)) {
	    dh.setStopOnError(true);
	} 
        if (descriptor!=null) {
            dh.setTopNode(getRootXMLNode(descriptor));
        }

        dh.setErrorReportingString(errorReporting);
        
        InputSource input =new InputSource(is);
        try {
            sp.parse(input,dh);
        } catch(SAXParseException e) {
            DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.saxParserError",
                                new Object[]{e.getMessage()});
            
            errorReporting += "  " + e.getLocalizedMessage();
            SAXParseException spe = new SAXParseException(errorReporting,
                                                        e.getSystemId(),
                                                        e.getPublicId(),
                                                        e.getLineNumber(),
                                                        e.getColumnNumber(),
                                                        e);

            throw spe;            
        } catch(SAXException e) {
            if (e.getException()!=null) {
                e.getException().printStackTrace();
            }
            DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.saxParserError",
                                new Object[]{e.getMessage()});            
            return null;
        } catch (IOException e) {
            DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.saxParserError",
                                e.getMessage() == null ? "" : new Object[]{e.getMessage()});            

            // Let's check if the root cause of this IOException is failing to 
            // connect. If yes, it means two things: 
            // 1. The public id declared is not one of the pre-defined ones. 
            //    So we need to ask user the check for typo.
            // 2. If the user does intend to use the system id to go outside.
            //    We need to ask them to check whether they have proper 
            //    access to the internet (proxy setting etc).      
            StackTraceElement[] stElements = e.getStackTrace();
            for (int i = 0; i < stElements.length; i++) {
                StackTraceElement stElement = stElements[i];
                if (stElement.getClassName().equals("java.net.Socket") &&
                    stElement.getMethodName().equals("connect")) {
                    String msg = localStrings.getLocalString(
                        "enterprise.deployment.can_not_locate_dtd", 
			"Unable to locate the DTD to validate your deployment descriptor file [{1}] in archive [{0}]. Please make sure the DOCTYPE is correct (no typo in public ID or system ID) and you have proper access to the Internet.", 
			 new Object []{ error, getDeploymentDescriptorPath()});
                    IOException ioe = new IOException(msg);
                    ioe.initCause(e);
                    throw ioe;
                }
            }

            IOException ioe = new IOException(localStrings.getLocalString(
                    "enterprise.deployment.backend.error_parsing_descr",
                    "Error parsing descriptor: {0}", new Object[] {errorReporting}));
            ioe.initCause(e);
            throw ioe;
        }
        if (dh.getTopNode()!=null) {
            Object topDesc = dh.getTopNode().getDescriptor();
            if (topDesc instanceof Descriptor) {
                return (Descriptor) topDesc;
            }
        }
        return null;
    
public voidsetErrorReportingString(java.lang.String s)
Sets the error reporting string

        this.errorReportingString = s;
    
public voidsetXMLValidation(boolean validate)
sets wether XML validation should be performed at load time

param
true to validate

        xmlValidation = validate;
    
public voidsetXMLValidationLevel(java.lang.String level)
Sets the xml validation error reporting/recovering level. The reporting level is active only when xml validation is turned on @see setXMLValidation. so far, two values can be passed, medium which reports the xml validation and continue and full which reports the xml validation and stop the xml parsing.

	validationLevel = level;
    
public voidwrite(com.sun.enterprise.deployment.Descriptor descriptor, java.io.OutputStream os)
writes the descriptor to an output stream

param
the descriptor
param
the output stream

        try {
            J2EEDocumentBuilder.write(descriptor, getRootXMLNode(descriptor), os);
        } catch(IOException ioe) {
            throw ioe;
        } catch(Exception e) {
            IOException ioe = new IOException(e.getMessage());
            ioe.initCause(e);
            throw ioe;
        }
    
public voidwrite(com.sun.enterprise.deployment.Descriptor descriptor, java.lang.String path)
writes the descriptor classes into a new XML file

param
descriptor the DOL descriptor to write
param
path the file to use

        
        String dir;
        String fileName = getDeploymentDescriptorPath();
        if (fileName.lastIndexOf('/")!=-1) {
            dir = path + File.separator + fileName.substring(0, fileName.lastIndexOf('/"));
            fileName = fileName.substring(fileName.lastIndexOf('/")+1);
        } else {
            dir = path;
        }
        File dirs = new File(dir.replace('/", File.separatorChar));
        if (!dirs.exists()) {
            dirs.mkdirs();
        }
        File out = new File(dirs, fileName);        
        write(descriptor, out);
    
public voidwrite(com.sun.enterprise.deployment.Descriptor descriptor, java.io.File out)
writes the descriptor classes into a new XML file

param
descriptor the DOL descriptor to write
param
out the file to use

        FileOutputStream fos = new FileOutputStream(out);
        try {
            write(descriptor, fos );
        } catch(Exception e) {
            IOException ioe = new IOException(e.getMessage());
            ioe.initCause(e);
            throw ioe;
        }
        fos.close();