001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.broker.jmx;
018
019import java.util.concurrent.Callable;
020import java.util.concurrent.ExecutionException;
021import java.util.concurrent.ExecutorService;
022import java.util.concurrent.Future;
023import java.util.concurrent.TimeUnit;
024
025import javax.management.MBeanException;
026import javax.management.NotCompliantMBeanException;
027import javax.management.ObjectInstance;
028import javax.management.ObjectName;
029import javax.management.ReflectionException;
030
031/**
032 * MBean that invokes the requested operation using an async operation and waits for the result
033 * if the operation times out then an exception is thrown.
034 */
035public class AsyncAnnotatedMBean extends AnnotatedMBean {
036
037    private ExecutorService executor;
038    private long timeout = 0;
039
040    public <T> AsyncAnnotatedMBean(ExecutorService executor, long timeout, T impl, Class<T> mbeanInterface, ObjectName objectName) throws NotCompliantMBeanException {
041        super(impl, mbeanInterface, objectName);
042
043        this.executor = executor;
044        this.timeout = timeout;
045    }
046
047    protected AsyncAnnotatedMBean(Class<?> mbeanInterface, ObjectName objectName) throws NotCompliantMBeanException {
048        super(mbeanInterface, objectName);
049    }
050
051    protected Object asyncInvole(String s, Object[] objects, String[] strings) throws MBeanException, ReflectionException {
052        return super.invoke(s, objects, strings);
053    }
054
055    @SuppressWarnings({ "unchecked", "rawtypes" })
056    public static ObjectInstance registerMBean(ExecutorService executor, long timeout, ManagementContext context, Object object, ObjectName objectName) throws Exception {
057
058        if (timeout < 0 && executor != null) {
059            throw new IllegalArgumentException("async timeout cannot be negative.");
060        }
061
062        if (timeout > 0 && executor == null) {
063            throw new NullPointerException("timeout given but no ExecutorService instance given.");
064        }
065
066        String mbeanName = object.getClass().getName() + "MBean";
067
068        for (Class c : object.getClass().getInterfaces()) {
069            if (mbeanName.equals(c.getName())) {
070                if (timeout == 0) {
071                    return context.registerMBean(new AnnotatedMBean(object, c, objectName), objectName);
072                } else {
073                    return context.registerMBean(new AsyncAnnotatedMBean(executor, timeout, object, c, objectName), objectName);
074                }
075            }
076        }
077
078        return context.registerMBean(object, objectName);
079    }
080
081    @Override
082    public Object invoke(String s, Object[] objects, String[] strings) throws MBeanException, ReflectionException {
083
084        final String action = s;
085        final Object[] params = objects;
086        final String[] signature = strings;
087
088        Future<Object> task = executor.submit(new Callable<Object>() {
089
090            @Override
091            public Object call() throws Exception {
092                return asyncInvole(action, params, signature);
093            }
094        });
095
096        try {
097            return task.get(timeout, TimeUnit.MILLISECONDS);
098        } catch (ExecutionException e) {
099            if (e.getCause() instanceof MBeanException) {
100                throw (MBeanException) e.getCause();
101            }
102
103            throw new MBeanException(e);
104        } catch (Exception e) {
105            throw new MBeanException(e);
106        } finally {
107            if (!task.isDone()) {
108                task.cancel(true);
109            }
110        }
111    }
112}