Search in sources :

Example 1 with LifecycleException

use of org.apache.catalina.LifecycleException in project tomcat by apache.

the class UserDatabaseRealm method startInternal.

// ------------------------------------------------------ Lifecycle Methods
/**
     * Prepare for the beginning of active use of the public methods of this
     * component and implement the requirements of
     * {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
     *
     * @exception LifecycleException if this component detects a fatal error
     *  that prevents this component from being used
     */
@Override
protected void startInternal() throws LifecycleException {
    try {
        Context context = getServer().getGlobalNamingContext();
        database = (UserDatabase) context.lookup(resourceName);
    } catch (Throwable e) {
        ExceptionUtils.handleThrowable(e);
        containerLog.error(sm.getString("userDatabaseRealm.lookup", resourceName), e);
        database = null;
    }
    if (database == null) {
        throw new LifecycleException(sm.getString("userDatabaseRealm.noDatabase", resourceName));
    }
    super.startInternal();
}
Also used : Context(javax.naming.Context) LifecycleException(org.apache.catalina.LifecycleException)

Example 2 with LifecycleException

use of org.apache.catalina.LifecycleException 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 3 with LifecycleException

use of org.apache.catalina.LifecycleException 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 4 with LifecycleException

use of org.apache.catalina.LifecycleException in project tomcat by apache.

the class SetParentClassLoaderRule method start.

/**
     * Start a new server instance.
     */
public void start() {
    if (getServer() == null) {
        load();
    }
    if (getServer() == null) {
        log.fatal("Cannot start server. Server instance is not configured.");
        return;
    }
    long t1 = System.nanoTime();
    // Start the new server
    try {
        getServer().start();
    } catch (LifecycleException e) {
        log.fatal(sm.getString("catalina.serverStartFail"), e);
        try {
            getServer().destroy();
        } catch (LifecycleException e1) {
            log.debug("destroy() failed for failed Server ", e1);
        }
        return;
    }
    long t2 = System.nanoTime();
    if (log.isInfoEnabled()) {
        log.info("Server startup in " + ((t2 - t1) / 1000000) + " ms");
    }
    // Register shutdown hook
    if (useShutdownHook) {
        if (shutdownHook == null) {
            shutdownHook = new CatalinaShutdownHook();
        }
        Runtime.getRuntime().addShutdownHook(shutdownHook);
        // If JULI is being used, disable JULI's shutdown hook since
        // shutdown hooks run in parallel and log messages may be lost
        // if JULI's hook completes before the CatalinaShutdownHook()
        LogManager logManager = LogManager.getLogManager();
        if (logManager instanceof ClassLoaderLogManager) {
            ((ClassLoaderLogManager) logManager).setUseShutdownHook(false);
        }
    }
    if (await) {
        await();
        stop();
    }
}
Also used : LifecycleException(org.apache.catalina.LifecycleException) ClassLoaderLogManager(org.apache.juli.ClassLoaderLogManager) LogManager(java.util.logging.LogManager) ClassLoaderLogManager(org.apache.juli.ClassLoaderLogManager)

Example 5 with LifecycleException

use of org.apache.catalina.LifecycleException in project redisson by redisson.

the class RedissonSessionManager method start.

@Override
public void start() throws LifecycleException {
    Config config = null;
    try {
        config = Config.fromJSON(new File(configPath), getClass().getClassLoader());
    } catch (IOException e) {
        // trying next format
        try {
            config = Config.fromYAML(new File(configPath), getClass().getClassLoader());
        } catch (IOException e1) {
            log.error("Can't parse json config " + configPath, e);
            throw new LifecycleException("Can't parse yaml config " + configPath, e1);
        }
    }
    try {
        redisson = Redisson.create(config);
    } catch (Exception e) {
        throw new LifecycleException(e);
    }
    lifecycle.fireLifecycleEvent(START_EVENT, null);
}
Also used : LifecycleException(org.apache.catalina.LifecycleException) Config(org.redisson.config.Config) IOException(java.io.IOException) File(java.io.File) LifecycleException(org.apache.catalina.LifecycleException) IOException(java.io.IOException)

Aggregations

LifecycleException (org.apache.catalina.LifecycleException)128 Lifecycle (org.apache.catalina.Lifecycle)36 IOException (java.io.IOException)29 Container (org.apache.catalina.Container)19 NamingException (javax.naming.NamingException)18 File (java.io.File)17 Realm (org.apache.catalina.Realm)16 MalformedURLException (java.net.MalformedURLException)15 ServletException (javax.servlet.ServletException)12 ArrayList (java.util.ArrayList)9 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)9 Manager (org.apache.catalina.Manager)9 Valve (org.apache.catalina.Valve)9 Tomcat (org.apache.catalina.startup.Tomcat)9 Lock (java.util.concurrent.locks.Lock)8 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)8 Cluster (org.apache.catalina.Cluster)8 Loader (org.apache.catalina.Loader)8 Server (org.apache.catalina.Server)8 Contained (org.apache.catalina.Contained)7