Example usage for javax.management ReflectionException ReflectionException

List of usage examples for javax.management ReflectionException ReflectionException

Introduction

In this page you can find the example usage for javax.management ReflectionException ReflectionException.

Prototype

public ReflectionException(java.lang.Exception e, String message) 

Source Link

Document

Creates a ReflectionException that wraps the actual java.lang.Exception with a detail message.

Usage

From source file:com.ecyrd.management.SimpleMBean.java

/**
 *  Invokes a particular method./*from   www.  j  ava2s. c  o  m*/
 *  
 *  @param arg0 Method name
 *  @param arg1 A list of arguments for the invocation
 */
public Object invoke(String arg0, Object[] arg1, String[] arg2) throws MBeanException, ReflectionException {
    Method[] methods = getClass().getMethods();

    for (int i = 0; i < methods.length; i++) {
        if (methods[i].getName().equals(arg0)) {
            try {
                return methods[i].invoke(this, arg1);
            } catch (IllegalArgumentException e) {
                throw new ReflectionException(e, "Wrong arguments");
            } catch (IllegalAccessException e) {
                throw new ReflectionException(e, "No access");
            } catch (InvocationTargetException e) {
                throw new ReflectionException(e, "Wrong target");
            }
        }
    }

    throw new ReflectionException(null, "There is no such method " + arg0); // TODO: Can you put a null exception?
}

From source file:com.ecyrd.management.SimpleMBean.java

public void setAttribute(Attribute attr)
        throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
    Method m;//from  w ww  . jav  a2s  . c o  m

    String mname = "set" + StringUtils.capitalize(attr.getName());
    m = findGetterSetter(getClass(), mname, attr.getValue().getClass());

    if (m == null)
        throw new AttributeNotFoundException(attr.getName());

    Object[] args = { attr.getValue() };

    try {
        m.invoke(this, args);
    } catch (IllegalArgumentException e) {
        throw new InvalidAttributeValueException("Faulty argument: " + e.getMessage());
    } catch (IllegalAccessException e) {
        throw new ReflectionException(e, "Cannot access attribute " + e.getMessage());
    } catch (InvocationTargetException e) {
        throw new ReflectionException(e, "Cannot invoke attribute " + e.getMessage());
    }
}

From source file:jef.tools.reflect.ClassEx.java

/**
 * ?/*from ww  w  . jav  a2 s  .c  o m*/
 * 
 * @param className
 * @param loder
 * @return
 * @throws ReflectionException
 */
public static final ClassEx getClassEx(String className, ClassLoader loder) throws ReflectionException {
    try {
        if (loder == null)
            loder = ClassEx.class.getClassLoader();
        Class<?> c = loder.loadClass(className);
        return new ClassEx(c);
    } catch (ClassNotFoundException e) {
        throw new ReflectionException(e, "Class: " + className + " not found.");
    }
}

From source file:com.cyberway.issue.crawler.admin.CrawlJob.java

public Object invoke(String operationName, Object[] params, String[] signature) throws ReflectionException {
    if (operationName == null) {
        throw new RuntimeOperationsException(new IllegalArgumentException("Operation name cannot be null"),
                "Cannot call invoke with null operation name");
    }// w w w .j  ava2s  .com

    controller.installThreadContextSettingsHandler();

    if (this.bdbjeOperationsNameList.contains(operationName)) {
        try {
            Object o = this.bdbjeMBeanHelper.invoke(this.controller.getBdbEnvironment(), operationName, params,
                    signature);
            // If OP_DB_ST, return String version of result.
            if (operationName.equals(OP_DB_STAT)) {
                return o.toString();
            }
            return o;
        } catch (MBeanException e) {
            throw new RuntimeOperationsException(new RuntimeException(e));
        }
    }

    // TODO: Exploit passed signature.

    // The pattern in the below is to match an operation and when found
    // do a return out of if clause.  Doing it this way, I can fall
    // on to the MethodNotFoundException for case where we've an
    // attribute but no handler.
    if (operationName.equals(IMPORT_URI_OPER)) {
        JmxUtils.checkParamsCount(IMPORT_URI_OPER, params, 3);
        mustBeCrawling();
        try {
            importUri((String) params[0], ((Boolean) params[1]).booleanValue(),
                    ((Boolean) params[2]).booleanValue());
        } catch (URIException e) {
            throw new RuntimeOperationsException(new RuntimeException(e));
        }
        return null;
    }

    if (operationName.equals(IMPORT_URIS_OPER)) {
        JmxUtils.checkParamsCount(IMPORT_URIS_OPER, params, 4);
        mustBeCrawling();
        return importUris((String) params[0], ((String) params[1]).toString(),
                ((Boolean) params[2]).booleanValue(), ((Boolean) params[3]).booleanValue());
    }

    if (operationName.equals(DUMP_URIS_OPER)) {
        JmxUtils.checkParamsCount(DUMP_URIS_OPER, params, 4);
        mustBeCrawling();
        if (!this.controller.isPaused()) {
            throw new RuntimeOperationsException(new IllegalArgumentException("Must " + "be paused"),
                    "Cannot dump URI's from running job.");
        }
        dumpUris((String) params[0], (String) params[1], ((Integer) params[2]).intValue(),
                ((Boolean) params[3]).booleanValue());
    }

    if (operationName.equals(PAUSE_OPER)) {
        JmxUtils.checkParamsCount(PAUSE_OPER, params, 0);
        mustBeCrawling();
        pause();
        return null;
    }

    if (operationName.equals(RESUME_OPER)) {
        JmxUtils.checkParamsCount(RESUME_OPER, params, 0);
        mustBeCrawling();
        resume();
        return null;
    }

    if (operationName.equals(FRONTIER_REPORT_OPER)) {
        JmxUtils.checkParamsCount(FRONTIER_REPORT_OPER, params, 1);
        mustBeCrawling();
        return getFrontierReport((String) params[0]);
    }

    if (operationName.equals(THREADS_REPORT_OPER)) {
        JmxUtils.checkParamsCount(THREADS_REPORT_OPER, params, 0);
        mustBeCrawling();
        return getThreadsReport();
    }

    if (operationName.equals(SEEDS_REPORT_OPER)) {
        JmxUtils.checkParamsCount(SEEDS_REPORT_OPER, params, 0);
        mustBeCrawling();
        StringWriter sw = new StringWriter();
        if (getStatisticsTracking() != null && getStatisticsTracking() instanceof StatisticsTracker) {
            ((StatisticsTracker) getStatisticsTracking()).writeSeedsReportTo(new PrintWriter(sw));
        } else {
            sw.write("Unsupported");
        }
        return sw.toString();
    }

    if (operationName.equals(CHECKPOINT_OPER)) {
        JmxUtils.checkParamsCount(CHECKPOINT_OPER, params, 0);
        mustBeCrawling();
        try {
            checkpoint();
        } catch (IllegalStateException e) {
            throw new RuntimeOperationsException(e);
        }
        return null;
    }

    if (operationName.equals(PROGRESS_STATISTICS_OPER)) {
        JmxUtils.checkParamsCount(PROGRESS_STATISTICS_OPER, params, 0);
        mustBeCrawling();
        return getStatisticsTracking().getProgressStatisticsLine();
    }

    if (operationName.equals(PROGRESS_STATISTICS_LEGEND_OPER)) {
        JmxUtils.checkParamsCount(PROGRESS_STATISTICS_LEGEND_OPER, params, 0);
        return getStatisticsTracking().progressStatisticsLegend();
    }

    throw new ReflectionException(new NoSuchMethodException(operationName),
            "Cannot find the operation " + operationName);
}

From source file:com.cyberway.issue.crawler.Heritrix.java

public Object invoke(final String operationName, final Object[] params, final String[] signature)
        throws ReflectionException {
    if (operationName == null) {
        throw new RuntimeOperationsException(new IllegalArgumentException("Operation name cannot be null"),
                "Cannot call invoke with null operation name");
    }/*from  w  w  w. jav  a 2 s . co m*/
    // INFO logging of JMX invokes: [#HER-907]
    if (logger.isLoggable(Level.INFO)) {
        String paramsString = "";
        for (Object o : params) {
            paramsString.concat("[" + o.toString() + "]");
        }
        logger.info("JMX invoke: " + operationName + " [" + paramsString + "]");
    }
    // The pattern in the below is to match an operation and when found
    // do a return out of if clause.  Doing it this way, I can fall
    // on to the MethodNotFoundException for case where we've an
    // attribute but no handler.
    if (operationName.equals(START_OPER)) {
        JmxUtils.checkParamsCount(START_OPER, params, 0);
        start();
        return null;
    }
    if (operationName.equals(STOP_OPER)) {
        JmxUtils.checkParamsCount(STOP_OPER, params, 0);
        stop();
        return null;
    }
    if (operationName.equals(DESTROY_OPER)) {
        JmxUtils.checkParamsCount(DESTROY_OPER, params, 0);
        destroy();
        return null;
    }
    if (operationName.equals(TERMINATE_CRAWL_JOB_OPER)) {
        JmxUtils.checkParamsCount(TERMINATE_CRAWL_JOB_OPER, params, 0);
        return new Boolean(this.jobHandler.terminateCurrentJob());
    }
    if (operationName.equals(REBIND_JNDI_OPER)) {
        JmxUtils.checkParamsCount(REBIND_JNDI_OPER, params, 0);
        try {
            registerContainerJndi();
        } catch (MalformedObjectNameException e) {
            throw new RuntimeOperationsException(new RuntimeException(e));
        } catch (UnknownHostException e) {
            throw new RuntimeOperationsException(new RuntimeException(e));
        } catch (NamingException e) {
            throw new RuntimeOperationsException(new RuntimeException(e));
        }
        return null;
    }
    if (operationName.equals(SHUTDOWN_OPER)) {
        JmxUtils.checkParamsCount(SHUTDOWN_OPER, params, 0);
        Heritrix.shutdown();
        return null;
    }
    if (operationName.equals(LOG_OPER)) {
        JmxUtils.checkParamsCount(LOG_OPER, params, 2);
        logger.log(Level.parse((String) params[0]), (String) params[1]);
        return null;
    }
    if (operationName.equals(INTERRUPT_OPER)) {
        JmxUtils.checkParamsCount(INTERRUPT_OPER, params, 1);
        return interrupt((String) params[0]);
    }
    if (operationName.equals(START_CRAWLING_OPER)) {
        JmxUtils.checkParamsCount(START_CRAWLING_OPER, params, 0);
        startCrawling();
        return null;
    }
    if (operationName.equals(STOP_CRAWLING_OPER)) {
        JmxUtils.checkParamsCount(STOP_CRAWLING_OPER, params, 0);
        stopCrawling();
        return null;
    }
    if (operationName.equals(ADD_CRAWL_JOB_OPER)) {
        JmxUtils.checkParamsCount(ADD_CRAWL_JOB_OPER, params, 4);
        try {
            return addCrawlJob((String) params[0], (String) params[1],
                    checkForEmptyPlaceHolder((String) params[2]), checkForEmptyPlaceHolder((String) params[3]));
        } catch (IOException e) {
            throw new RuntimeOperationsException(new RuntimeException(e));
        } catch (FatalConfigurationException e) {
            throw new RuntimeOperationsException(new RuntimeException(e));
        }
    }
    if (operationName.equals(DELETE_CRAWL_JOB_OPER)) {
        JmxUtils.checkParamsCount(DELETE_CRAWL_JOB_OPER, params, 1);
        this.jobHandler.deleteJob((String) params[0]);
        return null;
    }

    if (operationName.equals(ADD_CRAWL_JOB_BASEDON_OPER)) {
        JmxUtils.checkParamsCount(ADD_CRAWL_JOB_BASEDON_OPER, params, 4);
        return addCrawlJobBasedOn((String) params[0], (String) params[1],
                checkForEmptyPlaceHolder((String) params[2]), checkForEmptyPlaceHolder((String) params[3]));
    }
    if (operationName.equals(ALERT_OPER)) {
        JmxUtils.checkParamsCount(ALERT_OPER, params, 1);
        SinkHandlerLogRecord slr = null;
        if (this.alertManager.getCount() > 0) {
            // This is creating a vector of all alerts just so I can then
            // use passed index into resultant vector -- needs to be
            // improved.
            slr = (SinkHandlerLogRecord) this.alertManager.getAll().get(((Integer) params[0]).intValue());
        }
        return (slr != null) ? slr.toString() : null;
    }

    if (operationName.equals(PENDING_JOBS_OPER)) {
        JmxUtils.checkParamsCount(PENDING_JOBS_OPER, params, 0);
        try {
            return makeJobsTabularData(getJobHandler().getPendingJobs());
        } catch (OpenDataException e) {
            throw new RuntimeOperationsException(new RuntimeException(e));
        }
    }

    if (operationName.equals(COMPLETED_JOBS_OPER)) {
        JmxUtils.checkParamsCount(COMPLETED_JOBS_OPER, params, 0);
        try {
            return makeJobsTabularData(getJobHandler().getCompletedJobs());
        } catch (OpenDataException e) {
            throw new RuntimeOperationsException(new RuntimeException(e));
        }
    }

    if (operationName.equals(CRAWLEND_REPORT_OPER)) {
        JmxUtils.checkParamsCount(CRAWLEND_REPORT_OPER, params, 2);
        try {
            return getCrawlendReport((String) params[0], (String) params[1]);
        } catch (IOException e) {
            throw new RuntimeOperationsException(new RuntimeException(e));
        }
    }

    throw new ReflectionException(new NoSuchMethodException(operationName),
            "Cannot find the operation " + operationName);
}