/**
 *  Copyright 2009 Martin Traverso
 *
 *  Licensed 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.weakref.jmx;

import javax.management.InstanceAlreadyExistsException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;

import org.weakref.jmx.JmxException.Reason;

import java.lang.management.ManagementFactory;

public class MBeanExporter
{
    private final MBeanServer server;

    public MBeanExporter(MBeanServer server)
    {
        this.server = server;
    }

    public void export(String name, Object object)
    {
        ObjectName objectName;
        try {
            objectName = new ObjectName(name);
            MBeanBuilder builder = new MBeanBuilder(object);
            MBean mbean = builder.build();

            server.registerMBean(mbean, objectName);
        }
        catch (MalformedObjectNameException mone) {
            throw new JmxException(Reason.MALFORMED_OBJECT_NAME, mone.getMessage());
        }
        catch (InstanceAlreadyExistsException iae) {
            throw new JmxException(JmxException.Reason.INSTANCE_ALREADY_EXISTS, iae.getMessage());
        }
        catch (MBeanRegistrationException mre) {
            throw new JmxException(Reason.MBEAN_REGISTRATION, mre.getMessage(), mre.getCause());
        }
        catch (NotCompliantMBeanException e) {
            // MBeanBuilder should never construct invalid mbeans
            throw new AssertionError(e);
        }
    }

    public void unexport(String name)
    {
        ObjectName objectName;

        try {
            objectName = new ObjectName(name);

            server.unregisterMBean(objectName);
        }
        catch (MalformedObjectNameException mone) {
            throw new JmxException(Reason.MALFORMED_OBJECT_NAME, mone.getMessage());
        }
        catch (MBeanRegistrationException mre) {
            throw new JmxException(JmxException.Reason.MBEAN_REGISTRATION, mre.getMessage(), mre.getCause());
        }
        catch (InstanceNotFoundException infe) {
            throw new JmxException(JmxException.Reason.INSTANCE_NOT_FOUND, infe.getMessage());
        }

    }

    /**
     * Get an MBeanExporter that uses the default platform mbean server
     *
     * @return an exporter
     */
    public static MBeanExporter withPlatformMBeanServer()
    {
        return new MBeanExporter(ManagementFactory.getPlatformMBeanServer());
    }
}

@@ -1570,35 +1570,32 @@
 ctNameException 
-mon
 e) %7B%0A           
@@ -1640,35 +1640,32 @@
 ED_OBJECT_NAME, 
-mon
 e.getMessage());
@@ -1721,18 +1721,16 @@
 ception 
-ia
 e) %7B%0A   
@@ -1806,18 +1806,16 @@
 EXISTS, 
-ia
 e.getMes
@@ -1867,34 +1867,32 @@
 rationException 
-mr
 e) %7B%0A           
@@ -1934,34 +1934,32 @@
 N_REGISTRATION, 
-mr
 e.getMessage(), 
@@ -1950,34 +1950,32 @@
 e.getMessage(), 
-mr
 e.getCause());%0A 
@@ -2394,19 +2394,16 @@
 ception 
-mon
 e) %7B%0A   
@@ -2468,11 +2468,8 @@
 ME, 
-mon
 e.ge
@@ -2533,18 +2533,16 @@
 ception 
-mr
 e) %7B%0A   
@@ -2613,18 +2613,16 @@
 RATION, 
-mr
 e.getMes
@@ -2633,10 +2633,8 @@
 (), 
-mr
 e.ge
@@ -2695,19 +2695,16 @@
 ception 
-inf
 e) %7B%0A   
@@ -2779,11 +2779,8 @@
 ND, 
-inf
 e.ge

rename variables