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.io.*;
009: import java.util.Properties;
010: import java.util.logging.Logger;
011: import java.util.logging.Level;
012:
013: import com.sun.portal.search.admin.model.SearchModel;
014: import com.sun.portal.search.admin.model.SearchModelImpl;
015: import com.sun.portal.log.common.PortalLogger;
016:
017: import javax.servlet.http.HttpServletRequest;
018: import javax.servlet.*;
019: import javax.servlet.http.*;
020:
021: import com.iplanet.jato.Log;
022: import com.iplanet.jato.RequestContext;
023: import com.iplanet.jato.model.Model;
024: import com.iplanet.jato.ModelManager;
025:
026: import com.iplanet.jato.view.event.DisplayEvent;
027: import com.iplanet.jato.view.event.ChildDisplayEvent;
028: import com.iplanet.jato.view.event.RequestInvocationEvent;
029:
030: import com.iplanet.jato.view.html.StaticTextField;
031: import com.iplanet.jato.view.html.RadioButtonGroup;
032: import com.iplanet.jato.view.html.OptionList;
033: import com.iplanet.jato.view.html.TextField;
034: import com.iplanet.jato.view.html.Button;
035: import com.iplanet.jato.view.html.HREF;
036:
037: import com.iplanet.jato.view.View;
038: import com.iplanet.jato.view.ViewBean;
039: import com.iplanet.jato.view.ViewBeanBase;
040:
041: import com.iplanet.jato.ViewBeanManager;
042:
043: import com.iplanet.jato.model.ModelControlException;
044:
045: import com.iplanet.am.console.components.view.html.ParentagePath;
046: import com.iplanet.am.console.components.view.html.IPlanetButton;
047: import com.iplanet.am.console.components.view.html.MessageBox;
048:
049: /**
050: * iCS admin console view bean: TODO
051: */
052: public class ServerViewBean extends CSViewBeanBase {
053: public static final String DEFAULT_DISPLAY_URL = "/ps/searchadmin/Server.jsp";
054: public static final String PAGE_NAME = "Server";
055:
056: // list of Jato elements
057: public static final String ERROR_MSG = "errorMsg";
058: public static final String SERVER_STATUS_TEXT = "ServerStatusText";
059: public static final String SERVER_INSTANCE_NAME_TEXT = "ServerInstanceNameText";
060: public static final String SERVER_ROOT_TEXT = "ServerRootText";
061: public static final String SERVER_TMP_FILES_TEXT = "ServerTmpFilesText";
062: public static final String SERVER_ADVANCED_LOG_HREF = "ServerAdvancedLogLink";
063: public static final String DOC_SECURITY_MODE = "DocumentSecurityMode";
064: public static final String DOC_SECURITY_MGR = "DocumentSecurityMgr";
065: public static final String RESTART_WARNING = "RestartWarning";
066: public static final String SERVER_BUTTON_SWITCH = "ServerButtonSwitch";
067: public static final String SERVER_BUTTON_RESTART = "ServerButtonRestart";
068: public static final String SERVER_BUTTON_SUBMIT = "ServerButtonSubmit";
069: public static final String SERVER_BUTTON_RESET = "ServerButtonReset";
070: private static final String defaultSecurityModeLabel = "On";
071: private static final String defaultSecurityModeValue = "ON";
072: public static final String CHILD_ERROR_MSG_BOX = "errorMsgBox";
073: public static final String TMP_ERR_MSG = "tmpErrMsg";
074:
075: private static Properties stateProperties = new Properties();
076:
077: // Create a Logger for this class
078: private static Logger debugLogger = PortalLogger
079: .getLogger(ServerViewBean.class);
080:
081: /**
082: * constructor
083: *
084: * @param PageName of this view bean
085: * @param displayURL default display URL
086: */
087: public ServerViewBean() {
088: super (PAGE_NAME);
089: setDefaultDisplayURL(DEFAULT_DISPLAY_URL);
090: registerChildren();
091: }
092:
093: /**
094: * register child component
095: */
096: protected void registerChildren() {
097: registerChild(SERVER_STATUS_TEXT, StaticTextField.class);
098: registerChild(SERVER_INSTANCE_NAME_TEXT, TextField.class);
099: registerChild(SERVER_ROOT_TEXT, StaticTextField.class);
100: registerChild(SERVER_ADVANCED_LOG_HREF, HREF.class);
101: registerChild(SERVER_TMP_FILES_TEXT, TextField.class);
102: registerChild(DOC_SECURITY_MODE, RadioButtonGroup.class);
103:
104: registerChild(DOC_SECURITY_MGR, TextField.class);
105: registerChild(RESTART_WARNING, MessageBox.class);
106: registerChild(SERVER_BUTTON_SWITCH, IPlanetButton.class);
107: registerChild(SERVER_BUTTON_RESTART, IPlanetButton.class);
108: registerChild(CHILD_ERROR_MSG_BOX, MessageBox.class);
109: registerChild(SERVER_BUTTON_SUBMIT, IPlanetButton.class);
110: registerChild(SERVER_BUTTON_RESET, IPlanetButton.class);
111: registerChild(TMP_ERR_MSG, StaticTextField.class);
112: }
113:
114: /**
115: * create child component
116: *
117: * @param name of component
118: * @return child component
119: */
120: protected View createChild(String name) {
121: debugLogger.log(Level.FINER, "PSSH_CSPSA0044", name);
122: View child = super .createChild(name);
123: if (child != null)
124: return child;
125: if (name.equals(TMP_ERR_MSG)) {
126: return new StaticTextField(this , TMP_ERR_MSG,
127: getErrMsg(TMP_ERR_MSG));
128: }
129: if (name.equals(SERVER_STATUS_TEXT)) {
130: return new StaticTextField(this , SERVER_STATUS_TEXT, "");
131: }
132: if (name.equals(SERVER_INSTANCE_NAME_TEXT)) {
133: return new TextField(this , SERVER_INSTANCE_NAME_TEXT, "");
134:
135: }
136: if (name.equals(SERVER_ROOT_TEXT)) {
137: return new StaticTextField(this , SERVER_ROOT_TEXT, "");
138: }
139: if (name.equals(SERVER_ADVANCED_LOG_HREF)) {
140: return new HREF(this , SERVER_ADVANCED_LOG_HREF, "Advanced");
141: }
142: if (name.equals(SERVER_TMP_FILES_TEXT)) {
143: return new TextField(this , SERVER_TMP_FILES_TEXT, "");
144: }
145: if (name.equals(DOC_SECURITY_MODE)) {
146: child = new RadioButtonGroup(this , DOC_SECURITY_MODE, "");
147: OptionList securityModeOptions = new OptionList(
148: new String[] {
149: getLocalizedString("server.security.label.on"),
150: getLocalizedString("server.security.label.off") },
151: new String[] { "ON", "OFF" });
152:
153: ((RadioButtonGroup) child).setOptions(securityModeOptions);
154: return child;
155: }
156:
157: if (name.equals(DOC_SECURITY_MGR)) {
158: child = new TextField(this , DOC_SECURITY_MGR, "");
159: if (child == null) {
160: debugLogger.log(Level.FINER, "PSSH_CSPSA0109",
161: DOC_SECURITY_MGR);
162: }
163: return child;
164: }
165: if (name.equals(RESTART_WARNING)) {
166: child = new MessageBox(this , RESTART_WARNING, "");
167: if (child != null) {
168: ((MessageBox) child).setEnabled(getSearchModel()
169: .needSrvRestart());
170: ((MessageBox) child).setType(MessageBox.TYPE_WARNING);
171: ((MessageBox) child)
172: .setMessage(getLocalizedString("server.restart_warning"));
173: }
174: return child;
175: }
176: if (name.equals(CHILD_ERROR_MSG_BOX)) {
177: child = new MessageBox(this , CHILD_ERROR_MSG_BOX, "");
178: return child;
179: }
180:
181: if (name.equals(SERVER_BUTTON_SWITCH)) {
182: return new IPlanetButton(this , SERVER_BUTTON_SWITCH,
183: "Restart");
184: }
185: if (name.equals(SERVER_BUTTON_RESTART)) {
186: return new IPlanetButton(this , SERVER_BUTTON_RESTART,
187: "Restart");
188: }
189: if (name.equals(SERVER_BUTTON_SUBMIT)) {
190: return new IPlanetButton(this , SERVER_BUTTON_SUBMIT,
191: getLocalizedString("server.submit"));
192: }
193: if (name.equals(SERVER_BUTTON_RESET)) {
194: return new IPlanetButton(this , SERVER_BUTTON_RESET,
195: getLocalizedString("server.reset"));
196: }
197: throw new IllegalArgumentException("Invalid child name ["
198: + name + "]");
199: }
200:
201: /**
202: * Returns the model to be used for auto-retrieving. This means the
203: * model will automatically be executed via its retrieve() method
204: * when the tiled view begins display.
205: * @param modelType the auto-execution model type.
206: * @return an array of Model objects for the auto-execution.
207: */
208: public Model[] getWebActionModels(int modelType) {
209: Model[] result = null;
210:
211: switch (modelType) {
212: case MODEL_TYPE_RETRIEVE:
213: result = new Model[] { (Model) getSearchModel() };
214: break;
215:
216: default:
217: break;
218: }
219: return result;
220: }
221:
222: /** begin displaying page. we set the required information
223: *
224: * @param event display event
225: * @throws ModelControlException if problem access value of component
226: */
227: public void beginDisplay(DisplayEvent event) {
228: setPageEncoding();
229:
230: setPageEncoding();
231: // Get request attributes and store them as Page Session Attributes
232: RequestContext rc = getRequestContext();
233: HttpServletRequest req = rc.getRequest();
234:
235: // display instance Name
236: setDisplayFieldValue(SERVER_INSTANCE_NAME_TEXT,
237: getSearchModel().getServerInstanceName());
238:
239: // display server hostname
240: setDisplayFieldValue(SERVER_ROOT_TEXT, getSearchModel()
241: .getServerRoot());
242: setDisplayFieldValue(SERVER_TMP_FILES_TEXT, getSearchModel()
243: .getServerTmpFiles());
244: setDisplayFieldValue(DOC_SECURITY_MODE, getSearchModel()
245: .getServerDocSecurityMode());
246: setDisplayFieldValue(DOC_SECURITY_MGR, getSearchModel()
247: .getServerDocSecurityMgr());
248: setDisplayFieldValue(SERVER_BUTTON_RESET,
249: getLocalizedString("server.reset"));
250: setDisplayFieldValue(SERVER_BUTTON_SUBMIT,
251: getLocalizedString("server.submit"));
252:
253: // checking for error message
254: String errorMsg = getErrMsg(TMP_ERR_MSG);
255: MessageBox errorMsgBox = (MessageBox) getChild(CHILD_ERROR_MSG_BOX);
256: MessageBox RestartWarning = (MessageBox) getChild(RESTART_WARNING);
257: if (errorMsg != null) {
258: errorMsgBox.setType(MessageBox.TYPE_ERROR);
259: errorMsgBox.setMessage(errorMsg.toString());
260: errorMsgBox.setEnabled(true);
261: RestartWarning.setEnabled(false);
262: } else {
263: errorMsgBox.setEnabled(false);
264: }
265:
266: }
267:
268: /*
269: * getting an error attribut set in request
270: */
271: public String getErrMsg(String errorAttributeName) {
272: return (String) getRequestContext().getRequest().getAttribute(
273: errorAttributeName);
274: }
275:
276: /*
277: * setting an error attribute set in request
278: */
279: public void setErrMsg(String errorAttributeName, String value) {
280: debugLogger.log(Level.FINER, "PSSH_CSPSA0061", new String[] {
281: errorAttributeName, value });
282: getRequestContext().getRequest().setAttribute(
283: errorAttributeName, value);
284: }
285:
286: /**
287: * handles the click on the log advanced link
288: * @param event request invocation event
289: */
290: public void handleServerAdvancedLogLinkRequest(
291: RequestInvocationEvent event) {
292: // Forward to the link
293: ViewBean viewBean = getViewBean(AdvancedLogViewBean.class);
294: viewBean.forwardTo(getRequestContext());
295: }
296:
297: /**
298: * handles the switch button
299: * @param event request invocation event
300: * 1.get the switch parameter from servlet request
301: * 2.perform a restart of the server, STEPS HOWTO??
302: * 3.forward to same page ServerViewBean
303: */
304: public void handleServerButtonSwitchRequest(
305: RequestInvocationEvent event) {
306: // 1.get the switch parameter from servlet request
307: boolean serverState;
308: try {
309: serverState = getServerState();
310:
311: // 2.perform a restart of the server, STEPS HOWTO??
312: if (serverState) {
313: stopCompass();
314: } else {
315: startCompass();
316: }
317: } catch (Exception e) {
318: debugLogger.log(Level.INFO, "PSSH_CSPSA0111", e
319: .getMessage());
320: }
321:
322: // 3.forward to same page ServerViewBean
323: forwardTo();
324: }
325:
326: /**
327: * handles the restart button
328: * @param event request invocation event
329: * 1.perform a restart of the server, STEPS HOWTO??
330: * 2.forward to same page ServerViewBean
331: */
332: public void handleServerButtonRestartRequest(
333: RequestInvocationEvent event) {
334:
335: // 1.perform a restart of the server, STEPS HOWTO??
336: restartCompass();
337:
338: // 3.forward to same page ServerViewBean
339: forwardTo();
340: }
341:
342: /**
343: * handles the Submit button
344: * @param event request invocation event
345: * 1. get input parameters from servlet request
346: * 2. persist information
347: * 3. refreshes
348: */
349: public void handleServerButtonSubmitRequest(
350: RequestInvocationEvent event) {
351: // 1. get input parameters from servlet request
352: update();
353: if (getErrMsg(TMP_ERR_MSG) == null) {
354: // 2. persists information
355: getSearchModel().store();
356: // 3. force reload of last saved
357: reset();
358: }
359: // 4. refresh page
360: forwardTo();
361: }
362:
363: /**
364: * handles the reset button
365: * @param event request invocation event
366: * Do nothing, refreshes the page
367: */
368: public void handleServerButtonResetRequest(
369: RequestInvocationEvent event) {
370:
371: // resetting the model
372: this .reset();
373:
374: // refresh Model from currently persited values
375: getSearchModel().load();
376:
377: // refresh the display
378: forwardTo();
379: }
380:
381: /**
382: *
383: */
384: private void update() {
385: // parse in user's input form
386: /*
387: // server instance name
388: String instanceName = getDisplayFieldStringValue(ServerViewBean.SERVER_INSTANCE_NAME_TEXT).trim();
389: getSearchModel().setServerInstanceName(instanceName);
390:
391: // server root
392: String serverRoot = getDisplayFieldStringValue(ServerViewBean.SERVER_ROOT_TEXT).trim();
393: try {
394: getSearchModel().setServerRoot(serverRoot);
395: } catch (Exception e) {
396: setErrMsg(SERVER_ROOT_ERR_MSG,
397: e.getMessage());
398: CSDebug.logln("ServerViewBean - cannot access server root path "+serverRoot);
399: }
400: */
401:
402: // server tmp dir
403: String tmpDir = getDisplayFieldStringValue(
404: ServerViewBean.SERVER_TMP_FILES_TEXT).trim();
405: try {
406: getSearchModel().setServerTmpFiles(tmpDir);
407: } catch (Exception e) {
408: setErrMsg(TMP_ERR_MSG, e.getMessage());
409: debugLogger.log(Level.FINER, "PSSH_CSPSA0112", tmpDir);
410: }
411: // server doc security mode
412: String serverDocSecMode = getDisplayFieldStringValue(
413: ServerViewBean.DOC_SECURITY_MODE).trim();
414: getSearchModel().setServerDocSecurityMode(serverDocSecMode);
415:
416: // server doc security mode
417: /*
418: String serverDocSecMgr = getDisplayFieldStringValue(ServerViewBean.DOC_SECURITY_MGR).trim();
419: try {
420: getSearchModel().setServerDocSecurityMgr(serverDocSecMgr);
421: } catch (Exception e) {
422: setErrMsg(SERVER_SECURITY_MGR_ERR_MSG,
423: "The security Manager class '"+serverDocSecMgr+"' is invalid");
424: CSDebug.logln("ServerViewBean - cannot find class "+serverDocSecMgr);
425: }
426: */
427: }
428:
429: /**
430: * reset()
431: */
432: private void reset() {
433: // simply removing the Model from the context will for reloading of it
434: ServletContext sc = getRequestContext().getServletContext();
435: SearchModel scSearchModel = (SearchModel) sc
436: .getAttribute(SearchModel.MODEL_NAME);
437: if (scSearchModel != null) {
438: scSearchModel = null;
439: sc.removeAttribute(SearchModel.MODEL_NAME);
440: }
441: }
442:
443: /**
444: *
445: *
446: */
447: public SearchModel getSearchModel() {
448: ServletContext sc = getRequestContext().getServletContext();
449: SearchModel scSearchModel = (SearchModel) sc
450: .getAttribute(SearchModel.MODEL_NAME);
451: if (scSearchModel == null) {
452: scSearchModel = new SearchModelImpl(getRequestContext()
453: .getRequest(),
454: CompassAdminConstants.RESOURCE_BUNDLE_FILE);
455: sc.setAttribute(SearchModel.MODEL_NAME, scSearchModel);
456: }
457: return scSearchModel;
458: }
459:
460: /*
461: * stopping the compass server
462: * false = failed to stop
463: * true = succeed to stop
464: */
465: private static boolean stopCompass() {
466: return switchCompassServer(false);
467: }
468:
469: /*
470: * starting the compass server
471: * false = failed to start
472: * true = succeed to stat
473: */
474: private static boolean startCompass() {
475: return switchCompassServer(true);
476: }
477:
478: /*
479: * restarting the compass server
480: * false = failed to restart
481: * true = succeeded to restart
482: */
483: private static boolean restartCompass() {
484: try {
485: if (stopCompass()) {
486: return (startCompass());
487: } else {
488: return false;
489: }
490: } catch (Exception e) {
491: debugLogger.log(Level.INFO, "PSSH_CSPSA0010", e
492: .getMessage());
493: return false;
494: }
495: }
496:
497: /*
498: * switch on/off the server
499: */
500: private static boolean switchCompassServer(boolean state) {
501: try {
502: boolean currentServerState = getServerState();
503: if (currentServerState != state) {
504: Runtime runtime = Runtime.getRuntime();
505: String cmd = "";
506: if (state) {
507: //cmd = CSConfig.getAV("start_server_cmd");
508: } else {
509: //cmd = CSConfig.getAV("stop_server_cmd");
510: }
511: Process process = runtime.exec(cmd);
512: int exitValue = process.waitFor();
513: if (exitValue == 0) {
514: setServerState(state);
515: return true;
516: } else {
517: return false;
518: }
519: } else {
520: return true;
521: }
522: } catch (Exception e) {
523: debugLogger.log(Level.FINER, "PSSH_CSPSA0111", e
524: .getMessage());
525: return false;
526: }
527: }
528:
529: /*
530: * switch on/off the server
531: */
532: private static boolean getServerState() throws Exception {
533: String server_root = CSConfig.getServerRoot();
534: return false;
535: }
536:
537: /*
538: * switch on/off the server
539: */
540: private static void setServerState(boolean state) throws Exception {
541: String server_root = CSConfig.getServerRoot();
542: }
543: }
|