1 package net.sourceforge.ejtools.management.util; 2 3 import java.io.InputStream ; 4 import java.util.Hashtable ; 5 import java.util.Properties ; 6 import java.util.StringTokenizer ; 7 8 import javax.management.ObjectName ; 9 10 import org.apache.log4j.Category; 11 12 20 public abstract class JSR77 21 { 22 23 static Category logger = Category.getInstance(JSR77.class); 24 25 static Properties hierarchy = null; 26 27 static Properties types = null; 28 29 static Properties attributes = null; 30 31 static final String J2EE_TYPE = "j2eeType"; 32 33 static final String NAME = "name"; 34 35 public static boolean isGenericManagedObject(ObjectName objectName) { 36 boolean result = true; 37 38 if (!hasRequiredKeys(objectName)) { 39 return false; 40 } 41 42 try { 43 String j2eeType = objectName.getKeyProperty(JSR77.J2EE_TYPE); 45 46 return types.containsKey(j2eeType); 47 } catch(Exception e) { 48 result = false; 49 } 50 51 return result; 52 } 53 54 public static boolean hasRequiredKeys(ObjectName objectName) { 55 boolean result = true; 56 57 try { 58 Hashtable keys = objectName.getKeyPropertyList(); 60 61 result = result && keys.containsKey(JSR77.J2EE_TYPE); 63 result = result && keys.containsKey(JSR77.NAME); 65 } catch(Exception e) { 66 result = false; 67 } 68 69 return result; 70 } 71 72 public static String extractJ2EEType(ObjectName objectName) { 73 try { 74 return objectName.getKeyProperty(JSR77.J2EE_TYPE); 76 } catch(Exception e) { 77 } 78 return null; 79 } 80 81 public static String extractName(ObjectName objectName) { 82 try { 83 return objectName.getKeyProperty(JSR77.NAME); 85 } catch(Exception e) { 86 } 87 return null; 88 } 89 90 public static boolean hasRequiredParentTypes(ObjectName objectName) { 91 boolean result = true; 92 93 if (!hasRequiredKeys(objectName)) { 94 return false; 95 } 96 97 if (isGenericManagedObject(objectName)) { 100 101 try { 102 Hashtable keys = objectName.getKeyPropertyList(); 104 105 String parents = (String ) hierarchy.get(keys.get(JSR77.J2EE_TYPE)); 106 if (parents != null) { 107 StringTokenizer st = new StringTokenizer (parents , ","); 108 while(st.hasMoreTokens()) { 109 String parent = st.nextToken(); 110 result = result && keys.containsKey(parent); 111 } 112 } 113 } catch(Exception e) { 114 result = false; 115 } 116 117 } 118 119 return result; 120 } 121 122 public static String getRequiredAttributes(ObjectName objectName) { 123 String result = ""; 124 125 if (!hasRequiredKeys(objectName)) { 126 return ""; 127 } 128 129 if (isGenericManagedObject(objectName)) { 130 try { 131 Hashtable keys = objectName.getKeyPropertyList(); 133 134 result = (String ) attributes.get(keys.get(JSR77.J2EE_TYPE)); 135 } catch(Exception e) { 136 result = ""; 137 } 138 } 139 140 return result; 141 } 142 143 static { 144 InputStream in = null; 145 146 try 147 { 148 in = JSR77.class.getResourceAsStream("/j2eeTypes.properties"); 149 if (in != null) 150 { 151 logger.debug("Found j2eeTypes.properties in classpath"); 152 153 types = new Properties (); 154 types.load(in); 155 in.close(); 156 } 157 158 in = JSR77.class.getResourceAsStream("/parent-j2eeTypes.properties"); 159 if (in != null) 160 { 161 logger.debug("Found parent-j2eeTypes.properties in classpath"); 162 163 hierarchy = new Properties (); 164 hierarchy.load(in); 165 in.close(); 166 } 167 168 in = JSR77.class.getResourceAsStream("/attributes.properties"); 169 if (in != null) 170 { 171 logger.debug("Found attributes.properties in classpath"); 172 173 attributes = new Properties (); 174 attributes.load(in); 175 in.close(); 176 } 177 } 178 catch (Exception e) 179 { 180 e.printStackTrace(); 181 } 182 logger.debug("Types and Hierarchy loaded"); 183 } 184 } 185 | Popular Tags |