001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/search/tags/sakai_2-4-1/search-tool/tool/src/java/org/sakaiproject/search/tool/SearchAdminBeanImpl.java $
003: * $Id: SearchAdminBeanImpl.java 29315 2007-04-20 14:28:12Z ajpoland@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.search.tool;
021:
022: import java.text.MessageFormat;
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import javax.servlet.http.HttpServletRequest;
028:
029: import org.sakaiproject.authz.cover.SecurityService;
030: import org.sakaiproject.component.cover.ServerConfigurationService;
031: import org.sakaiproject.exception.IdUnusedException;
032: import org.sakaiproject.exception.PermissionException;
033: import org.sakaiproject.search.api.SearchService;
034: import org.sakaiproject.search.api.SearchStatus;
035: import org.sakaiproject.search.model.SearchBuilderItem;
036: import org.sakaiproject.search.tool.api.SearchAdminBean;
037: import org.sakaiproject.search.tool.model.AdminOption;
038: import org.sakaiproject.search.tool.model.MasterRecord;
039: import org.sakaiproject.search.tool.model.Segment;
040: import org.sakaiproject.search.tool.model.WorkerThread;
041: import org.sakaiproject.site.api.Site;
042: import org.sakaiproject.site.api.SiteService;
043: import org.sakaiproject.tool.api.SessionManager;
044: import org.sakaiproject.tool.api.ToolManager;
045: import org.sakaiproject.util.FormattedText;
046:
047: /**
048: * @author ieb
049: */
050: public class SearchAdminBeanImpl implements SearchAdminBean {
051:
052: private static final String COMMAND = "command";
053:
054: private static final String REBUILDSITE = "rebuildsite";
055:
056: private static final String COMMAND_REBUILDSITE = "?" + COMMAND
057: + "=" + REBUILDSITE;
058:
059: private static final String REFRESHSITE = "refreshsite";
060:
061: private static final String COMMAND_REFRESHSITE = "?" + COMMAND
062: + "=" + REFRESHSITE;
063:
064: private static final String REBUILDINSTANCE = "rebuildinstance";
065:
066: private static final String COMMAND_REBUILDINSTANCE = "?" + COMMAND
067: + "=" + REBUILDINSTANCE;
068:
069: private static final String REFRESHINSTNACE = "refreshinstance";
070:
071: private static final String COMMAND_REFRESHINSTANCE = "?" + COMMAND
072: + "=" + REFRESHINSTNACE;
073:
074: private static final String REFRESHSTATUS = "refreshstatus";
075:
076: private static final String COMMAND_REFRESHSTATUS = "?" + COMMAND
077: + "=" + REFRESHSTATUS;
078:
079: private static final String REMOVELOCK = "removelock";
080:
081: private static final String COMMAND_REMOVELOCK = "?" + COMMAND
082: + "=" + REMOVELOCK;
083:
084: private static final String RELOADINDEX = "reloadindex";
085:
086: private static final String COMMAND_RELOADINDEX = "?" + COMMAND
087: + "=" + RELOADINDEX;
088:
089: private static final String DISABLEDIAGNOSTICS = "disablediag";
090:
091: private static final String COMMAND_DISABLEDIAGNOSTICS = "?"
092: + COMMAND + "=" + DISABLEDIAGNOSTICS;
093:
094: private static final String ENABLEDIAGNOSTICS = "enablediag";
095:
096: private static final String COMMAND_ENABLEDIAGNOSTICS = "?"
097: + COMMAND + "=" + ENABLEDIAGNOSTICS;
098:
099: private SearchService searchService = null;
100:
101: private SiteService siteService = null;
102:
103: private String internCommand = null;
104:
105: private String siteId;
106:
107: private String commandFeedback = "";
108:
109: private boolean super User = false;
110:
111: private String userName = null;
112:
113: private String siteCheck = null;
114:
115: /**
116: * Construct a SearchAdminBean, checking permissions first
117: *
118: * @param request
119: * @param searchService
120: * @param siteService
121: * @param portalService
122: * @throws IdUnusedException
123: * @throws PermissionException
124: */
125: public SearchAdminBeanImpl(HttpServletRequest request,
126: SearchService searchService, SiteService siteService,
127: ToolManager toolManager, SessionManager sessionManager)
128: throws IdUnusedException, PermissionException {
129: siteId = toolManager.getCurrentPlacement().getContext();
130: Site currentSite = siteService.getSite(siteId);
131: siteCheck = currentSite.getReference();
132: userName = sessionManager.getCurrentSessionUserId();
133: super User = SecurityService.isSuperUser();
134: boolean allow = (super User)
135: || ("true".equals(ServerConfigurationService.getString(
136: "search.allow.maintain.admin", "false")) && siteService
137: .allowUpdateSite(siteId));
138: if (!allow) {
139: throw new PermissionException(userName, "site.update",
140: siteCheck);
141: }
142: this .searchService = searchService;
143: this .siteService = siteService;
144:
145: // process any commands
146: String command = request.getParameter(COMMAND);
147: if (command != null) {
148: internCommand = command.intern();
149:
150: }
151: doCommand();
152: }
153:
154: private void doCommand() throws PermissionException {
155: if (internCommand == null)
156: return;
157: if (internCommand == REBUILDSITE) {
158: doRebuildSite();
159: } else if (internCommand == REFRESHSITE) {
160: doRefreshSite();
161: } else if (internCommand == REBUILDINSTANCE) {
162: doRebuildInstance();
163: } else if (internCommand == REFRESHINSTNACE) {
164: doRefreshInstance();
165: } else if (internCommand == REFRESHSTATUS) {
166: doRefreshStatus();
167:
168: } else if (internCommand == REMOVELOCK) {
169: doRemoveLock();
170:
171: } else if (internCommand == RELOADINDEX) {
172: doReloadIndex();
173: } else if (internCommand == DISABLEDIAGNOSTICS) {
174: searchService.disableDiagnostics();
175: } else if (internCommand == ENABLEDIAGNOSTICS) {
176: searchService.enableDiagnostics();
177: }
178:
179: internCommand = null;
180:
181: }
182:
183: private void doReloadIndex() {
184: searchService.forceReload();
185: searchService.reload();
186: commandFeedback = Messages.getString("searchadmin_reloadindex");
187: }
188:
189: private void doRemoveLock() {
190: if (!searchService.removeWorkerLock()) {
191: commandFeedback = Messages
192: .getString("searchadmin_failedremovewl");
193: }
194:
195: }
196:
197: /**
198: * Refresh the status of the search engine index, does nothing
199: */
200: private void doRefreshStatus() {
201: commandFeedback = Messages.getString("searchadmin_statok");
202:
203: }
204:
205: /**
206: * Refresh all the documents in the index
207: * @throws PermissionException
208: */
209: private void doRefreshInstance() throws PermissionException {
210: if (!super User) {
211: throw new PermissionException(userName, "site.update",
212: siteCheck);
213: }
214: searchService.refreshInstance();
215: commandFeedback = Messages.getString("searchadmin_statok");
216: ;
217:
218: }
219:
220: /**
221: * Rebuild the index from scratch, this dumps the existing index and reloads
222: * all entities from the EntityContentProviders
223: * @throws PermissionException
224: */
225: private void doRebuildInstance() throws PermissionException {
226: if (!super User) {
227: throw new PermissionException(userName, "site.update",
228: siteCheck);
229: }
230: searchService.rebuildInstance();
231: commandFeedback = Messages.getString("searchadmin_statok");
232: ;
233:
234: }
235:
236: /**
237: * Refresh just this suite
238: */
239: private void doRefreshSite() {
240: searchService.refreshSite(siteId);
241: commandFeedback = Messages.getString("searchadmin_statok");
242: ;
243:
244: }
245:
246: /**
247: * rebuild just this site
248: */
249: private void doRebuildSite() {
250: searchService.rebuildSite(siteId);
251: commandFeedback = Messages.getString("searchadmin_statok");
252: ;
253:
254: }
255:
256: /**
257: * {@inheritDoc}
258: */
259: public String getTitle() {
260: return Messages.getString("searchadmin_title");
261: }
262:
263: /**
264: * {@inheritDoc}
265: * @throws PermissionException
266: */
267: public String getIndexStatus(String statusFormat)
268: throws PermissionException {
269: SearchStatus ss = searchService.getSearchStatus();
270:
271: return MessageFormat.format(statusFormat, new Object[] {
272: ss.getLastLoad(), ss.getLoadTime(),
273: ss.getCurrentWorker(), ss.getCurrentWorkerETC(),
274: ss.getNDocuments(), ss.getPDocuments() });
275: }
276:
277: public String getWorkers(String rowFormat) {
278: SearchStatus ss = searchService.getSearchStatus();
279: StringBuffer sb = new StringBuffer();
280: List l = ss.getWorkerNodes();
281: for (Iterator i = l.iterator(); i.hasNext();) {
282: Object[] worker = (Object[]) i.next();
283: sb.append(MessageFormat.format(rowFormat, worker));
284: }
285: return sb.toString();
286: }
287:
288: public String getIndexDocuments(String rowFormat) {
289: StringBuffer sb = new StringBuffer();
290: List l = searchService.getAllSearchItems();
291: for (Iterator i = l.iterator(); i.hasNext();) {
292: SearchBuilderItem sbi = (SearchBuilderItem) i.next();
293: sb.append(MessageFormat.format(rowFormat, new Object[] {
294: sbi.getId(),
295: FormattedText.escapeHtml(sbi.getName(), false),
296: sbi.getContext(),
297: SearchBuilderItem.actions[sbi.getSearchaction()
298: .intValue()],
299: SearchBuilderItem.states[sbi.getSearchstate()
300: .intValue()], sbi.getVersion() }));
301: }
302: return sb.toString();
303: }
304:
305: public String getGlobalMasterDocuments(String rowFormat) {
306: StringBuffer sb = new StringBuffer();
307: List l = searchService.getGlobalMasterSearchItems();
308: for (Iterator i = l.iterator(); i.hasNext();) {
309: SearchBuilderItem sbi = (SearchBuilderItem) i.next();
310: sb.append(MessageFormat.format(rowFormat, new Object[] {
311: sbi.getId(),
312: FormattedText.escapeHtml(sbi.getName(), false),
313: sbi.getContext(),
314: SearchBuilderItem.actions[sbi.getSearchaction()
315: .intValue()],
316: SearchBuilderItem.states[sbi.getSearchstate()
317: .intValue()], sbi.getVersion() }));
318: }
319: return sb.toString();
320: }
321:
322: public String getSiteMasterDocuments(String rowFormat) {
323: StringBuffer sb = new StringBuffer();
324: List l = searchService.getSiteMasterSearchItems();
325: for (Iterator i = l.iterator(); i.hasNext();) {
326: SearchBuilderItem sbi = (SearchBuilderItem) i.next();
327: sb.append(MessageFormat.format(rowFormat, new Object[] {
328: sbi.getId(),
329: FormattedText.escapeHtml(sbi.getName(), false),
330: sbi.getContext(),
331: SearchBuilderItem.actions[sbi.getSearchaction()
332: .intValue()],
333: SearchBuilderItem.states[sbi.getSearchstate()
334: .intValue()], sbi.getVersion() }));
335: }
336: return sb.toString();
337: }
338:
339: /**
340: * {@inheritDoc}
341: */
342: public String getAdminOptions(String adminOptionsFormat) {
343: StringBuffer sb = new StringBuffer();
344: sb
345: .append(MessageFormat
346: .format(
347: adminOptionsFormat,
348: new Object[] {
349: COMMAND_REFRESHSTATUS,
350: Messages
351: .getString("searchadmin_cmd_refreshstat"),
352: "" }));
353: sb
354: .append(MessageFormat
355: .format(
356: adminOptionsFormat,
357: new Object[] {
358: COMMAND_REBUILDSITE,
359: Messages
360: .getString("searchadmin_cmd_rebuildsiteind"),
361: "" }));
362: sb
363: .append(MessageFormat
364: .format(
365: adminOptionsFormat,
366: new Object[] {
367: COMMAND_REFRESHSITE,
368: Messages
369: .getString("searchadmin_cmd_refreshsiteind"),
370: "" }));
371: if (super User) {
372: sb
373: .append(MessageFormat
374: .format(
375: adminOptionsFormat,
376: new Object[] {
377: COMMAND_REBUILDINSTANCE,
378: Messages
379: .getString("searchadmin_cmd_rebuildind"),
380: "" }));
381: sb
382: .append(MessageFormat
383: .format(
384: adminOptionsFormat,
385: new Object[] {
386: COMMAND_REFRESHINSTANCE,
387: Messages
388: .getString("searchadmin_cmd_refreshind"),
389: "" }));
390: sb
391: .append(MessageFormat
392: .format(
393: adminOptionsFormat,
394: new Object[] {
395: COMMAND_REMOVELOCK,
396: Messages
397: .getString("searchadmin_cmd_removelock"),
398: "onclick=\"return confirm('"
399: + Messages
400: .getString("searchadmin_cmd_removelockconfirm")
401: + "');\"" }));
402: sb
403: .append(MessageFormat
404: .format(
405: adminOptionsFormat,
406: new Object[] {
407: COMMAND_RELOADINDEX,
408: Messages
409: .getString("searchadmin_cmd_reloadind"),
410: "" }));
411: if (searchService.hasDiagnostics()) {
412: sb
413: .append(MessageFormat
414: .format(
415: adminOptionsFormat,
416: new Object[] {
417: COMMAND_DISABLEDIAGNOSTICS,
418: Messages
419: .getString("searchadmin_cmd_disablediagnostics"),
420: "" }));
421: } else {
422: sb
423: .append(MessageFormat
424: .format(
425: adminOptionsFormat,
426: new Object[] {
427: COMMAND_ENABLEDIAGNOSTICS,
428: Messages
429: .getString("searchadmin_cmd_enablediagnostics"),
430: "" }));
431:
432: }
433: }
434: return sb.toString();
435: }
436:
437: public String getCommandFeedback() {
438: return commandFeedback;
439: }
440:
441: public String getSegmentInfo(String rowFormat) {
442: List segmentInfo = searchService.getSegmentInfo();
443: StringBuffer sb = new StringBuffer();
444: for (Iterator i = segmentInfo.iterator(); i.hasNext();) {
445: sb.append(MessageFormat.format(rowFormat, (Object[]) i
446: .next()));
447: }
448: return sb.toString();
449: }
450:
451: /* (non-Javadoc)
452: * @see org.sakaiproject.search.tool.SearchAdminBean#getGlobalMasterRecords()
453: */
454: public List<MasterRecord> getGlobalMasterRecords() {
455: List<MasterRecord> masters = new ArrayList<MasterRecord>();
456: List l = searchService.getGlobalMasterSearchItems();
457: for (Iterator i = l.iterator(); i.hasNext();) {
458:
459: final SearchBuilderItem sbi = (SearchBuilderItem) i.next();
460: masters.add(new MasterRecord() {
461:
462: public String getContext() {
463: return sbi.getContext();
464: }
465:
466: public String getLastUpdate() {
467: return String.valueOf(sbi.getVersion());
468: }
469:
470: public String getOperation() {
471: return SearchBuilderItem.actions[sbi
472: .getSearchaction().intValue()];
473: }
474:
475: public String getStatus() {
476: return SearchBuilderItem.states[sbi
477: .getSearchstate().intValue()];
478: }
479:
480: });
481: }
482: return masters;
483: }
484:
485: /* (non-Javadoc)
486: * @see org.sakaiproject.search.tool.SearchAdminBean#getOptons()
487: */
488: public List<AdminOption> getOptons() {
489: List<AdminOption> o = new ArrayList<AdminOption>();
490: o.add(new AdminOptionImpl(COMMAND_REBUILDSITE, Messages
491: .getString("searchadmin_cmd_rebuildsiteind"), ""));
492: o.add(new AdminOptionImpl(COMMAND_REFRESHSITE, Messages
493: .getString("searchadmin_cmd_refreshsiteind"), ""));
494: if (super User) {
495: o.add(new AdminOptionImpl(COMMAND_REBUILDINSTANCE, Messages
496: .getString("searchadmin_cmd_rebuildind"), ""));
497: o.add(new AdminOptionImpl(COMMAND_REFRESHINSTANCE, Messages
498: .getString("searchadmin_cmd_refreshind"), ""));
499: o
500: .add(new AdminOptionImpl(
501: COMMAND_REMOVELOCK,
502: Messages
503: .getString("searchadmin_cmd_removelock"),
504: "onclick=\"return confirm('"
505: + Messages
506: .getString("searchadmin_cmd_removelockconfirm")
507: + "');\""));
508: o.add(new AdminOptionImpl(COMMAND_RELOADINDEX, Messages
509: .getString("searchadmin_cmd_reloadind"), ""));
510: if (searchService.hasDiagnostics()) {
511: o
512: .add(new AdminOptionImpl(
513: COMMAND_DISABLEDIAGNOSTICS,
514: Messages
515: .getString("searchadmin_cmd_disablediagnostics"),
516: ""));
517: } else {
518: o
519: .add(new AdminOptionImpl(
520: COMMAND_ENABLEDIAGNOSTICS,
521: Messages
522: .getString("searchadmin_cmd_enablediagnostics"),
523: ""));
524:
525: }
526: }
527: return o;
528: }
529:
530: /* (non-Javadoc)
531: * @see org.sakaiproject.search.tool.SearchAdminBean#getSegments()
532: */
533: public List<Segment> getSegments() {
534: List<Segment> segments = new ArrayList<Segment>();
535: List segmentInfo = searchService.getSegmentInfo();
536: StringBuffer sb = new StringBuffer();
537: for (Iterator i = segmentInfo.iterator(); i.hasNext();) {
538: // name, size, lastup
539: final Object[] r = (Object[]) i.next();
540: segments.add(new Segment() {
541:
542: public String getLastUpdate() {
543:
544: return String.valueOf(r[2]);
545: }
546:
547: public String getName() {
548: return String.valueOf(r[0]);
549: }
550:
551: public String getSize() {
552: return String.valueOf(r[1]);
553: }
554:
555: });
556: }
557: return segments;
558: }
559:
560: /* (non-Javadoc)
561: * @see org.sakaiproject.search.tool.SearchAdminBean#getSiteMasterRecords()
562: */
563: public List<MasterRecord> getSiteMasterRecords() {
564: List<MasterRecord> masters = new ArrayList<MasterRecord>();
565: List l = searchService.getSiteMasterSearchItems();
566: for (Iterator i = l.iterator(); i.hasNext();) {
567:
568: final SearchBuilderItem sbi = (SearchBuilderItem) i.next();
569: masters.add(new MasterRecord() {
570:
571: public String getContext() {
572: return sbi.getContext();
573: }
574:
575: public String getLastUpdate() {
576: return String.valueOf(sbi.getVersion());
577: }
578:
579: public String getOperation() {
580: return SearchBuilderItem.actions[sbi
581: .getSearchaction().intValue()];
582: }
583:
584: public String getStatus() {
585: return SearchBuilderItem.states[sbi
586: .getSearchstate().intValue()];
587: }
588:
589: });
590: }
591: return masters;
592: }
593:
594: public class AdminOptionImpl implements AdminOption {
595:
596: private String attributes;
597: private String name;
598: private String url;
599:
600: public AdminOptionImpl(String uri, String name,
601: String attributes) {
602: this .attributes = attributes;
603: this .name = name;
604: this .url = uri;
605:
606: }
607:
608: /* (non-Javadoc)
609: * @see org.sakaiproject.search.tool.AdminOption#getAttributes()
610: */
611: public String getAttributes() {
612: return attributes;
613: }
614:
615: /* (non-Javadoc)
616: * @see org.sakaiproject.search.tool.AdminOption#getName()
617: */
618: public String getName() {
619: return name;
620: }
621:
622: /* (non-Javadoc)
623: * @see org.sakaiproject.search.tool.AdminOption#getUrl()
624: */
625: public String getUrl() {
626: return url;
627: }
628:
629: }
630:
631: /* (non-Javadoc)
632: * @see org.sakaiproject.search.tool.SearchAdminBean#getSearchStatus()
633: */
634: public SearchStatus getSearchStatus() {
635: return searchService.getSearchStatus();
636: }
637:
638: /* (non-Javadoc)
639: * @see org.sakaiproject.search.tool.SearchAdminBean#getWorkerThreads()
640: */
641: public List<WorkerThread> getWorkerThreads() {
642: List<WorkerThread> workers = new ArrayList<WorkerThread>();
643: SearchStatus ss = searchService.getSearchStatus();
644: StringBuffer sb = new StringBuffer();
645: List l = ss.getWorkerNodes();
646: for (Iterator i = l.iterator(); i.hasNext();) {
647: final Object[] w = (Object[]) i.next();
648: workers.add(new WorkerThread() {
649:
650: public String getEta() {
651: return FormattedText.escapeHtml(String
652: .valueOf(w[1]), false);
653: }
654:
655: public String getName() {
656: return FormattedText.escapeHtml(String
657: .valueOf(w[0]), false);
658: }
659:
660: public String getStatus() {
661: return FormattedText.escapeHtml(String
662: .valueOf(w[2]), false);
663: }
664:
665: });
666: }
667: return workers;
668: }
669:
670: }
|