Search in sources :

Example 1 with SaxParserHandlerBundled

use of com.sun.enterprise.deployment.node.SaxParserHandlerBundled in project Payara by payara.

the class AppClientFacade method readConfig.

private static ClientContainer readConfig(final String configPath, final ClassLoader loader) throws UserError, JAXBException, FileNotFoundException, ParserConfigurationException, SAXException, URISyntaxException, IOException {
    ClientContainer result = null;
    Reader configReader = null;
    String configFileLocationForErrorMessage = "";
    try {
        /*
             * During a Java Web Start launch, the config is passed as a property
             * value.
             */
        String configInProperty = System.getProperty(ACC_CONFIG_CONTENT_PROPERTY_NAME);
        if (configInProperty != null) {
            /*
                 * Awkwardly, the glassfish-acc.xml content refers to a config file.
                 * We work around this for Java Web Start launch by capturing the
                 * content of that config file into a property setting in the
                 * generated JNLP document.  We need to write that content into
                 * a temporary file here on the client and then replace a
                 * placeholder in the glassfish-acc.xml content with the path to that
                 * temp file.
                 */
            final File securityConfigTempFile = Util.writeTextToTempFile(configInProperty, "wss-client-config", ".xml", false);
            final Properties p = new Properties();
            p.setProperty("security.config.path", securityConfigTempFile.getAbsolutePath());
            configInProperty = Util.replaceTokens(configInProperty, p);
            configReader = new StringReader(configInProperty);
        } else {
            /*
                 * This is not a Java Web Start launch, so read the configuration
                 * from a disk file.
                 */
            File configFile = checkXMLFile(configPath);
            checkXMLFile(appClientCommandArgs.getConfigFilePath());
            configReader = new FileReader(configFile);
            configFileLocationForErrorMessage = configFile.getAbsolutePath();
        }
        /*
             * Although JAXB makes it very simple to parse the XML into Java objects,
             * we have to do several things explicitly to use our local copies of
             * DTDs and XSDs.
             */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setValidating(true);
        spf.setNamespaceAware(true);
        SAXParser parser = spf.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        /*
             * Get the local entity resolver that knows about the
             * bundled .dtd and .xsd files.
             */
        reader.setEntityResolver(new SaxParserHandlerBundled());
        /*
             * To support installation-directory independence the default glassfish-acc.xml
             * refers to the wss-config file using ${com.sun.aas.installRoot}...  So
             * preprocess the glassfish-acc.xml file to replace any tokens with the
             * corresponding values, then submit that result to JAXB.
             */
        InputSource inputSource = replaceTokensForParsing(configReader);
        SAXSource saxSource = new SAXSource(reader, inputSource);
        JAXBContext jc = JAXBContext.newInstance(ClientContainer.class);
        final ValidationEventCollector vec = new ValidationEventCollector();
        Unmarshaller u = jc.createUnmarshaller();
        u.setEventHandler(vec);
        result = (ClientContainer) u.unmarshal(saxSource);
        if (vec.hasEvents()) {
            /*
                 * The parser reported at least one warning or error.  If all
                 * events were warnings, display them as a message and continue.
                 * Otherwise there was at least one error or fatal, so say so
                 * and try to continue but say that such errors might be fatal
                 * in future releases.
                 */
            boolean isError = false;
            final StringBuilder sb = new StringBuilder();
            for (ValidationEvent ve : vec.getEvents()) {
                sb.append(ve.getMessage()).append(LINE_SEP);
                isError |= (ve.getSeverity() != ValidationEvent.WARNING);
            }
            final String messageIntroduction = localStrings.getLocalString(AppClientFacade.class, isError ? "appclient.errParsingConfig" : "appclient.warnParsingConfig", isError ? "Error parsing app client container configuration {0}.  Attempting to continue.  In future releases such parsing errors might become fatal.  Please correct your configuration file." : "Warning(s) parsing app client container configuration {0}.  Continuing.", new Object[] { configFileLocationForErrorMessage });
            /*
                 * Following code - which throws an exception if the config
                 * validation fails - is commented out to prevent possible
                 * regressions.  Users might have customized the acc config file
                 * in a way that does not validate but would have worked silently
                 * before.
                 */
            // if (isErrorOrWorse) {
            // throw new UserError(messageIntroduction,
            // new ValidationException(sb.toString()));
            // } else {
            System.err.println(messageIntroduction + LINE_SEP + sb.toString());
        // }
        }
        return result;
    } finally {
        if (configReader != null) {
            configReader.close();
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) ClientContainer(org.glassfish.appclient.client.acc.config.ClientContainer) AppClientContainer(org.glassfish.appclient.client.acc.AppClientContainer) Reader(java.io.Reader) XMLReader(org.xml.sax.XMLReader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Properties(java.util.Properties) SaxParserHandlerBundled(com.sun.enterprise.deployment.node.SaxParserHandlerBundled) SAXSource(javax.xml.transform.sax.SAXSource) StringReader(java.io.StringReader) SAXParser(javax.xml.parsers.SAXParser) FileReader(java.io.FileReader) File(java.io.File) ValidationEventCollector(javax.xml.bind.util.ValidationEventCollector) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

SaxParserHandlerBundled (com.sun.enterprise.deployment.node.SaxParserHandlerBundled)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileReader (java.io.FileReader)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 Properties (java.util.Properties)1 ValidationEventCollector (javax.xml.bind.util.ValidationEventCollector)1 SAXParser (javax.xml.parsers.SAXParser)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 SAXSource (javax.xml.transform.sax.SAXSource)1 AppClientContainer (org.glassfish.appclient.client.acc.AppClientContainer)1 ClientContainer (org.glassfish.appclient.client.acc.config.ClientContainer)1 InputSource (org.xml.sax.InputSource)1 XMLReader (org.xml.sax.XMLReader)1