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