Search in sources :

Example 11 with MotechException

use of org.motechproject.commons.api.MotechException in project motech by motech.

the class MBeanService method getQueueMessages.

/**
 * Retrieves a list of messages for the given JMS queue.
 *
 * @param queueName The name of the queue for which messages should be retrieved.
 * @return {@link List} of messages for the given queue.
 */
@PreAuthorize(SecurityConstants.MANAGE_ACTIVEMQ)
public List<QueueMessage> getQueueMessages(String queueName) {
    try {
        ArrayList<QueueMessage> queueMessages = new ArrayList<>();
        QueueViewMBean queueViewMBean = mBeanServer.getQueueViewMBean(queueName);
        for (CompositeData compositeData : queueViewMBean.browse()) {
            if (compositeData != null) {
                String messageId = (String) compositeData.get(JMS_MESSAGE_ID);
                Boolean redelivered = (Boolean) compositeData.get(JMS_REDELIVERED);
                Date timestamp = (Date) compositeData.get(JMS_TIMESTAMP);
                queueMessages.add(new QueueMessage(messageId, redelivered, DateUtil.newDateTime(timestamp)));
            }
        }
        return queueMessages;
    } catch (OpenDataException openDataException) {
        throw new MotechException(String.format("Could not Browse MBean for queue %s", queueName), openDataException);
    } catch (IOException ioException) {
        throw new MotechException(String.format("Could not access MBean for queue %s", queueName), ioException);
    }
}
Also used : MotechException(org.motechproject.commons.api.MotechException) QueueMessage(org.motechproject.admin.domain.QueueMessage) OpenDataException(javax.management.openmbean.OpenDataException) CompositeData(javax.management.openmbean.CompositeData) ArrayList(java.util.ArrayList) QueueViewMBean(org.apache.activemq.broker.jmx.QueueViewMBean) IOException(java.io.IOException) Date(java.util.Date) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 12 with MotechException

use of org.motechproject.commons.api.MotechException in project motech by motech.

the class MBeanService method getTopicStatistics.

/**
 * Returns topic statistics for the JMS topics.
 *
 * @return {@link List} of {@link TopicMBean}. One for each topic.
 */
@PreAuthorize(SecurityConstants.MANAGE_ACTIVEMQ)
public List<TopicMBean> getTopicStatistics() {
    try {
        List<TopicMBean> topics = new ArrayList<>();
        for (ObjectName name : mBeanServer.getTopics()) {
            String destination = name.getKeyProperty(mBeanServer.getDestinationProperty());
            TopicViewMBean topicView = mBeanServer.getTopicViewMBean(name);
            TopicMBean topic = new TopicMBean(destination);
            topic.setEnqueueCount(topicView.getEnqueueCount());
            topic.setDequeueCount(topicView.getDequeueCount());
            topic.setExpiredCount(topicView.getExpiredCount());
            topic.setConsumerCount(topicView.getConsumerCount());
            topics.add(topic);
        }
        return topics;
    } catch (IOException ex) {
        throw new MotechException("Could not access MBeans ", ex);
    }
}
Also used : MotechException(org.motechproject.commons.api.MotechException) TopicViewMBean(org.apache.activemq.broker.jmx.TopicViewMBean) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TopicMBean(org.motechproject.admin.domain.TopicMBean) ObjectName(javax.management.ObjectName) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 13 with MotechException

use of org.motechproject.commons.api.MotechException in project motech by motech.

the class MBeanService method getQueueStatistics.

/**
 * Returns queue statistics for the JMS queues.
 *
 * @return {@link List} of {@link QueueMBean}. One for each queue belonging.
 */
@PreAuthorize(SecurityConstants.MANAGE_ACTIVEMQ)
public List<QueueMBean> getQueueStatistics() {
    try {
        List<QueueMBean> queues = new ArrayList<>();
        for (ObjectName name : mBeanServer.getQueues()) {
            String destination = name.getKeyProperty(mBeanServer.getDestinationProperty());
            QueueViewMBean queueView = mBeanServer.getQueueViewMBean(name);
            QueueMBean queue = new QueueMBean(destination);
            queue.setEnqueueCount(queueView.getEnqueueCount());
            queue.setDequeueCount(queueView.getDequeueCount());
            queue.setExpiredCount(queueView.getExpiredCount());
            queue.setConsumerCount(queueView.getConsumerCount());
            queue.setQueueSize(queueView.getQueueSize());
            queues.add(queue);
        }
        return queues;
    } catch (IOException ex) {
        throw new MotechException("Could not access MBeans ", ex);
    }
}
Also used : MotechException(org.motechproject.commons.api.MotechException) QueueMBean(org.motechproject.admin.domain.QueueMBean) ArrayList(java.util.ArrayList) QueueViewMBean(org.apache.activemq.broker.jmx.QueueViewMBean) IOException(java.io.IOException) ObjectName(javax.management.ObjectName) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 14 with MotechException

use of org.motechproject.commons.api.MotechException in project motech by motech.

the class SettingsServiceImpl method loadMultipartFileIntoProperties.

private Properties loadMultipartFileIntoProperties(MultipartFile configFile) {
    if (configFile == null) {
        throw new IllegalArgumentException("Config file cannot be null");
    }
    InputStream is = null;
    Properties settings = new Properties();
    try {
        is = configFile.getInputStream();
        settings.load(is);
    } catch (IOException e) {
        throw new MotechException("Error saving config file", e);
    } finally {
        IOUtils.closeQuietly(is);
    }
    return settings;
}
Also used : MotechException(org.motechproject.commons.api.MotechException) InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties)

Example 15 with MotechException

use of org.motechproject.commons.api.MotechException in project motech by motech.

the class SettingsServiceImpl method saveRawFile.

@Override
public void saveRawFile(MultipartFile file, String filename, long bundleId) {
    InputStream is = null;
    try {
        is = file.getInputStream();
        configurationService.saveRawConfig(getBundleSymbolicName(bundleId), getVersion(bundleId), filename, is);
    } catch (IOException e) {
        throw new MotechException("Error reading uploaded file", e);
    } finally {
        IOUtils.closeQuietly(is);
    }
}
Also used : MotechException(org.motechproject.commons.api.MotechException) InputStream(java.io.InputStream) IOException(java.io.IOException)

Aggregations

MotechException (org.motechproject.commons.api.MotechException)33 IOException (java.io.IOException)25 InputStream (java.io.InputStream)9 Properties (java.util.Properties)8 ArrayList (java.util.ArrayList)6 MessageDigest (java.security.MessageDigest)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 HashMap (java.util.HashMap)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 Map (java.util.Map)3 ObjectName (javax.management.ObjectName)3 SettingsRecord (org.motechproject.config.domain.SettingsRecord)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStream (java.io.OutputStream)2 DigestInputStream (java.security.DigestInputStream)2 QueueViewMBean (org.apache.activemq.broker.jmx.QueueViewMBean)2 TaskHandlerException (org.motechproject.tasks.exception.TaskHandlerException)2 Bundle (org.osgi.framework.Bundle)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 Resource (org.springframework.core.io.Resource)2