1 8 9 package mx4j.examples.services.loading; 10 11 import java.io.File ; 12 import java.net.URL ; 13 import java.util.Arrays ; 14 import java.util.Iterator ; 15 import java.util.Set ; 16 import javax.management.MBeanServer ; 17 import javax.management.MBeanServerFactory ; 18 import javax.management.ObjectInstance ; 19 import javax.management.ObjectName ; 20 import javax.management.ReflectionException ; 21 import javax.management.ServiceNotFoundException ; 22 import javax.management.loading.MLet ; 23 24 30 public class Main 31 { 32 public static void main(String [] args) throws Exception 33 { 34 MBeanServer server = MBeanServerFactory.createMBeanServer(); 36 37 MLet mlet = new MLet (); 39 ObjectName mletName = new ObjectName ("system:mbean=loader"); 40 server.registerMBean(mlet, mletName); 41 42 Thread.currentThread().setContextClassLoader(mlet); 45 46 URL mbeansURL = null; 50 if (args.length == 1) 51 { 52 String file = args[0]; 53 mbeansURL = new File (file).toURL(); 54 } 55 else 56 { 57 mbeansURL = mlet.getResource("examples/services/loading/mbeans.mlet"); 58 } 59 60 if (mbeansURL == null) throw new ServiceNotFoundException ("Could not find MBeans to load"); 62 63 Set mbeans = mlet.getMBeansFromURL(mbeansURL); 65 66 System.out.println("MLet has now the following classpath: " + Arrays.asList(mlet.getURLs())); 67 68 checkMBeansLoadedSuccessfully(mbeans); 70 71 initializeMBeans(server, mbeans); 73 startMBeans(server, mbeans); 74 75 System.out.println("System up and running !"); 77 78 } 80 81 private static void checkMBeansLoadedSuccessfully(Set mbeans) throws ServiceNotFoundException 82 { 83 boolean allLoaded = true; 85 for (Iterator i = mbeans.iterator(); i.hasNext();) 86 { 87 Object mbean = i.next(); 88 if (mbean instanceof Throwable ) 89 { 90 ((Throwable )mbean).printStackTrace(); 91 allLoaded = false; 92 } 94 else 95 { 96 System.out.println("Registered MBean: " + mbean); 98 } 99 } 100 101 if (!allLoaded) throw new ServiceNotFoundException ("Some MBean could not be loaded"); 102 } 103 104 private static void initializeMBeans(MBeanServer server, Set mbeans) 105 { 106 for (Iterator i = mbeans.iterator(); i.hasNext();) 107 { 108 try 109 { 110 ObjectInstance instance = (ObjectInstance )i.next(); 111 if (server.isInstanceOf(instance.getObjectName(), "org.apache.avalon.framework.activity.Initializable")) 112 { 113 try 114 { 115 server.invoke(instance.getObjectName(), "initialize", null, null); 116 } 117 catch (ReflectionException ignored) 118 { 119 } 121 } 122 } 123 catch (Exception x) 124 { 125 x.printStackTrace(); 126 } 127 } 128 } 129 130 private static void startMBeans(MBeanServer server, Set mbeans) 131 { 132 for (Iterator i = mbeans.iterator(); i.hasNext();) 133 { 134 try 135 { 136 ObjectInstance instance = (ObjectInstance )i.next(); 137 if (server.isInstanceOf(instance.getObjectName(), "org.apache.avalon.framework.activity.Startable")) 138 { 139 try 140 { 141 server.invoke(instance.getObjectName(), "start", null, null); 142 } 143 catch (ReflectionException ignored) 144 { 145 } 147 } 148 } 149 catch (Exception x) 150 { 151 x.printStackTrace(); 152 } 153 } 154 } 155 } 156 | Popular Tags |