001: /**
002: * $Id: AMTaskUtil.java,v 1.9 2007/01/26 03:48:34 portalbld Exp $
003: * Copyright 2004 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.fabric.util;
014:
015: import java.io.File;
016: import java.io.FileWriter;
017: import java.io.IOException;
018: import java.util.ArrayList;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Set;
022: import java.util.logging.Level;
023: import java.util.logging.Logger;
024: import java.util.Random;
025:
026: import com.sun.portal.log.common.PortalLogger;
027: import com.sun.portal.util.Platform;
028:
029: /**
030: * This is a utility class implements the functionality tasks related to
031: * configuring services and other portal server related that modify Access
032: * Manager data
033: */
034: public class AMTaskUtil {
035:
036: private static final String fs = Platform.fs;
037: private String command;
038: private String amAdminDN;
039: private String amAdminPassword;
040: private File amPasswordFile = null;
041: private String amPasswordMode = null;
042: private String amPasswordData;
043: private static Logger logger = PortalLogger
044: .getLogger(AMTaskUtil.class);
045: private ExecuteUtil execUtil;
046:
047: /**
048: * Constructor for the Access Manager Utility class
049: * @param isBaseDir The install directory of Accesss Manager as String
050: * @param amAdminDN The Access Manager super user DN as String
051: * @param amAdminPassword Access Manager super user password as String
052: */
053: public AMTaskUtil(String isBaseDir, String amAdminDN,
054: String amAdminPassword) {
055:
056: this .amAdminDN = amAdminDN;
057: this .amAdminPassword = amAdminPassword;
058: command = isBaseDir + fs + "bin" + fs
059: + Platform.getCommand("amadmin");
060: // amadmin calls can generate a lot of output
061: // set storeOutput to false to prevent memory overflow
062: execUtil = new ExecuteUtil();
063: execUtil.storeOutput(false);
064: }
065:
066: /**
067: * Create a password file for the amadmin command
068: * If creation of password file fails for some reason, fallback on
069: * passing the amadmin password in through CLI args (non-secure)
070: */
071: private void initPasswordData() {
072: if (amPasswordMode == null) {
073: //try creating password file
074: amPasswordMode = "--passwordfile";
075: try {
076: StringBuffer fileData = new StringBuffer();
077: fileData.append(amAdminPassword);
078:
079: Random random = new Random();
080: String sTmpFile = "" + random.nextInt();
081: amPasswordFile = File.createTempFile(sTmpFile, "");
082: amPasswordFile.deleteOnExit();
083:
084: FileWriter fwriter = new FileWriter(amPasswordFile);
085: fwriter.write(fileData.toString());
086: fwriter.close();
087:
088: amPasswordData = amPasswordFile.getAbsolutePath();
089: } catch (IOException ex) {
090: logger.log(Level.SEVERE, "PSFB_CSPFU0034");
091: amPasswordMode = "--password";
092: amPasswordData = amAdminPassword;
093: }
094: }
095: }
096:
097: /**
098: * This method executes the amAdmin CLI to invoke the Request XMLs that are
099: * passed as a parameter
100: * @param requestFiles Set of amadmin request XML file paths
101: */
102: public void execAMRequests(List requestFiles) {
103:
104: // Check if the set is empty of null
105: if (requestFiles != null && !requestFiles.isEmpty()) {
106:
107: initPasswordData();
108:
109: String[] amAdminArgs = new String[7 + requestFiles.size()];
110:
111: amAdminArgs[0] = "--runasdn";
112: amAdminArgs[1] = amAdminDN;
113: amAdminArgs[2] = amPasswordMode;
114: amAdminArgs[3] = amPasswordData;
115: amAdminArgs[4] = "--verbose";
116: amAdminArgs[5] = "--continue";
117: amAdminArgs[6] = "--data";
118:
119: Iterator it = requestFiles.iterator();
120: int index = 7;
121: while (it.hasNext()) {
122: // Add this tagswapped xml file to the list of service XML
123: // files that will be loaded by amadmin
124: amAdminArgs[index] = (String) it.next();
125: index++;
126: }
127: // Invoke the amadmin to remove the set of services
128: logger.log(Level.INFO, "PSFB_CSPFU0035", requestFiles);
129: execUtil.exec(command, amAdminArgs);
130: } else {
131: logger.log(Level.INFO, "PSFB_CSPFU0036");
132: }
133: }
134:
135: /**
136: * This method process a list of .request files by tagswapping the
137: * tokens in these files with the values that are provided.
138: * These processed files are converted into XML files and
139: * the amadmin CLI is used to executed these requests XMLs
140: */
141: public void processRequestXML(String[] tokens, String[] values,
142: List requests, String tmpDir) {
143:
144: // Get an iteratror for the list of request files
145: Iterator itr = requests.iterator();
146: // Initialize a Set stores tagswapped request XML Paths
147: List swappedRequestPaths = new ArrayList();
148: // Locations for tagswapped request XML files
149: String reqTmpDirLoc = tmpDir + fs + FileUtil.getRandomDirName();
150:
151: // Create the tmp dir
152: File reqTmpDirDir = new File(reqTmpDirLoc);
153: logger.log(Level.FINEST, "PSFB_CSPFU0037", reqTmpDirLoc);
154: if (!reqTmpDirDir.mkdirs()) {
155: logger.log(Level.SEVERE, "PSFB_CSPFU0038");
156: }
157:
158: while (itr.hasNext()) {
159: // Get the request filename. The file extension is expected to
160: // be "request"
161: String requestFilePath = (String) itr.next();
162: File origRequestFile = new File(requestFilePath);
163:
164: // amadmin cli accepts only files with extension "xml"
165: // Set the destination file extension to be "xml"
166: String origReqName = origRequestFile.getName();
167: String xmlReqName = origReqName.replaceAll(".request",
168: ".xml");
169: // Create file handles for source and destination
170: File destRequestFile = new File(reqTmpDirLoc + fs
171: + xmlReqName);
172:
173: // Check if the source .request file exists before attempting to
174: // tagswap and adding to list of requests to be loaded
175: if (origRequestFile.exists()) {
176: // Copy the original .request file as .xml file
177: logger
178: .log(Level.FINEST, "PSFB_CSPFU0039",
179: new Object[] { origRequestFile,
180: destRequestFile });
181: FileUtil.copyFile(origRequestFile, destRequestFile);
182:
183: // Tag swap the copied xml file to prepare it to be loaded
184: logger.log(Level.FINEST, "PSFB_CSPFU0040",
185: destRequestFile);
186: FileUtil.replaceTokensInFile(destRequestFile, tokens,
187: values);
188:
189: // Add this file path to the set of request files to load
190: swappedRequestPaths.add(destRequestFile
191: .getAbsolutePath());
192: } else {
193: logger.log(Level.SEVERE, "PSFB_CSPFU0041",
194: requestFilePath);
195: }
196: }
197:
198: // Load the requests using the AMTaskUtils
199: logger.log(Level.FINEST, "PSFB_CSPFU0042");
200: execAMRequests(swappedRequestPaths);
201: FileUtil.deleteDir(reqTmpDirDir);
202: }
203:
204: /**
205: * This method executes the amAdmin CLI to invoke the Request XMLs that are
206: * passed as a parameter
207: * @param requestFiles Set of amadmin request XML file paths
208: *
209: * @return Gives the output of the amadmin command
210: */
211: // WARNING: Should be called only when the output is not too long.
212: public String execAMRequest(List requestFiles) {
213: String result = null;
214: // Check if the set is empty of null
215: if (requestFiles != null && !requestFiles.isEmpty()) {
216:
217: initPasswordData();
218:
219: String[] amAdminArgs = new String[6 + requestFiles.size()];
220:
221: amAdminArgs[0] = "--runasdn";
222: amAdminArgs[1] = amAdminDN;
223: amAdminArgs[2] = amPasswordMode;
224: amAdminArgs[3] = amPasswordData;
225: amAdminArgs[4] = "--continue";
226: amAdminArgs[5] = "--data";
227:
228: Iterator it = requestFiles.iterator();
229: int index = 6;
230: while (it.hasNext()) {
231: // Add this tagswapped xml file to the list of service XML
232: // files that will be loaded by amadmin
233: amAdminArgs[index] = (String) it.next();
234: index++;
235: }
236: // Invoke the amadmin to remove the set of services
237: logger.log(Level.INFO, "PSFB_CSPFU0043", requestFiles);
238: execUtil.storeOutput(true);
239: execUtil.exec(command, amAdminArgs);
240: result = execUtil.getOutput();
241: } else {
242: logger.log(Level.INFO, "PSFB_CSPFU0044");
243: }
244: return result;
245: }
246:
247: }
|