{
// Create the MBeanServer
MBeanServer server = MBeanServerFactory.createMBeanServer();
// Register the MLet in the MBeanServer
MLet mlet = new MLet();
ObjectName mletName = new ObjectName("system:mbean=loader");
server.registerMBean(mlet, mletName);
// Set the MLet as context classloader
// Can be useful for the loaded services that want to access this classloader.
Thread.currentThread().setContextClassLoader(mlet);
// Resolve the file to load MBeans from
// If we got a program argument, we load it from there, otherwise
// we assume we have a 'mbeans.mlet' file in this example's directory
URL mbeansURL = null;
if (args.length == 1)
{
String file = args[0];
mbeansURL = new File(file).toURL();
}
else
{
mbeansURL = mlet.getResource("examples/services/loading/mbeans.mlet");
}
// If the URL is still null, abort
if (mbeansURL == null) throw new ServiceNotFoundException("Could not find MBeans to load");
// Load the MBeans
Set mbeans = mlet.getMBeansFromURL(mbeansURL);
System.out.println("MLet has now the following classpath: " + Arrays.asList(mlet.getURLs()));
// Now let's check everything is ok.
checkMBeansLoadedSuccessfully(mbeans);
// Now the system is loaded, but maybe we should initialize and start them