Package org.mc4j.ems.impl.jmx.connection.bean.operation

Source Code of org.mc4j.ems.impl.jmx.connection.bean.operation.Future

/*
* Copyright 2002-2004 Greg Hinkle
*
* 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.mc4j.ems.impl.jmx.connection.bean.operation;

import org.mc4j.ems.impl.jmx.connection.bean.DMBean;
import org.mc4j.ems.impl.jmx.connection.bean.parameter.DParameter;
import org.mc4j.ems.connection.bean.operation.EmsOperation;
import org.mc4j.ems.connection.bean.parameter.EmsParameter;
import org.mc4j.ems.connection.EmsInvocationException;

import javax.management.MBeanOperationInfo;
import javax.management.MBeanParameterInfo;
import javax.management.ReflectionException;
import java.util.List;
import java.util.ArrayList;

/**
* @author Greg Hinkle (ghinkle@users.sourceforge.net), Apr 4, 2005
* @version $Revision: 1.2 $($Author: ghinkl $ / $Date: 2006/04/12 19:11:35 $)
*/
public class DOperation implements EmsOperation {

    protected MBeanOperationInfo info;
    protected DMBean bean;
    public static final int MAX_EXECUTION_TIME = 10000;

    protected List<EmsParameter> parameters = new ArrayList<EmsParameter>();
    protected Impact impact;

    public DOperation(MBeanOperationInfo info, DMBean bean) {
        this.info = info;
        this.bean = bean;

        MBeanParameterInfo[] params = info.getSignature();
        for (MBeanParameterInfo param : params) {
            parameters.add(new DParameter(param));
        }

        switch (info.getImpact()) {
            case MBeanOperationInfo.ACTION:
                impact = Impact.ACTION;
                break;
            case MBeanOperationInfo.INFO:
                impact = Impact.INFO;
                break;
            case MBeanOperationInfo.ACTION_INFO:
                impact = Impact.ACTION_INFO;
                break;
            default:
                impact = Impact.UNKNOWN;
        }
    }

    public String getName() {
        return info.getName();
    }

    public String getDescription() {
        return info.getDescription();
    }

    public List<EmsParameter> getParameters() {
        return parameters;
    }

    public Impact getImpact() {
        return impact;
    }

    public String getReturnType() {
        return info.getReturnType();
    }

    @SuppressWarnings({"EmptyCatchBlock"})
    public Object invoke(Object... parameters) throws EmsInvocationException {


        MBeanParameterInfo[] parameterInfos = info.getSignature();

        final Object[] parameterValues = new Object[parameterInfos.length];
        final String[] parameterTypes = new String[parameterInfos.length];


        int i = 0;
        for (Object param : parameters) {
            String name = parameterInfos[i].getName();
            parameterValues[i] = param;
            parameterTypes[i] = parameterInfos[i].getType();
            i++;
        }

        class Future {
            boolean done = false;
            Object results;
            Exception e;
        }

        final Future f = new Future();

        Runnable execution = new Runnable() {
            public void run() {
                try {
                    f.results =
                            bean.getConnectionProvider().getMBeanServer().invoke(
                                    bean.getObjectName(),
                                    getName(),
                                    parameterValues,
                                    parameterTypes);

                } catch (ReflectionException re) {
                    Exception cause = re.getTargetException();
                    if (cause != null) {
                        f.e = cause;
                    } else {
                        f.e = re;
                    }
                } catch (Exception e) {
                    f.e = e;
                } finally {
                    f.done = true;
                }
            }
        };

        // Need to run this outside the AWT update thread
        // so as not to freeze the UI in case of failed connection.
        Thread t = new Thread(execution, "MC4J Operation Execution [" + getName() + "]");
        t.start();

        // TODO: The timeout is nice, but providing a method that actually returns a future would be nice
        try {
            t.join(MAX_EXECUTION_TIME);
        } catch (InterruptedException e) { }
        if (!f.done) {
            // TODO: Write exceptions
            throw new EmsInvocationException("Operation timed out.");
        }




        if (f.e != null) {
            throw new EmsInvocationException("Exception on invocation of [" + getName() + "]" + f.e.toString(),f.e);
        } else {
            return f.results;
        }
    }

    public int compareTo(Object o) {
        DOperation otherOperation = (DOperation) o;
        return this.getName().compareTo(
                otherOperation.getName());
    }


}
TOP

Related Classes of org.mc4j.ems.impl.jmx.connection.bean.operation.Future

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.
/div>
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.