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.enterprise.server.core.StartupBean.java

private PluginDeploymentScannerMBean getPluginDeploymentScanner() {
    PluginDeploymentScannerMBean deployer;
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    ObjectName name = PluginDeploymentScannerMBean.OBJECT_NAME;
    Class<?> iface = PluginDeploymentScannerMBean.class;
    deployer = (PluginDeploymentScannerMBean) MBeanServerInvocationHandler.newProxyInstance(mbs, name, iface,
            false);/*  w w w  . j  a  v a  2s . c  om*/
    return deployer;
}

From source file:org.rhq.enterprise.server.core.StartupBean.java

/**
 * Installs the JAAS login modules so our users can login.
 *
 * @throws RuntimeException// w  ww  . ja  v  a 2 s.  c  o m
 */
private void upgradeRhqUserSecurityDomainIfNeeded() throws RuntimeException {

    try {
        CustomJaasDeploymentServiceMBean jaas_mbean;
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        ObjectName name = CustomJaasDeploymentServiceMBean.OBJECT_NAME;
        Class<?> iface = CustomJaasDeploymentServiceMBean.class;
        jaas_mbean = (CustomJaasDeploymentServiceMBean) MBeanServerInvocationHandler.newProxyInstance(mbs, name,
                iface, false);
        jaas_mbean.upgradeRhqUserSecurityDomainIfNeeded();
    } catch (Exception e) {
        error += (error.isEmpty() ? "" : ", ") + "security domain upgrade";
        throw new RuntimeException("Cannot upgrade JAAS login modules!", e);
    }
}

From source file:org.rhq.enterprise.server.scheduler.SchedulerBean.java

/**
 * Returns a reference to a proxy to the scheduler service MBean, which itself is a proxy that delegates to the
 * Quartz scheduler.//from w ww  .  j av  a 2  s  .c o  m
 *
 * @return proxy to the scheduler service MBean
 *
 * @throws SchedulerException if failed to find the MBean
 */
private SchedulerServiceMBean getSchedulerService() throws SchedulerException {
    try {
        return (SchedulerServiceMBean) MBeanServerInvocationHandler.newProxyInstance(
                MBeanServerLocator.locateJBoss(), SCHEDULER_MBEAN_NAME, SchedulerServiceMBean.class, false);
    } catch (Exception e) {
        throw new SchedulerException("Failed to get a proxy to the scheduler service MBean", e);
    }
}

From source file:org.rhq.enterprise.server.system.SystemManagerBean.java

@RequiredPermission(Permission.MANAGE_SETTINGS)
public void reconfigureSystem(Subject whoami) {
    try {//from w w w. j  a  v  a  2 s . co m
        Object mbean;

        mbean = MBeanServerInvocationHandler.newProxyInstance(MBeanServerLocator.locateJBoss(),
                CustomJaasDeploymentServiceMBean.OBJECT_NAME, CustomJaasDeploymentServiceMBean.class, false);
        ((CustomJaasDeploymentServiceMBean) mbean).installJaasModules();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.rhq.enterprise.server.system.SystemManagerBean.java

/**
 * Ensures the installer is no longer deployed.
 *///  ww w  .  ja  v a 2 s. co m
public void undeployInstaller() {
    try {
        File serverHomeDir = LookupUtil.getCoreServer().getJBossServerHomeDir();

        File deployDirectory = new File(serverHomeDir, "deploy");

        if (deployDirectory.exists()) {
            File deployedInstallWar = new File(deployDirectory.getAbsolutePath(), "rhq-installer.war");
            File undeployedInstallWar = new File(deployDirectory.getAbsolutePath(), "rhq-installer.war.rej");

            if (deployedInstallWar.exists()) {
                // we need to undeploy it first - on windows the files are locked and can't be renamed until undeployed
                ObjectName name = ObjectNameFactory.create("jboss.system:service=MainDeployer");
                MBeanServerConnection mbs = MBeanServerLocator.locateJBoss();
                MainDeployerMBean mbean = (MainDeployerMBean) MBeanServerInvocationHandler.newProxyInstance(mbs,
                        name, MainDeployerMBean.class, false);
                URL url = deployedInstallWar.toURI().toURL();
                String urlString = url.toString().replace("%20", " "); // bug in undeployer doesn't like %20 - it wants a real space
                ((MainDeployerMBean) mbean).undeploy(urlString);
                if (((MainDeployerMBean) mbean).isDeployed(urlString) == false) {
                    log.info("Installer war has been hot-undeployed from memory");
                } else {
                    log.warn("Installer hot-undeploy failed - full installer undeploy may not work...");
                }

                if (!deployedInstallWar.renameTo(undeployedInstallWar)) {
                    throw new RuntimeException("Cannot undeploy the installer war: " + deployedInstallWar);
                }

                // I don't trust it - make sure we removed it
                if (deployedInstallWar.exists()) {
                    throw new RuntimeException("Failed to undeploy the installer war: " + deployedInstallWar);
                }

                // now that the installer is removed, put something in its place to avoid
                // getting tomcat errors and to at least point the user back to the GUI
                File deployedPostInstallWar = new File(deployDirectory.getAbsolutePath(),
                        "rhq-postinstaller.war");
                File undeployedPostInstallWar = new File(deployDirectory.getAbsolutePath(),
                        "rhq-postinstaller.war.rej");
                if (!deployedPostInstallWar.exists()) {
                    if (undeployedPostInstallWar.exists()) {
                        if (undeployedPostInstallWar.renameTo(deployedPostInstallWar)) {
                            log.debug("Post-install notification war has been deployed");
                        } else {
                            log.info("Post-install notification war failed to deploy - this can be ignored");
                        }
                    } else {
                        log.info(
                                "Post-install notification war not found and not deployed - this can be ignored");
                    }
                } else {
                    log.info("Post-install notification war already deployed");
                }
            } else if (undeployedInstallWar.exists()) {
                log.debug("Installer looks to be undeployed already, this is good: " + undeployedInstallWar);
            } else {
                log.debug("Installer can't be found - assume it has been completely purged: "
                        + deployedInstallWar);
            }
        } else {
            throw new RuntimeException(
                    "Your deployment seems corrupted - missing deploy dir: " + deployDirectory);
        }
    } catch (Exception e) {
        log.warn("Please manually remove installer war to secure your deployment: " + e);
        return;
    }

    // we only get here if we are SURE we removed it!
    log.info("Confirmed that the installer has been undeployed");
    return;
}

From source file:org.rhq.plugins.agent.AgentDiscoveryComponent.java

/**
 * Method that gets a proxy to the agent's management MBean interface.
 *
 * <p>This is package-scoped so other components can use this to connect to the agent's management interface.</p>
 *
 * @return agent's management MBean//from  w ww  . j a  v  a2  s.  co m
 *
 * @throws RuntimeException if failed to get the agent management MBean - this should really never occur since that
 *                          MBean should always been available whenever the agent is started
 */
static AgentManagementMBean getAgentManagementMBean() {
    try {
        AgentManagementMBean mbean;

        mbean = (AgentManagementMBean) MBeanServerInvocationHandler.newProxyInstance(
                getAgentManagementMBeanServer(), AgentManagement.singletonObjectName,
                AgentManagementMBean.class, false);

        return mbean;
    } catch (Exception e) {
        throw new RuntimeException("Cannot get the agent's management MBean", e);
    }
}

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

public AvailabilityType getAvailability() {
    if (log.isTraceEnabled()) {
        log.trace("getAvailability() called.");
    }//from  www.ja  v a  2 s . c  o m

    try {
        MBeanServerConnection connection = this.mbeanUtils.getConnection();
        mbeanUtils.login();
        DeploymentMBean deploymentMBean = (DeploymentMBean) MBeanServerInvocationHandler.newProxyInstance(
                connection, this.deploymentObjName, javax.slee.management.DeploymentMBean.class, false);

        DeployableUnitDescriptor deployableUnitDescriptor = deploymentMBean
                .getDescriptor(this.deployableUnitID);
        deployableUnitDescriptor.getDeploymentDate();
    } catch (Exception e) {
        log.error("getAvailability failed for DeployableUnitID = " + this.deployableUnitID);

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

    return AvailabilityType.UP;
}

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

public void deleteResource() throws Exception {
    if (deployableUnitID.getURL().contains(deployPathIdentifier.replaceAll("\\\\", "/"))) {
        if (!new File(new URL(deployableUnitID.getURL()).toURI()).delete()) {
            throw new IOException(
                    "File '" + deployableUnitID.getURL() + "' could not be deleted. Does it exists?");
        }//from   w  ww . j a  v  a2  s  .  c  o m
    } else if (deployableUnitID.getURL().contains(farmDeployPathIdentifier.replaceAll("\\\\", "/"))) {
        if (!new File(new URL(deployableUnitID.getURL()).toURI()).delete()) {
            throw new IOException(
                    "File '" + deployableUnitID.getURL() + "' could not be deleted. Does it exists?");
        }
    } else {
        try {
            MBeanServerConnection connection = this.mbeanUtils.getConnection();
            mbeanUtils.login();

            DeploymentMBean deploymentMBean = (DeploymentMBean) MBeanServerInvocationHandler.newProxyInstance(
                    connection, this.deploymentObjName, javax.slee.management.DeploymentMBean.class, false);
            deploymentMBean.uninstall(this.deployableUnitID);
        } 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.DeployableUnitComponent.java

private OperationResult doListComponents() throws Exception {
    try {//  w w w. jav  a2s  . c  om
        OperationResult result = new OperationResult();

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

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

        DeployableUnitDescriptor deployableUnitDescriptor = deploymentMBean
                .getDescriptor(this.deployableUnitID);

        ComponentID[] components = deployableUnitDescriptor.getComponents();

        // The pretty table we are building as result
        PropertyList columnList = new PropertyList("result");

        // Add the components
        for (ComponentID componentID : components) {
            PropertyMap col = new PropertyMap("element");

            col.put(new PropertySimple("Name", componentID.getName()));
            col.put(new PropertySimple("Vendor", componentID.getVendor()));
            col.put(new PropertySimple("Version", componentID.getVersion()));

            columnList.add(col);
        }

        result.getComplexResults().put(columnList);

        return result;
    } 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.DeployableUnitDiscoveryComponent.java

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

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

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

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

        ObjectName deploymentmanagement = new ObjectName(DeploymentMBean.OBJECT_NAME);

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

        DeployableUnitID[] deployableUnitIDs = deploymentMBean.getDeployableUnits();
        for (DeployableUnitID deployableUnitID : deployableUnitIDs) {
            DeployableUnitDescriptor deployableUnitDescriptor = deploymentMBean.getDescriptor(deployableUnitID);

            String key = deployableUnitID.getURL();

            // replace needed for Windows OS as \ needs to be escaped
            String[] elements = key.split(System.getProperty("file.separator").replaceAll("\\\\", "\\\\\\\\"));
            String lastElement = elements[(elements.length - 1)];
            String name = lastElement.substring(0, lastElement.lastIndexOf("."));

            String description = name + " -- Deployed on : " + deployableUnitDescriptor.getDeploymentDate();

            DiscoveredResourceDetails discoveredDu = new DiscoveredResourceDetails(context.getResourceType(),
                    key, name, null, description, null, null);
            discoveredDu.getPluginConfiguration().put(new PropertySimple("url", deployableUnitID.getURL()));

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

    if (log.isInfoEnabled()) {
        log.info("Discovered " + discoveredDUs.size() + " JAIN SLEE Deployable Unit Components.");
    }

    return discoveredDUs;
}