001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: *
005: * File:CLIPSpec.java
006: *
007: * Copyright (c) 2001 Sun Microsystems, Inc.
008: * All rights reserved.
009: *
010: * Date - Dec/15/2001
011: * Author - alejandro.abdelnur@sun.com
012: * @author Raja Nagendra Kumar, Nagendra.Raja@sun.com *
013: */
014:
015: package com.sun.portal.rewriter.util.clip;
016:
017: import com.sun.portal.rewriter.RewriterModule;
018: import com.sun.portal.rewriter.util.Resource;
019: import com.sun.portal.rewriter.util.i18n.LocaleHelper;
020: import com.sun.portal.rewriter.util.xml.Document;
021: import com.sun.portal.rewriter.util.xml.Node;
022:
023: import java.util.PropertyResourceBundle;
024: import java.util.ResourceBundle;
025:
026: public class CLIPSpec {
027: private static LocaleHelper defaultLocaleHelper;
028:
029: private Class commandHandler;
030: private String commandHandlerClassName;
031: private String localeID;
032:
033: private CLIPParser clipParser;
034:
035: public static final String COMMANDNAME = "rwadmin";
036: public static final String COMMANDVERSION = "1.1";
037:
038: public CLIPSpec(final String aSpecFile) throws Exception {
039: String lXMLString = Resource.read(aSpecFile);
040: if (lXMLString.trim().length() == 0) {
041: System.out
042: .println("CLI Spec file: "
043: + aSpecFile
044: + " not found or permisstion denied (contanct vendor)");
045: System.exit(0);
046: }
047:
048: Node rootNode = Document.create(lXMLString).getRootNode();
049:
050: setMetaData(rootNode);
051: CLIPOption[] commonOptions = CLIPSpecHelper
052: .getCommonOptions(rootNode);
053:
054: Node[] subCommands = rootNode
055: .selectNodes(CLIPConstants.SUB_COMMAND_XPATH);
056: CLIPSubCommand[] clipSubCommands = new CLIPSubCommand[subCommands.length];
057: for (int i = 0; i < subCommands.length; i++) {
058: clipSubCommands[i] = CLIPSpecHelper.createSubCommand(
059: subCommands[i], commonOptions);
060: }//for loop
061:
062: clipParser = new CLIPParser(clipSubCommands, rootNode
063: .getAttributeValue(CLIPConstants.HELP));
064: }//constructor
065:
066: private void setMetaData(final Node aRootNode) throws Exception {
067: commandHandlerClassName = aRootNode
068: .getAttributeValue(CLIPConstants.COMMAND_HANDLER);
069: commandHandler = Class.forName(commandHandlerClassName);
070: localeID = aRootNode.getAttributeValue(CLIPConstants.LOCALE_ID);
071: }//setMetaData()
072:
073: public CLIPParser getCLIPParser() {
074: return clipParser;
075: }//getCLIPParser()
076:
077: public void doDefaultTasks(final String[] args)
078: throws CLIPException {
079: final String stdExitOption = CLIPParser
080: .scanForStdExitOptions(args);
081: if (stdExitOption != null
082: && stdExitOption.equals(CLIPConstants.LONG_OPTION
083: + CLIPConstants.OPTION_VERSION)) {
084: printVersionInfo();
085: }
086:
087: //at first verify if the aruments are o.k else provide
088: //appropriate messages
089: clipParser.verifyArguments(args);
090:
091: //check if the user is asking for help
092: checkForHelp(args);
093:
094: }//doDefaultTasks()
095:
096: private static void printVersionInfo() {
097: ResourceBundle prodRB = PropertyResourceBundle
098: .getBundle("PSversion");
099: StringBuffer vinfo = new StringBuffer();
100: vinfo.append(COMMANDNAME).append(" (").append(
101: prodRB.getString("productname")).append(" ").append(
102: prodRB.getString("productversion")).append(") ")
103: .append(COMMANDVERSION);
104: vinfo.append("\n").append(prodRB.getString("copyright"));
105: System.err.println(vinfo.toString());
106: System.exit(0);
107: }//printVersionInfo()
108:
109: private void checkForHelp(final String[] args) {
110: if (clipParser.needsHelp(args)) {
111: System.out.println(clipParser.getHelp(args));
112: System.exit(0);
113: }//if help
114: }//checkForHelp()
115:
116: public Object executeCommand(final String[] args) {
117: try {
118: CLIHandler cliHandler = (CLIHandler) commandHandler
119: .newInstance();
120: return cliHandler.executeCommand(this , args);
121: } catch (Exception e) {
122: e.printStackTrace();
123: System.exit(0);
124: }//try/catch
125:
126: return null;
127: }//excuteCommand()
128:
129: public String toString() {
130: return "Tool Name: " + COMMANDNAME + "\n"
131: + clipParser.toString();
132: }//toString()
133:
134: public static LocaleHelper getLocaleHelper() {
135: return defaultLocaleHelper;
136: }//getLocaleHelper()
137:
138: public static String[] getCLIArgs(final String[] args) {
139: //remove the meta data which is not a part of cli but which
140: //is a part of tool framework
141: String[] cliArgs = CLIPSpecHelper
142: .removeFirstNArguments(args, 1);
143:
144: //if the user has not provided any aruments show the fill help
145: if (cliArgs.length == 0) {
146: cliArgs = new String[] { "--help" };
147: }
148:
149: return cliArgs;
150: }//getCLIArgs()
151:
152: public static void main(String[] args) throws Exception {
153: if (args.length < 1) {
154: System.out
155: .println("Error:Script File should prepend two parms "
156: + "i.e CLI Spec File Location");
157: System.exit(0);
158: }
159:
160: RewriterModule.initIDSAME();
161: String[] cliArgs = getCLIArgs(args);
162: CLIPSpec spec = new CLIPSpec(args[0]);
163: LocaleHelper.store(spec.localeID, CLIPSpecHelper
164: .findLocale(cliArgs));
165: defaultLocaleHelper = LocaleHelper
166: .getLocaleHelper(spec.localeID);
167: spec.doDefaultTasks(cliArgs);
168: spec.executeCommand(cliArgs);
169: }//main()
170: }//class CLIPSpec
|