001: /*
002: * snapper
003: *
004: * Enhydra super-servlet presentation object
005: *
006: */
007:
008: package org.enhydra.snapperAdmin.presentation;
009:
010: // Enhydra SuperServlet imports
011:
012: import java.util.HashMap;
013: import java.util.Iterator;
014: import java.util.Map;
015:
016: import org.enhydra.snapper.SnapperManager;
017: import org.enhydra.snapperAdmin.presentation.BasePO;
018: import org.enhydra.snapperAdmin.SnapperAdmin;
019: import org.enhydra.snapperAdmin.spec.Site;
020: import org.enhydra.snapperAdmin.spec.SiteList;
021: import org.enhydra.snapperAdmin.spec.SiteListFactory;
022: import org.enhydra.snapperAdmin.spec.Optimize;
023: import org.enhydra.snapperAdmin.spec.OptimizeAll;
024: import org.enhydra.snapperAdmin.spec.OptimizeAllFactory;
025: import org.enhydra.snapperAdmin.spec.OptimizeThreadFactory;
026: import org.enhydra.xml.xmlc.XMLObject;
027:
028: import com.lutris.appserver.server.httpPresentation.ClientPageRedirectException;
029:
030: // Standard imports
031:
032: public class OptimizeSite extends BasePO {
033:
034: protected XMLObject getDOM() throws Exception {
035:
036: String siteID = comms.request.getParameter("id");
037:
038: try {
039: boolean com = true;
040: try {
041: Class pm = Class
042: .forName("org.enhydra.snapperAdmin.business.Optimizer");
043: if (pm != null)
044: com = false;
045: } catch (Exception e) {
046: }
047:
048: if (com)
049: throw new ClientPageRedirectException(comms.request
050: .getAppFileURIPath("SiteListPresentation.po?d="
051: + siteID));
052:
053: } catch (Exception e) {
054:
055: }
056:
057: Optimize optimize = null;
058:
059: if (comms.request.getParameter("action") != null) {
060: if (comms.request.getParameter("action").equals(
061: "optimizeAllAtOnce")) {
062: optimizeAllAtOnce();
063: } else
064: optimizeOneByOne();
065:
066: throw new ClientPageRedirectException(comms.request
067: .getAppFileURIPath("SiteListPresentation.po"));
068: }
069:
070: try {
071:
072: Map threads = new HashMap();
073: threads = SnapperManager.getInstance().getThreads();
074:
075: for (Iterator iterator = threads.entrySet().iterator(); iterator
076: .hasNext();) {
077: Map.Entry entry = (Map.Entry) iterator.next();
078: String key = (String) entry.getKey();
079:
080: if (siteID.equals(key)) {
081: SnapperAdmin.logInfo("Site " + siteID
082: + " locked! Optimizing Aborted.");
083: throw new ClientPageRedirectException(
084: comms.request
085: .getAppFileURIPath("SiteListPresentation.po?l="
086: + siteID));
087: }
088: }
089: } catch (Exception e) {
090: SnapperAdmin.logError("Could not check lock");
091: }
092:
093: try {
094: optimize = OptimizeThreadFactory
095: .getOptimizeThread("org.enhydra.snapperAdmin.business.OptimizeThread");
096: } catch (Exception ex) {
097: System.out.println("Exception: " + ex);
098: }
099:
100: SnapperManager.getInstance().addThread(siteID, optimize);
101: optimize.optimize(siteID);
102: Thread newThread = new Thread((Runnable) optimize, siteID);
103: newThread.start();
104:
105: throw new ClientPageRedirectException(comms.request
106: .getAppFileURIPath("SiteListPresentation.po?u="
107: + siteID));
108:
109: }
110:
111: private void optimizeAllAtOnce() {
112: Site[] list;
113: try {
114: SiteList sl = SiteListFactory
115: .getSiteList("org.enhydra.snapperAdmin.business.SiteListImpl");
116: list = sl.getList();
117:
118: } catch (Exception ex) {
119: System.out.println("Exception: " + ex);
120: list = null;
121: }
122: if (list != null) {
123: for (int numSites = 0; numSites < list.length; numSites++) {
124: try {
125: String id = list[numSites].getNAME();
126: Optimize optimize = OptimizeThreadFactory
127: .getOptimizeThread("org.enhydra.snapperAdmin.business.OptimizeThread");
128: optimize.optimize(id);
129: Thread newThread = new Thread((Runnable) optimize,
130: id);
131: newThread.start();
132:
133: SnapperManager.getInstance()
134: .addThread(id, optimize);
135: } catch (Exception e) {
136: SnapperAdmin
137: .logError("Could not optimzie index for site");
138: }
139: }
140: }
141: }
142:
143: private void optimizeOneByOne() {
144: Site[] list;
145: try {
146: SiteList sl = SiteListFactory
147: .getSiteList("org.enhydra.snapperAdmin.business.SiteListImpl");
148: list = sl.getList();
149: if (list == null) {
150: return;
151: }
152:
153: String[] ids = new String[list.length];
154: for (int i = 0; i < list.length; i++) {
155: ids[i] = list[i].getNAME();
156: }
157:
158: OptimizeAll optimizeall;
159: optimizeall = OptimizeAllFactory
160: .getOptimizeAll("org.enhydra.snapperAdmin.business.OptimizeAllImpl");
161: optimizeall.optimize(ids);
162: Thread newThread = new Thread((Runnable) optimizeall);
163: newThread.start();
164:
165: } catch (Exception ex) {
166: System.out.println("Exception: " + ex);
167: list = null;
168: }
169:
170: }
171:
172: }
|