Search in sources :

Example 6 with BlockingQueueHandler

use of fish.payara.nucleus.notification.BlockingQueueHandler in project Payara by payara.

the class TestXmppNotifier method execute.

@Override
public void execute(AdminCommandContext context) {
    ActionReport actionReport = context.getActionReport();
    Config config = targetUtil.getConfig(target);
    if (config == null) {
        context.getActionReport().setMessage("No such config named: " + target);
        context.getActionReport().setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    XmppNotifierConfiguration xmppConfig = config.getExtensionByType(XmppNotifierConfiguration.class);
    if (hostName == null) {
        hostName = xmppConfig.getHost();
    }
    if (port == null) {
        port = Integer.parseInt(xmppConfig.getPort());
    }
    if (serviceName == null) {
        serviceName = xmppConfig.getServiceName();
    }
    if (username == null) {
        username = xmppConfig.getUsername();
    }
    if (password == null) {
        password = xmppConfig.getPassword();
    }
    if (securityDisabled == null) {
        securityDisabled = Boolean.valueOf(xmppConfig.getSecurityDisabled());
    }
    if (roomId == null) {
        roomId = xmppConfig.getRoomId();
    }
    // prepare xmpp message
    XmppNotificationEvent event = factory.buildNotificationEvent(SUBJECT, MESSAGE);
    event.setEventType(SUBJECT);
    XmppMessageQueue queue = new XmppMessageQueue();
    queue.addMessage(new XmppMessage(event, event.getSubject(), event.getMessage()));
    XmppNotifierConfigurationExecutionOptions options = new XmppNotifierConfigurationExecutionOptions();
    options.setHost(hostName);
    options.setPort(port);
    options.setServiceName(serviceName);
    if (!Strings.isNullOrEmpty(username)) {
        options.setUsername(username);
    }
    if (!Strings.isNullOrEmpty(password)) {
        options.setPassword(password);
    }
    options.setSecurityDisabled(securityDisabled);
    options.setRoomId(roomId);
    XMPPTCPConnection connection = null;
    try {
        // Create connection
        XMPPTCPConnectionConfiguration configuration = XMPPTCPConnectionConfiguration.builder().setSecurityMode(options.getSecurityDisabled() ? ConnectionConfiguration.SecurityMode.disabled : ConnectionConfiguration.SecurityMode.required).setServiceName(options.getServiceName()).setHost(options.getHost()).setPort(options.getPort()).build();
        connection = new XMPPTCPConnection(configuration);
        connection.connect();
        if (options.getUsername() != null && options.getPassword() != null) {
            connection.login(options.getUsername(), options.getPassword());
        } else {
            connection.login();
        }
    } catch (SmackException | IOException | XMPPException ex) {
        actionReport.setMessage(ex.getMessage());
        actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
        queue.resetQueue();
        return;
    }
    XmppNotificationRunnable notifierRun = new XmppNotificationRunnable(queue, options, connection);
    // set up logger to store result
    Logger logger = Logger.getLogger(XmppNotificationRunnable.class.getCanonicalName());
    BlockingQueueHandler bqh = new BlockingQueueHandler();
    bqh.setLevel(Level.FINE);
    Level oldLevel = logger.getLevel();
    logger.setLevel(Level.FINE);
    logger.addHandler(bqh);
    // send message, this occurs in its own thread
    Thread notifierThread = new Thread(notifierRun, "test-xmpp-notifier-thread");
    notifierThread.start();
    try {
        notifierThread.join();
    } catch (InterruptedException ex) {
        Logger.getLogger(TestXmppNotifier.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        connection.disconnect();
        logger.setLevel(oldLevel);
    }
    LogRecord message = bqh.poll();
    bqh.clear();
    logger.removeHandler(bqh);
    if (message == null) {
        // something's gone wrong
        Logger.getGlobal().log(Level.SEVERE, "Failed to send XMPP message");
        actionReport.setMessage("Failed to send XMPP message");
        actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
    } else {
        actionReport.setMessage(message.getMessage());
        if (message.getLevel() == Level.FINE) {
            actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
        } else {
            actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
        }
    }
}
Also used : XMPPTCPConnection(org.jivesoftware.smack.tcp.XMPPTCPConnection) Config(com.sun.enterprise.config.serverbeans.Config) SmackException(org.jivesoftware.smack.SmackException) XMPPTCPConnectionConfiguration(org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration) IOException(java.io.IOException) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) BlockingQueueHandler(fish.payara.nucleus.notification.BlockingQueueHandler) LogRecord(java.util.logging.LogRecord) Level(java.util.logging.Level) XMPPException(org.jivesoftware.smack.XMPPException)

Example 7 with BlockingQueueHandler

use of fish.payara.nucleus.notification.BlockingQueueHandler in project Payara by payara.

the class TestEmailNotifier method execute.

@Override
public void execute(AdminCommandContext context) {
    ActionReport actionReport = context.getActionReport();
    Config config = targetUtil.getConfig(target);
    if (config == null) {
        context.getActionReport().setMessage("No such config named: " + target);
        context.getActionReport().setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    EmailNotifierConfiguration emailConfig = config.getExtensionByType(EmailNotifierConfiguration.class);
    if (jndiName == null) {
        jndiName = emailConfig.getJndiName();
    }
    if (to == null) {
        to = emailConfig.getTo();
    }
    // prepare email
    EmailNotificationEvent event = factory.buildNotificationEvent(SUBJECT, MESSAGE);
    EmailMessageQueue queue = new EmailMessageQueue();
    queue.addMessage(new EmailMessage(event, event.getSubject(), event.getMessage()));
    EmailNotifierConfigurationExecutionOptions options = new EmailNotifierConfigurationExecutionOptions();
    options.setJndiName(jndiName);
    options.setTo(to);
    Session session;
    try {
        InitialContext initialContext = new InitialContext();
        session = (Session) initialContext.lookup(jndiName);
    } catch (NamingException ex) {
        Logger.getLogger(TestEmailNotifier.class.getName()).log(Level.SEVERE, "Unable to find session" + jndiName);
        context.getActionReport().setMessage("Unable to find session: " + jndiName);
        context.getActionReport().setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    EmailNotificationRunnable notifierRun = new EmailNotificationRunnable(queue, session, options);
    // set up logger to store result
    Logger logger = Logger.getLogger(EmailNotificationRunnable.class.getCanonicalName());
    BlockingQueueHandler bqh = new BlockingQueueHandler(10);
    bqh.setLevel(Level.FINE);
    Level oldLevel = logger.getLevel();
    logger.setLevel(Level.FINE);
    logger.addHandler(bqh);
    // send message, this occurs in its own thread
    Thread notifierThread = new Thread(notifierRun, "test-email-notifier-thread");
    notifierThread.start();
    try {
        notifierThread.join();
    } catch (InterruptedException ex) {
        Logger.getLogger(TestEmailNotifier.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        logger.setLevel(oldLevel);
    }
    LogRecord message = bqh.poll();
    logger.removeHandler(bqh);
    if (message == null) {
        // something's gone wrong
        Logger.getLogger(TestEmailNotifier.class.getName()).log(Level.SEVERE, "Failed to send email");
        actionReport.setMessage("Failed to send email message");
        actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
    } else {
        if (message.getLevel() == Level.FINE) {
            actionReport.setMessage(message.getMessage());
            actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
        } else {
            actionReport.setMessage(message.getMessage() + message.getThrown().getMessage());
            actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
        }
    }
}
Also used : Config(com.sun.enterprise.config.serverbeans.Config) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) InitialContext(javax.naming.InitialContext) BlockingQueueHandler(fish.payara.nucleus.notification.BlockingQueueHandler) LogRecord(java.util.logging.LogRecord) NamingException(javax.naming.NamingException) Level(java.util.logging.Level) Session(javax.mail.Session)

Example 8 with BlockingQueueHandler

use of fish.payara.nucleus.notification.BlockingQueueHandler in project Payara by payara.

the class TestDatadogNotifier method execute.

@Override
public void execute(AdminCommandContext context) {
    ActionReport actionReport = context.getActionReport();
    Config config = targetUtil.getConfig(target);
    if (config == null) {
        context.getActionReport().setMessage("No such config named: " + target);
        context.getActionReport().setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    DatadogNotifierConfiguration datadogConfig = config.getExtensionByType(DatadogNotifierConfiguration.class);
    if (key == null) {
        key = datadogConfig.getKey();
    }
    // prepare Datadog message
    DatadogNotificationEvent event = factory.buildNotificationEvent(SUBJECT, MESSAGE);
    DatadogMessageQueue queue = new DatadogMessageQueue();
    queue.addMessage(new DatadogMessage(event, event.getSubject(), event.getMessage()));
    DatadogNotifierConfigurationExecutionOptions options = new DatadogNotifierConfigurationExecutionOptions();
    options.setKey(key);
    DatadogNotificationRunnable notifierRun = new DatadogNotificationRunnable(queue, options);
    // set up logger to store result
    Logger logger = Logger.getLogger(DatadogNotificationRunnable.class.getCanonicalName());
    BlockingQueueHandler bqh = new BlockingQueueHandler();
    bqh.setLevel(Level.FINE);
    Level oldLevel = logger.getLevel();
    logger.setLevel(Level.FINE);
    logger.addHandler(bqh);
    // send message, this occurs in its own thread
    Thread notifierThread = new Thread(notifierRun, "test-datadog-notifier-thread");
    notifierThread.start();
    try {
        notifierThread.join();
    } catch (InterruptedException ex) {
        Logger.getLogger(TestDatadogNotifier.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        logger.setLevel(oldLevel);
    }
    LogRecord message = bqh.poll();
    logger.removeHandler(bqh);
    if (message == null) {
        // something's gone wrong
        Logger.getGlobal().log(Level.SEVERE, "Failed to send Datadog message");
        actionReport.setMessage("Failed to send Datadog message");
        actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
    } else {
        ;
        actionReport.setMessage(message.getMessage());
        if (message.getLevel() == Level.FINE) {
            actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
        } else {
            actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
        }
    }
}
Also used : Config(com.sun.enterprise.config.serverbeans.Config) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) BlockingQueueHandler(fish.payara.nucleus.notification.BlockingQueueHandler) LogRecord(java.util.logging.LogRecord) Level(java.util.logging.Level)

Example 9 with BlockingQueueHandler

use of fish.payara.nucleus.notification.BlockingQueueHandler in project Payara by payara.

the class TestJmsNotifier method execute.

@Override
public void execute(AdminCommandContext context) {
    ActionReport actionReport = context.getActionReport();
    Config config = targetUtil.getConfig(target);
    if (config == null) {
        context.getActionReport().setMessage("No such config named: " + target);
        context.getActionReport().setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    JmsNotifierConfiguration jmsConfig = config.getExtensionByType(JmsNotifierConfiguration.class);
    if (contextFactoryClass == null) {
        contextFactoryClass = jmsConfig.getContextFactoryClass();
    }
    if (connectionFactoryName == null) {
        connectionFactoryName = jmsConfig.getConnectionFactoryName();
    }
    if (queueName == null) {
        queueName = jmsConfig.getQueueName();
    }
    if (url == null) {
        url = jmsConfig.getUrl();
    }
    if (username == null) {
        username = jmsConfig.getUsername();
    }
    if (password == null) {
        password = jmsConfig.getPassword();
    }
    // prepare JMS message
    JmsNotificationEvent event = factory.buildNotificationEvent(SUBJECT, MESSAGE);
    JmsMessageQueue queue = new JmsMessageQueue();
    queue.addMessage(new JmsMessage(event, event.getSubject(), event.getMessage()));
    JmsNotifierConfigurationExecutionOptions options = new JmsNotifierConfigurationExecutionOptions();
    options.setContextFactoryClass(contextFactoryClass);
    options.setConnectionFactoryName(connectionFactoryName);
    options.setQueueName(queueName);
    options.setUrl(url);
    options.setUsername(username);
    options.setPassword(password);
    JmsNotificationRunnable notifierRun = null;
    try {
        InitialContext ctx = new InitialContext();
        ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup(options.getConnectionFactoryName());
        Connection connection = connectionFactory.createConnection();
        notifierRun = new JmsNotificationRunnable(queue, options, connection);
    } catch (NamingException | JMSException ex) {
        Logger.getLogger(TestJmsNotifier.class.getName()).log(Level.SEVERE, null, ex);
        actionReport.setMessage(ex.getMessage());
        actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    // set up logger to store result
    Logger logger = Logger.getLogger(JmsNotificationRunnable.class.getCanonicalName());
    BlockingQueueHandler bqh = new BlockingQueueHandler(10);
    bqh.setLevel(Level.FINE);
    Level oldLevel = logger.getLevel();
    logger.setLevel(Level.FINE);
    logger.addHandler(bqh);
    // send message, this occurs in its own thread
    Thread notifierThread = new Thread(notifierRun, "test-jms-notifier-thread");
    notifierThread.start();
    try {
        notifierThread.join();
    } catch (InterruptedException ex) {
        Logger.getLogger(TestJmsNotifier.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        logger.setLevel(oldLevel);
    }
    LogRecord message = bqh.poll();
    logger.removeHandler(bqh);
    if (message == null) {
        // something's gone wrong
        Logger.getLogger(TestJmsNotifier.class.getName()).log(Level.SEVERE, "Failed to send JMS message");
        actionReport.setMessage("Failed to send JMS message");
        actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
    } else {
        if (message.getLevel() == Level.FINE) {
            actionReport.setMessage(message.getMessage());
            actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
        } else {
            actionReport.setMessage(message.getMessage() + message.getThrown().getMessage());
            actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
        }
    }
}
Also used : Config(com.sun.enterprise.config.serverbeans.Config) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) InitialContext(javax.naming.InitialContext) ConnectionFactory(javax.jms.ConnectionFactory) BlockingQueueHandler(fish.payara.nucleus.notification.BlockingQueueHandler) LogRecord(java.util.logging.LogRecord) NamingException(javax.naming.NamingException) Level(java.util.logging.Level)

Example 10 with BlockingQueueHandler

use of fish.payara.nucleus.notification.BlockingQueueHandler in project Payara by payara.

the class TestSlackNotifier method execute.

@Override
public void execute(AdminCommandContext context) {
    ActionReport actionReport = context.getActionReport();
    Config config = targetUtil.getConfig(target);
    if (config == null) {
        context.getActionReport().setMessage("No such config named: " + target);
        context.getActionReport().setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    SlackNotifierConfiguration slackConfig = config.getExtensionByType(SlackNotifierConfiguration.class);
    if (token1 == null) {
        token1 = slackConfig.getToken1();
    }
    if (token2 == null) {
        token2 = slackConfig.getToken2();
    }
    if (token3 == null) {
        token3 = slackConfig.getToken3();
    }
    // prepare Slack message
    SlackNotificationEvent event = factory.buildNotificationEvent(SUBJECT, MESSAGE);
    SlackMessageQueue queue = new SlackMessageQueue();
    queue.addMessage(new SlackMessage(event, event.getSubject(), event.getMessage()));
    SlackNotifierConfigurationExecutionOptions options = new SlackNotifierConfigurationExecutionOptions();
    options.setToken1(token1);
    options.setToken2(token2);
    options.setToken3(token3);
    SlackNotificationRunnable notifierRun = new SlackNotificationRunnable(queue, options);
    // set up logger to store result
    Logger logger = Logger.getLogger(SlackNotificationRunnable.class.getCanonicalName());
    BlockingQueueHandler bqh = new BlockingQueueHandler(10);
    bqh.setLevel(Level.FINE);
    Level oldLevel = logger.getLevel();
    logger.setLevel(Level.FINE);
    logger.addHandler(bqh);
    // send message, this occurs in its own thread
    Thread notifierThread = new Thread(notifierRun, "test-slack-notifier-thread");
    notifierThread.start();
    try {
        notifierThread.join();
    } catch (InterruptedException ex) {
        Logger.getLogger(TestSlackNotifier.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        logger.setLevel(oldLevel);
    }
    LogRecord message = bqh.poll();
    logger.removeHandler(bqh);
    if (message == null) {
        // something's gone wrong
        Logger.getLogger(TestSlackNotifier.class.getName()).log(Level.SEVERE, "Failed to send Slack message");
        actionReport.setMessage("Failed to send Slack message");
        actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
    } else {
        actionReport.setMessage(message.getMessage());
        if (message.getLevel() == Level.FINE) {
            actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
        } else {
            actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
        }
    }
}
Also used : Config(com.sun.enterprise.config.serverbeans.Config) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) BlockingQueueHandler(fish.payara.nucleus.notification.BlockingQueueHandler) LogRecord(java.util.logging.LogRecord) Level(java.util.logging.Level)

Aggregations

Config (com.sun.enterprise.config.serverbeans.Config)10 BlockingQueueHandler (fish.payara.nucleus.notification.BlockingQueueHandler)10 Logger (java.util.logging.Logger)10 ActionReport (org.glassfish.api.ActionReport)10 Level (java.util.logging.Level)9 LogRecord (java.util.logging.LogRecord)9 IOException (java.io.IOException)2 InitialContext (javax.naming.InitialContext)2 NamingException (javax.naming.NamingException)2 InvalidSnmpVersion (fish.payara.notification.snmp.exception.InvalidSnmpVersion)1 Connection (javax.jms.Connection)1 ConnectionFactory (javax.jms.ConnectionFactory)1 JMSException (javax.jms.JMSException)1 Session (javax.mail.Session)1 RestEndpoint (org.glassfish.api.admin.RestEndpoint)1 SmackException (org.jivesoftware.smack.SmackException)1 XMPPException (org.jivesoftware.smack.XMPPException)1 XMPPTCPConnection (org.jivesoftware.smack.tcp.XMPPTCPConnection)1 XMPPTCPConnectionConfiguration (org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration)1 CommunityTarget (org.snmp4j.CommunityTarget)1