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: package com.sun.portal.subscriptions.admin.util;
007:
008: import com.sun.portal.search.admin.CSConfig;
009: import java.util.*;
010: import java.io.*;
011: import java.lang.*;
012:
013: import com.sun.portal.subscriptions.admin.SubscriptionsAdminConstants;
014:
015: /*
016: * This class should really be renamed UnixOSTask
017: * since it now supports multiple unix flavors
018: */
019:
020: public class SunOSTask implements Task {
021:
022: public static final String CRON_BEGIN = "# Subscriptions cron entries start here. Don't modify this section";
023: public static final String CRON_END = "# Subscriptions cron entries end here.";
024: private static final String CRONTAB = "/bin/crontab";
025: private static final String CRONTAB_LINUX = "/usr/bin/crontab";
026: private static final String CRONTAB_HPUX = "/usr/bin/crontab";
027: private static String crontab = CRONTAB;
028: static {
029: if (isLinux()) {
030: crontab = CRONTAB_LINUX;
031: }
032: if (isHpux()) {
033: crontab = CRONTAB_HPUX;
034: }
035: }
036:
037: static public void main(String[] args) {
038: if (args.length < 5) {
039: System.out
040: .println("Usage:CronController action org serverroot hours minutes days_of_week");
041: return;
042: }
043:
044: SunOSTask cron = new SunOSTask();
045: cron.init(args[0], args[1], args[2]);
046: cron.setValue(HOURS, args[3]);
047: cron.setValue(MINUTES, args[4]);
048: cron.setValue(DAYS_OF_WEEK, args[5]);
049: cron.Update();
050: }
051:
052: HashMap valuesMap = new HashMap();
053: String name = null;
054: String organization = null;
055:
056: public SunOSTask() {
057: }
058:
059: public void init(String name, String organization, String serverRoot) {
060: this .name = name;
061: this .organization = organization;
062: String headerName = " " + name + ":" + organization;
063: if (name.indexOf("Start") != -1)
064: setValue(COMMAND, serverRoot + File.separator
065: + "run-cs-cli profiler run -o \"" + organization
066: + "\"");
067: else
068: setValue(COMMAND, serverRoot + File.separator
069: + "run-cs-cli profiler stop -o \"" + organization
070: + "\"");
071: try {
072: Process process = Runtime.getRuntime()
073: .exec(crontab + " -l");
074: InputStream instream = process.getInputStream();
075: BufferedReader buffReader = new BufferedReader(
076: new InputStreamReader(instream));
077: String temp = null;
078: boolean inSection = false;
079: boolean gotIt = false;
080: String theName = null;
081: while ((temp = buffReader.readLine()) != null) {
082: if (inSection) {
083: if (temp.startsWith(CRON_END)) {
084: break;
085: }
086: if (temp.startsWith("#")) {
087: theName = temp.substring(1);
088: continue;
089: }
090: if (theName != null && theName.equals(headerName)) {
091:
092: int i = 0;
093: String f[] = new String[6];
094: StringTokenizer st = new StringTokenizer(temp,
095: " ");
096: while (st.hasMoreTokens()) {
097: f[i] = st.nextToken();
098: if (i == 5) {
099: setValue(HOURS, f[1]);
100: setValue(MINUTES, f[0]);
101: setValue(DAYS_OF_WEEK, f[4]);
102: gotIt = true;
103: break;
104: }
105: i++;
106: }
107: theName = null;
108: if (gotIt) {
109: break;
110: }
111: }
112: } else if (temp.startsWith(CRON_BEGIN)) {
113: inSection = true;
114: }
115:
116: }
117: } catch (Exception e) {
118: }
119: }
120:
121: public void Remove() {
122: Update(true);
123: }
124:
125: public void Update() {
126: Update(false);
127: }
128:
129: private void Update(boolean isDelete) {
130: try {
131: String headerName = " " + name + ":" + organization;
132: Process process = Runtime.getRuntime()
133: .exec(crontab + " -l");
134: InputStream instream = process.getInputStream();
135: BufferedReader buffReader = new BufferedReader(
136: new InputStreamReader(instream));
137: String tmpFileName = "/tmp/ipscron.txt."
138: + Long.toString(System.currentTimeMillis());
139: FileWriter outf = new FileWriter(tmpFileName);
140: String temp = null;
141: boolean inSection = false;
142: boolean gotIt = false;
143: String theName = null;
144:
145: while ((temp = buffReader.readLine()) != null) {
146: if (gotIt) {
147: outf.write(temp + "\n");
148: continue;
149: }
150:
151: if (inSection) {
152: if (temp.startsWith(CRON_END)) {
153: if (!isDelete) {
154: outf.write("#" + headerName + "\n");
155: outf.write(getValue(MINUTES) + " "
156: + getValue(HOURS) + " * * "
157: + getValue(DAYS_OF_WEEK) + " "
158: + getValue(COMMAND) + "\n");
159: }
160: outf.write(CRON_END + "\n");
161: gotIt = true;
162: continue;
163: }
164:
165: if (temp.startsWith("#")) {
166: theName = temp.substring(1);
167: if (!isDelete || !theName.equals(headerName)) {
168: outf.write(temp + "\n");
169: }
170: continue;
171: }
172: if (theName != null && theName.equals(headerName)) {
173: if (!isDelete) {
174: outf.write(getValue(MINUTES) + " "
175: + getValue(HOURS) + " * * "
176: + getValue(DAYS_OF_WEEK) + " "
177: + getValue(COMMAND) + "\n");
178: }
179: gotIt = true;
180: continue;
181: }
182: } else if (temp.startsWith(CRON_BEGIN)) {
183: System.out.println("CRON_BEGIN reached");
184: inSection = true;
185: }
186: outf.write(temp + "\n");
187: }
188: if (!inSection) {
189: outf.write(CRON_BEGIN + "\n");
190: outf.write("#" + headerName + "\n");
191: outf.write(getValue(MINUTES) + " " + getValue(HOURS)
192: + " * * " + getValue(DAYS_OF_WEEK) + " "
193: + getValue(COMMAND) + "\n");
194: outf.write(CRON_END + "\n");
195: }
196:
197: outf.close();
198: process = Runtime.getRuntime().exec(
199: crontab + " " + tmpFileName);
200: try {
201: process.waitFor();
202: } catch (java.lang.InterruptedException e) {
203: // Might need to do something here
204: }
205: File f = new File(tmpFileName);
206: f.delete();
207:
208: } catch (Exception e) {
209: }
210: /* need to add the new entry */
211: }
212:
213: /**
214: * Find a configuration parameter
215: */
216: public String getValue(String param) {
217: return (String) valuesMap.get(param);
218: }
219:
220: /**
221: * sets a value in the cron
222: */
223: public void setValue(String key, String value) {
224: if (valuesMap != null) {
225: if (valuesMap.containsKey(key)) {
226: valuesMap.remove(key);
227: }
228: valuesMap.put(key, value);
229: }
230: }
231:
232: /**
233: * check if operating system is Linux
234: * @return true if operating system is Linux
235: */
236: public static boolean isLinux() {
237: if (System.getProperty("os.name").startsWith("Linux")) {
238: return true;
239: } else {
240: return false;
241: }
242: }
243:
244: /**
245: * check if operating system is Linux
246: * @return true if operating system is Linux
247: */
248: public static boolean isHpux() {
249: if (System.getProperty("os.name").startsWith("HP-UX")) {
250: return true;
251: } else {
252: return false;
253: }
254: }
255:
256: }
|