Java Code Examples for javax.management.MBeanServerFactory#findMBeanServer()

The following examples show how to use javax.management.MBeanServerFactory#findMBeanServer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private void testCreation(boolean referenceShouldExist, String failMsg) throws Exception {
	MBeanServerFactoryBean bean = new MBeanServerFactoryBean();
	bean.setRegisterWithFactory(referenceShouldExist);
	bean.afterPropertiesSet();

	try {
		MBeanServer server = bean.getObject();
		List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);

		boolean found = false;
		for (MBeanServer candidate : servers) {
			if (candidate == server) {
				found = true;
				break;
			}
		}

		if (!(found == referenceShouldExist)) {
			fail(failMsg);
		}
	}
	finally {
		bean.destroy();
	}
}
 
Example 2
Source Project: gemfirexd-oss   File: ReplicatedTree.java    License: Apache License 2.0 6 votes vote down vote up
public ReplicatedTree(String groupname, String props, long state_fetch_timeout, boolean jmx) throws Exception {
        if(groupname != null)
            this.groupname=groupname;
        if(props != null)
            this.props=props;
//        this.jmx=jmx; GemStoneAddition
        this.state_fetch_timeout=state_fetch_timeout;
        channel=new JChannel(this.props);
        channel.connect(this.groupname);
        if(jmx) {
            ArrayList servers=MBeanServerFactory.findMBeanServer(null);
            if(servers == null || servers.size() == 0) {
                throw new Exception("No MBeanServers found;" +
                                    "\nJmxTest needs to be run with an MBeanServer present, or inside JDK 5");
            }
//            MBeanServer server=(MBeanServer)servers.get(0); GemStoneAddition
//            JmxConfigurator.registerChannel(channel, server, "JGroups:channel=" + channel.getChannelName() , true);
        }
        start();
    }
 
Example 3
/**
 * Locate the MBean server to use based on user input from startup.
 *
 * @return The MBean server to use.
 */
private MBeanServer findServer() {
	if ( usePlatformServer ) {
		// they specified to use the platform (vm) server
		return ManagementFactory.getPlatformMBeanServer();
	}

	// otherwise lookup all servers by (optional) agentId.
	// IMPL NOTE : the findMBeanServer call treats a null agentId to mean match all...
	ArrayList<MBeanServer> mbeanServers = MBeanServerFactory.findMBeanServer( agentId );

	if ( defaultDomain == null ) {
		// they did not specify a domain by which to locate a particular MBeanServer to use, so chose the first
		return mbeanServers.get( 0 );
	}

	for ( MBeanServer mbeanServer : mbeanServers ) {
		// they did specify a domain, so attempt to locate an MBeanServer with a matching default domain, returning it
		// if we find it.
		if ( defaultDomain.equals( mbeanServer.getDefaultDomain() ) ) {
			return mbeanServer;
		}
	}

	return null;
}
 
Example 4
Source Project: spring-analysis-note   File: JmxUtils.java    License: MIT License 5 votes vote down vote up
/**
 * Attempt to find a locally running {@code MBeanServer}. Fails if no
 * {@code MBeanServer} can be found. Logs a warning if more than one
 * {@code MBeanServer} found, returning the first one from the list.
 * @param agentId the agent identifier of the MBeanServer to retrieve.
 * If this parameter is {@code null}, all registered MBeanServers are considered.
 * If the empty String is given, the platform MBeanServer will be returned.
 * @return the {@code MBeanServer} if found
 * @throws MBeanServerNotFoundException if no {@code MBeanServer} could be found
 * @see javax.management.MBeanServerFactory#findMBeanServer(String)
 */
public static MBeanServer locateMBeanServer(@Nullable String agentId) throws MBeanServerNotFoundException {
	MBeanServer server = null;

	// null means any registered server, but "" specifically means the platform server
	if (!"".equals(agentId)) {
		List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(agentId);
		if (!CollectionUtils.isEmpty(servers)) {
			// Check to see if an MBeanServer is registered.
			if (servers.size() > 1 && logger.isInfoEnabled()) {
				logger.info("Found more than one MBeanServer instance" +
						(agentId != null ? " with agent id [" + agentId + "]" : "") +
						". Returning first from list.");
			}
			server = servers.get(0);
		}
	}

	if (server == null && !StringUtils.hasLength(agentId)) {
		// Attempt to load the PlatformMBeanServer.
		try {
			server = ManagementFactory.getPlatformMBeanServer();
		}
		catch (SecurityException ex) {
			throw new MBeanServerNotFoundException("No specific MBeanServer found, " +
					"and not allowed to obtain the Java platform MBeanServer", ex);
		}
	}

	if (server == null) {
		throw new MBeanServerNotFoundException(
				"Unable to locate an MBeanServer instance" +
				(agentId != null ? " with agent id [" + agentId + "]" : ""));
	}

	if (logger.isDebugEnabled()) {
		logger.debug("Found MBeanServer: " + server);
	}
	return server;
}
 
Example 5
private static Class<?> load(ClassLoader without, String className)
        throws ClassNotFoundException {
    final List<MBeanServer> mbsList = MBeanServerFactory.findMBeanServer(null);

    for (MBeanServer mbs : mbsList) {
        ClassLoaderRepository clr = mbs.getClassLoaderRepository();
        try {
            return clr.loadClassWithout(without, className);
        } catch (ClassNotFoundException e) {
            // OK : Try with next one...
        }
    }
    throw new ClassNotFoundException(className);
}
 
Example 6
private List<MBeanServer> getMBeanServers() {
    List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
    if (servers == null) {
        servers = Collections.emptyList();
    }
    return servers;
}
 
Example 7
public void start() throws Exception {
  this.gmlcPropertiesManagement = GmlcPropertiesManagement.getInstance(this.name);
  this.gmlcPropertiesManagement.setPersistDir(this.persistDir);
  this.gmlcPropertiesManagement.start();

  // Register the MBeans
  boolean servFound = false;
  String agentId = "jboss";
  List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
  if (servers != null && servers.size() > 0) {
    for (MBeanServer server : servers) {
      String defaultDomain = server.getDefaultDomain();

      if (defaultDomain != null && defaultDomain.equals(agentId)) {
        mbeanServer = server;
        servFound = true;
        logger.info(String.format("Found MBeanServer matching for agentId=%s", agentId));
      } else {
        logger.warn(String.format("Found non-matching MBeanServer with default domian = %s", defaultDomain));
      }
    }
  }

  if (!servFound) {
    this.mbeanServer = ManagementFactory.getPlatformMBeanServer();
  }

  ObjectName gmlcPropObjNname = new ObjectName(GmlcManagement.JMX_DOMAIN + ":name=GmlcPropertiesManagement");
  StandardMBean gmlcPropMxBean = new StandardMBean(this.gmlcPropertiesManagement,
      GmlcPropertiesManagementMBean.class, true);
  this.mbeanServer.registerMBean(gmlcPropMxBean, gmlcPropObjNname);

  this.isStarted = true;

  logger.info("Started GMLC Management");
}
 
Example 8
private static Class<?> load(ClassLoader without, String className)
        throws ClassNotFoundException {
    final List<MBeanServer> mbsList = MBeanServerFactory.findMBeanServer(null);

    for (MBeanServer mbs : mbsList) {
        ClassLoaderRepository clr = mbs.getClassLoaderRepository();
        try {
            return clr.loadClassWithout(without, className);
        } catch (ClassNotFoundException e) {
            // OK : Try with next one...
        }
    }
    throw new ClassNotFoundException(className);
}
 
Example 9
private void testIsolation_javax_management_MBeanServerFactory_mBeanServerList() throws TenantException {
    Runnable task = () -> {
        try {
            MBeanServerFactory.findMBeanServer(null);
        } catch (Throwable t) { /* ignore */ }
    };
    testStaticFieldIsolation(MBeanServerFactory.class, task, "mBeanServerList");
}
 
Example 10
private static Class<?> load(ClassLoader without, String className)
        throws ClassNotFoundException {
    final List<MBeanServer> mbsList = MBeanServerFactory.findMBeanServer(null);

    for (MBeanServer mbs : mbsList) {
        ClassLoaderRepository clr = mbs.getClassLoaderRepository();
        try {
            return clr.loadClassWithout(without, className);
        } catch (ClassNotFoundException e) {
            // OK : Try with next one...
        }
    }
    throw new ClassNotFoundException(className);
}
 
Example 11
Source Project: ironjacamar   File: Server.java    License: Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Get the MBeanServer instance
 * @param domain The domain
 * @return The instance
 */
public static MBeanServer getMBeanServer(String domain)
{
   try
   {
      ArrayList<MBeanServer> l = MBeanServerFactory.findMBeanServer(null);

      if (l != null)
      {
         for (MBeanServer ms : l)
         {
            String[] domains = ms.getDomains();

            if (domains != null)
            {
               for (String d : domains)
               {
                  if (domain.equals(d))
                  {
                     return ms;
                  }
               }
            }
         }
      }
   }
   catch (SecurityException se)
   {
      // Ignore
   }

   return null;
}
 
Example 12
private static Class<?> load(ClassLoader without, String className)
        throws ClassNotFoundException {
    final List<MBeanServer> mbsList = MBeanServerFactory.findMBeanServer(null);

    for (MBeanServer mbs : mbsList) {
        ClassLoaderRepository clr = mbs.getClassLoaderRepository();
        try {
            return clr.loadClassWithout(without, className);
        } catch (ClassNotFoundException e) {
            // OK : Try with next one...
        }
    }
    throw new ClassNotFoundException(className);
}
 
Example 13
Source Project: spring4-understanding   File: JmxUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Attempt to find a locally running {@code MBeanServer}. Fails if no
 * {@code MBeanServer} can be found. Logs a warning if more than one
 * {@code MBeanServer} found, returning the first one from the list.
 * @param agentId the agent identifier of the MBeanServer to retrieve.
 * If this parameter is {@code null}, all registered MBeanServers are considered.
 * If the empty String is given, the platform MBeanServer will be returned.
 * @return the {@code MBeanServer} if found
 * @throws org.springframework.jmx.MBeanServerNotFoundException
 * if no {@code MBeanServer} could be found
 * @see javax.management.MBeanServerFactory#findMBeanServer(String)
 */
public static MBeanServer locateMBeanServer(String agentId) throws MBeanServerNotFoundException {
	MBeanServer server = null;

	// null means any registered server, but "" specifically means the platform server
	if (!"".equals(agentId)) {
		List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(agentId);
		if (servers != null && servers.size() > 0) {
			// Check to see if an MBeanServer is registered.
			if (servers.size() > 1 && logger.isWarnEnabled()) {
				logger.warn("Found more than one MBeanServer instance" +
						(agentId != null ? " with agent id [" + agentId + "]" : "") +
						". Returning first from list.");
			}
			server = servers.get(0);
		}
	}

	if (server == null && !StringUtils.hasLength(agentId)) {
		// Attempt to load the PlatformMBeanServer.
		try {
			server = ManagementFactory.getPlatformMBeanServer();
		}
		catch (SecurityException ex) {
			throw new MBeanServerNotFoundException("No specific MBeanServer found, " +
					"and not allowed to obtain the Java platform MBeanServer", ex);
		}
	}

	if (server == null) {
		throw new MBeanServerNotFoundException(
				"Unable to locate an MBeanServer instance" +
				(agentId != null ? " with agent id [" + agentId + "]" : ""));
	}

	if (logger.isDebugEnabled()) {
		logger.debug("Found MBeanServer: " + server);
	}
	return server;
}
 
Example 14
private static MBeanServer getMBeanServer() {
    List<MBeanServer> mBeanServers = MBeanServerFactory.findMBeanServer(null);
    if (!mBeanServers.isEmpty()) {
        return mBeanServers.get(0);
    }
    return ManagementFactory.getPlatformMBeanServer();
}
 
Example 15
private static MBeanServer getMBeanServer() {
    List<MBeanServer> mBeanServers = MBeanServerFactory.findMBeanServer(null);
    if (!mBeanServers.isEmpty()) {
        return mBeanServers.get(0);
    }
    return ManagementFactory.getPlatformMBeanServer();
}
 
Example 16
private static Class<?> load(ClassLoader without, String className)
        throws ClassNotFoundException {
    final List<MBeanServer> mbsList = MBeanServerFactory.findMBeanServer(null);

    for (MBeanServer mbs : mbsList) {
        ClassLoaderRepository clr = mbs.getClassLoaderRepository();
        try {
            return clr.loadClassWithout(without, className);
        } catch (ClassNotFoundException e) {
            // OK : Try with next one...
        }
    }
    throw new ClassNotFoundException(className);
}
 
Example 17
private static Class<?> load(ClassLoader without, String className)
        throws ClassNotFoundException {
    final List<MBeanServer> mbsList = MBeanServerFactory.findMBeanServer(null);

    for (MBeanServer mbs : mbsList) {
        ClassLoaderRepository clr = mbs.getClassLoaderRepository();
        try {
            return clr.loadClassWithout(without, className);
        } catch (ClassNotFoundException e) {
            // OK : Try with next one...
        }
    }
    throw new ClassNotFoundException(className);
}
 
Example 18
@Test
public void startEnvironment_test() throws SchedulerException {
	assertNotNull(scheduler);
	assertNotNull(schedulerFactory);
	
	assertThat(	scheduler.getSchedulerName()).isEqualTo("MyTestScheduler");
	
	assertThat(scheduler.getSchedulerInstanceId()).isEqualTo("MyTestInstanceId");
	
	try {
		ManagementFactory.getPlatformMBeanServer();
		List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
		assertNotNull(servers);
		assertThat(servers.size()).isGreaterThan(0);
		MBeanServer server = servers.get(0);
		List<String> domains = Arrays.asList(server.getDomains());
		assertNotNull(domains);
		assertThat(domains.size()).isGreaterThan(0);
		
		String domain = "quartz";
		assertThat(domains).doesNotContain(domain);
		
		//wait a while until some jobs have been triggered
		Thread.sleep(1000L);
		
		assertThat(queueService.getGroups()).containsOnlyOnce(QueuedInstance.DEFAULT_GROUP, CallbackQueuedJob.GROUP);
		
	} catch (Exception e) {
		assertTrue(e.getMessage(), false);
	}
}
 
Example 19
Source Project: tomee   File: MEJBBean.java    License: Apache License 2.0 5 votes vote down vote up
public MEJBBean() {
    final List mbeanServers = MBeanServerFactory.findMBeanServer(null);
    if (mbeanServers.size() > 0) {
        mbeanServer = (MBeanServer) mbeanServers.get(0);
    } else {
        mbeanServer = MBeanServerFactory.createMBeanServer();
    }
}
 
Example 20
private static Class<?> load(ClassLoader without, String className)
        throws ClassNotFoundException {
    final List<MBeanServer> mbsList = MBeanServerFactory.findMBeanServer(null);

    for (MBeanServer mbs : mbsList) {
        ClassLoaderRepository clr = mbs.getClassLoaderRepository();
        try {
            return clr.loadClassWithout(without, className);
        } catch (ClassNotFoundException e) {
            // OK : Try with next one...
        }
    }
    throw new ClassNotFoundException(className);
}