001: /**
002: * $Id: WindowsTasks.java,v 1.11 2007/03/07 00:27:39 ac120954 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.admin.common.context.PSConfigContext;
016: import com.sun.portal.admin.common.PSConfigConstants;
017: import com.sun.portal.log.common.PortalLogger;
018: import com.sun.portal.fabric.util.AntUtil;
019: import com.sun.portal.fabric.util.FileUtil;
020: import com.sun.portal.util.Platform;
021:
022: import java.util.*;
023: import java.util.logging.Logger;
024: import java.util.logging.Level;
025: import java.io.*;
026: import java.text.DateFormat;
027: import java.text.SimpleDateFormat;
028:
029: public class WindowsTasks extends OSTasksImpl {
030:
031: private String fs = Platform.fs;
032: private AntUtil antUtil;
033: private static Logger logger;
034: private static String AntFile = "winconfig.xml";
035: private static String LibDir;
036: private static String buildFileLoc;
037: private static String propFileLoc;
038: private static final String PS_CONFIG_PROPERTIES = "PS_CONFIG";
039: private static final String AT_CMD = "at";
040: private static final String ATID = "atid";
041:
042: public WindowsTasks(PSConfigContext globalContext) {
043: super (globalContext);
044: }
045:
046: public void schedule() {
047: try {
048: //unschedule();
049: String cmd = AT_CMD + " "
050: + WindowsTasks.toATCmdString(this .valuesMap);
051: Process p = Runtime.getRuntime().exec(cmd);
052: } catch (Exception e) {
053: logger.log(Level.SEVERE, e.toString());
054: }
055: }
056:
057: public void unschedule() {
058: try {
059: List tasks = WindowsTasks.getWindowsTasks();
060: Iterator it = tasks.iterator();
061: while (it.hasNext()) {
062: Map v = (Map) it.next();
063: String cmd = (String) v.get(COMMAND);
064: if (cmd != null
065: && cmd.equals(this .valuesMap.get(COMMAND))) {
066: String d = (String) v.get(DAYOFTHEWEEK);
067: String h = (String) v.get(HOUR);
068: String m = (String) v.get(MINUTE);
069: if (d != null
070: && d.equals(valuesMap
071: .get(this .DAYOFTHEWEEK))
072: && h != null
073: && h.equals(valuesMap.get(this .HOUR))
074: && m != null
075: && m.equals(valuesMap.get(this .MINUTE))) {
076: String atcmd = AT_CMD + " "
077: + v.get(WindowsTasks.ATID) + " /delete";
078: //System.out.println("Deleting task:" + cmd);
079: Process p = Runtime.getRuntime().exec(atcmd);
080: }
081: }
082: }
083:
084: } catch (Exception e) {
085: logger.log(Level.SEVERE, e.toString());
086: }
087: }
088:
089: public ArrayList schedules() {
090: String requestCommand = getValue(COMMAND);
091:
092: ArrayList al = new ArrayList();
093: try {
094: List tasks = WindowsTasks.getWindowsTasks();
095: Iterator it = tasks.iterator();
096: while (it.hasNext()) {
097: Map v = (Map) it.next();
098: if (requestCommand.equalsIgnoreCase((String) v
099: .get(WindowsTasks.COMMAND))) {
100: al.add(WindowsTasks.toInternalFormatString(v));
101: }
102: }
103: } catch (Exception e) {
104: logger.log(Level.SEVERE, e.toString());
105: }
106: return al;
107: }
108:
109: public String getPatchInfo() {
110: return null;
111: }
112:
113: public String getPatchVerbose() {
114: return null;
115: }
116:
117: public void createSymbolicLink(String source, String target) {
118: // Symbolic links not supported in windows
119: // So copy instead of link
120: // If source is a dir then copydir, else copyfile
121: File file = new File(source);
122: if (file.exists()) {
123: if (file.isDirectory()) {
124: FileUtil.copyDir(source, target);
125: } else {
126: FileUtil.copyFile(source, target);
127: }
128: }
129: }
130:
131: public void removeSymbolicLink(String target) {
132: File file = new File(target);
133: if (file.exists()) {
134: if (file.isDirectory()) {
135: FileUtil.deleteDir(target);
136: } else {
137: file.delete();
138: }
139: }
140: }
141:
142: public void addNetFilePAM() {
143: }
144:
145: private void init() {
146: logger = PortalLogger.getLogger(WindowsTasks.class);
147: antUtil = new AntUtil(context.getPSDataDir());
148: buildFileLoc = context.getPSConfigDir() + fs + AntFile;
149: propFileLoc = context.getPSConfigDir() + fs
150: + PSConfigConstants.PS_CONFIG_FILE;
151: }
152:
153: public void preconfig() {
154: init();
155: Vector targets = new Vector();
156: targets.add("pre-configtask");
157: antUtil.runant(buildFileLoc, targets, "WondowsConfig",
158: PS_CONFIG_PROPERTIES, propFileLoc);
159: }
160:
161: public void postconfig() {
162: init();
163: Vector targets = new Vector();
164: targets.add("post-configtask");
165: antUtil.runant(buildFileLoc, targets, "WondowsConfig",
166: PS_CONFIG_PROPERTIES, propFileLoc);
167: }
168:
169: public void preunconfig() {
170: init();
171: Vector targets = new Vector();
172: targets.add("pre-unconfigtask");
173: antUtil.runant(buildFileLoc, targets, "WondowsConfig",
174: PS_CONFIG_PROPERTIES, propFileLoc);
175: }
176:
177: public void postunconfig() {
178: init();
179: Vector targets = new Vector();
180: targets.add("post-unconfigtask");
181: antUtil.runant(buildFileLoc, targets, "WondowsConfig",
182: PS_CONFIG_PROPERTIES, propFileLoc);
183: }
184:
185: static public List getWindowsTasks() throws Exception {
186: String[] headers = new String[] { "Status", "ID", "Day",
187: "Time", "Command Line" };
188: String[] values = new String[] { "", "", "", "", "" };
189: int[] field_position = { 0, 0, 0, 0, 0 };
190:
191: List tasks = new ArrayList();
192:
193: Process p = Runtime.getRuntime().exec(AT_CMD);
194: InputStream is = p.getInputStream();
195: BufferedReader br = new BufferedReader(
196: new InputStreamReader(is));
197: String line;
198: boolean in_list = false;
199: while ((line = br.readLine()) != null) {
200: if (line.startsWith("Status")) {
201: for (int i = 0; i < 5; i++) {
202: field_position[i] = line.indexOf(headers[i]);
203: }
204: continue;
205: }
206: if (!in_list) {
207: if (line.startsWith("----------------")) {
208: in_list = true;
209: }
210: continue;
211: }
212: for (int i = 0; i < headers.length; i++) {
213: if (i < headers.length - 1) {
214: values[i] = line.substring(field_position[i],
215: field_position[i + 1]);
216: } else {
217: values[i] = line.substring(field_position[i]);
218: }
219: }
220: if (values[4].startsWith("\"") && values[4].endsWith("\"")) {
221: values[4] = values[4].substring(1,
222: values[4].length() - 1);
223: }
224: Map valuesMap = new HashMap();
225:
226: //Time
227: Date time = DateFormat.getTimeInstance(DateFormat.SHORT)
228: .parse(values[3]);
229: DateFormat hourF = new SimpleDateFormat("H");
230: DateFormat minF = new SimpleDateFormat("m");
231: String hours = hourF.format(time);
232: String minutes = minF.format(time);
233: valuesMap.put(WindowsTasks.HOUR, hours);
234: valuesMap.put(WindowsTasks.MINUTE, minutes);
235: valuesMap.put(WindowsTasks.COMMAND, values[4]);
236: String[] days = values[2].split(" ");
237: String daysString = "";
238: for (int i = 0; i < days.length; i++) {
239: if (days[i].equalsIgnoreCase("SU")) {
240: daysString += "0 ";
241: } else if (days[i].equalsIgnoreCase("M")) {
242: daysString += "1 ";
243: } else if (days[i].equalsIgnoreCase("T")) {
244: daysString += "2 ";
245: } else if (days[i].equalsIgnoreCase("W")) {
246: daysString += "3 ";
247: } else if (days[i].equalsIgnoreCase("TH")) {
248: daysString += "4 ";
249: } else if (days[i].equalsIgnoreCase("F")) {
250: daysString += "5 ";
251: } else if (days[i].equalsIgnoreCase("S")) {
252: daysString += "6 ";
253: }
254: }
255: valuesMap.put(WindowsTasks.DAYOFTHEWEEK, daysString.trim());
256: valuesMap.put(WindowsTasks.COMMAND, values[4]);
257: valuesMap.put(WindowsTasks.ATID, values[1]);
258: //valuesMap.put("ATLINE", line);
259: tasks.add(valuesMap);
260: }
261: return tasks;
262:
263: }
264:
265: static private String toInternalFormatString(Map valuesMap) {
266: return valuesMap.get(WindowsTasks.COMMAND) + " | "
267: + valuesMap.get(WindowsTasks.DAYOFTHEWEEK) + "@"
268: + valuesMap.get(WindowsTasks.HOUR) + ":"
269: + valuesMap.get(WindowsTasks.MINUTE);
270: }
271:
272: static private String toATCmdString(Map valuesMap) {
273: StringBuffer sb = new StringBuffer();
274: sb.append(valuesMap.get(WindowsTasks.HOUR) + ":"
275: + valuesMap.get(WindowsTasks.MINUTE) + " /every:");
276: String cmdString = valuesMap.get(WindowsTasks.HOUR) + ":"
277: + valuesMap.get(WindowsTasks.MINUTE) + "/every:";
278: String days = (String) valuesMap.get(WindowsTasks.DAYOFTHEWEEK);
279: char[] ca = days.toCharArray();
280: for (int i = 0; i < ca.length; i++) {
281: if (ca[i] == '0') {
282: sb.append("SU,");
283: } else if (ca[i] == '1') {
284: sb.append("M,");
285: } else if (ca[i] == '2') {
286: sb.append("T,");
287: } else if (ca[i] == '3') {
288: sb.append("W,");
289: } else if (ca[i] == '4') {
290: sb.append("TH,");
291: } else if (ca[i] == '5') {
292: sb.append("F,");
293: } else if (ca[i] == '6') {
294: sb.append("S,");
295: }
296: }
297: sb.append(" " + valuesMap.get(WindowsTasks.COMMAND));
298: return sb.toString();
299: }
300:
301: }
|