001: /**
002: * $Id: GenerateUBTReportCommand.java,v 1.5 2005/12/13 07:46:17 sm140826 Exp $
003: */package com.sun.portal.admin.cli.commands.ubt;
004:
005: import com.sun.portal.admin.cli.commands.AdminBaseCommand;
006: import com.sun.portal.admin.common.util.AdminUtil;
007: import com.sun.portal.admin.common.PSMBeanException;
008: import com.sun.portal.ubt.report.client.UBTReportSaver;
009: import com.sun.enterprise.cli.framework.CommandException;
010: import com.sun.enterprise.cli.framework.CommandValidationException;
011: import com.sun.enterprise.cli.framework.CLILogger;
012:
013: import javax.management.MBeanServerConnection;
014: import javax.management.ObjectName;
015: import javax.management.InstanceNotFoundException;
016: import javax.management.MBeanException;
017: import javax.management.ReflectionException;
018: import java.util.LinkedList;
019: import java.util.Locale;
020: import java.io.File;
021:
022: import net.sf.jasperreports.engine.JasperPrint;
023:
024: public class GenerateUBTReportCommand extends AdminBaseCommand {
025:
026: private static final String destination = "destinationfolder";
027: private static final String format = "format";
028: private static final String report = "reportname";
029:
030: public boolean validateOptions() throws CommandValidationException {
031:
032: //Following check is required to avoid mbean call if specified format
033: // is is not supported.
034: if (!UBTReportSaver.validateFormat(getOption(format))) {
035: throw new CommandValidationException(
036: getLocalizedString("error.ubt.unsupported.format"));
037: }
038:
039: // ADD CODE FOR REPORT NAME VALIDATION
040:
041: //Check whether destination folder is existing or it
042: //If it is not existing, create it
043: String dest_folder = getOption(destination);
044: File dest = new File(dest_folder);
045: if (!dest.isDirectory()) {
046: boolean success = dest.mkdir();
047: if (!success) {
048: throw new CommandValidationException(
049: getLocalizedString("error.ubt.unable.to.create.destination"));
050: }
051: }
052:
053: //Check whether destination folder has write permission
054: if (!dest.canWrite()) {
055: throw new CommandValidationException(
056: getLocalizedString("error.ubt.unable.to.write.destination"));
057: }
058:
059: return super .validateOptions();
060: }
061:
062: public void runCommand() throws CommandException,
063: CommandValidationException {
064:
065: if (!validateOptions()) {
066: throw new CommandValidationException(
067: getLocalizedString(ERROR_OPTION_VALIDATION_FAILED));
068: }
069:
070: String portalId = getOption(OPT_PORTAL_ID);
071: if (portalId != null) {
072: validatePortalId();
073: }
074:
075: Object[] params = { getOption(report), Locale.getDefault() };
076: String[] signature = { "java.lang.String", "java.util.Locale" };
077:
078: MBeanServerConnection mbsc = getMBeanServerConnection(
079: getUserId(), getPassword(), getHost());
080:
081: try {
082:
083: LinkedList path = new LinkedList();
084: path.addFirst(getDomainId());
085: path.addFirst(getPortalId());
086: path.addFirst("UBTReportAggregator");
087:
088: ObjectName objName = AdminUtil.getResourceMBeanObjectName(
089: "PortalDomain.Portal.UBTReportAggregator", path);
090: Object obj = mbsc.invoke(objName, "generateUBTReport",
091: params, signature);
092:
093: UBTReportSaver reportSaver = new UBTReportSaver(
094: (JasperPrint[]) obj, getOption(destination));
095: reportSaver.save(getOption(format));
096:
097: } catch (InstanceNotFoundException ie) {
098: throw new CommandException(getLocalizedString(
099: ERROR_MBEAN_INSTANCE_NOT_FOUND,
100: new Object[] { "generateUBTReport" }), ie);
101: } catch (MBeanException me) {
102: boolean psmbe = me.getCause() instanceof PSMBeanException;
103: if (getBooleanOption(OPT_DEBUG)) {
104: if (psmbe) {
105: String dbgMsg = me.getCause().getMessage();
106: if (dbgMsg != null) {
107: CLILogger.getInstance().printMessage(dbgMsg);
108: }
109: }
110: }
111: if (psmbe) {
112: throw new CommandException(
113: getLocalizedString(((PSMBeanException) me
114: .getCause()).getErrorKey()), me);
115: } else {
116: throw new CommandException(getLocalizedString(
117: ERROR_JMX_INVOKE,
118: new Object[] { "generateUBTReport" }), me);
119: }
120: } catch (ReflectionException re) {
121: throw new CommandException(getLocalizedString(
122: ERROR_MBEAN_REFLECTION_ERROR,
123: new Object[] { "generateUBTReport" }), re);
124: } catch (Exception ex) {
125: throw new CommandException(
126: getLocalizedString(COMMAND_FAILED), ex);
127: } finally {
128: closeMBeanServerConnection();
129: }
130: }
131:
132: }
|