001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: /**
007: * FIXME(susu): may need to be repackaged under core pkg
008: */package com.sun.portal.desktop.dp.cli;
009:
010: import java.util.Map;
011: import java.util.TreeMap;
012: import java.util.Set;
013: import java.util.ResourceBundle;
014: import java.util.PropertyResourceBundle;
015: import java.util.Locale;
016: import java.util.Iterator;
017: import java.util.logging.Logger;
018: import java.util.logging.Level;
019:
020: import java.io.BufferedReader;
021: import java.io.StringReader;
022: import java.io.InputStreamReader;
023: import java.io.File;
024: import java.io.FileReader;
025: import java.io.IOException;
026:
027: import com.sun.portal.desktop.context.AdminDPContext;
028: import com.sun.portal.log.common.PortalLogger;
029:
030: class DPAMain {
031:
032: public static final String COMMANDNAME = "dpadmin";
033: public static final String COMMANDVERSION = "1.2";
034:
035: public static final String DSAME_CACHEON = "com.iplanet.am.sdk.caching.enabled";
036: //private static Debug debug = null;
037:
038: private static Logger logger = PortalLogger
039: .getLogger(DPAMain.class);
040:
041: DPAMain(String[] args) {
042:
043: // first thing first - extract locale information
044: for (int i = 0; i < args.length; i++) {
045: if (args[i].equals("-l")
046: || args[i].equals("--" + DPACommand.LOCALE)) {
047: if (i < args.length - 1) {
048: Locale locale = DPAUtil.getLocale(args[i + 1]);
049: DPAException.setLocale(locale);
050: DPAUtil.setLocale(locale);
051: }
052: }
053: }
054:
055: // set up debug
056: /*
057: debug = Debug.getInstance(LOG_FILENAME);
058: if (debug == null) {
059: Object[] tokens = { LOG_FILENAME };
060: System.err.println(
061: DPAUtil.getLocalizedString("errorLogInitFailed", tokens));
062: } else {
063: debug.setDebug(Debug.ERROR);
064: }
065: */
066: }
067:
068: private static Map getBatchInput(String batchFile)
069: throws DPAException {
070: File f = new File(batchFile);
071: Map batch = new TreeMap();
072: try {
073: BufferedReader br = new BufferedReader(new FileReader(
074: batchFile));
075: String line = null;
076: long lineNum = 1;
077: while ((line = br.readLine()) != null) {
078: if (line.length() > 0) {
079: StringBuffer buf = new StringBuffer();
080: buf.append(line + "\n");
081: batch.put(new Long(lineNum), buf.toString());
082: }
083: lineNum++;
084: }
085: br.close();
086: } catch (IOException ioe) {
087: Object[] tokens = { batchFile };
088: throw new DPAException("errorFileRead", ioe, tokens);
089: }
090:
091: if (batch.size() == 0) {
092: throw new DPAException("errorEmptyBatch");
093: }
094:
095: return batch;
096: }
097:
098: static public void main(String[] args) {
099:
100: DPAMain dpa = new DPAMain(args);
101:
102: CLIPParser clipp = null;
103: try {
104: clipp = DPACommand.getCLIPParser(false);
105: DPACommand cmd = new DPACommand(clipp, args, false);
106: if (!cmd.isBatch()) {
107: cmd.runCommand();
108: } else {
109: //
110: // turn off dsame caching for batch mode.
111: // if caching is explicitly turned on already,
112: // do not turn it off.
113: //
114: if (System.getProperty(DSAME_CACHEON) == null) {
115: System.setProperty(DSAME_CACHEON, "false");
116: }
117:
118: AdminDPContext adc = null;
119: boolean isPreAuthorized = false;
120: if (cmd.isAuthProvided()) {
121: adc = cmd.doAuth();
122: isPreAuthorized = true;
123: }
124:
125: Map batch = getBatchInput(cmd.batchFile);
126: CLIPParser cp = DPACommand.getCLIPParser(true);
127: Set keySet = batch.keySet();
128: for (Iterator i = keySet.iterator(); i.hasNext();) {
129: Long key = (Long) i.next();
130: String cmdline = (String) batch.get(key);
131: if (cmd.verbose) {
132: Object[] tokens = { key, cmdline };
133: logger.log(Level.FINEST, "PSDT_CSPDDC0019",
134: tokens);
135: }
136: try {
137: DPACommand c = new DPACommand(cp, cmdline,
138: true, isPreAuthorized);
139: if (isPreAuthorized) {
140: c.runCommand(adc);
141: } else {
142: c.runCommand();
143: }
144: } catch (DPAException dpaex) {
145: String wrappedMsg = dpaex.getWrappedMessage();
146: if (wrappedMsg != null
147: && wrappedMsg.length() > 0) {
148: Object[] tokens = {
149: dpaex.getLocalizedMessage(), key,
150: wrappedMsg };
151: System.err.println(DPAUtil
152: .getLocalizedString(
153: "msgWrappedErrorLine",
154: tokens));
155: } else {
156: Object[] tokens = {
157: dpaex.getLocalizedMessage(), key };
158: System.err.println(DPAUtil
159: .getLocalizedString("msgErrorLine",
160: tokens));
161: }
162: logger
163: .log(Level.INFO, "PSDT_CSPDDC0059",
164: dpaex);
165: // discontinue if not in continous mode
166: if (!cmd.cont) {
167: System.exit(1);
168: }
169: } catch (Exception ex) {
170: // output exception message
171: Object[] tokens = { ex.getLocalizedMessage(),
172: key };
173: System.err.println(DPAUtil.getLocalizedString(
174: "msgErrorLine", tokens));
175: logger.log(Level.INFO, "PSDT_CSPDDC0059", ex);
176: // discontinue if not in continous mode
177: if (!cmd.cont) {
178: System.exit(1);
179: }
180: }
181: }
182: }
183: System.exit(0);
184:
185: } catch (DPAException dpaex) {
186:
187: // output exception message
188: String wrappedMsg = dpaex.getWrappedMessage();
189: if (wrappedMsg != null && wrappedMsg.length() > 0) {
190: Object[] tokens = { dpaex.getLocalizedMessage(),
191: wrappedMsg };
192: System.err.println(DPAUtil.getLocalizedString(
193: "msgWrappedError", tokens));
194: } else {
195: Object[] tokens = { dpaex.getLocalizedMessage() };
196: System.err.println(DPAUtil.getLocalizedString(
197: "msgError", tokens));
198: }
199: logger.log(Level.SEVERE, "PSDT_CSPDDC0059", dpaex);
200: System.exit(1);
201:
202: } catch (Exception ex) {
203:
204: // output exception message
205: Object[] tokens = { ex.getLocalizedMessage() };
206: System.err.println(DPAUtil.getLocalizedString("msgError",
207: tokens));
208: logger.log(Level.SEVERE, "PSDT_CSPDDC0059", ex);
209: System.exit(1);
210: }
211: }
212: }
|