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;
007:
008: import java.util.*;
009: import java.text.*;
010: import java.io.IOException;
011:
012: import com.sun.portal.search.admin.cli.RobotController;
013: import com.sun.portal.search.robot.*;
014: import com.sun.portal.search.util.SearchConfig;
015:
016: import javax.servlet.http.HttpServletRequest;
017: import javax.servlet.http.HttpServletResponse;
018: import com.iplanet.jato.RequestContext;
019:
020: import com.iplanet.jato.view.event.DisplayEvent;
021: import com.iplanet.jato.view.event.ChildDisplayEvent;
022: import com.iplanet.jato.view.event.RequestInvocationEvent;
023:
024: import com.iplanet.jato.view.html.StaticTextField;
025: import com.iplanet.jato.view.html.TextField;
026: import com.iplanet.jato.view.html.CheckBox;
027: import com.iplanet.jato.view.html.Button;
028: import com.iplanet.jato.view.html.Option;
029: import com.iplanet.jato.view.html.OptionList;
030:
031: import com.iplanet.jato.view.View;
032: import com.iplanet.jato.view.ViewBean;
033: import com.iplanet.jato.view.ViewBeanBase;
034:
035: import com.iplanet.jato.ViewBeanManager;
036:
037: import com.iplanet.jato.model.ModelControlException;
038:
039: import com.iplanet.am.console.components.view.html.MessageBox;
040: import com.iplanet.am.console.components.view.html.IPlanetButton;
041:
042: /**
043: * iPS admin console view bean: TODO
044: */
045: public class ControlViewBean extends CSViewBeanBase {
046: public static final String DEFAULT_DISPLAY_URL = "/ps/searchadmin/Control.jsp";
047: public static final String PAGE_NAME = "Control";
048: public static final String REFRESH_SCRIPT = "RefreshScript";
049: public static final String ROBOT_STATUS = "RobotStatus";
050: public static final String LBL_ROBOT_STATUS = "r_status_label";
051: public static final String BTN_START = "start";
052: public static final String BTN_STOP = "stop";
053: public static final String STATUS_UPDATE_TIME = "UpdateTime";
054: public static final String STATUS_STARTING = "StartingPoints";
055: public static final String STATUS_URLPOOL = "URLsWaiting";
056: public static final String STATUS_EXTRACTING = "Extracting";
057: public static final String STATUS_FILTERLING = "Filtering";
058: public static final String STATUS_INDEXING = "Indexing";
059: public static final String STATUS_EXCLUDE_BY_FILTER = "ExcludeByFilter";
060: public static final String STATUS_EXCLUDE_BY_ERROR = "ExcludeByError";
061: public static final String STATUS_RD_NUM = "RDNumber";
062: public static final String STATUS_RD_SIZE = "RDSize";
063: public static final String STATUS_URL_RETRIEVED = "URLRetrieved";
064: public static final String STATUS_RD_AVE_SIZE = "RDAverageSize";
065: public static final String STATUS_RUNNING_TIME = "RunningTime";
066:
067: public static final String BTN_PAUSE = "pause";
068: public static final String BTN_RESUME = "resume";
069: public static final String BTN_FRESH = "fresh";
070: private boolean isRunning = false;
071: private boolean isPaused = false;
072: private RobotController robotController = null;
073:
074: private static String states[] = { "0", "0", "0", "0", "0", "0",
075: "0", "0", "0", "0", "0", "0" };
076: private static Date cachedDate = null;
077:
078: /**
079: * constructor
080: *
081: * @param PageName of this view bean
082: * @param displayURL default display URL
083: */
084: public ControlViewBean() {
085: super (PAGE_NAME);
086: setDefaultDisplayURL(DEFAULT_DISPLAY_URL);
087: registerChildren();
088: robotController = new RobotController(CSConfig.getServerRoot(),
089: CSConfig.getBinPath(), CSConfig.getLibDir(), CSConfig
090: .getLibPath(), null, null, null);
091: }
092:
093: /**
094: * register child component
095: */
096: protected void registerChildren() {
097: registerChild(REFRESH_SCRIPT, StaticTextField.class);
098: registerChild(ROBOT_STATUS, StaticTextField.class);
099: registerChild(BTN_START, IPlanetButton.class);
100: registerChild(BTN_STOP, IPlanetButton.class);
101: registerChild(BTN_PAUSE, IPlanetButton.class);
102: registerChild(BTN_RESUME, IPlanetButton.class);
103: registerChild(BTN_FRESH, IPlanetButton.class);
104: registerChild(this .STATUS_UPDATE_TIME, StaticTextField.class);
105: registerChild(this .STATUS_STARTING, StaticTextField.class);
106: registerChild(this .STATUS_URLPOOL, StaticTextField.class);
107: registerChild(this .STATUS_EXTRACTING, StaticTextField.class);
108: registerChild(this .STATUS_FILTERLING, StaticTextField.class);
109: registerChild(this .STATUS_INDEXING, StaticTextField.class);
110: registerChild(this .STATUS_EXCLUDE_BY_FILTER,
111: StaticTextField.class);
112: registerChild(this .STATUS_EXCLUDE_BY_ERROR,
113: StaticTextField.class);
114: registerChild(this .STATUS_RD_NUM, StaticTextField.class);
115: registerChild(this .STATUS_RD_SIZE, StaticTextField.class);
116: registerChild(this .STATUS_URL_RETRIEVED, StaticTextField.class);
117: registerChild(this .STATUS_RD_AVE_SIZE, StaticTextField.class);
118: registerChild(this .STATUS_RUNNING_TIME, StaticTextField.class);
119:
120: }
121:
122: /**
123: * create child component
124: *
125: * @param name of component
126: * @return child component
127: */
128: protected View createChild(String name) {
129: View child = super .createChild(name);
130: if (child != null)
131: return child;
132: if (name.equals(REFRESH_SCRIPT)) {
133: return new StaticTextField(this , REFRESH_SCRIPT, "");
134: }
135: if (name.equals(ROBOT_STATUS)) {
136: return new StaticTextField(this , ROBOT_STATUS, "");
137: }
138: if (name.equals(BTN_START)) {
139: return new IPlanetButton(this , BTN_START, "");
140: }
141: if (name.equals(BTN_STOP)) {
142: return new IPlanetButton(this , BTN_STOP, "");
143: }
144: if (name.equals(BTN_PAUSE)) {
145: return new IPlanetButton(this , BTN_PAUSE, "");
146: }
147: if (name.equals(BTN_RESUME)) {
148: return new IPlanetButton(this , BTN_RESUME, "");
149: }
150: if (name.equals(BTN_FRESH)) {
151: return new IPlanetButton(this , BTN_FRESH, "");
152: }
153: if (name.equals(this .STATUS_UPDATE_TIME)) {
154: return new StaticTextField(this , this .STATUS_UPDATE_TIME,
155: "");
156: }
157: if (name.equals(this .STATUS_STARTING)) {
158: return new StaticTextField(this , this .STATUS_STARTING, "");
159: }
160: if (name.equals(this .STATUS_URLPOOL)) {
161: return new StaticTextField(this , this .STATUS_URLPOOL, "");
162: }
163: if (name.equals(this .STATUS_EXTRACTING)) {
164: return new StaticTextField(this , this .STATUS_EXTRACTING, "");
165: }
166: if (name.equals(this .STATUS_FILTERLING)) {
167: return new StaticTextField(this , this .STATUS_FILTERLING, "");
168: }
169: if (name.equals(this .STATUS_INDEXING)) {
170: return new StaticTextField(this , this .STATUS_INDEXING, "");
171: }
172: if (name.equals(this .STATUS_EXCLUDE_BY_FILTER)) {
173: return new StaticTextField(this ,
174: this .STATUS_EXCLUDE_BY_FILTER, "");
175: }
176: if (name.equals(this .STATUS_EXCLUDE_BY_ERROR)) {
177: return new StaticTextField(this ,
178: this .STATUS_EXCLUDE_BY_ERROR, "");
179: }
180: if (name.equals(this .STATUS_RD_NUM)) {
181: return new StaticTextField(this , this .STATUS_RD_NUM, "");
182: }
183: if (name.equals(this .STATUS_RD_SIZE)) {
184: return new StaticTextField(this , this .STATUS_RD_SIZE, "");
185: }
186: if (name.equals(this .STATUS_URL_RETRIEVED)) {
187: return new StaticTextField(this , this .STATUS_URL_RETRIEVED,
188: "");
189: }
190: if (name.equals(this .STATUS_RD_AVE_SIZE)) {
191: return new StaticTextField(this , this .STATUS_RD_AVE_SIZE,
192: "");
193: }
194: if (name.equals(this .STATUS_RUNNING_TIME)) {
195: return new StaticTextField(this , this .STATUS_RUNNING_TIME,
196: "");
197: }
198: throw new IllegalArgumentException("Invalid child name ["
199: + name + "]");
200: }
201:
202: /** begin displaying page. we set the required information
203: *
204: * @param event display event
205: * @throws ModelControlException if problem access value of component
206: */
207: public void beginDisplay(DisplayEvent event) {
208: setPageEncoding();
209: setDisplayFieldValue(BTN_START,
210: getLocalizedString("robot.start"));
211: setDisplayFieldValue(BTN_STOP, getLocalizedString("robot.stop"));
212: setDisplayFieldValue(BTN_PAUSE,
213: getLocalizedString("robot.pause"));
214: setDisplayFieldValue(BTN_RESUME,
215: getLocalizedString("robot.resume"));
216: setDisplayFieldValue(BTN_FRESH,
217: getLocalizedString("robot.freshstart"));
218:
219: // Get request attributes and store them as Page Session Attributes
220: RequestContext rc = getRequestContext();
221: HttpServletRequest req = rc.getRequest();
222: DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG,
223: DateFormat.LONG, getUserLocale());
224: if (robotController.isRunning()) {
225: isRunning = true;
226: isPaused = robotController.isPaused();
227: states = this .robotController.getRobotStates();
228: cachedDate = new Date();
229: setDisplayFieldValue(this .STATUS_UPDATE_TIME, df
230: .format(cachedDate));
231: String localizedStatus = getLocalizedString("robot.status.unknown");
232:
233: if (states[11].equalsIgnoreCase("Idle")) {
234: localizedStatus = getLocalizedString("robot.status.idle");
235: } else if (states[11].equalsIgnoreCase("Running")) {
236: localizedStatus = getLocalizedString("robot.status.running");
237: } else if (states[11].equalsIgnoreCase("Paused")) {
238: localizedStatus = getLocalizedString("robot.status.paused");
239: } else {
240: localizedStatus = getLocalizedString("robot.status.unknown");
241: }
242:
243: setDisplayFieldValue(ROBOT_STATUS, localizedStatus);
244: setDisplayFieldValue(this .STATUS_STARTING, CSConfig
245: .getRobotConfig().numOfStartingPoint(true));
246:
247: Object[] msgArgs;
248: msgArgs = getMessageArguments(states[0]);
249: setDisplayFieldValue(this .STATUS_URLPOOL, getMessageFormat(
250: msgArgs,
251: getLocalizedString("robot.urlwaiting.pattern")));
252: /*
253: msgArgs = getMessageArguments(
254: states[1] ,
255: getLocalizedString("robot.connpersec.1"),
256: getLocalizedString("robot.connpersec.2") );
257: setDisplayFieldValue(this.STATUS_EXTRACTING, getMessageFormat(
258: msgArgs, getLocalizedString("robot.connpersec.pattern")));
259: */
260: msgArgs = getMessageArguments(states[1]);
261: setDisplayFieldValue(
262: this .STATUS_EXTRACTING,
263: getMessageFormat(
264: msgArgs,
265: getLocalizedString("robot.connpersec.pattern")));
266:
267: msgArgs = getMessageArguments(states[2]);
268: setDisplayFieldValue(
269: this .STATUS_FILTERLING,
270: getMessageFormat(
271: msgArgs,
272: getLocalizedString("robot.urlrejected.pattern")));
273:
274: msgArgs = getMessageArguments(states[3]);
275: setDisplayFieldValue(
276: this .STATUS_INDEXING,
277: getMessageFormat(
278: msgArgs,
279: getLocalizedString("robot.urlpersec.pattern")));
280:
281: msgArgs = getMessageArguments(states[4]);
282: setDisplayFieldValue(
283: this .STATUS_EXCLUDE_BY_FILTER,
284: getMessageFormat(
285: msgArgs,
286: getLocalizedString("robot.excludedbyfilter.pattern")));
287:
288: msgArgs = getMessageArguments(states[5]);
289: setDisplayFieldValue(
290: this .STATUS_EXCLUDE_BY_ERROR,
291: getMessageFormat(
292: msgArgs,
293: getLocalizedString("robot.excludedbyerror.pattern")));
294:
295: msgArgs = getMessageArguments(states[6]);
296: setDisplayFieldValue(this .STATUS_RD_NUM, getMessageFormat(
297: msgArgs, getLocalizedString("robot.rds.pattern")));
298:
299: msgArgs = getMessageArguments(states[7]);
300: setDisplayFieldValue(this .STATUS_RD_SIZE, getMessageFormat(
301: msgArgs,
302: getLocalizedString("robot.rdgenerated.pattern")));
303:
304: msgArgs = getMessageArguments(states[8]);
305: setDisplayFieldValue(
306: this .STATUS_URL_RETRIEVED,
307: getMessageFormat(
308: msgArgs,
309: getLocalizedString("robot.retrieved.pattern")));
310:
311: msgArgs = getMessageArguments(states[9]);
312: setDisplayFieldValue(
313: this .STATUS_RD_AVE_SIZE,
314: getMessageFormat(msgArgs,
315: getLocalizedString("robot.avgsize.pattern")));
316:
317: msgArgs = getMessageArguments(getRunTime(states[10]));
318: setDisplayFieldValue(
319: this .STATUS_RUNNING_TIME,
320: getMessageFormat(
321: msgArgs,
322: getLocalizedString("robot.runningtime.pattern")));
323:
324: String ft = SearchConfig
325: .getValue(SearchConfig.ROBOT_REFRESH);
326: if (ft == null) {
327: ft = "30000";
328: }
329: if (!isPaused) {
330: setDisplayFieldValue(
331: this .REFRESH_SCRIPT,
332: "<script>\nvar timerid\nfunction freshPage() {\nwindow.location.reload();\n}\ntimerid = setTimeout(freshPage, "
333: + ft + ");\n</script>\n");
334: }
335:
336: } else {
337: isRunning = false;
338: setDisplayFieldValue(this .ROBOT_STATUS,
339: getLocalizedString("robot.status.off"));
340: setDisplayFieldValue(this .STATUS_STARTING, CSConfig
341: .getRobotConfig().numOfStartingPoint(true));
342:
343: if (states[1].compareTo("0") == 0) {
344: setDisplayFieldValue(this .STATUS_UPDATE_TIME, df
345: .format(new Date()));
346: setDisplayFieldValue(this .STATUS_URLPOOL, "");
347: setDisplayFieldValue(this .STATUS_FILTERLING, "");
348: setDisplayFieldValue(this .STATUS_EXTRACTING, "");
349: setDisplayFieldValue(this .STATUS_EXCLUDE_BY_FILTER, "");
350: setDisplayFieldValue(this .STATUS_EXCLUDE_BY_ERROR, "");
351: setDisplayFieldValue(this .STATUS_RD_NUM, "");
352: setDisplayFieldValue(this .STATUS_RD_SIZE, "");
353: setDisplayFieldValue(this .STATUS_URL_RETRIEVED, "");
354: setDisplayFieldValue(this .STATUS_RD_AVE_SIZE, "");
355: setDisplayFieldValue(this .STATUS_RUNNING_TIME, "");
356: } else {
357: setDisplayFieldValue(this .STATUS_UPDATE_TIME, df
358: .format(cachedDate));
359:
360: Object[] msgArgs;
361: msgArgs = getMessageArguments(states[0]);
362: setDisplayFieldValue(
363: this .STATUS_URLPOOL,
364: getMessageFormat(
365: msgArgs,
366: getLocalizedString("robot.urlwaiting.pattern")));
367:
368: msgArgs = getMessageArguments(states[1]);
369: setDisplayFieldValue(
370: this .STATUS_EXTRACTING,
371: getMessageFormat(
372: msgArgs,
373: getLocalizedString("robot.connpersec.pattern")));
374:
375: msgArgs = getMessageArguments(states[2]);
376: setDisplayFieldValue(
377: this .STATUS_FILTERLING,
378: getMessageFormat(
379: msgArgs,
380: getLocalizedString("robot.urlrejected.pattern")));
381:
382: msgArgs = getMessageArguments(states[3]);
383: setDisplayFieldValue(
384: this .STATUS_INDEXING,
385: getMessageFormat(
386: msgArgs,
387: getLocalizedString("robot.urlpersec.pattern")));
388:
389: msgArgs = getMessageArguments(states[4]);
390: setDisplayFieldValue(
391: this .STATUS_EXCLUDE_BY_FILTER,
392: getMessageFormat(
393: msgArgs,
394: getLocalizedString("robot.excludedbyfilter.pattern")));
395:
396: msgArgs = getMessageArguments(states[5]);
397: setDisplayFieldValue(
398: this .STATUS_EXCLUDE_BY_ERROR,
399: getMessageFormat(
400: msgArgs,
401: getLocalizedString("robot.excludedbyerror.pattern")));
402:
403: msgArgs = getMessageArguments(states[6]);
404: setDisplayFieldValue(
405: this .STATUS_RD_NUM,
406: getMessageFormat(msgArgs,
407: getLocalizedString("robot.rds.pattern")));
408:
409: msgArgs = getMessageArguments(states[7]);
410: setDisplayFieldValue(
411: this .STATUS_RD_SIZE,
412: getMessageFormat(
413: msgArgs,
414: getLocalizedString("robot.rdgenerated.pattern")));
415:
416: msgArgs = getMessageArguments(states[8]);
417: setDisplayFieldValue(
418: this .STATUS_URL_RETRIEVED,
419: getMessageFormat(
420: msgArgs,
421: getLocalizedString("robot.retrieved.pattern")));
422:
423: msgArgs = getMessageArguments(states[9]);
424: setDisplayFieldValue(
425: this .STATUS_RD_AVE_SIZE,
426: getMessageFormat(
427: msgArgs,
428: getLocalizedString("robot.avgsize.pattern")));
429:
430: msgArgs = getMessageArguments(getRunTime(states[10]));
431: setDisplayFieldValue(
432: this .STATUS_RUNNING_TIME,
433: getMessageFormat(
434: msgArgs,
435: getLocalizedString("robot.runningtime.pattern")));
436:
437: } // end of else
438:
439: }
440: RobotConfig robotConfig = CSConfig.getRobotConfig();
441: if (CSConfig.getRobotConfig().numOfStartingPoint(true) == 0) {
442: IPlanetButton child = (IPlanetButton) getChild(BTN_START);
443: child.setEnable(false);
444: }
445:
446: }
447:
448: public boolean beginStartDisplay(ChildDisplayEvent event) {
449: return !isRunning;
450: }
451:
452: public boolean beginStopDisplay(ChildDisplayEvent event) {
453: return isRunning;
454: }
455:
456: public boolean beginFreshDisplay(ChildDisplayEvent event) {
457: return !isRunning;
458: }
459:
460: public boolean beginPauseDisplay(ChildDisplayEvent event) {
461: return isRunning && !isPaused;
462: }
463:
464: public boolean beginResumeDisplay(ChildDisplayEvent event) {
465: return isRunning && isPaused;
466: }
467:
468: /** use this function instead of forwardTo to avoid user reload page cause redo action */
469: private void redirect() {
470: HttpServletResponse response = getRequestContext()
471: .getResponse();
472: try {
473: response.sendRedirect("Control");
474: } catch (IOException e) {
475: //TOFIX: handle this
476: }
477: }
478:
479: private String getUnitString(String s) {
480:
481: double generated = 0;
482: int unit = 0;
483: String[] unitString = { "robot.bytes", "robot.kbytes",
484: "robot.mbytes", "robot.gbytes" };
485: try {
486: generated = (double) Long.parseLong(s);
487: if (generated > 1000.0) {
488: generated = generated / 1000.0;
489: if (generated > 1000.0) {
490: generated = generated / 1000.0;
491: unit = 2;
492: } else {
493: unit = 1;
494: }
495: }
496: } catch (Exception e) {
497: }
498: NumberFormat nf = NumberFormat.getInstance();
499: nf = NumberFormat.getInstance();
500: nf.setMaximumFractionDigits(2);
501: return nf.format(generated) + " "
502: + getLocalizedString(unitString[unit]);
503: }
504:
505: private String getRunTime(String s) {
506:
507: String runTime = "";
508: long run_time = Long.parseLong(s);
509: long day = 0, hour = 0, minute = 0, second = 0;
510: if (run_time >= 86400) {
511: day = run_time / 86400;
512: run_time = run_time % 86400;
513: if (day == 1) {
514: runTime = "1 " + getLocalizedString("time.day") + " ";
515: } else {
516: runTime = Long.toString(day) + " "
517: + getLocalizedString("time.days") + " ";
518: }
519: }
520: if (run_time > 3600) {
521: hour = run_time / 3600;
522: run_time = run_time % 3600;
523: if (hour == 1) {
524: runTime = runTime + "1 "
525: + getLocalizedString("time.hour") + " ";
526: } else {
527: runTime = runTime + Long.toString(hour) + " "
528: + getLocalizedString("time.hours") + " ";
529: }
530: }
531: if (run_time > 60) {
532: minute = run_time / 60;
533: run_time = run_time % 60;
534: if (minute == 1) {
535: runTime = runTime + "1 "
536: + getLocalizedString("time.minute") + " ";
537: } else {
538: runTime = runTime + Long.toString(minute) + " "
539: + getLocalizedString("time.minutes") + " ";
540: }
541: }
542: if (run_time == 1) {
543: runTime = runTime + "1 "
544: + getLocalizedString("time.second") + " ";
545: } else {
546: runTime = runTime + Long.toString(run_time) + " "
547: + getLocalizedString("time.seconds") + " ";
548: }
549: return runTime;
550: }
551:
552: private String getMessageFormat(Object[] arguments, String pattern) {
553:
554: MessageFormat msgFormat = new MessageFormat("");
555: msgFormat.applyPattern(pattern);
556: return msgFormat.format(arguments);
557: }
558:
559: private Object[] getMessageArguments(String s1) {
560:
561: Object[] arguments = { s1 };
562: return arguments;
563:
564: }
565:
566: private Object[] getMessageArguments(String s1, String s2) {
567:
568: Object[] arguments = { s1, s2 };
569: return arguments;
570:
571: }
572:
573: private Object[] getMessageArguments(String s1, String s2, String s3) {
574:
575: Object[] arguments = { s1, s2, s3 };
576: return arguments;
577:
578: }
579:
580: /**
581: * handles invocation of view bean
582: *
583: * @param event request invocation event
584: */
585: public void handleStartRequest(RequestInvocationEvent event) {
586: String action = "start";
587: robotController.command(action);
588: redirect();
589: }
590:
591: public void handleStopRequest(RequestInvocationEvent event) {
592: String action = "stop";
593: robotController.command(action);
594: redirect();
595: }
596:
597: public void handlePauseRequest(RequestInvocationEvent event) {
598: String action = "pause";
599: robotController.command(action);
600: redirect();
601: }
602:
603: public void handleResumeRequest(RequestInvocationEvent event) {
604: String action = "resume";
605: robotController.command(action);
606: redirect();
607: }
608:
609: public void handleFreshRequest(RequestInvocationEvent event) {
610: String action = "freshstart";
611: robotController.command(action);
612: infoMessage = getLocalizedString("robot.msg.refreshed");
613: states[1] = "0";
614: //use forwardTo to show message
615: forwardTo();
616: }
617:
618: }
|