001: package com.sun.portal.portlet.cli;
002:
003: import java.util.Map;
004: import java.util.Locale;
005: import java.util.ResourceBundle;
006: import java.util.PropertyResourceBundle;
007:
008: import java.io.File;
009:
010: import com.sun.portal.common.clip.CLIPParser;
011: import com.sun.portal.common.clip.CLIPException;
012:
013: import com.sun.portal.desktop.context.DSAMEAdminDPContext;
014: import com.sun.portal.desktop.context.ContextError;
015: import com.sun.portal.util.ResourceLoader;
016:
017: /**
018: * This class is responsible for getting hold of the CLIPParser
019: * and parsing the Command Line options using the CLIPParser.
020: * Based on the SubCommand, PDDeploy/PDUnDeploy are invoked with
021: * the parsed options.
022: */
023: class PortletDeployerCommand {
024:
025: // subcommands
026: protected static final String DEPLOY = "deploy";
027: protected static final String UNDEPLOY = "undeploy";
028: //protected static final String UPDATE = "update";
029:
030: protected static final int SUBCMD_DEPLOY = 1;
031: protected static final int SUBCMD_UNDEPLOY = 2;
032: //protected static final int SUBCMD_UPDATE = 3;
033:
034: // options
035: protected static final String UID = "runasdn";
036: protected static final String PASSWORD = "password";
037: protected static final String ADMIN_PASSWORD = "wc_password";
038: protected static final String WC_INSTANCE = "instance";
039: protected static final String DN = "dn";
040: protected static final String GLOBAL = "global";
041: protected static final String VERBOSE = "verbose";
042: protected static final String VERSION = "version";
043: protected static final String LOCALE = "locale";
044: protected static final String ROLES_FILE = "rolesfile";
045: protected static final String USERINFO_FILE = "userinfofile";
046:
047: // default value for unrequired options
048: public static final String OPT_DEFAULT = "*";
049:
050: //default value for the locale option
051: public static final String LOCALE_DEFAULT = Locale.getDefault()
052: .toString();
053:
054: // option values
055: protected int subcmd = -1;
056: protected String uid = null;
057: protected String password = null;
058: protected String dn = null;
059: protected boolean global = false;
060: protected boolean verbose = false;
061: protected boolean version = false;
062: protected String rolesfile = null;
063: protected String userinfofile = null;
064: protected String warfileLocation = null;
065: protected File warFile = null;
066: protected String warName = null;
067:
068: protected static final String DESKTOP_PROPERTIES = "desktop.propertiesFile";
069:
070: PortletDeployerCommand(CLIPParser clipp, String[] args)
071: throws PortletDeployerException {
072:
073: parse(clipp, args);
074: }
075:
076: private void parse(CLIPParser clipp, String[] argv)
077: throws PortletDeployerException {
078:
079: // version (before CLIP parsing begins)
080: if (needsVersion(argv)) {
081: System.err.println(getVersionInfo());
082: System.exit(1);
083: }
084:
085: // help
086: if (clipp.needsHelp(argv)) {
087: System.err.println(clipp.getHelp(argv));
088: System.exit(1);
089: }
090:
091: // get options
092: Map options = null;
093: try {
094: options = clipp.getOptions(argv);
095: } catch (CLIPException ce) {
096: throw new PortletDeployerException("errorCLIPParseError",
097: ce);
098: }
099:
100: //
101: // for debugging purpose
102: //
103:
104: /**try {
105: System.out.println("------------------------------------------------------------------");
106: System.out.println("CLIP CONTENT (for debug purpose only): " + clipp.toString(argv));
107: System.out.println("------------------------------------------------------------------");
108: } catch (CLIPException ce) {
109: ce.printStackTrace();
110: } */
111: try {
112: clipp.verifyArguments(argv);
113: } catch (CLIPException ce) {
114: throw new PortletDeployerException("errorCLIPParseError",
115: ce);
116: }
117:
118: // verbose
119: if (((String[]) options.get(VERBOSE))[0].equals("true")) {
120: verbose = true;
121: } else {
122: verbose = false;
123: }
124:
125: // get subcommad
126: String sc = null;
127: try {
128: sc = clipp.getSubCommand(argv);
129: } catch (CLIPException ce) {
130: Object[] tokens = { sc };
131: throw new PortletDeployerException("errorInvalidSubCmd",
132: ce, tokens);
133: }
134: if (sc.equals(DEPLOY)) {
135: subcmd = SUBCMD_DEPLOY;
136: } else if (sc.equals(UNDEPLOY)) {
137: subcmd = SUBCMD_UNDEPLOY;
138: }
139: //else if (sc.equals(UPDATE)) {
140: //subcmd = SUBCMD_UPDATE;
141: //}
142:
143: // required options
144: uid = ((String[]) options.get(UID))[0];
145: password = ((String[]) options.get(PASSWORD))[0];
146:
147: dn = ((String[]) options.get(DN))[0];
148: global = ((String[]) options.get(GLOBAL))[0].equals("true");
149: if (dn.equals(OPT_DEFAULT)) {
150: if (!global) {
151: throw new PortletDeployerException("errorNoDNGlobal");
152: }
153: } else if (global) {
154: throw new PortletDeployerException("errorBothDNGlobal");
155: }
156:
157: // get operand
158: String[] files = null;
159: try {
160: files = clipp.getOperands(argv);
161: } catch (CLIPException ce) {
162: Object[] tokens = { ce.toString() };
163: throw new PortletDeployerException("errorOperand", tokens);
164: }
165:
166: // get args for subcmd deploy
167: if (subcmd == SUBCMD_DEPLOY) {
168: rolesfile = ((String[]) options.get(ROLES_FILE))[0];
169: userinfofile = ((String[]) options.get(USERINFO_FILE))[0];
170: if (rolesfile.equals(OPT_DEFAULT)) {
171: rolesfile = null;
172: }
173: if (userinfofile.equals(OPT_DEFAULT)) {
174: userinfofile = null;
175: }
176:
177: if (files != null && files.length > 0) {
178: warfileLocation = files[0];
179: }
180: if (warfileLocation != null) {
181: warFile = new File(warfileLocation);
182: }
183: if (warFile == null || warFile.length() == 0) {
184: throw new PortletDeployerException("errorEmptyFile");
185: }
186:
187: }
188:
189: if (subcmd == SUBCMD_UNDEPLOY) {
190: if (files != null && files.length > 0) {
191: warName = files[0];
192: }
193: if (warName == null || warName.length() == 0) {
194: throw new PortletDeployerException(
195: "errorPortletAppName");
196: }
197: }
198:
199: }
200:
201: protected void runCommand() throws PortletDeployerException {
202:
203: DSAMEAdminDPContext dadc = doAuth();
204:
205: switch (subcmd) {
206: case SUBCMD_DEPLOY:
207: PDDeploy pdd = new PDDeploy();
208: String res = pdd.process(dadc, dn, global, warFile,
209: rolesfile, userinfofile, verbose);
210: if (res != null) {
211: byte[] bytes = null;
212: try {
213: bytes = res.getBytes("UTF-8");
214: } catch (java.io.UnsupportedEncodingException ue) {
215: //This may never happen because utf-8 is always supported!
216: bytes = res.getBytes();
217: }
218: System.out.write(bytes, 0, bytes.length);
219: } else {
220: throw new PortletDeployerException("errorNotFound");
221: }
222: break;
223:
224: case SUBCMD_UNDEPLOY:
225: PDUNDeploy pdud = new PDUNDeploy();
226: res = pdud.process(dadc, dn, global, warName, verbose);
227: if (res != null) {
228: byte[] bytes = null;
229: try {
230: bytes = res.getBytes("UTF-8");
231: } catch (java.io.UnsupportedEncodingException ue) {
232: //This may never happen because utf-8 is always supported!
233: bytes = res.getBytes();
234: }
235: System.out.write(bytes, 0, bytes.length);
236: } else {
237: throw new PortletDeployerException("errorNotFound");
238: }
239: break;
240:
241: //case SUBCMD_UPDATE:
242: //break;
243:
244: default:
245: Object[] tokens = { new Integer(subcmd) };
246: throw new PortletDeployerException("errorInvalidSubCmd",
247: tokens);
248: }
249: }
250:
251: protected static CLIPParser getCLIPParser() throws CLIPException {
252:
253: CLIPParser.SubCommand[] subcmds = null;
254:
255: CLIPParser.Option[] optsDeploy = {
256: new CLIPParser.Option(UID, "u", CLIPParser.REGULAR,
257: null, PortletDeployerLocalizer
258: .getLocalizedString("optUID")),
259: new CLIPParser.Option(PASSWORD, "w",
260: CLIPParser.REGULAR, null,
261: PortletDeployerLocalizer
262: .getLocalizedString("optPassword")),
263: new CLIPParser.Option(ADMIN_PASSWORD, "p",
264: CLIPParser.REGULAR, null,
265: PortletDeployerLocalizer
266: .getLocalizedString("optAdminPassword")),
267: new CLIPParser.Option(WC_INSTANCE, "i",
268: CLIPParser.REGULAR, OPT_DEFAULT,
269: PortletDeployerLocalizer
270: .getLocalizedString("optInstance")),
271: new CLIPParser.Option(DN, "d", CLIPParser.REGULAR,
272: OPT_DEFAULT, PortletDeployerLocalizer
273: .getLocalizedString("optDN")),
274: new CLIPParser.Option(GLOBAL, "g", CLIPParser.BOOLEAN,
275: "false", PortletDeployerLocalizer
276: .getLocalizedString("optGlobal")),
277: new CLIPParser.Option(ROLES_FILE, "r",
278: CLIPParser.REGULAR, OPT_DEFAULT,
279: PortletDeployerLocalizer
280: .getLocalizedString("optRolesFile")),
281: new CLIPParser.Option(USERINFO_FILE, "f",
282: CLIPParser.REGULAR, OPT_DEFAULT,
283: PortletDeployerLocalizer
284: .getLocalizedString("optUIFile")),
285: new CLIPParser.Option(VERBOSE, "v", CLIPParser.BOOLEAN,
286: "false", PortletDeployerLocalizer
287: .getLocalizedString("optVerbose")),
288: new CLIPParser.Option(VERSION, "V", CLIPParser.BOOLEAN,
289: "false", PortletDeployerLocalizer
290: .getLocalizedString("optVersion")),
291: new CLIPParser.Option(LOCALE, "l", CLIPParser.REGULAR,
292: LOCALE_DEFAULT, PortletDeployerLocalizer
293: .getLocalizedString("optLocale")) };
294:
295: CLIPParser.SubCommand scDeploy = new CLIPParser.SubCommand(
296: DEPLOY, optsDeploy, 0, 1, PortletDeployerLocalizer
297: .getLocalizedString("helpDeploy"),
298: PortletDeployerLocalizer.getLocalizedString("helpFile"));
299:
300: CLIPParser.Option[] optsUnDeploy = {
301: new CLIPParser.Option(UID, "u", CLIPParser.REGULAR,
302: null, PortletDeployerLocalizer
303: .getLocalizedString("optUID")),
304: new CLIPParser.Option(PASSWORD, "w",
305: CLIPParser.REGULAR, null,
306: PortletDeployerLocalizer
307: .getLocalizedString("optPassword")),
308: new CLIPParser.Option(ADMIN_PASSWORD, "p",
309: CLIPParser.REGULAR, null,
310: PortletDeployerLocalizer
311: .getLocalizedString("optAdminPassword")),
312: new CLIPParser.Option(WC_INSTANCE, "i",
313: CLIPParser.REGULAR, OPT_DEFAULT,
314: PortletDeployerLocalizer
315: .getLocalizedString("optInstance")),
316: new CLIPParser.Option(DN, "d", CLIPParser.REGULAR,
317: OPT_DEFAULT, PortletDeployerLocalizer
318: .getLocalizedString("optDN")),
319: new CLIPParser.Option(GLOBAL, "g", CLIPParser.BOOLEAN,
320: "false", PortletDeployerLocalizer
321: .getLocalizedString("optGlobal")),
322: new CLIPParser.Option(VERBOSE, "v", CLIPParser.BOOLEAN,
323: "false", PortletDeployerLocalizer
324: .getLocalizedString("optVerbose")),
325: new CLIPParser.Option(VERSION, "V", CLIPParser.BOOLEAN,
326: "false", PortletDeployerLocalizer
327: .getLocalizedString("optVersion")),
328: new CLIPParser.Option(LOCALE, "l", CLIPParser.REGULAR,
329: LOCALE_DEFAULT, PortletDeployerLocalizer
330: .getLocalizedString("optLocale")) };
331:
332: CLIPParser.SubCommand scUnDeploy = new CLIPParser.SubCommand(
333: UNDEPLOY, optsUnDeploy, 0, 1, PortletDeployerLocalizer
334: .getLocalizedString("helpUnDeploy"),
335: PortletDeployerLocalizer
336: .getLocalizedString("unDeployPortletName"));
337:
338: CLIPParser.SubCommand[] sc = { scDeploy, scUnDeploy };
339: subcmds = sc;
340:
341: return new CLIPParser(subcmds, PortletDeployerLocalizer
342: .getLocalizedString("helpCmd"));
343:
344: }
345:
346: private DSAMEAdminDPContext doAuth()
347: throws PortletDeployerException {
348:
349: DSAMEAdminDPContext dadc = null;
350:
351: String propertiesFile = System.getProperty(DESKTOP_PROPERTIES);
352: if (propertiesFile == null) {
353: throw new PortletDeployerException("errorProperties");
354: }
355: try {
356: dadc = new DSAMEAdminDPContext();
357: dadc.init(uid, password, propertiesFile, ResourceLoader
358: .getInstance(System.getProperties()).getPortalId());
359: } catch (ContextError ce) {
360: throw new PortletDeployerException("errorAuthFailed", ce);
361: }
362: return dadc;
363: }
364:
365: private String getVersionInfo() {
366: ResourceBundle prodRB = PropertyResourceBundle
367: .getBundle("PSversion");
368: StringBuffer vinfo = new StringBuffer();
369: vinfo.append(PortletDeployerMain.COMMANDNAME).append(" (")
370: .append(prodRB.getString("productname")).append(" ")
371: .append(prodRB.getString("productversion"))
372: .append(") ")
373: .append(PortletDeployerMain.COMMANDVERSION);
374: vinfo.append("\n").append(prodRB.getString("copyright"));
375: return vinfo.toString();
376: }
377:
378: /**
379: * Returns if the command invocation needs version or not.
380: * <P>
381: * When version is detected anywhere in the options, no further parsing
382: * is performed. A version option is considered to be one of the
383: * followings: <code>"-V"</code>, <code>"--version"</code> or
384: * <code>"--version=true"</code>.
385: * <P>
386: *
387: * @param args the array argument.
388: *
389: * @return <B>true</B> if version is needed, <B>false</B> otherwise.
390: *
391: */
392: public boolean needsVersion(String[] args) {
393: for (int i = 0; i < args.length; i++) {
394: if (args[i].equals("-V") || args[i].equals("--version")) {
395: return true;
396: }
397: }
398: return false;
399: }
400:
401: }
|