1 7 package net.sourceforge.ejtools.management.util; 8 9 import java.io.InputStream ; 10 import java.util.Properties ; 11 import java.util.Set ; 12 13 import javax.management.ObjectName ; 14 import javax.management.j2ee.Management ; 15 import javax.management.j2ee.ManagementHome ; 16 import javax.naming.Context ; 17 import javax.naming.InitialContext ; 18 import javax.rmi.PortableRemoteObject ; 19 20 import org.ejtools.util.JNDI; 21 import org.apache.log4j.Category; 22 23 29 public abstract class MEJB 30 { 31 32 static Category logger = Category.getInstance(MEJB.class); 33 34 static String name = "ejb/mgmt/MEJB"; 35 36 37 42 public static Management createMEJB() 43 { 44 try 45 { 46 return lookupMEJB().create(); 47 } 48 catch (Exception e) 49 { 50 e.printStackTrace(); 51 } 52 return null; 53 } 54 55 56 61 public static String getDefaultDomain() 62 { 63 try 64 { 65 Management mejb = createMEJB(); 66 return mejb.getDefaultDomain(); 67 } 68 catch (Exception e) 69 { 70 e.printStackTrace(); 71 } 72 return null; 73 } 74 75 76 81 public static Integer getMBeanCount() 82 { 83 try 84 { 85 Management mejb = createMEJB(); 86 return mejb.getMBeanCount(); 87 } 88 catch (Exception e) 89 { 90 e.printStackTrace(); 91 } 92 return null; 93 } 94 95 96 101 public static Set listManagedObject() 102 { 103 try 104 { 105 Management mejb = createMEJB(); 106 ObjectName pattern = new ObjectName ("*:*"); 108 return mejb.queryNames(pattern, null); 109 } 110 catch (Exception e) 111 { 112 e.printStackTrace(); 113 } 114 return null; 115 } 116 117 118 123 public static ManagementHome lookupMEJB() 124 { 125 try 126 { 127 Context context = new InitialContext (); 129 Object ref = context.lookup(name); 130 ManagementHome home = (ManagementHome ) PortableRemoteObject.narrow(ref, ManagementHome .class); 131 132 return home; 133 } 134 catch (Exception e) 135 { 136 e.printStackTrace(); 137 } 138 return null; 139 } 140 141 public static String getMEJBBinding() { 142 return name; 143 } 144 145 static 146 { 147 try 148 { 149 JNDI.setJNDIProperties(); 151 152 InputStream in = MEJB.class.getResourceAsStream("/mejb.properties"); 153 154 if (in != null) 155 { 156 logger.debug("Found mejb.properties in classpath"); 157 158 Properties props = new Properties (); 160 props.load(in); 161 in.close(); 162 163 name = props.getProperty("mejb.name"); 164 } 165 } 166 catch (Exception e) 167 { 168 e.printStackTrace(); 169 } 170 logger.debug("JNDI binding is " + name); 171 } 172 } 173 | Popular Tags |