FileDocCategorySizeDatePackage
GenerateReportCommand.javaAPI DocGlassfish v2 API17217Fri May 04 22:25:10 BST 2007com.sun.enterprise.cli.commands

GenerateReportCommand

public class GenerateReportCommand extends BaseLifeCycleCommand
This command will get the version of the application server
version
$Revision: 1.12 $

Fields Summary
private static final String
LOG_START_DATE_OPTION
private static final String
LOG_END_DATE_OPTION
private static final String
LOCAL_OPTION
private static final String
OUTPUT_FILE_OPTION
private static final String
FILE_OPTION
private static final String
BUGIDS_OPTION
private static final String
TARGET_DIR_OPTION
private static final String
TARGET
private final String
CONF_ATTR_MSG
private final String
INSTRUCTIONS
private final String
PROMPT_STRING
private final String
YES_STRING
private final String
NO_STRING
private String
sOutputFile
Constructors Summary
Methods Summary
private voidcheckDiagnosticFile(java.lang.String sDownloadPath)

        final File dlDir = new File(sDownloadPath);
        if (!dlDir.exists() )
        {
            dlDir.mkdirs();
        }
        if(!dlDir.exists() || !dlDir.canWrite() || !dlDir.isDirectory() ) {
            throw new CommandValidationException(getLocalizedString(
						 "InvalidDirectory",new Object [] {sDownloadPath}));
        }
    
private booleancompareDates(java.lang.String startDateStr, java.lang.String endDateStr)

        if ((startDateStr == null) || (endDateStr == null)) return true;
        try
        {
            DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
            Date startDate = df.parse(startDateStr);
            Date endDate = df.parse(endDateStr);
            if (startDate.compareTo(endDate) > 0)
            {
                throw new CommandException(
                                getLocalizedString("InvalidDateBeforeDate"));
            }
        }
        catch (ParseException pe)
        {
            throw new CommandException(
                                getLocalizedString("InvalidDateError"));
        }
        return true;
    
private java.util.MapcreateCLIOptionsMap()

        Map cliOptions = new HashMap();
        String target = (String)operands.firstElement();
        CLILogger.getInstance().printDebugMessage(" Target : " +  target);
        cliOptions.put(LOCAL_OPTION, getOption(LOCAL_OPTION));
        cliOptions.put(TARGET, target);
        if (getOption(LOG_START_DATE_OPTION) != null)
            cliOptions.put(LOG_START_DATE_OPTION, 
                            new Date(getOption(LOG_START_DATE_OPTION)));
        if (getOption(LOG_END_DATE_OPTION) != null)
            cliOptions.put(LOG_END_DATE_OPTION, 
                            new Date(getOption(LOG_END_DATE_OPTION)));
        if (getOption(FILE_OPTION) != null)
            cliOptions.put(FILE_OPTION, getOption(FILE_OPTION));
        if (sOutputFile != null && getBooleanOption(LOCAL_OPTION))
            cliOptions.put(OUTPUT_FILE_OPTION, sOutputFile);
        cliOptions.put(TARGET_DIR_OPTION, getOption(TARGET_DIR_OPTION));
        if (getOption(BUGIDS_OPTION) != null)
            cliOptions.put(BUGIDS_OPTION, getOption(BUGIDS_OPTION));

        return cliOptions;
    
private voidexecuteCommandLocalMode()
This routine will generate the diagnostic report locally.

            final DiagnosticAgent agent = getFeatureFactory().getDiagnosticAgent();
            final List<String> confidentialAttrs = getConfidentialAttributes(
                getOption(TARGET_DIR_OPTION), 
                (String)operands.firstElement(), agent);
            printAttributes(confidentialAttrs);
            
            final String input = printPromptToContinue();
            
            if (input.equalsIgnoreCase("y"))
                CLILogger.getInstance().printDebugMessage("continue");
            else
            {
                CLILogger.getInstance().printDebugMessage("exiting");
                return;
            }
      
            final String reportFile = generateReport(createCLIOptionsMap(), agent);
            CLILogger.getInstance().printMessage(
                                getLocalizedString("ReportFile",
                                                new Object[] {reportFile}));
    
private voidexecuteCommandRemoteMode()
This routine will generate the diagnostic report remotely.

        final String objectName = getObjectName();        
        //use http connector
        MBeanServerConnection mbsc = getMBeanServerConnection(getHost(),
                                                              getPort(),
                                                              getUser(),
                                                              getPassword());

        List<String> confidentialAttrs = (List)mbsc.getAttribute(new ObjectName(objectName),
                                                                 "ConfidentialProperties");
        printAttributes(confidentialAttrs);
        final String input = printPromptToContinue();
        if (input.equalsIgnoreCase(YES_STRING))
            CLILogger.getInstance().printDebugMessage("continue");
        else
        {
            CLILogger.getInstance().printDebugMessage("exiting");
            return;
        }
        final String operationName = getOperationName();
        final Object[] params = new Object[] {createCLIOptionsMap()};
        final String[] types = getTypesInfo();

        final String sServerLocation = (String)mbsc.invoke(new ObjectName(objectName), 
                                                 operationName, params, types);

        final File fOutputFile = new File(sOutputFile);
        getDiagnosticReport(mbsc, sServerLocation,
                            fOutputFile.getAbsoluteFile().getParent(),
                            fOutputFile.getName());
    
private java.lang.StringgenerateReport(java.util.Map clioptions, com.sun.enterprise.diagnostics.DiagnosticAgent agent)

        
        try {
            return agent.generateReport(clioptions);
        } catch (DiagnosticException de) {
            de.printStackTrace();
            throw new CommandException(de.getMessage());
        }
         
        //return null;
    
private java.util.ListgetConfidentialAttributes(java.lang.String targetDir, java.lang.String targetName, com.sun.enterprise.diagnostics.DiagnosticAgent agent)

        
        try {
            return agent.getConfidentialProperties(targetDir + 
                    File.separator + targetName);
        } catch(DiagnosticException e) {
            throw new CommandException(e.getMessage());
        }
         
        //return null;
        
    
private voidgetDiagnosticReport(javax.management.MBeanServerConnection mbsc, java.lang.String sFileDownload, java.lang.String sDownloadPath, java.lang.String sFileName)
retrieve diagnostic report

        try 
        {
            final String fileName = new JMXFileTransfer(mbsc).downloadFile(sFileDownload,
                                                                           sDownloadPath,
                                                                           sFileName);
            CLILogger.getInstance().printDebugMessage("downloaded from  : " + sFileDownload + " to : " + sDownloadPath);
        }
        catch (Exception e)
        {
            Throwable t = e.getCause();
            while(t!=null && t.getCause()!=null)
                t=t.getCause();
            if(t==null)
                t=e;
            throw new CommandException(t.getLocalizedMessage(),t);
        }
    
private booleanisDateValid(java.lang.String dateStr)
This method gets the Version locally

        if (dateStr == null) return true;
        try
        {
            DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
            Date date = df.parse(dateStr);
            return true;
        }
        catch (ParseException pe)
        {
            throw new CommandException(
                                getLocalizedString("InvalidDate",
                                                new Object[] {dateStr}));
        }
    
private booleanisDirectoryValid(java.lang.String directory)

        if (directory == null) return true;
        File file = new File(directory);
        if (!file.isDirectory() || !file.canRead()) 
            throw new CommandException(getLocalizedString("InvalidDirectory",
                                                new Object[]{directory}));
        return true;
    
private booleanisFileValid(java.lang.String fileStr)

        if (fileStr == null) return true;
        final File file = checkForFileExistence(null, fileStr);
        if (file == null) 
            throw new CommandException(getLocalizedString("FileDoesNotExist",
                                                      new Object[] {fileStr}));
        return true;
    
private booleanisOutputFileValid(java.lang.String sFile)

        if (sFile == null) return false;
        final File file = new File(sFile);
        final String sFileName = file.getName();
        
        if (file.getParent()!=null) {
            checkDiagnosticFile(file.getParent());
        }

        if (!sFileName.endsWith(".jar"))
            throw new CommandException(getLocalizedString("InvalidOutputFile"));
        return true;
    
private booleanisValidInput(java.lang.String line)

        if ( line == null )
        {
            return false;
        }
        if ( line == null ||
                line.trim().equals("") ||
                line.length() < 1 ||
                (!line.equalsIgnoreCase(YES_STRING) && 
                    !line.equalsIgnoreCase(NO_STRING)))
            return false;
        return true;
    
private voidprintAttributes(java.util.List attrs)

        if(attrs != null) 
        {
            CLILogger.getInstance().printMessage(CONF_ATTR_MSG);
            
            Iterator<String> iterator = attrs.iterator();
            while(iterator.hasNext()) 
            {
                CLILogger.getInstance().printMessage(iterator.next());
            }
        }
    
private java.lang.StringprintPromptToContinue()

        try 
        {
            String line = null;
            while (!isValidInput(line))
            {
                CLILogger.getInstance().printMessage(INSTRUCTIONS);
                InputsAndOutputs.getInstance().getUserOutput().print(PROMPT_STRING);
                line = InputsAndOutputs.getInstance().getUserInput().getLine().trim();
            }
            return line;
        }
        catch (IOException ioe)
        {
            throw new CommandException(getLocalizedString("CouldNotPrintOrRead"), 
                                       ioe);
        }
    
public voidrunCommand()
An abstract method that Executes the command

throws
CommandException

        //if validateOptions is false, then it must  be that --help option
        //is provided and there is no need to execute the command since
        //either manpage or usage text is displayed
        if (!validateOptions())
            return;
        
        try {
            if (!isDateValid(getOption(LOG_START_DATE_OPTION)) || 
                !isDateValid(getOption(LOG_END_DATE_OPTION)) ||
                !compareDates(getOption(LOG_START_DATE_OPTION),
                                getOption(LOG_END_DATE_OPTION)) ||
                !isFileValid(getOption(FILE_OPTION)) || 
                !isOutputFileValid(sOutputFile) ||
                !isDirectoryValid(getOption(TARGET_DIR_OPTION)))
            return;
            
            if (getBooleanOption(LOCAL_OPTION)) {
                executeCommandLocalMode();
            }
                //Start the code for remote version of the command
                //
            else {
                executeCommandRemoteMode();
            }
        
            CLILogger.getInstance().printDetailMessage(getLocalizedString(
                                                       "CommandSuccessful",
                                                       new Object[] {name}));
        }
        catch(Exception e) {
            if (e.getLocalizedMessage() != null)
                displayExceptionMessage(e);
            throw new CommandException(getLocalizedString("CommandUnSuccessful",
                                                     new Object[] {name} ), e);
        }
     
public booleanvalidateOptions()
An abstract method that validates the options on the specification in the xml properties file This method verifies for the correctness of number of operands and if all the required options are supplied by the client.

return
boolean returns true if success else returns false

    
                                                           
        
    
        if (!super.validateOptions()) 
            return false;
        if (getBooleanOption(LOCAL_OPTION) == true)
        {
            if (getOption(TARGET_DIR_OPTION) == null)
                throw new CommandValidationException(
                                getLocalizedString("OptionRequiredInLocalMode",
                                            new Object[] {TARGET_DIR_OPTION}));
        }
        if ((getOption(LOG_END_DATE_OPTION) != null) && 
                (getOption(LOG_START_DATE_OPTION) == null))
        {
            throw new CommandValidationException(
                                getLocalizedString("OptionRequiredWithOption",
                                        new Object[] {LOG_START_DATE_OPTION,
                                                      LOG_END_DATE_OPTION}));
        }
        sOutputFile = getOption(OUTPUT_FILE_OPTION);
        return true;