/****************************************************************
 * 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;
    }
    
    
}

@@ -1268,20 +1268,68 @@
 va.util.
-List
+HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0Aimpor
@@ -1410,24 +1410,24 @@
 ndardMBean;%0A
-
 import javax
@@ -1462,16 +1462,211 @@
 iteData;
+%0Aimport javax.management.openmbean.CompositeDataSupport;%0Aimport javax.management.openmbean.CompositeType;%0Aimport javax.management.openmbean.OpenType;%0Aimport javax.management.openmbean.SimpleType;
 %0A%0Aimport
@@ -4613,127 +4613,575 @@
     
-%0A            //TODO implement me!%0A            %0A        %7D%0A        it.close();%0A        // TODO Auto-g
+Map%3CString, Object%3E map = new HashMap%3CString, Object%3E();%0A            map.put(%22name%22, m.getName());%0A            map.put(%22sender%22, m.getSender());%0A            map.put(%22state%22, m.getState());%0A            CompositeDataSupport c = new CompositeDataSupport(new CompositeType(Mail.class.getName(), %22Mail%22, new String%5B%5D %7B%22name%22, %22sender%22, %22state%22%7D, new String%5B%5D %7B%22The name of the Mail%22, %22The s
 en
+d
 er
-ated method stub
+ of the Mail%22, %22The state of the Mail%22%7D, new OpenType%5B%5D %7B SimpleType.STRING, SimpleType.STRING, SimpleType.STRING%7D), map);%0A            data.add(c);%0A        %7D%0A        it.close();
 %0A   

Add some proof of concept for browse mailqueue via JMX. JAMES-1180