001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
003: * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
004: */
005:
006: package com.sun.portal.admin.cli.commands;
007:
008: import com.sun.enterprise.cli.framework.*;
009:
010: //jdk
011: import java.util.ResourceBundle;
012: import java.util.MissingResourceException;
013: import java.util.Iterator;
014: import java.util.Vector;
015: import java.util.Enumeration;
016: import java.util.Set;
017: import java.util.HashSet;
018:
019: import java.util.logging.Logger;
020: import java.util.logging.Level;
021:
022: import java.io.StringReader;
023: import java.io.Reader;
024: import java.io.PrintWriter;
025: import java.io.FileWriter;
026: import java.io.BufferedWriter;
027: import java.io.Writer;
028: import java.io.OutputStreamWriter;
029: import java.io.IOException;
030: import java.io.InputStreamReader;
031:
032: /**
033: * The help command will display the help text for all the commands and their
034: * options
035: */
036: public class HelpCommand extends AdminBaseCommand {
037:
038: private static final int DEFAULT_PAGE_LENGTH = 50;
039: private static final int NO_PAGE_LENGTH = -1;
040: private static final String DEFAULT_HELP = "help";
041: private static final String PSADMIN_MAIN_HELP = "psadmin.help.portal";
042: private static final String PSADMIN_HELP = "psadmin.help";
043: private static final String IO_ERROR = "error.psadmin.io.error";
044: private static final String OPT_COMPONENT = "component";
045:
046: private static final String[] VALID_COMPONENTS = { "portal",
047: "desktop", "logging", "monitoring", "portlet", "rewriter",
048: "search", "sra", "ssoadapter", "subscriptions",
049: "communities", "ubt", "wsrp" };
050:
051: private static Set validComponents = new HashSet();
052: static {
053: for (int i = 0; i < VALID_COMPONENTS.length; i++) {
054: validComponents.add(VALID_COMPONENTS[i]);
055: }
056: }
057:
058: /** Creates new HelpCommand */
059: public HelpCommand() {
060: }
061:
062: /**
063: * override abstract method validateOptions()
064: */
065: public boolean validateOptions() throws CommandValidationException {
066: return true;
067: }
068:
069: /**
070: * Executes the command
071: * @throws CommandException
072: */
073: public void runCommand() throws CommandException,
074: CommandValidationException {
075: validateOptions();
076:
077: try {
078:
079: final More m = new More(getPageLength(), getSource(),
080: getDestination(), getUserInput(), getUserOutput(),
081: getQuitChar(), getPrompt());
082: } catch (IOException ioe) {
083: logger.log(Level.SEVERE, "PSALI_CSPACC0003", ioe);
084: throw new CommandException(getLocalizedString(IO_ERROR),
085: ioe);
086: }
087:
088: }
089:
090: private String getCommandName() {
091: String command = DEFAULT_HELP;
092: if (operands.size() > 0) {
093: String operand = (String) getOperands().get(0);
094: if (operand.indexOf("component=") != -1) {
095: command = operand.substring(operand.indexOf("=") + 1);
096: if (!validComponents.contains(command)) {
097: command = DEFAULT_HELP;
098: }
099: } else {
100: command = operand;
101: }
102: }
103:
104: return command;
105:
106: }
107:
108: private Writer getDestination() throws IOException {
109: return new OutputStreamWriter(System.out);
110: }
111:
112: private int getPageLength() {
113: if ((getOption("isMultiMode") != null && getBooleanOption("isMultiMode"))
114: && (getOption("interactive") != null && getBooleanOption("interactive")))
115: return DEFAULT_PAGE_LENGTH;
116: else
117: return NO_PAGE_LENGTH;
118: }
119:
120: private String getPrompt() {
121: return getLocalizedString("ManpagePrompt");
122: }
123:
124: private String getQuitChar() {
125: return getLocalizedString("ManpageQuit");
126: }
127:
128: private Reader getSource() throws CommandValidationException {
129: LocalStringsManager lsm = null;
130: String helpKey = null;
131: StringBuffer helpStr = new StringBuffer(1096);
132: String command = getCommandName();
133:
134: if (command.equals(DEFAULT_HELP)) {
135: helpKey = PSADMIN_HELP;
136: } else {
137: helpKey = PSADMIN_HELP + "." + command;
138: }
139:
140: try {
141: lsm = LocalStringsManagerFactory
142: .getCommandLocalStringsManager();
143:
144: if (getCommandName().equals(DEFAULT_HELP)) {
145: String value = null;
146:
147: for (int i = 0; i < VALID_COMPONENTS.length; i++) {
148: value = lsm.getString(PSADMIN_HELP + "."
149: + VALID_COMPONENTS[i]);
150: if (value != null
151: && !value.startsWith("Key not found")) {
152: helpStr.append(value);
153: }
154: }
155: } else {
156: helpStr.append(lsm.getString(helpKey));
157: }
158: } catch (Exception e) {
159: throw new CommandValidationException("exception", e);
160: }
161:
162: String str = helpStr.toString();
163: if (str.startsWith("Key not found")) {
164: Object tokens[] = { command };
165: helpStr.delete(0, helpStr.length());
166: helpStr
167: .append(getLocalizedString("InvalidCommand", tokens));
168: }
169:
170: return new StringReader(helpStr.toString());
171: }
172:
173: private Reader getUserInput() {
174: return new InputStreamReader(System.in);
175: }
176:
177: private Writer getUserOutput() {
178: return new OutputStreamWriter(System.err);
179: }
180:
181: }
|