1 23 24 package com.sun.enterprise.cli.commands; 25 26 import com.sun.enterprise.cli.framework.*; 27 import com.sun.enterprise.diagnostics.DiagnosticException; 28 import com.sun.enterprise.diagnostics.DiagnosticAgent; 29 import com.sun.enterprise.admin.common.JMXFileTransfer; 30 31 import javax.management.MBeanServerConnection ; 32 import javax.management.ObjectName ; 33 import java.io.File ; 34 import java.io.IOException ; 35 import java.util.Date ; 36 import java.util.Map ; 37 import java.util.HashMap ; 38 import java.util.List ; 39 import java.util.Iterator ; 40 import java.util.StringTokenizer ; 41 import java.text.DateFormat ; 42 import java.text.SimpleDateFormat ; 43 import java.text.ParseException ; 44 import java.io.FileNotFoundException ; 45 46 47 51 public class GenerateReportCommand extends BaseLifeCycleCommand 52 { 53 private static final String LOG_START_DATE_OPTION = "logstartdate"; 54 private static final String LOG_END_DATE_OPTION = "logenddate"; 55 private static final String LOCAL_OPTION = "local"; 56 private static final String OUTPUT_FILE_OPTION = "outputfile"; 57 private static final String FILE_OPTION = "file"; 58 private static final String BUGIDS_OPTION = "bugids"; 59 private static final String TARGET_DIR_OPTION = "targetdir"; 60 private static final String TARGET ="target"; 61 private final String CONF_ATTR_MSG = 62 getLocalizedString("ConfidentialAttrMsg"); 63 private final String INSTRUCTIONS = 64 getLocalizedString("ConfidentialAttrInstructions"); 65 private final String PROMPT_STRING = 66 getLocalizedString("PromptToContinue"); 67 private static final String YES_STRING = "y"; 68 private static final String NO_STRING = "n"; 69 70 private String sOutputFile; 71 72 79 public boolean validateOptions() throws CommandValidationException 80 { 81 if (!super.validateOptions()) 82 return false; 83 if (getBooleanOption(LOCAL_OPTION) == true) 84 { 85 if (getOption(TARGET_DIR_OPTION) == null) 86 throw new CommandValidationException( 87 getLocalizedString("OptionRequiredInLocalMode", 88 new Object [] {TARGET_DIR_OPTION})); 89 } 90 if ((getOption(LOG_END_DATE_OPTION) != null) && 91 (getOption(LOG_START_DATE_OPTION) == null)) 92 { 93 throw new CommandValidationException( 94 getLocalizedString("OptionRequiredWithOption", 95 new Object [] {LOG_START_DATE_OPTION, 96 LOG_END_DATE_OPTION})); 97 } 98 sOutputFile = getOption(OUTPUT_FILE_OPTION); 99 return true; 100 } 101 102 106 public void runCommand() throws CommandException, CommandValidationException 107 { 108 if (!validateOptions()) 112 return; 113 114 try { 115 if (!isDateValid(getOption(LOG_START_DATE_OPTION)) || 116 !isDateValid(getOption(LOG_END_DATE_OPTION)) || 117 !compareDates(getOption(LOG_START_DATE_OPTION), 118 getOption(LOG_END_DATE_OPTION)) || 119 !isFileValid(getOption(FILE_OPTION)) || 120 !isOutputFileValid(sOutputFile) || 121 !isDirectoryValid(getOption(TARGET_DIR_OPTION))) 122 return; 123 124 if (getBooleanOption(LOCAL_OPTION)) { 125 executeCommandLocalMode(); 126 } 127 else { 130 executeCommandRemoteMode(); 131 } 132 133 CLILogger.getInstance().printDetailMessage(getLocalizedString( 134 "CommandSuccessful", 135 new Object [] {name})); 136 } 137 catch(Exception e) { 138 if (e.getLocalizedMessage() != null) 139 displayExceptionMessage(e); 140 throw new CommandException(getLocalizedString("CommandUnSuccessful", 141 new Object [] {name} ), e); 142 } 143 } 144 145 146 149 private void executeCommandRemoteMode() throws Exception 150 { 151 final String objectName = getObjectName(); 152 MBeanServerConnection mbsc = getMBeanServerConnection(getHost(), 154 getPort(), 155 getUser(), 156 getPassword()); 157 158 List <String > confidentialAttrs = (List )mbsc.getAttribute(new ObjectName (objectName), 159 "ConfidentialProperties"); 160 printAttributes(confidentialAttrs); 161 final String input = printPromptToContinue(); 162 if (input.equalsIgnoreCase("y")) 163 CLILogger.getInstance().printDebugMessage("continue"); 164 else 165 { 166 CLILogger.getInstance().printDebugMessage("exiting"); 167 return; 168 } 169 final String operationName = getOperationName(); 170 final Object [] params = new Object [] {createCLIOptionsMap()}; 171 final String [] types = getTypesInfo(); 172 173 final String sServerLocation = (String )mbsc.invoke(new ObjectName (objectName), 174 operationName, params, types); 175 176 getDiagnosticReport(mbsc, sServerLocation, 177 new File (sOutputFile).getParent(), 178 new File (sOutputFile).getName()); 179 } 180 181 182 183 186 private void getDiagnosticReport(MBeanServerConnection mbsc, 187 String sFileDownload, 188 String sDownloadPath, String sFileName) 189 throws CommandException 190 { 191 try 192 { 193 final String fileName = new JMXFileTransfer(mbsc).downloadFile(sFileDownload, 194 sDownloadPath, 195 sFileName); 196 CLILogger.getInstance().printDebugMessage("downloaded from : " + sFileDownload + " to : " + sDownloadPath); 197 } 198 catch (Exception e) 199 { 200 Throwable t = e.getCause(); 201 while(t!=null && t.getCause()!=null) 202 t=t.getCause(); 203 if(t==null) 204 t=e; 205 throw new CommandException(t.getLocalizedMessage(),t); 206 } 207 } 208 209 210 private void checkDiagnosticFile(final String sDownloadPath) throws Exception 211 { 212 final File dlDir = new File (sDownloadPath); 213 if (!dlDir.exists() ) 214 { 215 dlDir.mkdirs(); 216 } 217 if(!dlDir.exists() || !dlDir.canWrite() || !dlDir.isDirectory() ) { 218 throw new CommandValidationException(getLocalizedString( 219 "InvalidDirectory",new Object [] {sDownloadPath})); 220 } 221 } 222 223 224 227 private void executeCommandLocalMode() throws CommandException 228 { 229 final DiagnosticAgent agent = getFeatureFactory().getDiagnosticAgent(); 230 final List <String > confidentialAttrs = getConfidentialAttributes( 231 getOption(TARGET_DIR_OPTION), 232 (String )operands.firstElement(), agent); 233 printAttributes(confidentialAttrs); 234 235 final String input = printPromptToContinue(); 236 237 if (input.equalsIgnoreCase("y")) 238 CLILogger.getInstance().printDebugMessage("continue"); 239 else 240 { 241 CLILogger.getInstance().printDebugMessage("exiting"); 242 return; 243 } 244 245 final String reportFile = generateReport(createCLIOptionsMap(), agent); 246 CLILogger.getInstance().printMessage( 247 getLocalizedString("ReportFile", 248 new Object [] {reportFile})); 249 } 250 251 252 255 private boolean isDateValid(String dateStr) throws CommandException 256 { 257 if (dateStr == null) return true; 258 try 259 { 260 DateFormat df = new SimpleDateFormat ("MM/dd/yyyy"); 261 Date date = df.parse(dateStr); 262 return true; 263 } 264 catch (ParseException pe) 265 { 266 throw new CommandException( 267 getLocalizedString("InvalidDate", 268 new Object [] {dateStr})); 269 } 270 } 271 272 273 private boolean compareDates(String startDateStr, String endDateStr) 274 throws CommandException 275 { 276 if ((startDateStr == null) || (endDateStr == null)) return true; 277 try 278 { 279 DateFormat df = new SimpleDateFormat ("MM/dd/yyyy"); 280 Date startDate = df.parse(startDateStr); 281 Date endDate = df.parse(endDateStr); 282 if (startDate.compareTo(endDate) > 0) 283 { 284 throw new CommandException( 285 getLocalizedString("InvalidDateBeforeDate")); 286 } 287 } 288 catch (ParseException pe) 289 { 290 throw new CommandException( 291 getLocalizedString("InvalidDateError")); 292 } 293 return true; 294 } 295 296 297 private boolean isFileValid(String fileStr) throws CommandException 298 { 299 if (fileStr == null) return true; 300 final File file = checkForFileExistence(null, fileStr); 301 if (file == null) 302 throw new CommandException(getLocalizedString("FileDoesNotExist", 303 new Object [] {fileStr})); 304 return true; 305 } 306 307 308 private boolean isDirectoryValid(String directory) throws CommandException 309 { 310 if (directory == null) return true; 311 File file = new File (directory); 312 if (!file.isDirectory() || !file.canRead()) 313 throw new CommandException(getLocalizedString("InvalidDirectory", 314 new Object []{directory})); 315 return true; 316 } 317 318 319 private boolean isOutputFileValid(String sFile) throws Exception 320 { 321 if (sFile == null) return false; 322 final File file = new File (sFile); 323 final String sFileName = file.getName(); 324 325 if (file.getParent()!=null) { 326 checkDiagnosticFile(file.getParent()); 327 } 328 329 if (!sFileName.endsWith(".jar")) 330 throw new CommandException(getLocalizedString("InvalidOutputFile")); 331 return true; 332 } 333 334 335 private String generateReport(Map clioptions, DiagnosticAgent agent) throws CommandException 336 { 337 338 try { 339 return agent.generateReport(clioptions); 340 } catch (DiagnosticException de) { 341 de.printStackTrace(); 342 throw new CommandException(de.getMessage()); 343 } 344 345 } 347 348 349 private List <String > getConfidentialAttributes(String targetDir, 350 String targetName, DiagnosticAgent agent) throws CommandException 351 { 352 353 try { 354 return agent.getConfidentialProperties(targetDir + 355 File.separator + targetName); 356 } catch(DiagnosticException e) { 357 throw new CommandException(e.getMessage()); 358 } 359 360 362 } 363 364 365 private Map createCLIOptionsMap() 366 { 367 Map cliOptions = new HashMap (); 368 String target = (String )operands.firstElement(); 369 CLILogger.getInstance().printDebugMessage(" Target : " + target); 370 cliOptions.put(LOCAL_OPTION, getOption(LOCAL_OPTION)); 371 cliOptions.put(TARGET, target); 372 if (getOption(LOG_START_DATE_OPTION) != null) 373 cliOptions.put(LOG_START_DATE_OPTION, 374 new Date (getOption(LOG_START_DATE_OPTION))); 375 if (getOption(LOG_END_DATE_OPTION) != null) 376 cliOptions.put(LOG_END_DATE_OPTION, 377 new Date (getOption(LOG_END_DATE_OPTION))); 378 if (getOption(FILE_OPTION) != null) 379 cliOptions.put(FILE_OPTION, getOption(FILE_OPTION)); 380 if (sOutputFile != null && getBooleanOption(LOCAL_OPTION)) 381 cliOptions.put(OUTPUT_FILE_OPTION, sOutputFile); 382 cliOptions.put(TARGET_DIR_OPTION, getOption(TARGET_DIR_OPTION)); 383 if (getOption(BUGIDS_OPTION) != null) 384 cliOptions.put(BUGIDS_OPTION, getOption(BUGIDS_OPTION)); 385 386 return cliOptions; 387 } 388 389 390 private void printAttributes(List <String > attrs) 391 { 392 if(attrs != null) 393 { 394 CLILogger.getInstance().printMessage(CONF_ATTR_MSG); 395 396 Iterator <String > iterator = attrs.iterator(); 397 while(iterator.hasNext()) 398 { 399 CLILogger.getInstance().printMessage(iterator.next()); 400 } 401 } 402 } 403 404 405 private String printPromptToContinue() throws CommandException 406 { 407 try 408 { 409 String line = null; 410 while (!isValidInput(line)) 411 { 412 CLILogger.getInstance().printMessage(INSTRUCTIONS); 413 InputsAndOutputs.getInstance().getUserOutput().print(PROMPT_STRING); 414 line = InputsAndOutputs.getInstance().getUserInput().getLine().trim(); 415 } 416 return line; 417 } 418 catch (IOException ioe) 419 { 420 throw new CommandException(getLocalizedString("CouldNotPrintOrRead"), 421 ioe); 422 } 423 } 424 425 426 private boolean isValidInput(final String line) 427 { 428 if ( line == null ) 429 { 430 return false; 431 } 432 if ( line == null || 433 line.trim().equals("") || 434 line.length() < 1 || 435 (!line.equalsIgnoreCase(YES_STRING) && 436 !line.equalsIgnoreCase(NO_STRING))) 437 return false; 438 return true; 439 } 440 } 441 | Popular Tags |