001: /**
002: * $Id: CreateParCommand.java,v 1.4 2006/03/31 02:40:52 cathywu Exp $
003: * Copyright 2004 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.admin.cli.commands;
014:
015: import java.lang.reflect.*;
016:
017: import java.util.logging.Logger;
018: import java.util.logging.Level;
019:
020: import java.io.File;
021:
022: //CLI framework
023: import com.sun.enterprise.cli.framework.*;
024:
025: /**
026: * This class implements the psadmin create-par subcommand.
027: */
028: public class CreateParCommand extends AdminBaseCommand {
029: public static final String OPT_DIR = "dir";
030: public static final String CREATE_PAR_CLASS_NAME = "com.sun.portal.desktop.admin.mbeans.tasks.CreatePAR";
031:
032: private String parFileName;
033: private String parSrcPath;
034:
035: //validate operands
036: private void validateParFileName() throws CommandException {
037: final String name = (String) getOperands().get(0);
038: if (!name.endsWith(".par")) {
039: throw new CommandException(
040: getLocalizedString(ImportCommand.ERROR_IMPORT_PAR_EXT_NOT_RIGHT),
041: null);
042: }
043:
044: File f = new File(name);
045: File parent = f.getParentFile();
046:
047: if (name.indexOf(File.separator) != -1 && !parent.exists()) {
048: String token[] = { parent.getAbsolutePath() };
049: throw new CommandException(getLocalizedString(
050: ExportCommand.ERROR_EXPORT_FILE_PATH_DOESNOT_EXIST,
051: token));
052: }
053:
054: String fullPath = f.getAbsolutePath();
055:
056: if (f.exists()) {
057: String token[] = { fullPath };
058: logger.log(Level.INFO, "PSALI_CSPACC0025", token);
059: }
060:
061: //set import par file name
062: parFileName = fullPath;
063: }
064:
065: /*
066: * This method provides the type specific validations for the
067: * create-par subcommand .
068: */
069: private void validateParSrcPath() throws CommandException {
070: String dir = getOption(OPT_DIR);
071:
072: File f = new File(dir);
073: String fullPath = f.getAbsolutePath();
074:
075: if (!f.isDirectory()) {
076: String token[] = { fullPath };
077: logger.log(Level.SEVERE, "PSALI_CSPACC0012", token);
078: throw new CommandException(getLocalizedString(
079: ExportCommand.ERROR_EXPORT_FILE_PATH_DOESNOT_EXIST,
080: token));
081: }
082: parSrcPath = fullPath;
083: }
084:
085: /**
086: * Executes the create-par subcommand. This method uses the java
087: * reflection methods to initialize and invoke CreatePAR.parFromDir()
088: * in desktop module.
089: *
090: * @exception CommandValidationException If validation of options failed.
091: * @exception CommandException If error occurrs during the execution.
092: */
093: public void runCommand() throws CommandException,
094: CommandValidationException {
095: validateNonSRACommand();
096: validateParFileName();
097: validateParSrcPath();
098: try {
099: Class cl = Class.forName(CREATE_PAR_CLASS_NAME);
100: Class signatures[] = new Class[2];
101: Object params[] = { parSrcPath, parFileName };
102: signatures[0] = String.class;
103: signatures[1] = String.class;
104: Method createPar = cl.getMethod("parFromDir", signatures);
105: Object obj = cl.newInstance();
106: createPar.invoke(obj, params);
107: } catch (IllegalAccessException ie) {
108: logger.log(Level.SEVERE, "PSALI_CSPACC0019", ie);
109: if (getBooleanOption(OPT_DEBUG)) {
110: CLILogger.getInstance().printMessage(ie.getMessage());
111: }
112: throw new CommandException(
113: getLocalizedString(COMMAND_FAILED), null);
114: } catch (IllegalArgumentException iae) {
115: logger.log(Level.SEVERE, "PSALI_CSPACC0020", iae);
116: throw new CommandException(
117: getLocalizedString(COMMAND_FAILED), null);
118: } catch (InvocationTargetException ite) {
119: logger.log(Level.SEVERE, "PSALI_CSPACC0021", ite);
120: if (getBooleanOption(OPT_DEBUG)) {
121: CLILogger.getInstance().printMessage(ite.getMessage());
122: }
123: throw new CommandException(
124: getLocalizedString(COMMAND_FAILED), null);
125: } catch (Exception ex) {
126: logger.log(Level.SEVERE, "PSALI_CSPACC0010", ex);
127: if (getBooleanOption(OPT_DEBUG)) {
128: CLILogger.getInstance().printMessage(ex.getMessage());
129: }
130:
131: throw new CommandException(
132: getLocalizedString(COMMAND_FAILED), null);
133: } catch (Throwable t) {
134: logger.log(Level.SEVERE, "PSALI_CSPACC0022", t);
135: if (getBooleanOption(OPT_DEBUG)) {
136: CLILogger.getInstance().printMessage(t.getMessage());
137: }
138: throw new CommandException(
139: getLocalizedString(COMMAND_FAILED), null);
140: }
141: }
142:
143: }
|