001: /*
002: * Created on Oct 18, 2004
003: */
004: package com.openedit.modules.scheduler;
005:
006: import java.io.StringWriter;
007: import java.util.Iterator;
008: import java.util.Timer;
009: import java.util.TimerTask;
010:
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013:
014: import com.openedit.BaseWebPageRequest;
015: import com.openedit.Shutdownable;
016: import com.openedit.WebPageRequest;
017: import com.openedit.WebServer;
018: import com.openedit.error.ErrorHandler;
019: import com.openedit.modules.BaseModule;
020: import com.openedit.page.Page;
021: import com.openedit.users.User;
022: import com.openedit.util.PathUtilities;
023:
024: /**
025: * @author Matthew Avery, mavery@einnovation.com
026: */
027: public class SchedulerModule extends BaseModule implements Shutdownable {
028: private static final Log log = LogFactory
029: .getLog(SchedulerModule.class);
030: protected Timer fieldTimer;
031: protected ClassLoader fieldClassLoader; // this is the servlet/web-app class loader
032: protected WebServer fieldWebServer;
033: protected Scheduler fieldScheduler;
034: protected Action selectedAction;
035:
036: public SchedulerModule() {
037: super ();
038: // TODO Auto-generated constructor stub
039: }
040:
041: public void loadSettings() {
042: if (fieldTimer != null) {
043: getTimer().cancel();
044: }
045: fieldTimer = null;
046: //initFromFile("/openedit/scheduler/configuration.xml");
047: getScheduler();
048: }
049:
050: public void listActions(WebPageRequest inReq) {
051: inReq.putPageValue("actions", getScheduler().getActions());
052: }
053:
054: public void selectAction(WebPageRequest inReq) {
055: String actionName = inReq.getRequestParameter("actionname");
056: if (actionName != null && actionName.length() > 0) {
057: Action action = getScheduler().getAction(actionName);
058: setSelectedAction(action);
059: }
060: inReq.putPageValue("selectedAction", getSelectedAction());
061: }
062:
063: public void saveAction(WebPageRequest inReq) {
064: String actionName = inReq.getRequestParameter("actionname");
065: if (actionName != null && actionName.length() > 0) {
066: String delay = inReq.getRequestParameter("delay");
067: String period = inReq.getRequestParameter("period");
068: getSelectedAction().setPath(actionName);
069: if (delay != null && delay.length() > 0) {
070: getSelectedAction().setDelay(delay);
071: }
072: if (period != null && period.length() > 0) {
073: getSelectedAction().setPeriod(period);
074: }
075:
076: User user = inReq.getUser();
077: if (user != null) {
078: String username = user.getUserName();
079: getSelectedAction().setUserName(username);
080: }
081:
082: restartAction(getSelectedAction());
083: getScheduler().saveConfig(inReq);
084: }
085: }
086:
087: public void addNewAction(WebPageRequest inReq) {
088: Action action = new Action();
089: action.setPath("/run.html");
090: getScheduler().addAction(action);
091: setSelectedAction(action);
092: }
093:
094: public void deleteAction(WebPageRequest inReq) {
095: getSelectedAction().getTimerTask().cancel();
096: getScheduler().removeAction(getSelectedAction());
097: setSelectedAction(null);
098: getScheduler().saveConfig(inReq);
099: }
100:
101: public Timer getTimer() {
102: if (fieldTimer == null) {
103: fieldTimer = new Timer(false);
104: }
105: return fieldTimer;
106: }
107:
108: private void handleException(String inActionName, Exception e,
109: WebPageRequest inReq) {
110: log.error("Scheduler failed to execution action "
111: + inActionName + " " + e.getMessage());
112:
113: e.printStackTrace();
114:
115: try {
116: ErrorHandler handler = (ErrorHandler) getModuleManager()
117: .getBean("defaultErrorHandler");
118: handler.handleError(e, inReq);
119: } catch (Throwable ex) {
120: log.error("Error handle error ignored: " + ex);
121: }
122: }
123:
124: public ClassLoader getClassLoader() {
125: return fieldClassLoader;
126: }
127:
128: public void setClassLoader(ClassLoader inClassLoader) {
129: fieldClassLoader = inClassLoader;
130: }
131:
132: public void shutdown() {
133: if (fieldTimer != null) {
134: getTimer().cancel();
135: fieldTimer = null;
136: }
137: }
138:
139: public WebServer getWebServer() {
140: return fieldWebServer;
141: }
142:
143: public void setWebServer(WebServer inWebServer) {
144: fieldWebServer = inWebServer;
145: }
146:
147: public Scheduler getScheduler() {
148: if (fieldScheduler == null) {
149: fieldScheduler = new Scheduler();
150: fieldScheduler.setPageManager(getPageManager());
151: fieldScheduler.loadFromFile();
152: startAllTasks();
153: }
154:
155: return fieldScheduler;
156: }
157:
158: public void setScheduler(Scheduler inScheduler) {
159: fieldScheduler = inScheduler;
160: }
161:
162: private void startAllTasks() {
163: for (Iterator iter = getScheduler().getActions().iterator(); iter
164: .hasNext();) {
165: Action action = (Action) iter.next();
166: startTask(action);
167: }
168: }
169:
170: public void startTask(Action inAction) {
171: final String path = inAction.getPath();
172: final User user = getUserManager().getUser(
173: inAction.getUserName());
174: if (!inAction.isStarted()) {
175: TimerTask task = new TimerTask() {
176: public void run() {
177: BaseWebPageRequest request = new BaseWebPageRequest();
178: request.setWriter(new StringWriter());
179: request.setUser(user);
180: try {
181: String[] parts = path.split("[?]"); //TODO: Move this to Util class
182: Page page = getPageManager().getPage(parts[0]);
183:
184: if (parts.length > 1) {
185: String[] args = parts[1].split("&");
186: for (int i = 0; i < args.length; i++) {
187: String[] pairs = args[i].split("=");
188: request.setRequestParameter(pairs[0],
189: pairs[1]);
190: }
191: }
192:
193: request.setPage(page);
194: request.setContentPage(page);
195:
196: getWebServer().getOpenEditEngine()
197: .createPageStreamer(page, request);
198: Thread thread = Thread.currentThread();
199: ClassLoader oldLoader = thread
200: .getContextClassLoader();
201: try {
202: thread
203: .setContextClassLoader(getClassLoader());
204: log.debug("running " + path);
205: getModuleManager().executePageActions(page,
206: request);
207: getModuleManager().executePathActions(page,
208: request);
209: } finally {
210: thread.setContextClassLoader(oldLoader);
211: }
212: } catch (Exception e) {
213: handleException(path, e, request);
214: } catch (Throwable e) {
215: //handleException( path, e , request);
216: log.error(e);
217: }
218:
219: }
220: };
221: inAction.setTimerTask(task);
222: getTimer().scheduleAtFixedRate(task, inAction.getDelay(),
223: inAction.getPeriod());
224: }
225: }
226:
227: private void restartAction(Action inAction) {
228: inAction.stop();
229: startTask(inAction);
230: }
231:
232: public Action getSelectedAction() {
233: return selectedAction;
234: }
235:
236: public void setSelectedAction(Action inSelectedAction) {
237: selectedAction = inSelectedAction;
238: }
239:
240: }
|