Example usage for javax.management MBeanServerInvocationHandler newProxyInstance

List of usage examples for javax.management MBeanServerInvocationHandler newProxyInstance

Introduction

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

Prototype

public static <T> T newProxyInstance(MBeanServerConnection connection, ObjectName objectName,
        Class<T> interfaceClass, boolean notificationBroadcaster) 

Source Link

Document

Return a proxy that implements the given interface by forwarding its methods through the given MBean server to the named MBean.

Usage

From source file:org.rhq.plugins.jslee.ExecutorDiscoveryComponent.java

public Set<DiscoveredResourceDetails> discoverResources(
        ResourceDiscoveryContext<JainSleeServerComponent> context)
        throws InvalidPluginConfigurationException, Exception {
    if (log.isDebugEnabled()) {
        log.debug("ExecutorDiscoveryComponent.discoverResources() called");
    }//from w  w w  .j  a va2s  .c  o m

    Set<DiscoveredResourceDetails> discoveredExecutors = new HashSet<DiscoveredResourceDetails>();

    MBeanServerUtils mbeanUtils = context.getParentResourceComponent().getMBeanServerUtils();

    try {
        MBeanServerConnection connection = mbeanUtils.getConnection();
        mbeanUtils.login();

        ObjectName erConfigObjectName = new ObjectName(
                "org.mobicents.slee:name=EventRouterConfiguration"/* FIXME */);

        EventRouterConfigurationMBean erConfigMBean = (EventRouterConfigurationMBean) MBeanServerInvocationHandler
                .newProxyInstance(connection, erConfigObjectName, EventRouterConfigurationMBean.class, false);

        for (int executorId = 0; executorId < erConfigMBean.getEventRouterThreads(); executorId++) {
            String key = "Executor #" + executorId;

            String name = key;
            String description = "Mobicents JAIN SLEE Event Router " + name;

            DiscoveredResourceDetails discoveredExecutor = new DiscoveredResourceDetails(
                    context.getResourceType(), key, name, null, description, null, null);

            discoveredExecutor.getPluginConfiguration().put(new PropertySimple("executorId", executorId));
            discoveredExecutors.add(discoveredExecutor);

        }

        return discoveredExecutors;
    } finally {
        try {
            mbeanUtils.logout();
        } catch (LoginException e) {
            if (log.isDebugEnabled()) {
                log.debug("Failed to logout from secured JMX", e);
            }
        }
    }
}

From source file:org.rhq.plugins.jslee.JainSleeServerComponent.java

private OperationResult doManageCongestionControl(int op, Configuration parameters) throws Exception {
    OperationResult result = new OperationResult();

    MBeanServerUtils mbeanUtils = getMBeanServerUtils();
    try {//w  w  w  .  j ava2s  .  co m
        MBeanServerConnection connection = mbeanUtils.getConnection();
        mbeanUtils.login();

        ObjectName ccConfigObjectName = new ObjectName(
                "org.mobicents.slee:name=CongestionControlConfiguration");
        CongestionControlConfigurationMBean ccConfigMBean = MBeanServerInvocationHandler.newProxyInstance(
                connection, ccConfigObjectName, CongestionControlConfigurationMBean.class, false);

        switch (op) {
        case 0: // ccSetMemOn
            int oldValue = ccConfigMBean.getMinFreeMemoryToTurnOn();
            int newValue = parameters.getSimple("value").getIntegerValue();
            if (oldValue != newValue) {
                ccConfigMBean.setMinFreeMemoryToTurnOn(newValue);
                result.getComplexResults().put(
                        new PropertySimple("result", "Operation completed successfully (value changed from '"
                                + oldValue + "' to '" + newValue + "')."));
            } else {
                result.getComplexResults().put(new PropertySimple("result",
                        "Operation completed successfully (value wasn't changed, it was equal to the current)."));
            }
            break;

        case 1: // ccSetMemOff
            oldValue = ccConfigMBean.getMinFreeMemoryToTurnOff();
            newValue = parameters.getSimple("value").getIntegerValue();
            if (oldValue != newValue) {
                ccConfigMBean.setMinFreeMemoryToTurnOff(newValue);
                result.getComplexResults().put(
                        new PropertySimple("result", "Operation completed successfully (value changed from '"
                                + oldValue + "' to '" + newValue + "')."));
            } else {
                result.getComplexResults().put(new PropertySimple("result",
                        "Operation completed successfully (value wasn't changed, it was equal to the current)."));
            }
            break;

        case 2: // ccSetCheckPeriod
            oldValue = ccConfigMBean.getPeriodBetweenChecks();
            newValue = parameters.getSimple("value").getIntegerValue();
            if (oldValue != newValue) {
                ccConfigMBean.setPeriodBetweenChecks(newValue);
                result.getComplexResults().put(
                        new PropertySimple("result", "Operation completed successfully (value changed from '"
                                + oldValue + "' to '" + newValue + "')."));
            } else {
                result.getComplexResults().put(new PropertySimple("result",
                        "Operation completed successfully (value wasn't changed, it was equal to the current)."));
            }
            break;

        case 3: // ccSetRefuseStartActivity
            boolean oldValueBool = ccConfigMBean.isRefuseStartActivity();
            boolean newValueBool = parameters.getSimple("value").getBooleanValue();
            if (oldValueBool != newValueBool) {
                ccConfigMBean.setRefuseStartActivity(newValueBool);
                result.getComplexResults().put(
                        new PropertySimple("result", "Operation completed successfully (value changed from '"
                                + oldValueBool + "' to '" + newValueBool + "')."));
            } else {
                result.getComplexResults().put(new PropertySimple("result",
                        "Operation completed successfully (value wasn't changed, it was equal to the current)."));
            }
            break;

        case 4: // ccSetRefuseFireEvent
            oldValueBool = ccConfigMBean.isRefuseFireEvent();
            newValueBool = parameters.getSimple("value").getBooleanValue();
            if (oldValueBool != newValueBool) {
                ccConfigMBean.setRefuseFireEvent(newValueBool);
                result.getComplexResults().put(
                        new PropertySimple("result", "Operation completed successfully (value changed from '"
                                + oldValueBool + "' to '" + newValueBool + "')."));
            } else {
                result.getComplexResults().put(new PropertySimple("result",
                        "Operation completed successfully (value wasn't changed, it was equal to the current)."));
            }
            break;

        default:
            result.setErrorMessage("Unknown operation in Congestion Control Management (" + op + ")");
            break;
        }
    } finally {
        try {
            mbeanUtils.logout();
        } catch (LoginException e) {
            if (log.isDebugEnabled()) {
                log.debug("Failed to logout from secured JMX", e);
            }
        }
    }

    return result;
}

From source file:org.rhq.plugins.jslee.JainSleeServerComponent.java

private void doGetERSEventTypes(MBeanServerConnection connection, PropertyList statistics) throws Exception {
    ObjectName deploymentObjectName = new ObjectName(DeploymentMBean.OBJECT_NAME);
    DeploymentMBean deploymentMBean = (DeploymentMBean) MBeanServerInvocationHandler
            .newProxyInstance(connection, deploymentObjectName, DeploymentMBean.class, false);

    EventTypeID[] eventTypes = deploymentMBean.getEventTypes();

    EventRouterStatisticsMBean erStatsMBean = getEventRouterStatisticsMBean();

    for (EventTypeID eventType : eventTypes) {
        Long averageEventRoutingTime = erStatsMBean.getAverageEventRoutingTime(eventType);
        Long eventsRouted = erStatsMBean.getEventsRouted(eventType);

        PropertyMap query = new PropertyMap("EventRouterStatistics",
                new PropertySimple("id", eventType.toString()),
                new PropertySimple("averageEventRoutingTime", averageEventRoutingTime),
                new PropertySimple("eventsRouted", eventsRouted));

        statistics.add(query);/*from ww w  .jav  a2  s.  com*/
    }
}

From source file:org.rhq.plugins.jslee.JainSleeServerComponent.java

private void doGetERSCombined(MBeanServerConnection connection, PropertyList statistics) throws Exception {
    EventRouterConfigurationMBean erConfigMBean = getEventRouterConfigurationMBean();

    int numExecutors = erConfigMBean.getEventRouterThreads();

    ObjectName deploymentObjectName = new ObjectName(DeploymentMBean.OBJECT_NAME);
    DeploymentMBean deploymentMBean = (DeploymentMBean) MBeanServerInvocationHandler
            .newProxyInstance(connection, deploymentObjectName, DeploymentMBean.class, false);

    EventTypeID[] eventTypes = deploymentMBean.getEventTypes();

    EventRouterStatisticsMBean erStatsMBean = getEventRouterStatisticsMBean();

    for (EventTypeID eventType : eventTypes) {
        for (int executorId = 0; executorId < numExecutors; executorId++) {
            Long averageEventRoutingTime = erStatsMBean.getAverageEventRoutingTime(executorId, eventType);
            Long eventsRouted = erStatsMBean.getEventsRouted(executorId, eventType);
            Long routingTime = erStatsMBean.getRoutingTime(executorId, eventType);

            PropertyMap query = new PropertyMap("EventRouterStatistics",
                    new PropertySimple("id", eventType.toString() + "@Executor #" + executorId),
                    new PropertySimple("averageEventRoutingTime", averageEventRoutingTime),
                    new PropertySimple("routingTime", routingTime),
                    new PropertySimple("eventsRouted", eventsRouted));

            statistics.add(query);// w w  w.j  a  v  a 2  s  .  c  o  m
        }
    }
}

From source file:org.rhq.plugins.jslee.JainSleeServerComponent.java

private void createContentBasedResource(CreateResourceReport createResourceReport, ResourceType resourceType) {
    if (log.isTraceEnabled()) {
        log.trace("createContentBasedResource(" + createResourceReport + ", " + resourceType + ")");
    }// ww w .  j  av a2 s  .  com

    try {
        OutputStream os = null;

        ResourcePackageDetails details = createResourceReport.getPackageDetails();
        PackageDetailsKey key = details.getKey();
        String archiveName = key.getName();

        if (log.isDebugEnabled()) {
            log.debug("createContentBasedResource: archiveName = " + archiveName);
        }

        Configuration deployTimeConfig = createResourceReport.getPackageDetails()
                .getDeploymentTimeConfiguration();

        boolean persistentDeploy = deployTimeConfig.getSimple("persistentDeploy").getBooleanValue();
        boolean deployFarmed = deployTimeConfig.getSimple("deployFarmed").getBooleanValue();
        if (log.isDebugEnabled()) {
            log.debug("createContentBasedResource: persistentDeploy = " + persistentDeploy + ", deployFarmed = "
                    + deployFarmed);
        }

        // Validate deploy options
        if (deployFarmed) {
            if (!persistentDeploy) {
                throw new IllegalArgumentException(
                        "Invalid options. If 'Deploy Farmed' is set to 'Yes', 'Persistent Deploy' should also be set to 'Yes'.");
            }
            if (!(new File(this.farmDeployFolder).exists())) {
                throw new IllegalArgumentException("Invalid options. 'Deploy Farmed' is set to 'Yes', but "
                        + farmDeployFolder + " does not exist.");
            }
        }

        // Validate file name
        if (!archiveName.toLowerCase().endsWith(".jar")) {
            createResourceReport.setStatus(CreateResourceStatus.FAILURE);
            createResourceReport.setErrorMessage("Deployed file must have a .jar extension");
            return;
        }

        Configuration pluginConfiguration = this.resourceContext.getPluginConfiguration();
        PropertySimple propSimple = pluginConfiguration
                .getSimple(ApplicationServerPluginConfigurationProperties.SERVER_TMP_DIR);
        String tmpPath = propSimple.getStringValue();

        File serverTmpFile = new File(tmpPath);
        File tempDir = null;

        tempDir = createTempDirectory("jopr-jainslee-deploy-content", null, serverTmpFile);

        if (log.isDebugEnabled()) {
            log.debug("createContentBasedResource: tmpFile = " + tempDir.getAbsolutePath());
        }

        File archiveFile = new File(key.getName());

        // this is to ensure that we only get the filename part no matter whether the key contains full path or not.
        File contentCopy = new File(tempDir, archiveFile.getName());

        os = new BufferedOutputStream(new FileOutputStream(contentCopy));
        ContentContext contentContext = resourceContext.getContentContext();
        ContentServices contentServices = contentContext.getContentServices();
        contentServices.downloadPackageBitsForChildResource(contentContext, resourceType.getName(), key, os);

        if (log.isDebugEnabled()) {
            log.debug("createContentBasedResource: contentCopy = " + contentCopy.getAbsolutePath());
        }

        ObjectName deploymentObjName = new ObjectName(DeploymentMBean.OBJECT_NAME);

        MBeanServerConnection connection = this.mBeanServerUtils.getConnection();
        this.mBeanServerUtils.login();

        DeploymentMBean deploymentMBean = (DeploymentMBean) MBeanServerInvocationHandler.newProxyInstance(
                connection, deploymentObjName, javax.slee.management.DeploymentMBean.class, false);

        String depKey = null;

        if (!persistentDeploy) {
            DeployableUnitID deployableUnitID = deploymentMBean.install(contentCopy.toURI().toURL().toString());
            if (log.isDebugEnabled()) {
                log.debug("createContentBasedResource: Deployed " + deployableUnitID);
            }

            depKey = deployableUnitID.getURL();
        } else {
            File destination = new File(
                    (deployFarmed ? this.farmDeployFolder : this.deployFolder) + File.separator + archiveName);
            copyFile(contentCopy, destination);

            // Since toURL is deprecated
            depKey = destination.toURI().toURL().toString();
        }

        String[] elements = depKey.split(System.getProperty("file.separator").replaceAll("\\\\", "\\\\\\\\"));
        String lastElement = elements[(elements.length - 1)];
        String name = lastElement.substring(0, lastElement.lastIndexOf("."));

        createResourceReport.setResourceName(name);
        createResourceReport.setResourceKey(depKey);
        createResourceReport.setStatus(CreateResourceStatus.SUCCESS);
    } catch (Exception e) {
        createResourceReport.setStatus(CreateResourceStatus.FAILURE);
        createResourceReport.setErrorMessage(e.getMessage());
        createResourceReport.setException(e);
        return;
    } finally {
        try {
            this.mBeanServerUtils.logout();
        } catch (LoginException e) {
            if (log.isDebugEnabled()) {
                log.debug("Failed to logout from secured JMX", e);
            }
        }
    }
}

From source file:org.rhq.plugins.jslee.JainSleeServerComponent.java

private OperationResult doSleeState(Configuration parameters) throws Exception {
    try {/*from  w  ww . ja v  a 2  s. co m*/
        OperationResult result = new OperationResult();

        String message = null;
        String action = parameters.getSimple("action").getStringValue();
        ObjectName sleemanagement = new ObjectName(SleeManagementMBean.OBJECT_NAME);

        MBeanServerConnection connection = this.mBeanServerUtils.getConnection();
        this.mBeanServerUtils.login();

        SleeManagementMBean sleeManagementMBean = (SleeManagementMBean) MBeanServerInvocationHandler
                .newProxyInstance(connection, sleemanagement, javax.slee.management.SleeManagementMBean.class,
                        false);

        if ("start".equals(action)) {
            sleeManagementMBean.start();
            message = "Successfully started Mobicents JAIN SLEE Server";
        } else if ("stop".equals(action)) {
            sleeManagementMBean.stop();
            message = "Successfully stopped Mobicents JAIN SLEE Server";
        } else if ("shutdown".equals(action)) {
            sleeManagementMBean.shutdown();
            message = "Successfully shutdown Mobicents JAIN SLEE Server";
        }
        result.getComplexResults().put(new PropertySimple("result", message));

        return result;
    } finally {
        try {
            this.mBeanServerUtils.logout();
        } catch (LoginException e) {
            if (log.isDebugEnabled()) {
                log.debug("Failed to logout from secured JMX", e);
            }
        }
    }
}

From source file:org.rhq.plugins.jslee.JainSleeServerComponent.java

private OperationResult doQueryActivityContextLiveness(Configuration parameters) throws Exception {
    try {/*www.  j a v  a 2  s .  co  m*/
        OperationResult result = new OperationResult();

        MBeanServerConnection connection = this.mBeanServerUtils.getConnection();
        this.mBeanServerUtils.login();

        ObjectName actMana = new ObjectName("org.mobicents.slee:name=ActivityManagementMBean");

        ActivityManagementMBeanImplMBean aciManagMBean = (ActivityManagementMBeanImplMBean) MBeanServerInvocationHandler
                .newProxyInstance(connection, actMana,
                        org.mobicents.slee.container.management.jmx.ActivityManagementMBeanImplMBean.class,
                        false);

        aciManagMBean.queryActivityContextLiveness();

        result.getComplexResults()
                .put(new PropertySimple("result", "Activity Context Liveness queried successfully."));

        return result;
    } finally {
        try {
            this.mBeanServerUtils.logout();
        } catch (LoginException e) {
            if (log.isDebugEnabled()) {
                log.debug("Failed to logout from secured JMX", e);
            }
        }
    }
}

From source file:org.rhq.plugins.jslee.JainSleeServerComponent.java

private OperationResult doSwitchLoggingConfiguration(Configuration parameters) throws Exception {
    try {/*from  w w w . ja  v  a2s  .c o  m*/
        OperationResult result = new OperationResult();

        MBeanServerConnection connection = this.mBeanServerUtils.getConnection();
        this.mBeanServerUtils.login();

        ObjectName actMana = new ObjectName("org.mobicents.slee:service=MobicentsManagement" /* FIXME */);

        MobicentsManagementMBean mobicentsManagementMBean = (MobicentsManagementMBean) MBeanServerInvocationHandler
                .newProxyInstance(connection, actMana,
                        org.mobicents.slee.container.management.jmx.MobicentsManagementMBean.class, false);

        String profile = parameters.getSimple("profile").getStringValue();

        mobicentsManagementMBean.switchLoggingConfiguration(profile);

        result.getComplexResults().put(new PropertySimple("result",
                "Log4j Configuration Profile successfully changed to " + profile + "."));
        return result;
    } finally {
        try {
            this.mBeanServerUtils.logout();
        } catch (LoginException e) {
            if (log.isDebugEnabled()) {
                log.debug("Failed to logout from secured JMX", e);
            }
        }
    }
}

From source file:org.rhq.plugins.jslee.JainSleeServerComponent.java

private OperationResult doGetLoggingConfiguration(Configuration parameters) throws Exception {
    try {//from w ww .j  a va  2  s  . c om
        OperationResult result = new OperationResult();

        MBeanServerConnection connection = this.mBeanServerUtils.getConnection();
        this.mBeanServerUtils.login();

        ObjectName actMana = new ObjectName("org.mobicents.slee:service=MobicentsManagement" /* FIXME */);

        MobicentsManagementMBean mobicentsManagementMBean = (MobicentsManagementMBean) MBeanServerInvocationHandler
                .newProxyInstance(connection, actMana,
                        org.mobicents.slee.container.management.jmx.MobicentsManagementMBean.class, false);

        String profile = parameters.getSimple("profile").getStringValue();

        result.getComplexResults()
                .put(new PropertySimple("result", mobicentsManagementMBean.getLoggingConfiguration(profile)));
        return result;
    } finally {
        try {
            this.mBeanServerUtils.logout();
        } catch (LoginException e) {
            if (log.isDebugEnabled()) {
                log.debug("Failed to logout from secured JMX", e);
            }
        }
    }
}

From source file:org.rhq.plugins.jslee.JainSleeServerComponent.java

private OperationResult doSetLoggingConfiguration(Configuration parameters) throws Exception {
    try {/*from w  w w. j  a v  a 2s . c  o  m*/
        OperationResult result = new OperationResult();

        MBeanServerConnection connection = this.mBeanServerUtils.getConnection();
        this.mBeanServerUtils.login();

        ObjectName actMana = new ObjectName("org.mobicents.slee:service=MobicentsManagement" /* FIXME */);

        MobicentsManagementMBean mobicentsManagementMBean = (MobicentsManagementMBean) MBeanServerInvocationHandler
                .newProxyInstance(connection, actMana,
                        org.mobicents.slee.container.management.jmx.MobicentsManagementMBean.class, false);

        String profile = parameters.getSimple("profile").getStringValue().toLowerCase();
        String contents = parameters.getSimple("contents").getStringValue();

        mobicentsManagementMBean.setLoggingConfiguration(profile, contents);

        result.getComplexResults()
                .put(new PropertySimple("result", "Log4j " + profile + " Configuration successfully updated."));
        return result;
    } finally {
        try {
            this.mBeanServerUtils.logout();
        } catch (LoginException e) {
            if (log.isDebugEnabled()) {
                log.debug("Failed to logout from secured JMX", e);
            }
        }
    }
}