<commit_before>/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you under the Apache License, Version 2.0 (the            *
 * "License"); you may not use this file except in compliance   *
 * with the License.  You may obtain a copy of the License at   *
 *                                                              *
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 *                                                              *
 * Unless required by applicable law or agreed to in writing,   *
 * software distributed under the License is distributed on an  *
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
 * KIND, either express or implied.  See the License for the    *
 * specific language governing permissions and limitations      *
 * under the License.                                           *
 ****************************************************************/
package org.apache.james.queue.library;

import java.util.ArrayList;
import java.util.List;

import javax.management.NotCompliantMBeanException;
import javax.management.StandardMBean;
import javax.management.openmbean.CompositeData;

import org.apache.james.queue.api.MailQueueManagementMBean;
import org.apache.james.queue.api.ManageableMailQueue;
import org.apache.james.queue.api.MailQueue.MailQueueException;
import org.apache.james.queue.api.ManageableMailQueue.MailQueueIterator;
import org.apache.james.queue.api.ManageableMailQueue.Type;
import org.apache.mailet.Mail;

/**
 * 
 * JMX MBean implementation which expose management functions by wrapping a {@link ManageableMailQueue}
 * 
 *
 */
public class MailQueueManagement extends StandardMBean implements MailQueueManagementMBean{
    private final ManageableMailQueue queue;

    public MailQueueManagement(ManageableMailQueue queue) throws NotCompliantMBeanException {
        super(MailQueueManagementMBean.class);
        this.queue = queue;
        
    }
    
    /*
     * (non-Javadoc)
     * @see org.apache.james.queue.api.MailQueueManagementMBean#clear()
     */
    public long clear() {
        try {
            return queue.clear();
        } catch (MailQueueException e) {
            return -1;
        }
    }

    /*
     * (non-Javadoc)
     * @see org.apache.james.queue.api.MailQueueManagementMBean#flush()
     */
    public long flush() {
        try {
            return queue.flush();
        } catch (MailQueueException e) {
            return -1;
        }
    }

    /*
     * (non-Javadoc)
     * @see org.apache.james.queue.api.MailQueueManagementMBean#getSize()
     */
    public long getSize() {
        try {
            return queue.getSize();
        } catch (MailQueueException e) {
            return -1;
        }
    }

    /*
     * (non-Javadoc)
     * @see org.apache.james.queue.api.MailQueueManagementMBean#removeWithName(java.lang.String)
     */
    public long removeWithName(String name) {
        try {
            return queue.remove(Type.Name, name);
        } catch (MailQueueException e) {
            return -1;
        }
    }

    /*
     * (non-Javadoc)
     * @see org.apache.james.queue.api.MailQueueManagementMBean#removeWithRecipient(java.lang.String)
     */
    public long removeWithRecipient(String address) {
        try {
            return queue.remove(Type.Recipient, address);
        } catch (MailQueueException e) {
            return -1;
        }
    }

    /*
     * (non-Javadoc)
     * @see org.apache.james.queue.api.MailQueueManagementMBean#removeWithSender(java.lang.String)
     */
    public long removeWithSender(String address) {
        try {
            return queue.remove(Type.Sender, address);
        } catch (MailQueueException e) {
            return -1;
        }
    }

    /*
     * (non-Javadoc)
     * @see org.apache.james.queue.api.MailQueueManagementMBean#browse()
     */
    public List<CompositeData> browse() throws Exception {
        MailQueueIterator it = queue.browse();
        List<CompositeData> data = new ArrayList<CompositeData>();
        while(it.hasNext()) {
            Mail m = it.next();
            
            //TODO implement me!
            
        }
        it.close();
        // TODO Auto-generated method stub
        return data;
    }
    
    <commit_msg>Add some proof of concept for browse mailqueue via JMX. JAMES-1180<commit_after>/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you under the Apache License, Version 2.0 (the            *
 * "License"); you may not use this file except in compliance   *
 * with the License.  You may obtain a copy of the License at   *
 *                                                              *
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 *                                                              *
 * Unless required by applicable law or agreed to in writing,   *
 * software distributed under the License is distributed on an  *
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
 * KIND, either express or implied.  See the License for the    *
 * specific language governing permissions and limitations      *
 * under the License.                                           *
 ****************************************************************/
package org.apache.james.queue.library;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.management.NotCompliantMBeanException;
import javax.management.StandardMBean;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.CompositeType;
import javax.management.openmbean.OpenType;
import javax.management.openmbean.SimpleType;

import org.apache.james.queue.api.MailQueueManagementMBean;
import org.apache.james.queue.api.ManageableMailQueue;
import org.apache.james.queue.api.MailQueue.MailQueueException;
import org.apache.james.queue.api.ManageableMailQueue.MailQueueIterator;
import org.apache.james.queue.api.ManageableMailQueue.Type;
import org.apache.mailet.Mail;

/**
 * 
 * JMX MBean implementation which expose management functions by wrapping a {@link ManageableMailQueue}
 * 
 *
 */
public class MailQueueManagement extends StandardMBean implements MailQueueManagementMBean{
    private final ManageableMailQueue queue;

    public MailQueueManagement(ManageableMailQueue queue) throws NotCompliantMBeanException {
        super(MailQueueManagementMBean.class);
        this.queue = queue;
        
    }
    
    /*
     * (non-Javadoc)
     * @see org.apache.james.queue.api.MailQueueManagementMBean#clear()
     */
    public long clear() {
        try {
            return queue.clear();
        } catch (MailQueueException e) {
            return -1;
        }
    }

    /*
     * (non-Javadoc)
     * @see org.apache.james.queue.api.MailQueueManagementMBean#flush()
     */
    public long flush() {
        try {
            return queue.flush();
        } catch (MailQueueException e) {
            return -1;
        }
    }

    /*
     * (non-Javadoc)
     * @see org.apache.james.queue.api.MailQueueManagementMBean#getSize()
     */
    public long getSize() {
        try {
            return queue.getSize();
        } catch (MailQueueException e) {
            return -1;
        }
    }

    /*
     * (non-Javadoc)
     * @see org.apache.james.queue.api.MailQueueManagementMBean#removeWithName(java.lang.String)
     */
    public long removeWithName(String name) {
        try {
            return queue.remove(Type.Name, name);
        } catch (MailQueueException e) {
            return -1;
        }
    }

    /*
     * (non-Javadoc)
     * @see org.apache.james.queue.api.MailQueueManagementMBean#removeWithRecipient(java.lang.String)
     */
    public long removeWithRecipient(String address) {
        try {
            return queue.remove(Type.Recipient, address);
        } catch (MailQueueException e) {
            return -1;
        }
    }

    /*
     * (non-Javadoc)
     * @see org.apache.james.queue.api.MailQueueManagementMBean#removeWithSender(java.lang.String)
     */
    public long removeWithSender(String address) {
        try {
            return queue.remove(Type.Sender, address);
        } catch (MailQueueException e) {
            return -1;
        }
    }

    /*
     * (non-Javadoc)
     * @see org.apache.james.queue.api.MailQueueManagementMBean#browse()
     */
    public List<CompositeData> browse() throws Exception {
        MailQueueIterator it = queue.browse();
        List<CompositeData> data = new ArrayList<CompositeData>();
        while(it.hasNext()) {
            Mail m = it.next();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name", m.getName());
            map.put("sender", m.getSender());
            map.put("state", m.getState());
            CompositeDataSupport c = new CompositeDataSupport(new CompositeType(Mail.class.getName(), "Mail", new String[] {"name", "sender", "state"}, new String[] {"The name of the Mail", "The sender of the Mail", "The state of the Mail"}, new OpenType[] { SimpleType.STRING, SimpleType.STRING, SimpleType.STRING}), map);
            data.add(c);
        }
        it.close();
        return data;
    }
    
    
{'fraction_non_alphanumeric': 0.12337662337662338, 'fraction_numerical': 0.002435064935064935, 'mean_word_length': 2.2660702451954937, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6651358', 'n_tokens_mistral': 2707, 'n_tokens_neox': 2639, 'n_words': 636}