001: /**
002: * $Id: UnixTasks.java,v 1.10 2006/09/04 11:15:15 mr161608 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.os;
014:
015: import com.sun.portal.fabric.util.FileUtil;
016: import com.sun.portal.admin.common.context.PSConfigContext;
017: import com.sun.portal.log.common.PortalLogger;
018:
019: import java.util.logging.Logger;
020: import java.util.logging.Level;
021: import java.util.*;
022: import java.io.*;
023:
024: public abstract class UnixTasks extends OSTasksImpl {
025:
026: private String finalPatchList = "";
027: private static final String CRONTAB = "/usr/bin/crontab";
028: private static Logger logger = PortalLogger
029: .getLogger(UnixTasks.class);
030:
031: public UnixTasks(PSConfigContext globalContext) {
032: super (globalContext);
033: }
034:
035: public void schedule() {
036: String event = getValue(MINUTE) + " " + getValue(HOUR)
037: + " * * " + getValue(DAYOFTHEWEEK) + " "
038: + getValue(COMMAND);
039:
040: try {
041: String fileName = "/tmp/ps_schedule."
042: + Long.toString(System.currentTimeMillis());
043: File f = new File(fileName);
044: FileWriter fw = new FileWriter(f);
045: Process p = Runtime.getRuntime().exec(CRONTAB + " -l");
046: InputStream is = p.getInputStream();
047: BufferedReader br = new BufferedReader(
048: new InputStreamReader(is));
049: boolean exist = false;
050: String line;
051: while ((line = br.readLine()) != null) {
052: if (line.equals(event)) {
053: exist = true;
054: }
055: fw.write(line + "\n");
056: }
057: if (!exist) {
058: fw.write(event + "\n");
059: }
060: fw.close();
061: p = Runtime.getRuntime().exec(CRONTAB + " " + fileName);
062: p.waitFor();
063: f.delete();
064: } catch (Exception e) {
065: logger.log(Level.SEVERE, e.toString());
066: }
067: }
068:
069: public void unschedule() {
070: String event = getValue(MINUTE) + " " + getValue(HOUR)
071: + " * * " + getValue(DAYOFTHEWEEK) + " "
072: + getValue(COMMAND);
073:
074: try {
075: String fileName = "/tmp/ps_unschedule."
076: + Long.toString(System.currentTimeMillis());
077: File f = new File(fileName);
078: FileWriter fw = new FileWriter(f);
079: Process p = Runtime.getRuntime().exec(CRONTAB + " -l");
080: InputStream is = p.getInputStream();
081: BufferedReader br = new BufferedReader(
082: new InputStreamReader(is));
083: String line;
084: while ((line = br.readLine()) != null) {
085: if (!line.equals(event)) {
086: fw.write(line + "\n");
087: }
088: }
089: fw.close();
090: p = Runtime.getRuntime().exec(CRONTAB + " " + fileName);
091: p.waitFor();
092: f.delete();
093: } catch (Exception e) {
094: logger.log(Level.SEVERE, e.toString());
095: }
096: }
097:
098: public ArrayList schedules() {
099: String requestCommand = getValue(COMMAND);
100:
101: ArrayList al = new ArrayList();
102: try {
103: Process p = Runtime.getRuntime().exec(CRONTAB + " -l");
104: InputStream is = p.getInputStream();
105: BufferedReader br = new BufferedReader(
106: new InputStreamReader(is));
107: String line;
108: while ((line = br.readLine()) != null) {
109: if (!line.startsWith("#")) {
110: String[] strings = line.split("[ \t]");
111: String minute = strings[0];
112: String hour = strings[1];
113: String day = strings[2];
114: String month = strings[3];
115: String dayOfTheWeek = strings[4];
116: String command = "";
117: for (int i = 5; i < strings.length; i++) {
118: command = command + " " + strings[i];
119: }
120: if (command.trim().equals(requestCommand.trim())) {
121: String schedule = command.trim() + " | "
122: + dayOfTheWeek + "@" + hour + ":"
123: + minute;
124: al.add(schedule);
125: }
126: }
127: }
128: } catch (Exception e) {
129: logger.log(Level.SEVERE, e.toString());
130: }
131: return al;
132: }
133:
134: private void getFinalPatchList() {
135: String command = "/bin/pkginfo";
136: execUtil.storeOutput(true);
137: execUtil.exec(command, null);
138: String result = execUtil.getOutput();
139: StringTokenizer tokens = new StringTokenizer(result, "\n");
140:
141: while (tokens.hasMoreTokens()) {
142: String token = tokens.nextToken();
143: if ((token.indexOf("SUNW") != -1)
144: && (token.indexOf("Portal Server") != -1)) {
145: String subStr = token.substring(token.indexOf("SUNW"));
146: String pkgName = subStr.substring(0, subStr
147: .indexOf(" "));
148: String pkginfoFilePath = "/var/sadm/pkg/" + pkgName
149: + "/pkginfo";
150: File pkginfoFile = new File(pkginfoFilePath);
151: String patchList = null;
152: if (pkginfoFile.exists()) {
153: patchList = FileUtil.findTextInFile(pkginfoFile,
154: "PATCHLIST=");
155: if (patchList != null && patchList.length() != 0) {
156: if (patchList.substring(10) != null
157: && patchList.substring(10).length() != 0) {
158: finalPatchList = finalPatchList + " "
159: + patchList.substring(10);
160: }
161: }
162: }
163: }
164: finalPatchList = removeRepeatedPatches(finalPatchList);
165: }
166:
167: }
168:
169: public String getPatchInfo() {
170: StringBuffer sb = new StringBuffer();
171: getFinalPatchList();
172: if (!finalPatchList.equals("") && finalPatchList.length() != 0) {
173: sb
174: .append("\nCurrently, following patches are installed: \n");
175: sb.append("------------------------------------------- \n");
176: sb.append(finalPatchList.substring(1));
177: } else {
178: sb
179: .append("\nCurrently, there are no patches installed !!\n");
180: }
181: return sb.toString();
182: }
183:
184: public String getPatchVerbose() {
185: StringBuffer sb = new StringBuffer();
186: getFinalPatchList();
187: if (!finalPatchList.equals("") && finalPatchList.length() != 0) {
188: StringTokenizer tokens = new StringTokenizer(finalPatchList
189: .substring(1), " ");
190: sb.append("\n");
191: while (tokens.hasMoreTokens()) {
192: String token = tokens.nextToken();
193: sb.append(token);
194: sb.append("-------------------------------------- \n");
195: String patchDir = "/var/sadm/patch/" + token;
196: File readmeFile = new File(patchDir + "/README."
197: + token);
198: if (!FileUtil.directoryExists(patchDir)) {
199: sb
200: .append("For some unknown reason, no more details about this patch is available. \n");
201: } else if (!readmeFile.exists()) {
202: sb
203: .append(readmeFile
204: + " is not readable or does not exists, no more information available for this patch. \n");
205: } else {
206: sb.append(FileUtil.findTextInFile(readmeFile,
207: "Synopsis")
208: + "\n");
209: }
210: }
211: } else {
212: sb
213: .append("\nCurrently, there are no patches installed !!\n");
214: }
215: return sb.toString();
216: }
217:
218: private String removeRepeatedPatches(String finalPatchList) {
219: StringTokenizer patches = new StringTokenizer(finalPatchList,
220: " ");
221: String PatchList = "";
222: HashSet set = new HashSet();
223:
224: while (patches.hasMoreTokens()) {
225: String tmp = (String) patches.nextToken();
226: if (set.add(tmp))
227: PatchList = PatchList + " " + tmp;
228: }
229: return PatchList;
230: }
231:
232: }
|