Search in sources :

Example 1 with SerializableObject

use of lucee.commons.lang.SerializableObject in project Lucee by lucee.

the class SMTPClient method _send.

public void _send(lucee.runtime.config.ConfigWeb config, Server[] servers) throws MailException {
    long start = System.nanoTime();
    long _timeout = getTimeout(config);
    try {
        Proxy.start(proxyData);
        Log log = ((ConfigImpl) config).getLog("mail");
        // Server[] servers = config.getMailServers();
        if (host != null) {
            int prt;
            String usr, pwd;
            ServerImpl[] nServers = new ServerImpl[host.length];
            for (int i = 0; i < host.length; i++) {
                usr = null;
                pwd = null;
                prt = Server.DEFAULT_PORT;
                if (port > 0)
                    prt = port;
                if (!StringUtil.isEmpty(username)) {
                    usr = username;
                    pwd = password;
                }
                nServers[i] = toServerImpl(host[i], prt, usr, pwd, lifeTimespan, idleTimespan);
                if (ssl == SSL_YES)
                    nServers[i].setSSL(true);
                if (tls == TLS_YES)
                    nServers[i].setTLS(true);
            }
            servers = nServers;
        }
        if (servers.length == 0) {
            // return;
            throw new MailException("no SMTP Server defined");
        }
        boolean _ssl, _tls;
        for (int i = 0; i < servers.length; i++) {
            Server server = servers[i];
            String _username = null, _password = "";
            if (server.hasAuthentication()) {
                _username = server.getUsername();
                _password = server.getPassword();
            }
            // tls
            if (tls != TLS_NONE)
                _tls = tls == TLS_YES;
            else
                _tls = ((ServerImpl) server).isTLS();
            // ssl
            if (ssl != SSL_NONE)
                _ssl = ssl == SSL_YES;
            else
                _ssl = ((ServerImpl) server).isSSL();
            MimeMessageAndSession msgSess;
            boolean recyleConnection = ((ServerImpl) server).reuseConnections();
            {
                // synchronized(LOCK) {
                try {
                    msgSess = createMimeMessage(config, server.getHostName(), server.getPort(), _username, _password, ((ServerImpl) server).getLifeTimeSpan(), ((ServerImpl) server).getIdleTimeSpan(), _tls, _ssl, ((ConfigImpl) config).isMailSendPartial(), !recyleConnection, ((ConfigImpl) config).isUserset());
                } catch (MessagingException e) {
                    // listener
                    listener(config, server, log, e, System.nanoTime() - start);
                    MailException me = new MailException(e.getMessage());
                    me.setStackTrace(e.getStackTrace());
                    throw me;
                }
                try {
                    SerializableObject lock = new SerializableObject();
                    SMTPSender sender = new SMTPSender(lock, msgSess, server.getHostName(), server.getPort(), _username, _password, recyleConnection);
                    sender.start();
                    SystemUtil.wait(lock, _timeout);
                    if (!sender.isSent()) {
                        Throwable t = sender.getThrowable();
                        if (t != null)
                            throw Caster.toPageException(t);
                        // stop when still running
                        try {
                            if (sender.isAlive())
                                sender.stop();
                        } catch (Throwable t2) {
                            ExceptionUtil.rethrowIfNecessary(t2);
                        }
                        // after thread is stopped check sent flag again
                        if (!sender.isSent()) {
                            throw new MessagingException("timeout occurred after " + (_timeout / 1000) + " seconds while sending mail message");
                        }
                    }
                    // could have an exception but was send anyway
                    if (sender.getThrowable() != null) {
                        Throwable t = sender.getThrowable();
                        if (log != null)
                            LogUtil.log(log, Log.LEVEL_ERROR, "send mail", t);
                    }
                    clean(config, attachmentz);
                    listener(config, server, log, null, System.nanoTime() - start);
                    break;
                } catch (Exception e) {
                    SystemOut.printDate(e);
                    if (i + 1 == servers.length) {
                        listener(config, server, log, e, System.nanoTime() - start);
                        MailException me = new MailException(server.getHostName() + " " + ExceptionUtil.getStacktrace(e, true) + ":" + i);
                        me.setStackTrace(e.getStackTrace());
                        throw me;
                    }
                }
            }
        }
    } finally {
        Proxy.end();
    }
}
Also used : Server(lucee.runtime.net.mail.Server) Log(lucee.commons.io.log.Log) MessagingException(javax.mail.MessagingException) MessagingException(javax.mail.MessagingException) PageException(lucee.runtime.exp.PageException) FileNotFoundException(java.io.FileNotFoundException) MailException(lucee.runtime.net.mail.MailException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ExpressionException(lucee.runtime.exp.ExpressionException) ApplicationException(lucee.runtime.exp.ApplicationException) SerializableObject(lucee.commons.lang.SerializableObject) ServerImpl(lucee.runtime.net.mail.ServerImpl) MailException(lucee.runtime.net.mail.MailException) ConfigImpl(lucee.runtime.config.ConfigImpl)

Example 2 with SerializableObject

use of lucee.commons.lang.SerializableObject in project Lucee by lucee.

the class Execute method _execute.

private void _execute() throws Exception {
    Object monitor = new SerializableObject();
    String command = "";
    if (name == null) {
        if (StringUtil.isEmpty(body)) {
            required("execute", "name", name);
            required("execute", "arguments", arguments);
        } else
            command = body;
    } else {
        if (arguments == null)
            command = name;
        else
            command = name + arguments;
    }
    _Execute execute = new _Execute(pageContext, monitor, command, outputfile, variable, errorFile, errorVariable);
    // if(timeout<=0)execute._run();
    // else {
    execute.start();
    if (timeout > 0) {
        try {
            synchronized (monitor) {
                monitor.wait(timeout);
            }
        } finally {
            execute.abort(terminateOnTimeout);
        }
        if (execute.hasException()) {
            throw execute.getException();
        }
        if (!execute.hasFinished())
            throw new ApplicationException("timeout [" + (timeout) + " ms] expired while executing [" + command + "]");
    // }
    }
}
Also used : SerializableObject(lucee.commons.lang.SerializableObject) ApplicationException(lucee.runtime.exp.ApplicationException) SerializableObject(lucee.commons.lang.SerializableObject)

Aggregations

SerializableObject (lucee.commons.lang.SerializableObject)2 ApplicationException (lucee.runtime.exp.ApplicationException)2 FileNotFoundException (java.io.FileNotFoundException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MessagingException (javax.mail.MessagingException)1 Log (lucee.commons.io.log.Log)1 ConfigImpl (lucee.runtime.config.ConfigImpl)1 ExpressionException (lucee.runtime.exp.ExpressionException)1 PageException (lucee.runtime.exp.PageException)1 MailException (lucee.runtime.net.mail.MailException)1 Server (lucee.runtime.net.mail.Server)1 ServerImpl (lucee.runtime.net.mail.ServerImpl)1