001: /**
002: * Copyright 2003 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */package com.sun.portal.wsrp.consumer.cli;
013:
014: import java.util.Set;
015: import java.util.Map;
016: import java.util.Iterator;
017: import java.util.TreeMap;
018: import java.util.HashMap;
019: import java.util.Locale;
020: import java.util.logging.Level;
021: import java.util.logging.LogRecord;
022: import java.util.logging.Logger;
023:
024: import java.io.BufferedReader;
025: import java.io.StringReader;
026: import java.io.InputStreamReader;
027: import java.io.File;
028: import java.io.FileReader;
029: import java.io.IOException;
030:
031: import java.net.URL;
032: import java.net.MalformedURLException;
033:
034: import com.iplanet.am.util.Debug;
035:
036: import com.sun.portal.desktop.context.DSAMEAdminDPContext;
037:
038: import com.sun.portal.wsrp.common.stubs.RegistrationData;
039:
040: import com.sun.portal.wsrp.consumer.common.WSRPConsumerException;
041: import com.sun.portal.wsrp.consumer.common.WSRPConsumerBootstrap;
042:
043: import com.sun.portal.wsrp.consumer.producermanager.ProducerEntityManagerFactory;
044: import com.sun.portal.wsrp.consumer.producermanager.ProducerEntityManager;
045: import com.sun.portal.wsrp.consumer.producermanager.ProducerEntity;
046: import com.sun.portal.log.common.PortalLogger;
047:
048: public class PEAMain {
049:
050: public static final String COMMANDNAME = "wcadmin";
051: public static final String COMMANDVERSION = "1.0";
052:
053: private static Debug debug = null;
054: private static Logger logger = PortalLogger
055: .getLogger(PEAMain.class);
056:
057: PEAMain(String[] args) {
058: //
059: // extract locale information
060: //
061: for (int i = 0; i < args.length; i++) {
062: if (args[i].equals("-l")
063: || args[i].equals("--" + PEACommand.OPT_LOCALE)) {
064: if (i < args.length - 1) {
065: Locale locale = com.iplanet.am.util.Locale
066: .getLocale(args[i + 1]);
067: PEAException.setLocale(locale);
068: PEAUtil.setLocale(locale);
069: }
070: }
071: }
072:
073: //
074: // get propertiesFile
075: //
076: String propertiesFile = System
077: .getProperty("wsrp.consumer.propertiesFile");
078: if (propertiesFile == null) {
079: System.err.println(PEAUtil
080: .getLocalizedString("errorPropertiesFile"));
081: System.exit(1);
082: }
083:
084: //
085: // Bootstratp wsrp consumer
086: //
087: WSRPConsumerBootstrap.cliInitialized(propertiesFile);
088:
089: }
090:
091: private static Map getBatchInput(String batchFile)
092: throws PEAException {
093: File f = new File(batchFile);
094: Map batch = new TreeMap();
095: BufferedReader br = null;
096: try {
097: br = new BufferedReader(new FileReader(batchFile));
098: String line = null;
099: long lineNum = 1;
100: while ((line = br.readLine()) != null) {
101: if (line.length() > 0) {
102: StringBuffer buf = new StringBuffer();
103: buf.append(line + "\n");
104: batch.put(new Long(lineNum), buf.toString());
105: }
106: lineNum++;
107: }
108: } catch (IOException ioe) {
109: Object[] tokens = { batchFile };
110: throw new PEAException("errorFileRead", ioe, tokens);
111: } finally {
112: try {
113: br.close();
114: } catch (IOException ioe) {
115: Object[] tokens = { batchFile };
116: throw new PEAException("errorFileRead", ioe, tokens);
117: }
118: }
119:
120: if (batch.size() == 0) {
121: throw new PEAException("errorEmptyBatch");
122: }
123:
124: return batch;
125: }
126:
127: static public void main(String[] args) {
128:
129: PEAMain pea = new PEAMain(args);
130:
131: //
132: // parse args using clip parser
133: //
134: CLIPParser cp = null;
135: try {
136: cp = PEACommand.getCLIPParser(false);
137: PEACommand cmd = new PEACommand(cp, args, false, debug);
138: if (!cmd.isBatch()) {
139: cmd.runCommand(debug);
140: } else {
141: ProducerEntityManager pem = null;
142: DSAMEAdminDPContext dadc = null;
143: boolean isPreAuthorized = false;
144: if (cmd.isAuthProvided()) {
145: pem = cmd.getProducerEntityManager();
146: dadc = cmd.getDPContext();
147: isPreAuthorized = true;
148: }
149:
150: Map batch = getBatchInput(cmd.batchFile);
151: CLIPParser bcp = PEACommand.getCLIPParser(true);
152: Set keySet = batch.keySet();
153: for (Iterator i = keySet.iterator(); i.hasNext();) {
154: Long key = (Long) i.next();
155: String cmdline = (String) batch.get(key);
156: if (logger.isLoggable(Level.FINEST)) {
157: Object[] tokens = { key, cmdline };
158: logger.log(Level.FINEST, "PSWS_CSPWCCL0018",
159: tokens);
160: }
161:
162: try {
163: PEACommand c = new PEACommand(bcp, cmdline,
164: true, isPreAuthorized, debug);
165: if (isPreAuthorized) {
166: c.runCommand(pem, dadc, debug);
167: } else {
168: c.runCommand(debug);
169: }
170: } catch (PEAException peaex) {
171: String wrappedMsg = peaex.getWrappedMessage();
172: if (wrappedMsg != null
173: && wrappedMsg.length() > 0) {
174: Object[] tokens = {
175: peaex.getLocalizedMessage(), key,
176: wrappedMsg };
177: System.err.println(PEAUtil
178: .getLocalizedString(
179: "msgWrappedErrorLine",
180: tokens));
181: } else {
182: Object[] tokens = {
183: peaex.getLocalizedMessage(), key };
184: System.err.println(PEAUtil
185: .getLocalizedString("msgErrorLine",
186: tokens));
187: }
188: if (logger.isLoggable(Level.SEVERE))
189: logger.log(Level.SEVERE, "", peaex);
190: // discontinue if not in continous mode
191: if (!cmd.cont) {
192: System.exit(1);
193: }
194: } catch (Throwable th) {
195: // output any error message
196: Object[] tokens = { th.getMessage(), key };
197: System.err.println(PEAUtil.getLocalizedString(
198: "msgErrorLine", tokens));
199: if (logger.isLoggable(Level.SEVERE))
200: logger.log(Level.SEVERE, "", th);
201: // discontinue if not in continous mode
202: if (!cmd.cont) {
203: System.exit(1);
204: }
205: }
206: }
207: }
208: System.exit(0);
209:
210: } catch (PEAException peaex) {
211: // output exception message
212: String wrappedMsg = peaex.getWrappedMessage();
213: if (wrappedMsg != null && wrappedMsg.length() > 0) {
214: Object[] tokens = { peaex.getLocalizedMessage(),
215: wrappedMsg };
216: System.err.println(PEAUtil.getLocalizedString(
217: "msgWrappedError", tokens));
218: } else {
219: Object[] tokens = { peaex.getLocalizedMessage() };
220: System.err.println(PEAUtil.getLocalizedString(
221: "msgError", tokens));
222: }
223: if (logger.isLoggable(Level.SEVERE))
224: logger.log(Level.SEVERE, "", peaex);
225: System.exit(1);
226:
227: } catch (Throwable th) {
228: // output exception message
229:
230: Object[] tokens = { th.getMessage() };
231: System.err.println(PEAUtil.getLocalizedString("msgError",
232: tokens));
233: if (logger.isLoggable(Level.SEVERE)) {
234: LogRecord rec = new LogRecord(Level.SEVERE,
235: "PSWS_CSPWCCL0019");
236: rec.setLoggerName(logger.getName());
237: rec.setThrown(th);
238: rec.setParameters(tokens);
239: logger.log(rec);
240: }
241: System.exit(1);
242: }
243: }
244: }
|