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.SbbComponent.java

public AvailabilityType getAvailability() {
    if (log.isTraceEnabled()) {
        log.trace("getAvailability() called.");
    }//  w  ww  .  java2 s .co m

    this.isUp = false;

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

        DeploymentMBean depMBean = (DeploymentMBean) MBeanServerInvocationHandler.newProxyInstance(connection,
                deploymentMBeanObj, DeploymentMBean.class, false);

        for (SbbID activeSbbId : depMBean.getSbbs()) {
            if (activeSbbId.equals(sbbId)) {
                this.isUp = true;
            }
        }
    } catch (Exception e) {
        log.error("getAvailability failed for SbbID = " + this.sbbId);
    } finally {
        try {
            this.mbeanUtils.logout();
        } catch (LoginException e) {
            if (log.isDebugEnabled()) {
                log.debug("Failed to logout from secured JMX", e);
            }
        }
    }

    return this.isUp ? AvailabilityType.UP : AvailabilityType.DOWN;
}

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

public Set<DiscoveredResourceDetails> discoverResources(
        ResourceDiscoveryContext<JainSleeServerComponent> context)
        throws InvalidPluginConfigurationException, Exception {
    if (log.isTraceEnabled()) {
        log.trace("discoverResources() called");
    }//from   w  w w  . j  a v  a  2  s. c om

    // Get the connection up and ready
    MBeanServerUtils mbeanUtils = context.getParentResourceComponent().getMBeanServerUtils();
    try {
        MBeanServerConnection connection = mbeanUtils.getConnection();
        mbeanUtils.login();

        ObjectName depMBeanObj = new ObjectName(DeploymentMBean.OBJECT_NAME);

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

        DeploymentMBean depMBean = (DeploymentMBean) MBeanServerInvocationHandler.newProxyInstance(connection,
                depMBeanObj, DeploymentMBean.class, false);

        SbbID[] sbbIds = depMBean.getSbbs();

        addSbb(sbbIds, discoveredSBBs, context.getResourceType());

        if (log.isInfoEnabled()) {
            log.info("Discovered " + discoveredSBBs.size() + " JAIN SLEE SBB Components.");
        }
        return discoveredSBBs;
    } 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.ServiceComponent.java

private OperationResult doChangeServiceState(Configuration parameters) throws Exception {
    try {/*from  w  ww. ja va2 s. c  o m*/
        String message = null;
        String action = parameters.getSimple("action").getStringValue();

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

        ServiceManagementMBean serviceManagementMBean = (ServiceManagementMBean) MBeanServerInvocationHandler
                .newProxyInstance(connection, this.servicemanagement,
                        javax.slee.management.ServiceManagementMBean.class, false);
        if ("activate".equals(action)) {
            serviceManagementMBean.activate(this.serviceId);
            message = "Successfully Activated Service " + this.serviceId;
            this.serviceState = ServiceState.ACTIVE;
        } else if ("deactivate".equals(action)) {
            serviceManagementMBean.deactivate(this.serviceId);
            message = "Successfully Deactivated Service " + this.serviceId;
            this.serviceState = ServiceState.INACTIVE;
        }

        OperationResult result = new OperationResult();
        result.getComplexResults().put(new PropertySimple("result", message));
        return result;
    } finally {
        try {
            this.mbeanUtils.logout();
        } catch (LoginException e) {
            if (log.isDebugEnabled()) {
                log.debug("Failed to logout from secured JMX", e);
            }
        }
    }
}

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

/**
 * Obtain all SBB Entities and filter the ones which service id is the same as this.
 * /* www .ja v  a2 s  .co m*/
 * @return an ArrayList of SbbEntity
 * @throws Exception
 */
private ArrayList<Object[]> getServiceSbbEntities() throws Exception {
    try {
        MBeanServerConnection connection = this.mbeanUtils.getConnection();
        this.mbeanUtils.login();

        ObjectName sbbEntitiesMBeanObj = new ObjectName("org.mobicents.slee:name=SbbEntitiesMBean");
        SbbEntitiesMBeanImplMBean sbbEntititesMBean = (SbbEntitiesMBeanImplMBean) MBeanServerInvocationHandler
                .newProxyInstance(connection, sbbEntitiesMBeanObj, SbbEntitiesMBeanImplMBean.class, false);
        Object[] objs = sbbEntititesMBean.retrieveAllSbbEntities();

        ArrayList<Object[]> list = new ArrayList<Object[]>();
        for (Object obj : objs) {
            Object[] sbbEntity = (Object[]) obj;
            if (sbbEntity[7] != null && sbbEntity[7].equals(this.serviceId)) {
                list.add(sbbEntity);
            }
        }

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

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

public Set<DiscoveredResourceDetails> discoverResources(ResourceDiscoveryContext<ServiceComponent> context)
        throws InvalidPluginConfigurationException, Exception {
    if (log.isTraceEnabled()) {
        log.trace("discoverResources() called");
    }// w  ww  . j  a  v  a 2  s.co  m

    // Get the connection up and ready
    MBeanServerUtils mbeanUtils = context.getParentResourceComponent().getMBeanServerUtils();
    try {
        MBeanServerConnection connection = mbeanUtils.getConnection();
        mbeanUtils.login();

        ObjectName depMBeanObj = new ObjectName(DeploymentMBean.OBJECT_NAME);

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

        DeploymentMBean depMBean = (DeploymentMBean) MBeanServerInvocationHandler.newProxyInstance(connection,
                depMBeanObj, DeploymentMBean.class, false);

        if (serviceId == null) {
            serviceId = context.getParentResourceComponent().getServiceID();
        }

        SbbID[] sbbIds = depMBean.getSbbs(serviceId);

        addSbb(sbbIds, discoveredSBBs, context.getResourceType());

        if (log.isInfoEnabled()) {
            log.info(
                    "Discovered " + discoveredSBBs.size() + " JAIN SLEE SBB Components for " + serviceId + ".");
        }
        return discoveredSBBs;
    } 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.ServiceSbbUsageParameterSetComponent.java

@Override
public Configuration loadResourceConfiguration() throws Exception {
    try {//from  w w  w .  j  a v a2  s . c  o m
        Configuration config = new Configuration();

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

        // As an example, if a particular service has the name FooService, vendor FooCompany, and version 1.0,
        // then the Object Name of a ServiceUsageMBean for that service would be:
        // javax.slee.management.usage:type=ServiceUsage,serviceName="FooService", serviceVendor="FooCompany",serviceVersion="1.0"
        ObjectName serviceUsageON = new ObjectName(ServiceUsageMBean.BASE_OBJECT_NAME + ','
                + ServiceUsageMBean.SERVICE_NAME_KEY + '=' + ObjectName.quote(serviceId.getName()) + ','
                + ServiceUsageMBean.SERVICE_VENDOR_KEY + '=' + ObjectName.quote(serviceId.getVendor()) + ','
                + ServiceUsageMBean.SERVICE_VERSION_KEY + '=' + ObjectName.quote(serviceId.getVersion()));

        ServiceUsageMBean serviceUsageMBean = (ServiceUsageMBean) MBeanServerInvocationHandler.newProxyInstance(
                connection, serviceUsageON, javax.slee.management.ServiceUsageMBean.class, false);

        PropertyList columnList = new PropertyList("usageParameter");
        ObjectName sbbUsageON = null;
        if (usageParameterSetName != null && !usageParameterSetName.equals("<default>")) {
            sbbUsageON = serviceUsageMBean.getSbbUsageMBean(sbbId, usageParameterSetName);
        } else {
            sbbUsageON = serviceUsageMBean.getSbbUsageMBean(sbbId);
        }

        MBeanInfo usageInfo = connection.getMBeanInfo(sbbUsageON);

        for (MBeanOperationInfo operation : usageInfo.getOperations()) {
            String opName = operation.getName();
            if (opName.startsWith("get")) {
                PropertyMap col = new PropertyMap("usageParameterDefinition");

                col.put(new PropertySimple("usageParameterName", opName.replaceFirst("get", "")));

                boolean isSampleType = operation.getReturnType().equals("javax.slee.usage.SampleStatistics");
                col.put(new PropertySimple("usageParameterType", isSampleType ? "Sample" : "Counter"));

                Object value = connection.invoke(sbbUsageON, opName, new Object[] { false },
                        new String[] { "boolean" });
                col.put(new PropertySimple("usageParameterValue", value));

                columnList.add(col);
            }
        }
        config.put(columnList);

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

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

@Override
public void updateResourceConfiguration(ConfigurationUpdateReport configurationUpdateReport) {
    try {//from  www.  java2s  .co m
        MBeanServerConnection connection = this.mbeanUtils.getConnection();
        this.mbeanUtils.login();

        // As an example, if a particular service has the name FooService, vendor FooCompany, and version 1.0,
        // then the Object Name of a ServiceUsageMBean for that service would be:
        // javax.slee.management.usage:type=ServiceUsage,serviceName="FooService", serviceVendor="FooCompany",serviceVersion="1.0"
        ObjectName serviceUsageON = new ObjectName(ServiceUsageMBean.BASE_OBJECT_NAME + ','
                + ServiceUsageMBean.SERVICE_NAME_KEY + '=' + ObjectName.quote(serviceId.getName()) + ','
                + ServiceUsageMBean.SERVICE_VENDOR_KEY + '=' + ObjectName.quote(serviceId.getVendor()) + ','
                + ServiceUsageMBean.SERVICE_VERSION_KEY + '=' + ObjectName.quote(serviceId.getVersion()));

        ServiceUsageMBean serviceUsageMBean = (ServiceUsageMBean) MBeanServerInvocationHandler.newProxyInstance(
                connection, serviceUsageON, javax.slee.management.ServiceUsageMBean.class, false);

        PropertyList columnList = configurationUpdateReport.getConfiguration().getList("usageParameter");
        ObjectName sbbUsageON = serviceUsageMBean.getSbbUsageMBean(sbbId);

        for (Property p : columnList.getList()) {
            PropertyMap pMap = (PropertyMap) p;
            String usageParamName = ((PropertySimple) pMap.get("usageParameterName")).getStringValue();
            Object curValue = pMap.get("usageParameterValue");

            Object newValue = connection.invoke(sbbUsageON, "get" + usageParamName, new Object[] { false },
                    new String[] { "boolean" });
            if (newValue != null && !newValue.equals(curValue)) {
                if (log.isDebugEnabled()) {
                    log.debug("Changing Usage Parameter '" + usageParamName + "' from value [" + curValue
                            + "] to [" + newValue + "].");
                }
            }
        }
        configurationUpdateReport.setStatus(ConfigurationUpdateStatus.SUCCESS);
    } catch (Exception e) {
        log.error("Failed to update Resource Configuration.", e);
        configurationUpdateReport.setErrorMessageFromThrowable(e);
        configurationUpdateReport.setStatus(ConfigurationUpdateStatus.FAILURE);
    } finally {
        try {
            this.mbeanUtils.logout();
        } catch (LoginException e) {
            if (log.isDebugEnabled()) {
                log.debug("Failed to logout from secured JMX", e);
            }
        }
    }
}

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

public Set<DiscoveredResourceDetails> discoverResources(ResourceDiscoveryContext<ServiceSbbComponent> context)
        throws InvalidPluginConfigurationException, Exception {
    MBeanServerUtils mbeanUtils = context.getParentResourceComponent().getMBeanServerUtils();
    try {/*from w w  w .  j  av  a2 s.  c o m*/
        if (log.isDebugEnabled()) {
            log.debug("RAEntityDiscoveryComponent.discoverResources() called");
        }
        Set<DiscoveredResourceDetails> discoveredUsageParameterSets = new HashSet<DiscoveredResourceDetails>();

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

        ServiceID serviceId = context.getParentResourceComponent().getServiceID();

        // As an example, if a particular service has the name FooService, vendor FooCompany, and version 1.0,
        // then the Object Name of a ServiceUsageMBean for that service would be:
        // javax.slee.management.usage:type=ServiceUsage,serviceName="FooService", serviceVendor="FooCompany",serviceVersion="1.0"
        ObjectName serviceUsageON = new ObjectName(ServiceUsageMBean.BASE_OBJECT_NAME + ','
                + ServiceUsageMBean.SERVICE_NAME_KEY + '=' + ObjectName.quote(serviceId.getName()) + ','
                + ServiceUsageMBean.SERVICE_VENDOR_KEY + '=' + ObjectName.quote(serviceId.getVendor()) + ','
                + ServiceUsageMBean.SERVICE_VERSION_KEY + '=' + ObjectName.quote(serviceId.getVersion()));

        ServiceUsageMBean serviceUsageMBean = (ServiceUsageMBean) MBeanServerInvocationHandler.newProxyInstance(
                connection, serviceUsageON, javax.slee.management.ServiceUsageMBean.class, false);

        SbbID sbbId = context.getParentResourceComponent().getSbbID();
        String[] usageParameterSets = serviceUsageMBean.getUsageParameterSets(sbbId);

        // This is default one
        String defaultDescription = "Usage Parameter Set : " + "<default>" + " for " + sbbId + " @"
                + serviceId;
        String defaultKey = "<default>" + "@" + sbbId + "@" + serviceId;

        DiscoveredResourceDetails defaultDiscoveredEntity = new DiscoveredResourceDetails(
                context.getResourceType(), defaultKey, "<default>" + " Usage Paramaters Set",
                sbbId.getVersion(), defaultDescription, null, null);
        defaultDiscoveredEntity.getPluginConfiguration()
                .put(new PropertySimple("usageParameterSet", "<default>"));
        defaultDiscoveredEntity.getPluginConfiguration().put(new PropertySimple("name", sbbId.getName()));
        defaultDiscoveredEntity.getPluginConfiguration().put(new PropertySimple("version", sbbId.getVersion()));
        defaultDiscoveredEntity.getPluginConfiguration().put(new PropertySimple("vendor", sbbId.getVendor()));
        discoveredUsageParameterSets.add(defaultDiscoveredEntity);

        for (String usageParameterSet : usageParameterSets) {
            String description = "Usage Parameter Set : " + usageParameterSet + " for " + sbbId + " @"
                    + serviceId;
            String key = usageParameterSet + "@" + sbbId + "@" + serviceId;

            DiscoveredResourceDetails discoveredEntity = new DiscoveredResourceDetails(
                    context.getResourceType(), key, usageParameterSet + " Usage Paramaters Set",
                    sbbId.getVersion(), description, null, null);
            discoveredEntity.getPluginConfiguration()
                    .put(new PropertySimple("usageParameterSet", usageParameterSet));
            discoveredEntity.getPluginConfiguration().put(new PropertySimple("name", sbbId.getName()));
            discoveredEntity.getPluginConfiguration().put(new PropertySimple("version", sbbId.getVersion()));
            discoveredEntity.getPluginConfiguration().put(new PropertySimple("vendor", sbbId.getVendor()));
            discoveredUsageParameterSets.add(discoveredEntity);
        }

        if (log.isInfoEnabled()) {
            log.info("Discovered " + discoveredUsageParameterSets.size()
                    + " JAIN SLEE Usage Parameter Sets for " + sbbId + " @" + serviceId + ".");
        }
        return discoveredUsageParameterSets;
    } finally {
        try {
            mbeanUtils.logout();
        } catch (LoginException e) {
            if (log.isDebugEnabled()) {
                log.debug("Failed to logout from secured JMX", e);
            }
        }
    }
}

From source file:org.springside.modules.jmx.JmxClientTemplate.java

/**
 * MBean?.//from w ww  . ja  va2s  . c om
 */
public <T> T createMBeanProxy(final String mbeanName, final Class<T> mBeanInterface) {
    Assert.hasText(mbeanName, "mbeanName?");
    assertConnected();

    ObjectName objectName = buildObjectName(mbeanName);
    return (T) MBeanServerInvocationHandler.newProxyInstance(connection, objectName, mBeanInterface, false);
}

From source file:org.wso2.andes.test.utils.JMXTestUtils.java

public ManagedExchange getManagedExchange(String exchangeName) {
    ObjectName objectName = getExchangeObjectName("test", exchangeName);
    return MBeanServerInvocationHandler.newProxyInstance(_mbsc, objectName, ManagedExchange.class, false);
}