1 23 24 package com.sun.enterprise.diagnostics.collect; 25 26 import com.sun.enterprise.diagnostics.Data; 27 import com.sun.enterprise.diagnostics.DiagnosticException; 28 import com.sun.enterprise.diagnostics.Defaults; 29 import com.sun.enterprise.diagnostics.Constants; 30 import com.sun.enterprise.admin.server.core.jmx.AppServerMBeanServerFactory; 31 import com.sun.logging.LogDomains; 32 33 34 import javax.management.*; 35 import java.util.*; 36 import java.util.logging.Logger ; 37 import java.util.logging.Level ; 38 import java.io.*; 39 40 46 public class MonitoringInfoCollector extends InterruptableCollector { 47 48 Set<String > restrictedProperties = null; 51 public static final String DOTTED_NAME_REGISTRY_OPERATION_NAME = 52 "dottedNameToObjectName"; 53 public static final String KEY_NOT_FOUND = "key not found"; 54 public static final String LIST_COMMAND = "list"; 55 public static final String GET_COMMAND = "get"; 56 public static final String MONITOR_OPTION = "monitor"; 57 58 private String instanceName; 59 private String nodeAgentName; 60 61 private PrintStream out = System.out; 62 63 private String fileName; 64 private String destFolder; 65 66 private static Logger logger = 67 LogDomains.getLogger(LogDomains.ADMIN_LOGGER); 68 69 76 public MonitoringInfoCollector(String nodeAgentName, String instanceName, String destFolder) { 77 if (instanceName != null) { 78 this.instanceName = instanceName; 79 } 80 if(nodeAgentName !=null){ 81 this.nodeAgentName = nodeAgentName; 82 } 83 this.destFolder = destFolder; 84 85 initializeRestrictedPropertiesLookUp(); 86 } 87 88 91 private void initializeRestrictedPropertiesLookUp() { 92 restrictedProperties = new HashSet<String >(); 93 restrictedProperties.add("-description"); 94 restrictedProperties.add("-name"); 95 restrictedProperties.add("-lowerbound"); 96 restrictedProperties.add("-starttime"); 97 restrictedProperties.add("-upperbound"); 98 } 99 100 106 private boolean isRestircted(String dottedName) { 107 boolean restricted = false; 108 109 if (dottedName != null) { 110 int index = dottedName.lastIndexOf("-"); 111 if (index >= 0) { 112 String property = dottedName.substring(index); 113 if (restrictedProperties != null) { 114 restricted = restrictedProperties.contains(property); 115 } 116 } 117 } 118 return restricted; 119 } 120 121 126 public Data capture() throws DiagnosticException { 127 FileData data = null; 128 129 if (destFolder != null) { 130 File destFolderObj = new File(destFolder); 131 fileName = destFolder + File.separator + 132 Defaults.MONITORING_INFO_FILE; 133 134 if (!destFolderObj.exists()) { 135 destFolderObj.mkdirs(); 136 } 137 138 try { 139 out = new PrintStream( 140 new BufferedOutputStream( 141 new FileOutputStream(fileName)), true); 142 143 144 File reportFile = new File(fileName); 145 if(this.instanceName.equalsIgnoreCase(Constants.SERVER)) { 146 data = new FileData( reportFile.getName(), DataType.MONITORING_INFO); 147 }else{ 148 data = new FileData(nodeAgentName + File.separator + instanceName + File.separator + reportFile.getName(), DataType.MONITORING_INFO); 149 } 150 151 ArrayList<String > cmdOutput = new ArrayList<String >(); 152 MonitoringInfoHelper cmd = new MonitoringInfoHelper(); 153 cmd.setName(LIST_COMMAND); 154 155 ArrayList<String > dottedNames = new ArrayList<String >(); 156 if (instanceName != null) { 157 dottedNames.add(instanceName.trim() + ".*"); 158 159 cmd.setOperands(dottedNames); 160 161 cmd.setOption(MonitoringInfoHelper.SECURE, "true"); 162 cmd.setOption(MONITOR_OPTION, "true"); 163 164 cmd.runCommand(cmdOutput); 165 166 if (checkInterrupted()) { 167 logger.log(Level.WARNING, "diagnostic-service." + 168 "monitoring_info_collector_timeout", 169 new Object []{Thread.currentThread().getName(), 170 this.getClass().getName()}); 171 172 if(out!=null){ 173 out.print("Monitoring Info Collector Timeout"); 174 out.close(); 175 } 176 return data; 177 } 178 179 ArrayList<String > list = getIndividualProperties(cmdOutput); 180 181 MBeanServer mbs = AppServerMBeanServerFactory. 182 getMBeanServerInstance(); 183 184 final String [] types = new String []{String .class.getName()}; 185 186 for (String value : list) { 187 188 if (checkInterrupted()) { 189 logger.log(Level.WARNING, "diagnostic-service." + 190 "monitoring_info_collector_timeout", 191 new Object []{Thread.currentThread(). 192 getName(), this.getClass(). 193 getName()}); 194 195 if(out!=null){ 196 out.print("Monitoring Info Collector Timeout"); 197 out.close(); 198 } 199 return data; 200 } 201 202 Object [] params = new Object []{value}; 203 ObjectName dottedNameRegistry = (ObjectName) mbs.invoke( 204 new ObjectName("com.sun.appserv:name=" + 205 "dotted-name-monitoring-registry," + 206 "type=dotted-name-support"), 207 DOTTED_NAME_REGISTRY_OPERATION_NAME, params, 208 types); 209 210 Set set = getAllAttributeNames(mbs, dottedNameRegistry); 211 212 Iterator attributesIterator = set.iterator(); 213 214 ArrayList<String > properties = new ArrayList<String >(); 215 while (attributesIterator.hasNext()) { 216 String attr = (String ) attributesIterator.next(); 217 if (!isRestircted(attr)) { 218 properties.add(value + "." + attr); 219 } 220 } 221 222 if (properties.size() > 0) { 223 cmd.setName(GET_COMMAND); 224 cmd.setOperands(properties); 225 226 ArrayList<String > result = new ArrayList<String >(); 227 cmd.runCommand(result); 228 229 for (String attributeValue : result) { 230 if (!(attributeValue.toLowerCase(). 231 indexOf(KEY_NOT_FOUND) >= 0)) { 232 233 out.println(attributeValue); 234 235 } 236 } 237 } 238 if (checkInterrupted()) { 239 logger.log(Level.WARNING, "diagnostic-service." + 240 "monitoring_info_collector_timeout", 241 new Object []{Thread.currentThread(). 242 getName(), 243 this.getClass().getName()}); 244 if(out!=null){ 245 out.print("Monitoring Info Collector Timeout"); 246 out.close(); 247 } 248 return data; 249 } 250 } 251 out.close(); 252 } 253 return data; 254 } 255 catch (FileNotFoundException fnfe) { 256 logger.log(Level.WARNING, "File Not Found exception occurred " + 257 "while collecting Monitoring information", fnfe); 258 } 259 catch (IOException ioe) { 260 logger.log(Level.WARNING, "IO Exception occurred while " + 261 "collecting Monitoring information", ioe); 262 } 263 catch (Exception e) { 264 logger.log(Level.WARNING, "Exception occurred while collecting"+ 265 " Monitoring information", e); 266 } 267 } 268 return data; 269 } 270 271 272 275 public static Set<String > 276 getAllAttributeNames(final MBeanServer server, 277 final ObjectName objectName) 278 throws ReflectionException, InstanceNotFoundException, 279 IntrospectionException { 280 final Set<String > allNames = new HashSet<String >(); 281 282 final MBeanInfo info = server.getMBeanInfo(objectName); 284 final MBeanAttributeInfo[] attrsInfo = info.getAttributes(); 285 if (attrsInfo != null) { 286 for (MBeanAttributeInfo aAttrsInfo : attrsInfo) { 287 allNames.add(aAttrsInfo.getName()); 288 } 289 } 290 return (allNames); 291 } 292 293 302 public ArrayList<String > getIndividualProperties(ArrayList<String > list) 303 throws IOException { 304 305 ArrayList<String > modifiedList = new ArrayList<String >(); 306 307 String current = null; 308 309 Collections.reverse(list); 310 for (String next : list) { 311 if (current != null) { 312 if (current.indexOf(next) != 0) { 313 modifiedList.add(next); 314 current = next; 315 } 316 } else { 317 modifiedList.add(next); 318 current = next; 319 } 320 } 321 return modifiedList; 322 } 323 326 public void cleanUp() { 327 out.close(); 328 } 329 } 330 | Popular Tags |