Search in sources :

Example 6 with Digester

use of org.apache.tomcat.util.digester.Digester in project tomcat by apache.

the class SetParentClassLoaderRule method load.

/**
     * Start a new server instance.
     */
public void load() {
    long t1 = System.nanoTime();
    initDirs();
    // Before digester - it may be needed
    initNaming();
    // Create and execute our Digester
    Digester digester = createStartDigester();
    InputSource inputSource = null;
    InputStream inputStream = null;
    File file = null;
    try {
        try {
            file = configFile();
            inputStream = new FileInputStream(file);
            inputSource = new InputSource(file.toURI().toURL().toString());
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug(sm.getString("catalina.configFail", file), e);
            }
        }
        if (inputStream == null) {
            try {
                inputStream = getClass().getClassLoader().getResourceAsStream(getConfigFile());
                inputSource = new InputSource(getClass().getClassLoader().getResource(getConfigFile()).toString());
            } catch (Exception e) {
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("catalina.configFail", getConfigFile()), e);
                }
            }
        }
        // Alternative: don't bother with xml, just create it manually.
        if (inputStream == null) {
            try {
                inputStream = getClass().getClassLoader().getResourceAsStream("server-embed.xml");
                inputSource = new InputSource(getClass().getClassLoader().getResource("server-embed.xml").toString());
            } catch (Exception e) {
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("catalina.configFail", "server-embed.xml"), e);
                }
            }
        }
        if (inputStream == null || inputSource == null) {
            if (file == null) {
                log.warn(sm.getString("catalina.configFail", getConfigFile() + "] or [server-embed.xml]"));
            } else {
                log.warn(sm.getString("catalina.configFail", file.getAbsolutePath()));
                if (file.exists() && !file.canRead()) {
                    log.warn("Permissions incorrect, read permission is not allowed on the file.");
                }
            }
            return;
        }
        try {
            inputSource.setByteStream(inputStream);
            digester.push(this);
            digester.parse(inputSource);
        } catch (SAXParseException spe) {
            log.warn("Catalina.start using " + getConfigFile() + ": " + spe.getMessage());
            return;
        } catch (Exception e) {
            log.warn("Catalina.start using " + getConfigFile() + ": ", e);
            return;
        }
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
            // Ignore
            }
        }
    }
    getServer().setCatalina(this);
    getServer().setCatalinaHome(Bootstrap.getCatalinaHomeFile());
    getServer().setCatalinaBase(Bootstrap.getCatalinaBaseFile());
    // Stream redirection
    initStreams();
    // Start the new server
    try {
        getServer().init();
    } catch (LifecycleException e) {
        if (Boolean.getBoolean("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE")) {
            throw new java.lang.Error(e);
        } else {
            log.error("Catalina.start", e);
        }
    }
    long t2 = System.nanoTime();
    if (log.isInfoEnabled()) {
        log.info("Initialization processed in " + ((t2 - t1) / 1000000) + " ms");
    }
}
Also used : InputSource(org.xml.sax.InputSource) LifecycleException(org.apache.catalina.LifecycleException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SAXParseException(org.xml.sax.SAXParseException) Digester(org.apache.tomcat.util.digester.Digester) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) ConnectException(java.net.ConnectException) LifecycleException(org.apache.catalina.LifecycleException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException)

Example 7 with Digester

use of org.apache.tomcat.util.digester.Digester in project tomcat by apache.

the class SetParentClassLoaderRule method stopServer.

public void stopServer(String[] arguments) {
    if (arguments != null) {
        arguments(arguments);
    }
    Server s = getServer();
    if (s == null) {
        // Create and execute our Digester
        Digester digester = createStopDigester();
        File file = configFile();
        try (FileInputStream fis = new FileInputStream(file)) {
            InputSource is = new InputSource(file.toURI().toURL().toString());
            is.setByteStream(fis);
            digester.push(this);
            digester.parse(is);
        } catch (Exception e) {
            log.error("Catalina.stop: ", e);
            System.exit(1);
        }
    } else {
        // Server object already present. Must be running as a service
        try {
            s.stop();
        } catch (LifecycleException e) {
            log.error("Catalina.stop: ", e);
        }
        return;
    }
    // Stop the existing server
    s = getServer();
    if (s.getPort() > 0) {
        try (Socket socket = new Socket(s.getAddress(), s.getPort());
            OutputStream stream = socket.getOutputStream()) {
            String shutdown = s.getShutdown();
            for (int i = 0; i < shutdown.length(); i++) {
                stream.write(shutdown.charAt(i));
            }
            stream.flush();
        } catch (ConnectException ce) {
            log.error(sm.getString("catalina.stopServer.connectException", s.getAddress(), String.valueOf(s.getPort())));
            log.error("Catalina.stop: ", ce);
            System.exit(1);
        } catch (IOException e) {
            log.error("Catalina.stop: ", e);
            System.exit(1);
        }
    } else {
        log.error(sm.getString("catalina.stopServer"));
        System.exit(1);
    }
}
Also used : InputSource(org.xml.sax.InputSource) LifecycleException(org.apache.catalina.LifecycleException) Server(org.apache.catalina.Server) OutputStream(java.io.OutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ConnectException(java.net.ConnectException) LifecycleException(org.apache.catalina.LifecycleException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) Digester(org.apache.tomcat.util.digester.Digester) File(java.io.File) Socket(java.net.Socket) ConnectException(java.net.ConnectException)

Example 8 with Digester

use of org.apache.tomcat.util.digester.Digester in project tomcat by apache.

the class MemoryUserCreationFactory method open.

/**
     * Initialize access to this user database.
     *
     * @exception Exception if any exception is thrown during opening
     */
@Override
public void open() throws Exception {
    synchronized (groups) {
        synchronized (users) {
            // Erase any previous groups and users
            users.clear();
            groups.clear();
            roles.clear();
            String pathName = getPathname();
            try (InputStream is = ConfigFileLoader.getInputStream(getPathname())) {
                // Construct a digester to read the XML input file
                Digester digester = new Digester();
                try {
                    digester.setFeature("http://apache.org/xml/features/allow-java-encodings", true);
                } catch (Exception e) {
                    log.warn(sm.getString("memoryUserDatabase.xmlFeatureEncoding"), e);
                }
                digester.addFactoryCreate("tomcat-users/group", new MemoryGroupCreationFactory(this), true);
                digester.addFactoryCreate("tomcat-users/role", new MemoryRoleCreationFactory(this), true);
                digester.addFactoryCreate("tomcat-users/user", new MemoryUserCreationFactory(this), true);
                // Parse the XML input to load this database
                digester.parse(is);
            } catch (IOException ioe) {
                log.error(sm.getString("memoryUserDatabase.fileNotFound", pathName));
                return;
            }
        }
    }
}
Also used : InputStream(java.io.InputStream) Digester(org.apache.tomcat.util.digester.Digester) IOException(java.io.IOException) IOException(java.io.IOException)

Example 9 with Digester

use of org.apache.tomcat.util.digester.Digester in project tomcat by apache.

the class MbeansDescriptorsDigesterSource method createDigester.

private static Digester createDigester() {
    Digester digester = new Digester();
    digester.setNamespaceAware(false);
    digester.setValidating(false);
    URL url = Registry.getRegistry(null, null).getClass().getResource("/org/apache/tomcat/util/modeler/mbeans-descriptors.dtd");
    digester.register("-//Apache Software Foundation//DTD Model MBeans Configuration File", url.toString());
    // Configure the parsing rules
    digester.addObjectCreate("mbeans-descriptors/mbean", "org.apache.tomcat.util.modeler.ManagedBean");
    digester.addSetProperties("mbeans-descriptors/mbean");
    digester.addSetNext("mbeans-descriptors/mbean", "add", "java.lang.Object");
    digester.addObjectCreate("mbeans-descriptors/mbean/attribute", "org.apache.tomcat.util.modeler.AttributeInfo");
    digester.addSetProperties("mbeans-descriptors/mbean/attribute");
    digester.addSetNext("mbeans-descriptors/mbean/attribute", "addAttribute", "org.apache.tomcat.util.modeler.AttributeInfo");
    digester.addObjectCreate("mbeans-descriptors/mbean/notification", "org.apache.tomcat.util.modeler.NotificationInfo");
    digester.addSetProperties("mbeans-descriptors/mbean/notification");
    digester.addSetNext("mbeans-descriptors/mbean/notification", "addNotification", "org.apache.tomcat.util.modeler.NotificationInfo");
    digester.addObjectCreate("mbeans-descriptors/mbean/notification/descriptor/field", "org.apache.tomcat.util.modeler.FieldInfo");
    digester.addSetProperties("mbeans-descriptors/mbean/notification/descriptor/field");
    digester.addSetNext("mbeans-descriptors/mbean/notification/descriptor/field", "addField", "org.apache.tomcat.util.modeler.FieldInfo");
    digester.addCallMethod("mbeans-descriptors/mbean/notification/notification-type", "addNotifType", 0);
    digester.addObjectCreate("mbeans-descriptors/mbean/operation", "org.apache.tomcat.util.modeler.OperationInfo");
    digester.addSetProperties("mbeans-descriptors/mbean/operation");
    digester.addSetNext("mbeans-descriptors/mbean/operation", "addOperation", "org.apache.tomcat.util.modeler.OperationInfo");
    digester.addObjectCreate("mbeans-descriptors/mbean/operation/descriptor/field", "org.apache.tomcat.util.modeler.FieldInfo");
    digester.addSetProperties("mbeans-descriptors/mbean/operation/descriptor/field");
    digester.addSetNext("mbeans-descriptors/mbean/operation/descriptor/field", "addField", "org.apache.tomcat.util.modeler.FieldInfo");
    digester.addObjectCreate("mbeans-descriptors/mbean/operation/parameter", "org.apache.tomcat.util.modeler.ParameterInfo");
    digester.addSetProperties("mbeans-descriptors/mbean/operation/parameter");
    digester.addSetNext("mbeans-descriptors/mbean/operation/parameter", "addParameter", "org.apache.tomcat.util.modeler.ParameterInfo");
    return digester;
}
Also used : Digester(org.apache.tomcat.util.digester.Digester) URL(java.net.URL)

Example 10 with Digester

use of org.apache.tomcat.util.digester.Digester in project tomcat by apache.

the class TestSchemaValidation method testWebapp_3_0.

@Test
public void testWebapp_3_0() throws Exception {
    XmlErrorHandler handler = new XmlErrorHandler();
    Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(false), true);
    digester.setErrorHandler(handler);
    digester.push(new WebXml());
    WebXml desc = (WebXml) digester.parse(new File("test/webapp-3.0/WEB-INF/web.xml"));
    Assert.assertEquals("3.0", desc.getVersion());
    Assert.assertEquals(0, handler.getErrors().size());
    Assert.assertEquals(0, handler.getWarnings().size());
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) Digester(org.apache.tomcat.util.digester.Digester) XmlErrorHandler(org.apache.tomcat.util.descriptor.XmlErrorHandler) WebRuleSet(org.apache.tomcat.util.descriptor.web.WebRuleSet) File(java.io.File) Test(org.junit.Test)

Aggregations

Digester (org.apache.tomcat.util.digester.Digester)28 File (java.io.File)14 XmlErrorHandler (org.apache.tomcat.util.descriptor.XmlErrorHandler)11 IOException (java.io.IOException)8 WebRuleSet (org.apache.tomcat.util.descriptor.web.WebRuleSet)8 WebXml (org.apache.tomcat.util.descriptor.web.WebXml)8 Test (org.junit.Test)8 InputStream (java.io.InputStream)6 FileInputStream (java.io.FileInputStream)5 InputSource (org.xml.sax.InputSource)5 LifecycleException (org.apache.catalina.LifecycleException)4 SAXParseException (org.xml.sax.SAXParseException)3 ConnectException (java.net.ConnectException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 BufferedInputStream (java.io.BufferedInputStream)1 OutputStream (java.io.OutputStream)1 StringReader (java.io.StringReader)1 Socket (java.net.Socket)1